KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
23 import java.net.URL JavaDoc;
24 import javax.swing.BoundedRangeModel JavaDoc;
25 import javax.swing.DefaultBoundedRangeModel JavaDoc;
26 import javax.swing.SwingUtilities JavaDoc;
27
28 import org.w3c.dom.Document JavaDoc;
29 import org.w3c.dom.Element JavaDoc;
30 import org.xml.sax.SAXException JavaDoc;
31
32 import javax.help.HelpSet;
33 import javax.help.HelpSetException;
34
35 import org.openide.cookies.InstanceCookie;
36 import org.openide.loaders.XMLDataObject;
37 import org.openide.util.Lookup;
38
39 /** An XML processor for help set references.
40  * Provides an instance of javax.swing.HelpSet.
41  * @author Jesse Glick
42  */

43 public final class HelpSetProcessor implements XMLDataObject.Processor, InstanceCookie.Of {
44     
45     /** "context" for merge attribute on helpsets
46      */

47     public static final String JavaDoc HELPSET_MERGE_CONTEXT = "OpenIDE"; // NOI18N
48

49     /** attribute (type Boolean) on helpsets indicating
50      * whether they should be merged into the master or
51      * not; by default, true
52      */

53     public static final String JavaDoc HELPSET_MERGE_ATTR = "mergeIntoMaster"; // NOI18N
54

55     public static final BoundedRangeModel JavaDoc parseModel = new DefaultBoundedRangeModel JavaDoc(0, 0, 0, 0);
56     
57     /** the XML file being parsed
58      */

59     private XMLDataObject xml;
60     
61     /** the cached help set
62      */

63     private HelpSet hs;
64     
65     /** Bind to an XML file.
66      * @param xml the file
67      */

68     public void attachTo(XMLDataObject xml) {
69         if (this.xml == xml) return;
70         hs = null;
71         // XXX this is called way too often, why?
72
this.xml = xml;
73         Installer.log.fine("processing help set ref: " + xml.getPrimaryFile());
74         BPMChanger.invoke(BPMChanger.INC_MAXIMUM);
75     }
76     
77     /** Decrement count of available help sets. */
78     protected void finalize() {
79         BPMChanger.invoke(BPMChanger.DEC_VALUE_AND_MAXIMUM);
80     }
81     
82     /** The class being produced.
83      * @throws IOException doesn't
84      * @throws ClassNotFoundException doesn't
85      * @return the class of helpsets
86      */

87     public Class JavaDoc instanceClass() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
88         return HelpSet.class;
89     }
90     
91     /** Get the name of the produced class.
92      * @return the class of helpsets
93      */

94     public String JavaDoc instanceName() {
95         return "javax.help.HelpSet"; // NOI18N
96
}
97     
98     /** Test whether a given superclass will be produced.
99      * @param type the superclass
100      * @return true if it is HelpSet
101      */

102     public boolean instanceOf(Class JavaDoc type) {
103         return type == HelpSet.class;
104     }
105     
106     /** Create the help set.
107      * @throws IOException if there was a problem parsing the XML
108      * of the helpset file or otherwise producing
109      * the helpset from its resource
110      * @throws ClassNotFoundException doesn't
111      * @return the help set
112      */

113     public synchronized Object JavaDoc instanceCreate() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
114         if (hs == null) {
115             Installer.log.fine("creating help set from ref: " + xml.getPrimaryFile());
116             try {
117                 Document JavaDoc doc = xml.getDocument();
118                 Element JavaDoc el = doc.getDocumentElement();
119                 if (! el.getNodeName().equals("helpsetref")) throw new IOException JavaDoc(); // NOI18N
120
String JavaDoc url = el.getAttribute("url"); // NOI18N
121
if (url == null || url.equals("")) throw new IOException JavaDoc("no url attr on <helpsetref>! doc.class=" + doc.getClass().getName() + " doc.documentElement=" + el); // NOI18N
122
String JavaDoc mergeS = el.getAttribute("merge"); // NOI18N
123
boolean merge = (mergeS == null) || mergeS.equals("") || // NOI18N
124
Boolean.valueOf(mergeS).booleanValue();
125                 // Make sure nbdocs: protocol is ready:
126
Object JavaDoc ignore = NbDocsStreamHandler.class; // DO NOT DELETE THIS LINE
127
hs = new HelpSet(((ClassLoader JavaDoc)Lookup.getDefault().lookup(ClassLoader JavaDoc.class)), new URL JavaDoc(url));
128                 hs.setKeyData(HELPSET_MERGE_CONTEXT, HELPSET_MERGE_ATTR, merge ? Boolean.TRUE : Boolean.FALSE);
129                 BPMChanger.invoke(BPMChanger.INC_VALUE);
130             } catch (SAXException JavaDoc saxe) {
131                 throw (IOException JavaDoc) new IOException JavaDoc(saxe.toString()).initCause(saxe);
132             } catch (HelpSetException hse) {
133                 throw (IOException JavaDoc) new IOException JavaDoc(hse.toString()).initCause(hse);
134             }
135         }
136         return hs;
137     }
138     
139     private static final class BPMChanger implements Runnable JavaDoc {
140         public static final int INC_MAXIMUM = 0;
141         public static final int DEC_VALUE_AND_MAXIMUM = 1;
142         public static final int INC_VALUE = 2;
143         public static void invoke(int action) {
144             SwingUtilities.invokeLater(new BPMChanger(action));
145         }
146         private final int action;
147         private BPMChanger(int action) {
148             this.action = action;
149         }
150         public void run() {
151             switch (action) {
152             case INC_MAXIMUM:
153                 parseModel.setMaximum(parseModel.getMaximum() + 1);
154                 break;
155             case DEC_VALUE_AND_MAXIMUM:
156                 parseModel.setValue(parseModel.getValue() - 1);
157                 parseModel.setMaximum(parseModel.getMaximum() - 1);
158                 break;
159             case INC_VALUE:
160                 parseModel.setValue(parseModel.getValue() + 1);
161                 break;
162             default:
163                 throw new IllegalStateException JavaDoc();
164             }
165         }
166     }
167     
168 }
169
170
Popular Tags