source: java/SwingComponents/src/net/oni2/swingcomponents/HTMLLinkLabel.java@ 939

Last change on this file since 939 was 880, checked in by alloc, 11 years ago

AEI2.15:

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