~vcs-imports/xena/trunk

« back to all changes in this revision

Viewing changes to ext/src/javahelp/jhMaster/JavaHelp/src/new/javax/help/JHelpContentViewer.java

  • Committer: matthewoliver
  • Date: 2009-12-10 03:18:07 UTC
  • Revision ID: vcs-imports@canonical.com-20091210031807-l086qguzdlljtkl9
Merged Xena Testing into Xena Stable for the Xena 5 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * @(#)JHelpContentViewer.java  1.34 06/10/30
 
3
 * 
 
4
 * Copyright (c) 2006 Sun Microsystems, Inc.  All Rights Reserved.
 
5
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 
6
 * 
 
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.
 
12
 * 
 
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).
 
18
 * 
 
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.
 
22
 * 
 
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
 
25
 * have any questions.
 
26
 */
 
27
 
 
28
package javax.help;
 
29
 
 
30
import java.net.URL;
 
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;
 
43
 
 
44
/**
 
45
 * A component to represent the Help viewer that can be embedded if desired.
 
46
 *
 
47
 * @author Eduardo Pelegri-Llopart
 
48
 * @version   1.34     10/30/06
 
49
 */
 
50
public class JHelpContentViewer extends JComponent implements Accessible{
 
51
    protected TextHelpModel model;
 
52
    /**
 
53
     * Mark this JHelpContentViewer as synchronized with navigators
 
54
     * It is false for JHelpContentviewer used inside of glossary
 
55
     */
 
56
    private boolean synch = true;
 
57
 
 
58
    /**
 
59
     * Creates a JHelp with an instance of DefaultHelpModel as its data model.
 
60
     *
 
61
     * @param hs The HelpSet that provides context information. A null hs is valid
 
62
     * and creates a TextHelpModel with no HelpSet defined.
 
63
     */
 
64
    public JHelpContentViewer(HelpSet hs) {
 
65
        super();
 
66
        setModel(new DefaultHelpModel(hs));
 
67
        updateUI();
 
68
    }
 
69
    
 
70
    /**
 
71
     * Creates a JHelp with a default TextHelpModel.
 
72
     */
 
73
    public JHelpContentViewer() {
 
74
        super();
 
75
        setModel(new DefaultHelpModel(null)); // no HelpSet here!
 
76
        updateUI();
 
77
    }
 
78
 
 
79
    /**
 
80
     * Creates a JHelp with an specific TextHelpModel as its data model.
 
81
     *
 
82
     * @param model The TextHelpModel. A null model is valid.
 
83
     */
 
84
    public JHelpContentViewer(TextHelpModel model) {
 
85
        super();
 
86
        setModel(model);
 
87
        updateUI();
 
88
    }
 
89
    
 
90
    /**
 
91
     * Sets the HelpModel that provides the data.
 
92
     * Necessary to convert navigation action into visits...
 
93
     *
 
94
     * @param newModel The new Model to provide events for this viewer.
 
95
     */
 
96
    public void setModel(TextHelpModel newModel) {
 
97
        debug("setModel: "+newModel);
 
98
        TextHelpModel oldModel = model;
 
99
        if (newModel != oldModel) {
 
100
            model = newModel;
 
101
            firePropertyChange("helpModel", oldModel, model);
 
102
            invalidate();
 
103
        }
 
104
    }
 
105
    /**
 
106
     * @return The HelpModel that provides the events.
 
107
     */
 
108
    public TextHelpModel getModel() {
 
109
        return model;
 
110
    }
 
111
 
 
112
    /**
 
113
     * Sets the HelpViewerUI that provides the current look and feel.
 
114
     *
 
115
     * @param ui Provides the ComponentUI object. A null ui is valid.
 
116
     */
 
117
    public void setUI(HelpContentViewerUI ui) {
 
118
        debug("setUI");
 
119
        if ((HelpContentViewerUI)this.ui != ui) {
 
120
            super.setUI(ui);
 
121
            repaint();
 
122
        }
 
123
    }
 
124
 
 
125
    /**
 
126
     * @return The HelpViewerUI that provides the current look and feel.
 
127
     */
 
128
    public HelpContentViewerUI getUI() {
 
129
        return (HelpContentViewerUI)ui;
 
130
    }
 
131
 
 
132
    /**
 
133
     * Replaces the UI with the latest version from the default 
 
134
     * UIFactory.
 
135
     *
 
136
     * @overrides updateUI in class JComponent
 
137
     */
 
138
    public void updateUI() {
 
139
        SwingHelpUtilities.installUIDefaults();
 
140
        setUI((HelpContentViewerUI)UIManager.getUI(this));
 
141
        invalidate();
 
142
    }
 
143
 
 
144
    /**
 
145
     * @return "HelpViewerUI"
 
146
     */
 
147
    public String getUIClassID()
 
148
    {
 
149
        return "HelpContentViewerUI";
 
150
    }
 
151
 
 
152
    /*
 
153
     * Make sure the Look and Feel is set for the Help Component.
 
154
     */
 
155
    static {
 
156
        SwingHelpUtilities.installLookAndFeelDefaults();
 
157
    }
 
158
 
 
159
    /**
 
160
     * Visits a given ID.  Propagates down into the model.
 
161
     *
 
162
     * @param id The ID to visit.
 
163
     * @exception InvalidHelpSetContextException if id.hs is not contained in the
 
164
     * HelpSet of the current model.
 
165
     */
 
166
    public void setCurrentID(ID id) throws InvalidHelpSetContextException {
 
167
        model.setCurrentID(id);
 
168
    }
 
169
 
 
170
    /**
 
171
     * Visits a given ID.  Propagates down into the model.
 
172
     *
 
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.
 
175
     */
 
176
    public void setCurrentID(String id) throws BadIDException {
 
177
        try {
 
178
            model.setCurrentID(ID.create(id, getModel().getHelpSet()));
 
179
        } catch (InvalidHelpSetContextException ex) {
 
180
            // cannot happen
 
181
        }
 
182
    }
 
183
 
 
184
    /**
 
185
     * Visits a given URL.  Propagates down into the model.
 
186
     *
 
187
     * @param url The URL to visit.  Relative to the HS of the current model
 
188
     */
 
189
    public void setCurrentURL(URL url) {
 
190
        model.setCurrentURL(url);
 
191
    }
 
192
 
 
193
    /**
 
194
     * @return The URL currently being presented in the viewer.
 
195
     */
 
196
    public URL getCurrentURL() {
 
197
        return model.getCurrentURL();
 
198
    }
 
199
 
 
200
    /**
 
201
     * @return The document title.
 
202
     */
 
203
 
 
204
    public String getDocumentTitle() {
 
205
        return model.getDocumentTitle();
 
206
    }
 
207
 
 
208
    /**
 
209
     * Hightlights a section of the current document from p0 to p1.
 
210
     *
 
211
     * @param p0 Starting position.
 
212
     * @param p1 Ending position.
 
213
     */
 
214
    public void addHighlight(int p0, int p1) {
 
215
        model.addHighlight(p0,p1);
 
216
    }
 
217
 
 
218
    /**
 
219
     * Removes any Highlights.
 
220
     */
 
221
    public void removeAllHighlights() {
 
222
        model.removeAllHighlights();
 
223
    }
 
224
    
 
225
    /**
 
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. 
 
229
     */
 
230
    public void setSynch(boolean value){
 
231
        synch = value;
 
232
    }
 
233
    /**
 
234
     * Returns synchronization mode
 
235
     */
 
236
    public boolean getSynch(){
 
237
        return synch;
 
238
    }   
 
239
 
 
240
    /**
 
241
     * The local kitRegistry, indexed by mime type.
 
242
     */
 
243
    private Hashtable kitRegistry;
 
244
 
 
245
    /*
 
246
     * Creation of EditorKits.
 
247
     * This is similar to the registry used in JEditorPane, except:
 
248
     * <p>
 
249
     * (1) A separate registry is used so as to not interfere with other uses of JEditorPane
 
250
     * <br>
 
251
     * (2) The registry is centered around a HelpSet
 
252
     */
 
253
 
 
254
    /**
 
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.
 
262
     * <p>
 
263
     * Once a prototype EditorKit instance is successfully located,
 
264
     * it is cloned and the clone is returned.
 
265
     *
 
266
     * @param type the content type
 
267
     * @return the editor kit, or null if one cannot be created
 
268
     */
 
269
    public EditorKit createEditorKitForContentType(String type) {
 
270
        EditorKit k = null;
 
271
        if (kitRegistry == null) {
 
272
            // nothing has been loaded yet.
 
273
            kitRegistry = new Hashtable();
 
274
        } else {
 
275
            k = (EditorKit) kitRegistry.get(type);
 
276
        }
 
277
        if (k == null) {
 
278
            // try to dynamically load the support 
 
279
            HelpSet hs = model.getHelpSet();
 
280
            String classname =
 
281
                (String) hs.getKeyData(HelpSet.kitTypeRegistry,
 
282
                                       type);
 
283
            // I don't know of a class for this type
 
284
            if (classname == null) {
 
285
                return null;
 
286
            }
 
287
            ClassLoader loader =
 
288
                (ClassLoader) hs.getKeyData(HelpSet.kitLoaderRegistry,
 
289
                                            type);
 
290
            if (loader == null) {
 
291
                loader = hs.getLoader();
 
292
            }
 
293
            try {
 
294
                Class c;
 
295
                if (loader != null) {
 
296
                    c = loader.loadClass(classname);
 
297
                } else {
 
298
                    c = Class.forName(classname);
 
299
                }
 
300
                k = (EditorKit) c.newInstance();
 
301
                kitRegistry.put(type, k);
 
302
            } catch (Throwable e) {
 
303
                e.printStackTrace();
 
304
                k = null;
 
305
            }
 
306
        }
 
307
 
 
308
        // create a copy of the prototype or null if there
 
309
        // is no prototype.
 
310
        if (k != null) {
 
311
            return (EditorKit) k.clone();
 
312
        } else {
 
313
            // null, check the JEditorPane registry
 
314
            k = JEditorPane.createEditorKitForContentType(type);
 
315
        }
 
316
        return k;
 
317
    }
 
318
 
 
319
    /**
 
320
     * Adds a listener for the TExtHelpModelEvent posted after the model has
 
321
     * changed.
 
322
     * 
 
323
     * @param l - The listener to add.
 
324
     * @see javax.help.TextHelpModel#removeHelpModelListener
 
325
     */
 
326
    public void addTextHelpModelListener(TextHelpModelListener l) {
 
327
        getModel().addTextHelpModelListener(l);
 
328
    }
 
329
 
 
330
    /**
 
331
     * Removes a listener previously added with <tt>addTextHelpModelListener</tt>
 
332
     *
 
333
     * @param l - The listener to remove.
 
334
     * @see javax.help.TextHelpModel#addTextHelpModelListener
 
335
     */
 
336
    public void removeHelpModelListener(TextHelpModelListener l) {
 
337
        getModel().removeTextHelpModelListener(l);
 
338
    }
 
339
 
 
340
    /**
 
341
     * Adds a listener for the HelpModelEvent posted after the model has
 
342
     * changed.
 
343
     * 
 
344
     * @param l - The listener to add.
 
345
     * @see javax.help.HelpModel#removeHelpModelListener
 
346
     */
 
347
    public void addHelpModelListener(HelpModelListener l) {
 
348
        getModel().addHelpModelListener(l);
 
349
    }
 
350
 
 
351
    /**
 
352
     * Removes a listener previously added with <tt>addHelpModelListener</tt>
 
353
     *
 
354
     * @param l - The listener to remove.
 
355
     * @see javax.help.HelpModel#addHelpModelListener
 
356
     */
 
357
    public void removeHelpModelListener(HelpModelListener l) {
 
358
        getModel().removeHelpModelListener(l);
 
359
    }
 
360
 
 
361
    /**
 
362
     * Cleans the content
 
363
     */
 
364
    public void clear(){
 
365
        firePropertyChange("clear", " ","xyz");
 
366
    }
 
367
        
 
368
    /**
 
369
     * Reloads the content
 
370
     */
 
371
    public void reload(){
 
372
        firePropertyChange("reload", " ","xyz");
 
373
    }
 
374
        
 
375
    
 
376
    /**
 
377
     * Debug code
 
378
     */
 
379
    private boolean debug = false;
 
380
    private void debug(String msg) {
 
381
        if (debug) {
 
382
            System.err.println("JHelpContentViewer: "+msg);
 
383
        }
 
384
    }
 
385
 
 
386
/////////////////
 
387
// Accessibility support
 
388
////////////////
 
389
 
 
390
    /**
 
391
     * Get the AccessibleContext associated with this JComponent
 
392
     *
 
393
     * @return the AccessibleContext of this JComponent
 
394
     */
 
395
    public AccessibleContext getAccessibleContext() {
 
396
        if (accessibleContext == null) {
 
397
            accessibleContext = new AccessibleJHelpContentViewer();
 
398
        }
 
399
        return accessibleContext;
 
400
    }
 
401
 
 
402
    /**
 
403
     * The class used to obtain the accessible role for this object.
 
404
     * <p>
 
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.
 
411
     */
 
412
    protected class AccessibleJHelpContentViewer extends AccessibleJComponent {
 
413
 
 
414
        /**
 
415
         * Get the role of this object.
 
416
         *
 
417
         * @return an instance of AccessibleRole describing the role of the
 
418
         * object
 
419
         */
 
420
        public AccessibleRole getAccessibleRole() {
 
421
            return AccessibleRole.PANEL;
 
422
        }
 
423
    }
 
424
}
 
425