1 package org.catacomb.druid.swing;
2
3 import org.catacomb.interlish.interact.DComponent;
4 import org.catacomb.interlish.structure.HyperlinkHandler;
5 import org.catacomb.interlish.structure.Page;
6 import org.catacomb.interlish.util.JUtil;
7 import org.catacomb.report.E;
8 import org.catacomb.xdoc.XdocBase;
9
10
11 import java.awt.Color;
12 import java.awt.Graphics;
13 import java.awt.Graphics2D;
14 import java.awt.RenderingHints;
15 import java.awt.Dimension;
16 import java.io.StringReader;
17 import java.net.URL;
18
19 import javax.swing.JEditorPane;
20 import javax.swing.event.HyperlinkEvent;
21 import javax.swing.event.HyperlinkListener;
22 import javax.swing.text.html.HTMLDocument;
23 import javax.swing.text.html.HTMLEditorKit;
24 import javax.swing.text.html.StyleSheet;
25
26
27 public class DHTMLPane extends JEditorPane implements DComponent, HyperlinkListener {
28 static final long serialVersionUID = 1001;
29
30 HTMLEditorKit htmlEditorKit;
31 StyleSheet styleSheet;
32
33 static StyleSheet defaultStyleSheet;
34
35
36
37
38
39 HyperlinkHandler hyperlinkHandler;
40
41
42 public DHTMLPane() {
43 super();
44
45
46
47 setEditable(false);
48
49 htmlEditorKit = new HTMLEditorKit();
50 setEditorKit(htmlEditorKit);
51 }
52
53 public void setPrefSize(int w, int h) {
54 setPreferredSize(new Dimension(w, h));
55 }
56
57
58 protected void paintComponent(Graphics g) {
59 Graphics2D g2 = (Graphics2D) g;
60 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
61 RenderingHints.VALUE_ANTIALIAS_ON);
62
63 super.paintComponent(g2);
64 }
65
66
67 public void setTooltip(String s) {
68 setToolTipText(s);
69 }
70
71
72 public void setBg(Color c) {
73 setBackground(c);
74
75
76
77 colorizeAndApplyStylesheet();
78
79 }
80
81
82 public void setDefaultStyleSheet() {
83 if (defaultStyleSheet == null) {
84 defaultStyleSheet = loadRelStyleSheet(new XdocBase(), "defaultCSS.txt");
85 }
86 setStyleSheet(defaultStyleSheet);
87 }
88
89
90 public void setStylesheetPath(String s) {
91 String[] sa = s.split(":");
92
93 String scss = JUtil.getFileResource(sa[0], sa[1]);
94 StyleSheet styles = new StyleSheet();
95 try {
96 styles.loadRules(new StringReader(scss), null);
97 } catch (Exception ex) {
98 E.error("" + ex);
99 }
100 setStyleSheet(styles);
101
102 }
103
104
105 public void setRelStyleSheet(Object rel, String fnm) {
106 StyleSheet ss = loadRelStyleSheet(rel, fnm);
107 setStyleSheet(ss);
108 }
109
110
111
112
113
114 public void clearHyperlinkListeners() {
115 for (HyperlinkListener hl : getHyperlinkListeners()) {
116 removeHyperlinkListener(hl);
117 }
118 }
119
120
121
122
123
124
125
126
127
128 public void setHyperlinkHandler(HyperlinkHandler lh) {
129 hyperlinkHandler = lh;
130
131 addHyperlinkListener(this);
132
133 }
134
135
136
137 private StyleSheet loadRelStyleSheet(Object ref, String fnm) {
138
139 String scss = JUtil.getRelativeResource(ref, fnm);
140 StyleSheet styles = new StyleSheet();
141 try {
142 styles.loadRules(new StringReader(scss), null);
143 } catch (Exception ex) {
144 E.error("" + ex);
145 }
146 return styles;
147 }
148
149
150 public void setStyleSheet(StyleSheet ss) {
151 styleSheet = ss;
152 colorizeAndApplyStylesheet();
153 }
154
155
156 public void colorizeAndApplyStylesheet() {
157 if (styleSheet != null) {
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173 htmlEditorKit.setStyleSheet(styleSheet);
174 }
175 }
176
177 public void showPage(Page page) {
178 showHTML(page.getHTMLText());
179 }
180
181
182
183 public void showHTML(String s) {
184 HTMLDocument doc = new HTMLDocument(styleSheet);
185 try {
186 htmlEditorKit.read(new StringReader(s), doc, 0);
187 } catch (Exception ex) {
188
189 }
190
191 setDocument(doc);
192
193
194
195
196
197
198
199 }
200
201
202
203 public void hyperlinkUpdate(HyperlinkEvent e) {
204 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
205 String s = e.getDescription();
206 URL u = e.getURL();
207 if (u != null) {
208 hyperlinkHandler.hyperlinkClicked(u.toString());
209 } else {
210 hyperlinkHandler.hyperlinkClicked(s);
211 }
212
213 } else if (e.getEventType() == HyperlinkEvent.EventType.ENTERED) {
214
215 } else if (e.getEventType() == HyperlinkEvent.EventType.EXITED) {
216
217 }
218 }
219
220
221 }
222