Refactor URL opening in dedicated method
This commit is contained in:
parent
17199f0160
commit
c35a15a8cd
@ -1,5 +1,7 @@
|
||||
package net.sf.openrocket.gui.components;
|
||||
|
||||
import net.sf.openrocket.gui.util.URLUtil;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Desktop;
|
||||
import java.awt.Dimension;
|
||||
@ -143,7 +145,7 @@ public class DescriptionArea extends JScrollPane {
|
||||
}
|
||||
|
||||
try {
|
||||
Desktop.getDesktop().browse(uri);
|
||||
URLUtil.openWebpage(uri);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new RuntimeException(ex);
|
||||
|
@ -12,6 +12,7 @@ import java.net.URISyntaxException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import net.sf.openrocket.gui.util.URLUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -60,12 +61,9 @@ public class URLLabel extends SelectableLabel {
|
||||
this.addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
Desktop d = Desktop.getDesktop();
|
||||
try {
|
||||
d.browse(new URI(url));
|
||||
} catch (URISyntaxException e1) {
|
||||
throw new BugException("Illegal URL: " + url, e1);
|
||||
} catch (IOException e1) {
|
||||
URLUtil.openWebpage(url);
|
||||
} catch (Exception e1) {
|
||||
log.error("Unable to launch browser: " + e1.getMessage(), e1);
|
||||
}
|
||||
}
|
||||
|
@ -33,6 +33,7 @@ import net.sf.openrocket.gui.components.StyledLabel;
|
||||
import net.sf.openrocket.gui.util.GUIUtil;
|
||||
import net.sf.openrocket.gui.util.Icons;
|
||||
import net.sf.openrocket.gui.util.SwingPreferences;
|
||||
import net.sf.openrocket.gui.util.URLUtil;
|
||||
import net.sf.openrocket.l10n.Translator;
|
||||
import net.sf.openrocket.startup.Application;
|
||||
import net.sf.openrocket.gui.widgets.SelectColorButton;
|
||||
@ -94,9 +95,8 @@ public class UpdateInfoDialog extends JDialog {
|
||||
@Override
|
||||
public void hyperlinkUpdate(HyperlinkEvent e) {
|
||||
if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
|
||||
Desktop desktop = Desktop.getDesktop();
|
||||
try {
|
||||
desktop.browse(e.getURL().toURI());
|
||||
URLUtil.openWebpage(e.getURL().toURI());
|
||||
} catch (Exception ex) {
|
||||
log.warn("Exception hyperlink: " + ex.getMessage());
|
||||
}
|
||||
@ -180,9 +180,8 @@ public class UpdateInfoDialog extends JDialog {
|
||||
String url = AssetHandler.getInstallerURLForPlatform((UpdatePlatform) comboBox.getSelectedItem(),
|
||||
release.getReleaseName());
|
||||
if (url == null) return;
|
||||
Desktop desktop = Desktop.getDesktop();
|
||||
try {
|
||||
desktop.browse(new URI(url));
|
||||
URLUtil.openWebpage(url);
|
||||
} catch (Exception ex) {
|
||||
log.warn("Exception install link: " + ex.getMessage());
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import net.miginfocom.swing.MigLayout;
|
||||
import net.sf.openrocket.gui.components.StyledLabel;
|
||||
import net.sf.openrocket.gui.util.GUIUtil;
|
||||
import net.sf.openrocket.gui.util.Icons;
|
||||
import net.sf.openrocket.gui.util.URLUtil;
|
||||
import net.sf.openrocket.gui.widgets.SelectColorButton;
|
||||
import net.sf.openrocket.l10n.Translator;
|
||||
import net.sf.openrocket.startup.Application;
|
||||
@ -68,9 +69,8 @@ public class WelcomeDialog extends JDialog {
|
||||
@Override
|
||||
public void hyperlinkUpdate(HyperlinkEvent e) {
|
||||
if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
|
||||
Desktop desktop = Desktop.getDesktop();
|
||||
try {
|
||||
desktop.browse(e.getURL().toURI());
|
||||
URLUtil.openWebpage(e.getURL().toURI());
|
||||
} catch (Exception ex) {
|
||||
log.warn("Exception hyperlink: " + ex.getMessage());
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ import javax.swing.event.HyperlinkEvent;
|
||||
import javax.swing.event.HyperlinkEvent.EventType;
|
||||
import javax.swing.event.HyperlinkListener;
|
||||
|
||||
import net.sf.openrocket.gui.util.URLUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -35,13 +36,9 @@ public class SlideShowLinkListener implements HyperlinkListener {
|
||||
|
||||
URL url = event.getURL();
|
||||
if (url != null && (url.getProtocol().equalsIgnoreCase("http") || url.getProtocol().equals("https"))) {
|
||||
|
||||
if (Desktop.isDesktopSupported()) {
|
||||
try {
|
||||
Desktop.getDesktop().browse(url.toURI());
|
||||
} catch (Exception e) {
|
||||
// Ignore
|
||||
}
|
||||
URLUtil.openWebpage(url.toURI());
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
|
||||
} else {
|
||||
|
30
swing/src/net/sf/openrocket/gui/util/URLUtil.java
Normal file
30
swing/src/net/sf/openrocket/gui/util/URLUtil.java
Normal file
@ -0,0 +1,30 @@
|
||||
package net.sf.openrocket.gui.util;
|
||||
|
||||
import net.sf.openrocket.util.BugException;
|
||||
|
||||
import java.awt.Desktop;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
public abstract class URLUtil {
|
||||
public static boolean openWebpage(URI uri) {
|
||||
Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
|
||||
if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
|
||||
try {
|
||||
desktop.browse(uri);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean openWebpage(String url) {
|
||||
try {
|
||||
return openWebpage(new URI(url));
|
||||
} catch (URISyntaxException e) {
|
||||
throw new BugException("Illegal URL: " + url, e);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user