Le code
Voici le code de la visionneuse basée sur Cobra. Vous pouvez télécharger Cobra ici et la visionneuse s’utilise avec cette commande:
SwingHelpUtilities.setContentViewerUI (CobraContentViewerUI.class.getName());
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245 package be.meteo.sample.javahelp.ui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Locale;
import javax.help.HelpSet;
import javax.help.HelpUtilities;
import javax.help.JHelpContentViewer;
import javax.help.JHelpNavigator;
import javax.help.Map;
import javax.help.TextHelpModel;
import javax.help.Map.ID;
import javax.help.event.HelpModelEvent;
import javax.help.event.HelpModelListener;
import javax.help.event.TextHelpModelEvent;
import javax.help.event.TextHelpModelListener;
import javax.help.plaf.HelpContentViewerUI;
import javax.swing.JComponent;
import javax.swing.JViewport;
import javax.swing.plaf.ComponentUI;
import org.lobobrowser.html.UserAgentContext;
import org.lobobrowser.html.gui.HtmlPanel;
import org.lobobrowser.html.parser.DocumentBuilderImpl;
import org.lobobrowser.html.test.SimpleHtmlRendererContext;
import org.lobobrowser.html.test.SimpleUserAgentContext;
//import org.jdesktop.jdic.browser.WebBrowser;
public class CobraContentViewerUI extends HelpContentViewerUI
implements HelpModelListener, TextHelpModelListener, PropertyChangeListener, Serializable {
protected JHelpContentViewer theViewer;
private static Dimension PREF_SIZE = new Dimension(200, 300);
private static Dimension MIN_SIZE = new Dimension(80, 80);
private HtmlPanel html;
private JViewport vp;
private DocumentBuilderImpl dbi;
private SimpleHtmlRendererContext rcontext;
public static ComponentUI createUI(JComponent x) {
debug("createUI");
return new CobraContentViewerUI((JHelpContentViewer) x);
}
public CobraContentViewerUI(JHelpContentViewer b) {
debug("createUI - sort of");
}
public void installUI(JComponent c) {
debug("installUI");
theViewer = (JHelpContentViewer)c;
theViewer.setLayout(new BorderLayout());
// listen to property changes...
theViewer.addPropertyChangeListener(this);
TextHelpModel model = theViewer.getModel();
if (model != null) {
// listen to id changes...
model.addHelpModelListener(this);
// listen to highlight changes...
model.addTextHelpModelListener(this);
}
html = new HtmlPanel();
// This panel should be added to a JFrame or
// another Swing component.
UserAgentContext ucontext = new SimpleUserAgentContext();
rcontext = new SimpleHtmlRendererContext(html, ucontext);
// Note that document builder should receive both contexts.
dbi = new DocumentBuilderImpl(ucontext, rcontext);
// A documentURI should be provided to resolve relative URIs.
/**
* html future additions
* add any listeners here
*/
// if the model has a current URL then set it
if (model != null) {
URL url = model.getCurrentURL();
if (url != null) {
try {
rcontext.navigate(url.toExternalForm());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
theViewer.add("Center", html);
}
public void uninstallUI(JComponent c) {
debug("uninstallUI");
JHelpContentViewer viewer = (JHelpContentViewer) c;
viewer.removePropertyChangeListener(this);
/**
* html future additions
* remove all html listeners here - if we add any
*/
TextHelpModel model = viewer.getModel();
if (model != null) {
model.removeHelpModelListener(this);
model.removeTextHelpModelListener(this);
}
viewer.setLayout(null);
viewer.removeAll();
}
public Dimension getPreferredSize(JComponent c) {
return PREF_SIZE;
}
public Dimension getMinimumSize(JComponent c) {
return MIN_SIZE;
}
public Dimension getMaximumSize(JComponent c) {
// This doesn't seem right. But I'm not sure what to do for now
return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
}
public void idChanged(HelpModelEvent e) {
ID id = e.getID();
URL url = e.getURL();
TextHelpModel model = theViewer.getModel();
debug("idChanged("+e+")");
debug(" = " + id + " " + url);
debug(" my helpModel: "+model);
model.setDocumentTitle(null);
/**
* html future additions
* if we were doing any highlighting of the search text
* code would be needed here remove all the highlights before
* the new page is displayed
*/
rcontext.navigate(url,"_top");
debug("done with idChanged");
}
private void rebuild() {
debug("rebuild");
TextHelpModel model = theViewer.getModel();
if (model == null) {
debug("rebuild-end: model is null");
return;
}
/**
* html future additions
* if we were doing any highlighting the highlights would need
* to be removed here
*/
HelpSet hs = model.getHelpSet();
// for glossary - not set homeID page - glossary is not synchronized
if(theViewer.getSynch()){
try {
Map.ID homeID = hs.getHomeID();
Locale locale = hs.getLocale();
String name = HelpUtilities.getString(locale, "history.homePage");
model.setCurrentID(homeID, name, (JHelpNavigator)null);
rcontext.navigate(model.getCurrentURL(),"_top");
} catch (Exception e) {
// ignore
}
}
debug("rebuild-end");
}
public void propertyChange(PropertyChangeEvent event) {
debug("propertyChange: " + event.getPropertyName() + "\n\toldValue:" + event.getOldValue() + "\n\tnewValue:" + event.getNewValue());
if (event.getSource() == theViewer) {
String changeName = event.getPropertyName();
if (changeName.equals("helpModel")) {
TextHelpModel oldModel = (TextHelpModel) event.getOldValue();
TextHelpModel newModel = (TextHelpModel) event.getNewValue();
if (oldModel != null) {
oldModel.removeHelpModelListener(this);
oldModel.removeTextHelpModelListener(this);
}
if (newModel != null) {
newModel.addHelpModelListener(this);
newModel.addTextHelpModelListener(this);
}
rebuild();
} else if (changeName.equals("font")) {
debug("font changed");
Font newFont = (Font)event.getNewValue();
/**
* ~~
* Put font change handling code here
*/
}else if (changeName.equals("clear")) {
/**
* html future additions
* do not know how to do this at the current time
*/
// a~~ html.setText("");
}else if (changeName.equals("reload")) {
rcontext.reload();
}
}
}
/**
* Determines if highlights have changed.
* Collects all the highlights and marks the presentation.
*
* @param e The TextHelpModelEvent.
*/
public void highlightsChanged(TextHelpModelEvent e) {
debug("highlightsChanged "+e);
// if we do anything with highlighting it would need to
// be handled here.
}
/**
* For printf debugging.
*/
private final static boolean debug = false;
private static void debug(String str) {
if (debug) {
System.out.println("NativeContentViewerUI: " + str);
}
}
}
le composant « Browser » du SWT, autant que je sache, utilise le même principe que JDIC.
Super intéressant …
J’avais pour d’autre besoin html utilisé SWT et son composant Browser (le problème des librairies natives doit être le même qu’avec JDIC)
Et j’avais même trouvé une intégration Swing de ce composant SWT.
Lobo et Cobra, c’est bon à savoir.