001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.javahelp;
043:
044: import java.util.ArrayList;
045: import java.util.Collection;
046: import java.util.List;
047: import java.util.logging.Level;
048: import javax.help.HelpSet;
049: import javax.swing.SwingUtilities;
050: import javax.swing.event.ChangeListener;
051: import org.netbeans.api.javahelp.Help;
052: import org.openide.util.ChangeSupport;
053: import org.openide.util.Lookup;
054: import org.openide.util.LookupEvent;
055: import org.openide.util.LookupListener;
056:
057: /** An implementation of the JavaHelp system (a little more concrete).
058: * @author Jesse Glick
059: */
060: public abstract class AbstractHelp extends Help implements
061: HelpConstants {
062:
063: /** constructor for subclasses
064: */
065: protected AbstractHelp() {
066: }
067:
068: /** the results of the search for helpsets
069: */
070: private Lookup.Result<HelpSet> helpsets = null;
071:
072: /** Get all available help sets.
073: * Pay attention to {@link #helpSetsChanged} to see
074: * when this set will change.
075: * @return a collection of HelpSet
076: */
077: protected final Collection<? extends HelpSet> getHelpSets() {
078: if (helpsets == null) {
079: Installer.log.fine("searching for instances of HelpSet...");
080: helpsets = Lookup.getDefault().lookupResult(HelpSet.class);
081: helpsets.addLookupListener(new LookupListener() {
082: public void resultChanged(LookupEvent ev) {
083: helpSetsChanged();
084: }
085: });
086: fireChangeEvent(); // since someone may be listening to whether they are ready
087: }
088: Collection<? extends HelpSet> c = helpsets.allInstances();
089: if (Installer.log.isLoggable(Level.FINE)) {
090: List<String> l = new ArrayList<String>(Math
091: .min(1, c.size()));
092: for (HelpSet hs : c) {
093: l.add(hs.getTitle());
094: }
095: Installer.log.fine("listing helpsets: " + l);
096: }
097: return c;
098: }
099:
100: /** Are the help sets ready?
101: * @return true if they have been loaded
102: */
103: protected final boolean helpSetsReady() {
104: return helpsets != null;
105: }
106:
107: /** Whether a given help set is supposed to be merged
108: * into the master set.
109: * @param hs the help set
110: * @return true if so
111: */
112: protected final boolean shouldMerge(HelpSet hs) {
113: Boolean b = (Boolean) hs.getKeyData(HELPSET_MERGE_CONTEXT,
114: HELPSET_MERGE_ATTR);
115: return (b == null) || b.booleanValue();
116: }
117:
118: /** Called when the set of available help sets changes.
119: * Fires a change event to listeners; subclasses may
120: * do extra cleanup.
121: */
122: protected void helpSetsChanged() {
123: Installer.log.fine("helpSetsChanged");
124: fireChangeEvent();
125: }
126:
127: public final void addChangeListener(ChangeListener l) {
128: cs.addChangeListener(l);
129: }
130:
131: public final void removeChangeListener(ChangeListener l) {
132: cs.removeChangeListener(l);
133: }
134:
135: private final ChangeSupport cs = new ChangeSupport(this );
136:
137: /** Fire a change event to all listeners.
138: */
139: private final void fireChangeEvent() {
140: if (!SwingUtilities.isEventDispatchThread()) {
141: SwingUtilities.invokeLater(new Runnable() {
142: public void run() {
143: fireChangeEvent();
144: }
145: });
146: return;
147: }
148: Installer.log.fine("Help.stateChanged");
149: cs.fireChange();
150: }
151:
152: }
|