2
* @(#)JHelpContentViewer.java 1.34 06/10/30
4
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7
* This code is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU General Public License version 2 only, as
9
* published by the Free Software Foundation. Sun designates this
10
* particular file as subject to the "Classpath" exception as provided
11
* by Sun in the LICENSE file that accompanied this code.
13
* This code is distributed in the hope that it will be useful, but WITHOUT
14
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16
* version 2 for more details (a copy is included in the LICENSE file that
17
* accompanied this code).
19
* You should have received a copy of the GNU General Public License version
20
* 2 along with this work; if not, write to the Free Software Foundation,
21
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
23
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
24
* CA 95054 USA or visit www.sun.com if you need additional information or
31
import javax.swing.JComponent;
32
import javax.swing.UIManager;
33
import javax.swing.LookAndFeel;
34
import javax.swing.JEditorPane;
35
import javax.accessibility.*;
36
import java.util.Hashtable;
37
import java.beans.PropertyChangeEvent;
38
import java.beans.PropertyChangeListener;
39
import javax.help.event.*;
40
import javax.help.plaf.HelpContentViewerUI;
41
import javax.swing.text.EditorKit;
42
import javax.help.Map.ID;
45
* A component to represent the Help viewer that can be embedded if desired.
47
* @author Eduardo Pelegri-Llopart
48
* @version 1.34 10/30/06
50
public class JHelpContentViewer extends JComponent implements Accessible{
51
protected TextHelpModel model;
53
* Mark this JHelpContentViewer as synchronized with navigators
54
* It is false for JHelpContentviewer used inside of glossary
56
private boolean synch = true;
59
* Creates a JHelp with an instance of DefaultHelpModel as its data model.
61
* @param hs The HelpSet that provides context information. A null hs is valid
62
* and creates a TextHelpModel with no HelpSet defined.
64
public JHelpContentViewer(HelpSet hs) {
66
setModel(new DefaultHelpModel(hs));
71
* Creates a JHelp with a default TextHelpModel.
73
public JHelpContentViewer() {
75
setModel(new DefaultHelpModel(null)); // no HelpSet here!
80
* Creates a JHelp with an specific TextHelpModel as its data model.
82
* @param model The TextHelpModel. A null model is valid.
84
public JHelpContentViewer(TextHelpModel model) {
91
* Sets the HelpModel that provides the data.
92
* Necessary to convert navigation action into visits...
94
* @param newModel The new Model to provide events for this viewer.
96
public void setModel(TextHelpModel newModel) {
97
debug("setModel: "+newModel);
98
TextHelpModel oldModel = model;
99
if (newModel != oldModel) {
101
firePropertyChange("helpModel", oldModel, model);
106
* @return The HelpModel that provides the events.
108
public TextHelpModel getModel() {
113
* Sets the HelpViewerUI that provides the current look and feel.
115
* @param ui Provides the ComponentUI object. A null ui is valid.
117
public void setUI(HelpContentViewerUI ui) {
119
if ((HelpContentViewerUI)this.ui != ui) {
126
* @return The HelpViewerUI that provides the current look and feel.
128
public HelpContentViewerUI getUI() {
129
return (HelpContentViewerUI)ui;
133
* Replaces the UI with the latest version from the default
136
* @overrides updateUI in class JComponent
138
public void updateUI() {
139
SwingHelpUtilities.installUIDefaults();
140
setUI((HelpContentViewerUI)UIManager.getUI(this));
145
* @return "HelpViewerUI"
147
public String getUIClassID()
149
return "HelpContentViewerUI";
153
* Make sure the Look and Feel is set for the Help Component.
156
SwingHelpUtilities.installLookAndFeelDefaults();
160
* Visits a given ID. Propagates down into the model.
162
* @param id The ID to visit.
163
* @exception InvalidHelpSetContextException if id.hs is not contained in the
164
* HelpSet of the current model.
166
public void setCurrentID(ID id) throws InvalidHelpSetContextException {
167
model.setCurrentID(id);
171
* Visits a given ID. Propagates down into the model.
173
* @param id The String to visit. Relative to the HS of the current model.
174
* @exception BadIDException The ID is not valid for the HelpSet for the current model.
176
public void setCurrentID(String id) throws BadIDException {
178
model.setCurrentID(ID.create(id, getModel().getHelpSet()));
179
} catch (InvalidHelpSetContextException ex) {
185
* Visits a given URL. Propagates down into the model.
187
* @param url The URL to visit. Relative to the HS of the current model
189
public void setCurrentURL(URL url) {
190
model.setCurrentURL(url);
194
* @return The URL currently being presented in the viewer.
196
public URL getCurrentURL() {
197
return model.getCurrentURL();
201
* @return The document title.
204
public String getDocumentTitle() {
205
return model.getDocumentTitle();
209
* Hightlights a section of the current document from p0 to p1.
211
* @param p0 Starting position.
212
* @param p1 Ending position.
214
public void addHighlight(int p0, int p1) {
215
model.addHighlight(p0,p1);
219
* Removes any Highlights.
221
public void removeAllHighlights() {
222
model.removeAllHighlights();
226
* Sets synchronization of this JHelpContentViewer with navigators.
227
* If true then JHelpContentViewer will contain homeID page when comes up at first time.
228
* False value causes blank page in this case.
230
public void setSynch(boolean value){
234
* Returns synchronization mode
236
public boolean getSynch(){
241
* The local kitRegistry, indexed by mime type.
243
private Hashtable kitRegistry;
246
* Creation of EditorKits.
247
* This is similar to the registry used in JEditorPane, except:
249
* (1) A separate registry is used so as to not interfere with other uses of JEditorPane
251
* (2) The registry is centered around a HelpSet
255
* Creates a handler for the given type from the registry of editor kits.
256
* If the registered class has not yet been loaded, an attempt
257
* is made to dynamically load the prototype of the kit for the
258
* given type. If the type was registered with a ClassLoader,
259
* that ClassLoader is used to load the prototype. If there
260
* was no registered ClassLoader, the ClassLoader for the HelpSet
261
* is used to load the prototype.
263
* Once a prototype EditorKit instance is successfully located,
264
* it is cloned and the clone is returned.
266
* @param type the content type
267
* @return the editor kit, or null if one cannot be created
269
public EditorKit createEditorKitForContentType(String type) {
271
if (kitRegistry == null) {
272
// nothing has been loaded yet.
273
kitRegistry = new Hashtable();
275
k = (EditorKit) kitRegistry.get(type);
278
// try to dynamically load the support
279
HelpSet hs = model.getHelpSet();
281
(String) hs.getKeyData(HelpSet.kitTypeRegistry,
283
// I don't know of a class for this type
284
if (classname == null) {
288
(ClassLoader) hs.getKeyData(HelpSet.kitLoaderRegistry,
290
if (loader == null) {
291
loader = hs.getLoader();
295
if (loader != null) {
296
c = loader.loadClass(classname);
298
c = Class.forName(classname);
300
k = (EditorKit) c.newInstance();
301
kitRegistry.put(type, k);
302
} catch (Throwable e) {
308
// create a copy of the prototype or null if there
311
return (EditorKit) k.clone();
313
// null, check the JEditorPane registry
314
k = JEditorPane.createEditorKitForContentType(type);
320
* Adds a listener for the TExtHelpModelEvent posted after the model has
323
* @param l - The listener to add.
324
* @see javax.help.TextHelpModel#removeHelpModelListener
326
public void addTextHelpModelListener(TextHelpModelListener l) {
327
getModel().addTextHelpModelListener(l);
331
* Removes a listener previously added with <tt>addTextHelpModelListener</tt>
333
* @param l - The listener to remove.
334
* @see javax.help.TextHelpModel#addTextHelpModelListener
336
public void removeHelpModelListener(TextHelpModelListener l) {
337
getModel().removeTextHelpModelListener(l);
341
* Adds a listener for the HelpModelEvent posted after the model has
344
* @param l - The listener to add.
345
* @see javax.help.HelpModel#removeHelpModelListener
347
public void addHelpModelListener(HelpModelListener l) {
348
getModel().addHelpModelListener(l);
352
* Removes a listener previously added with <tt>addHelpModelListener</tt>
354
* @param l - The listener to remove.
355
* @see javax.help.HelpModel#addHelpModelListener
357
public void removeHelpModelListener(HelpModelListener l) {
358
getModel().removeHelpModelListener(l);
365
firePropertyChange("clear", " ","xyz");
369
* Reloads the content
371
public void reload(){
372
firePropertyChange("reload", " ","xyz");
379
private boolean debug = false;
380
private void debug(String msg) {
382
System.err.println("JHelpContentViewer: "+msg);
387
// Accessibility support
391
* Get the AccessibleContext associated with this JComponent
393
* @return the AccessibleContext of this JComponent
395
public AccessibleContext getAccessibleContext() {
396
if (accessibleContext == null) {
397
accessibleContext = new AccessibleJHelpContentViewer();
399
return accessibleContext;
403
* The class used to obtain the accessible role for this object.
405
* <strong>Warning:</strong>
406
* Serialized objects of this class will not be compatible with
407
* future Swing releases. The current serialization support is appropriate
408
* for short term storage or RMI between applications running the same
409
* version of Swing. A future release of Swing will provide support for
410
* long term persistence.
412
protected class AccessibleJHelpContentViewer extends AccessibleJComponent {
415
* Get the role of this object.
417
* @return an instance of AccessibleRole describing the role of the
420
public AccessibleRole getAccessibleRole() {
421
return AccessibleRole.PANEL;