KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javahelp > AbstractHelp


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.javahelp;
21
22 import java.util.*;
23 import java.util.logging.Level JavaDoc;
24 import javax.swing.SwingUtilities JavaDoc;
25 import javax.swing.event.ChangeEvent JavaDoc;
26 import javax.swing.event.ChangeListener JavaDoc;
27
28 import javax.help.HelpSet;
29
30 import org.openide.util.*;
31
32 import org.netbeans.api.javahelp.Help;
33
34 /** An implementation of the JavaHelp system (a little more concrete).
35 * @author Jesse Glick
36 */

37 public abstract class AbstractHelp extends Help implements HelpConstants {
38
39     /** constructor for subclasses
40      */

41     protected AbstractHelp() {}
42     
43     /** the results of the search for helpsets
44      */

45     private Lookup.Result<HelpSet> helpsets = null;
46     /** Get all available help sets.
47      * Pay attention to {@link #helpSetsChanged} to see
48      * when this set will change.
49      * @return a collection of HelpSet
50      */

51     protected final Collection<? extends HelpSet> getHelpSets() {
52         if (helpsets == null) {
53             Installer.log.fine("searching for instances of HelpSet...");
54             helpsets = Lookup.getDefault().lookupResult(HelpSet.class);
55             helpsets.addLookupListener(new LookupListener() {
56                 public void resultChanged(LookupEvent ev) {
57                     helpSetsChanged();
58                 }
59             });
60             fireChangeEvent(); // since someone may be listening to whether they are ready
61
}
62         Collection<? extends HelpSet> c = helpsets.allInstances();
63         if (Installer.log.isLoggable(Level.FINE)) {
64             List<String JavaDoc> l = new ArrayList<String JavaDoc>(Math.min(1, c.size()));
65             for (HelpSet hs: c) {
66                 l.add(hs.getTitle());
67             }
68             Installer.log.fine("listing helpsets: " + l);
69         }
70         return c;
71     }
72     
73     /** Are the help sets ready?
74      * @return true if they have been loaded
75      */

76     protected final boolean helpSetsReady() {
77         return helpsets != null;
78     }
79
80     /** Whether a given help set is supposed to be merged
81      * into the master set.
82      * @param hs the help set
83      * @return true if so
84      */

85     protected final boolean shouldMerge(HelpSet hs) {
86         Boolean JavaDoc b = (Boolean JavaDoc)hs.getKeyData(HELPSET_MERGE_CONTEXT, HELPSET_MERGE_ATTR);
87         return (b == null) || b.booleanValue();
88     }
89     
90     /** Called when the set of available help sets changes.
91      * Fires a change event to listeners; subclasses may
92      * do extra cleanup.
93      */

94     protected void helpSetsChanged() {
95         Installer.log.fine("helpSetsChanged");
96         fireChangeEvent();
97     }
98     
99     public final void addChangeListener(ChangeListener JavaDoc l) {
100         synchronized (listeners) {
101             listeners.add(l);
102         }
103     }
104     
105     public final void removeChangeListener(ChangeListener JavaDoc l) {
106         synchronized (listeners) {
107             listeners.remove(l);
108         }
109     }
110     
111     /** all change listeners
112      */

113     private final Set<ChangeListener JavaDoc> listeners = new HashSet<ChangeListener JavaDoc>(1);
114     
115     /** Fire a change event to all listeners.
116      */

117     private final void fireChangeEvent() {
118         if (!SwingUtilities.isEventDispatchThread()) {
119             SwingUtilities.invokeLater(new Runnable JavaDoc() {
120                 public void run() {
121                     fireChangeEvent();
122                 }
123             });
124             return;
125         }
126         Set<ChangeListener JavaDoc> lsnrs;
127         synchronized (listeners) {
128             lsnrs = new HashSet<ChangeListener JavaDoc>(listeners);
129         }
130         ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(this);
131         Installer.log.fine("Help.stateChanged");
132         for (ChangeListener JavaDoc chl: lsnrs) {
133             chl.stateChanged(ev);
134         }
135     }
136     
137 }
138
Popular Tags