1 | package net.oni2.swingcomponents;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 | import java.awt.Desktop;
|
---|
5 | import java.awt.Font;
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.net.URISyntaxException;
|
---|
8 |
|
---|
9 | import javax.swing.JEditorPane;
|
---|
10 | import javax.swing.JFrame;
|
---|
11 | import javax.swing.JLabel;
|
---|
12 | import javax.swing.UIDefaults;
|
---|
13 | import javax.swing.border.Border;
|
---|
14 | import javax.swing.event.HyperlinkEvent;
|
---|
15 | import javax.swing.event.HyperlinkListener;
|
---|
16 | import javax.swing.text.DefaultCaret;
|
---|
17 | import javax.swing.text.html.HTMLEditorKit;
|
---|
18 | import javax.swing.text.html.StyleSheet;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * @author Christian Illy
|
---|
22 | */
|
---|
23 | public class HTMLLinkLabel extends JEditorPane {
|
---|
24 | private static final long serialVersionUID = 2416829757362043910L;
|
---|
25 |
|
---|
26 | private String prefix;
|
---|
27 | private String suffix;
|
---|
28 |
|
---|
29 | private boolean borderEnabled = true;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Create a new HTMLLinkLabel
|
---|
33 | */
|
---|
34 | public HTMLLinkLabel() {
|
---|
35 | super();
|
---|
36 |
|
---|
37 | ((DefaultCaret) this.getCaret())
|
---|
38 | .setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
|
---|
39 |
|
---|
40 | setContentType("text/html");
|
---|
41 |
|
---|
42 | HTMLEditorKit hek = (HTMLEditorKit) getEditorKit();
|
---|
43 | StyleSheet css = hek.getStyleSheet();
|
---|
44 | css.removeStyle("p");
|
---|
45 |
|
---|
46 | Font font = new JLabel().getFont();
|
---|
47 |
|
---|
48 | StringBuffer style = new StringBuffer("font-family:" + font.getFamily()
|
---|
49 | + ";");
|
---|
50 | style.append("font-weight:" + (font.isBold() ? "bold" : "normal") + ";");
|
---|
51 | style.append("font-size:" + font.getSize() + "pt;");
|
---|
52 | style.append("margin: 0; padding: 0;");
|
---|
53 |
|
---|
54 | prefix = "<html><body style=\"" + style + "\">";
|
---|
55 | suffix = "</body></html>";
|
---|
56 |
|
---|
57 | addHyperlinkListener(new HyperlinkListener() {
|
---|
58 |
|
---|
59 | @Override
|
---|
60 | public void hyperlinkUpdate(HyperlinkEvent e) {
|
---|
61 | if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED))
|
---|
62 | try {
|
---|
63 | Desktop.getDesktop().browse(e.getURL().toURI());
|
---|
64 | } catch (IOException e1) {
|
---|
65 | e1.printStackTrace();
|
---|
66 | } catch (URISyntaxException e1) {
|
---|
67 | e1.printStackTrace();
|
---|
68 | }
|
---|
69 | }
|
---|
70 | });
|
---|
71 | setEditable(false);
|
---|
72 |
|
---|
73 | Color bgColor = ColorCopy.copyColor(new JFrame().getBackground());
|
---|
74 | UIDefaults defaults = new UIDefaults();
|
---|
75 | defaults.put("EditorPane[Enabled].backgroundPainter", bgColor);
|
---|
76 | putClientProperty("Nimbus.Overrides", defaults);
|
---|
77 | putClientProperty("Nimbus.Overrides.InheritDefaults", true);
|
---|
78 | setBackground(bgColor);
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Allow or forbid to set a non-null border
|
---|
83 | *
|
---|
84 | * @param enable
|
---|
85 | * Non-null border allowed?
|
---|
86 | */
|
---|
87 | public void enableBorder(boolean enable) {
|
---|
88 | this.borderEnabled = enable;
|
---|
89 | if (!enable)
|
---|
90 | setBorder(null);
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Override
|
---|
94 | public void setBorder(Border border) {
|
---|
95 | if (borderEnabled || border == null)
|
---|
96 | super.setBorder(border);
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Override
|
---|
100 | public void setText(String t) {
|
---|
101 | super.setText(prefix + t + suffix);
|
---|
102 | }
|
---|
103 |
|
---|
104 | }
|
---|