-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLink.java
More file actions
156 lines (140 loc) · 3.37 KB
/
Link.java
File metadata and controls
156 lines (140 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package de.binfalse.martin.x;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.Cursor;
/**
* Link
*
* this class creates a Link extending JLabel
*
* @author Martin Scharm, see http://binfalse.de
*
*/
public class Link extends javax.swing.JLabel implements java.awt.event.MouseListener
{
private String url;
public Link ()
{
super ();
init ("");
}
public Link (javax.swing.Icon image)
{
super (image);
init ("");
}
public Link (javax.swing.Icon image, int horizontalAlignment)
{
super (image, horizontalAlignment);
init ("");
}
public Link (String text)
{
super (text);
init (text);
}
public Link (String text, javax.swing.Icon icon, int horizontalAlignment)
{
super (text, icon, horizontalAlignment);
init (text);
}
public void setURL (String url)
{
this.url = url;
this.setToolTipText ("Open " + url + " in your browser");
}
private void init (String url)
{
setURL (url);
this.addMouseListener (this);
this.setForeground (Color.BLUE);
}
public Link (String text, int horizontalAlignment)
{
super (text, horizontalAlignment);
init (text);
}
@Override
public void mouseClicked (MouseEvent arg0)
{
browse ();
}
@Override
public void mouseEntered (MouseEvent arg0)
{
setCursor (new Cursor (Cursor.HAND_CURSOR));
}
@Override
public void mouseExited (MouseEvent arg0)
{
setCursor (new Cursor (Cursor.DEFAULT_CURSOR));
}
@Override
public void mousePressed (MouseEvent arg0) {}
@Override
public void mouseReleased (MouseEvent arg0) {}
private void browse ()
{
if (java.awt.Desktop.isDesktopSupported ())
{
java.awt.Desktop desktop = java.awt.Desktop.getDesktop ();
if (desktop.isSupported (java.awt.Desktop.Action.BROWSE))
{
try
{
desktop.browse (new java.net.URI (url));
return;
}
catch (java.io.IOException e)
{
e.printStackTrace ();
}
catch (java.net.URISyntaxException e)
{
e.printStackTrace ();
}
}
}
String osName = System.getProperty("os.name");
try
{
if (osName.startsWith ("Windows"))
{
Runtime.getRuntime ().exec ("rundll32 url.dll,FileProtocolHandler " + url);
}
else if (osName.startsWith ("Mac OS"))
{
Class<?> fileMgr = Class.forName ("com.apple.eio.FileManager");
java.lang.reflect.Method openURL = fileMgr.getDeclaredMethod ("openURL", new Class[] {String.class});
openURL.invoke (null, new Object[] {url});
}
else
{
//check for $BROWSER
java.util.Map<String, String> env = System.getenv ();
if (env.get ("BROWSER") != null)
{
Runtime.getRuntime ().exec (env.get ("BROWSER") + " " + url);
return;
}
//check for common browsers
String[] browsers = { "firefox", "iceweasel", "chrome", "opera", "konqueror", "epiphany", "mozilla", "netscape" };
String browser = null;
for (int count = 0; count < browsers.length && browser == null; count++)
if (Runtime.getRuntime ().exec (new String[] {"which", browsers[count]}).waitFor () == 0)
{
browser = browsers[count];
break;
}
if (browser == null)
throw new RuntimeException ("couldn't find any browser...");
else
Runtime.getRuntime ().exec (new String[] {browser, url});
}
}
catch (Exception e)
{
javax.swing.JOptionPane.showMessageDialog (null, "couldn't find a webbrowser to use...\nPlease browser for yourself:\n" + url);
}
}
}