KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.Image JavaDoc;
23 import java.awt.Toolkit JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.beans.*;
26 import java.io.ByteArrayInputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import javax.swing.AbstractAction JavaDoc;
29 import javax.swing.Action JavaDoc;
30 import javax.swing.ImageIcon JavaDoc;
31 import javax.swing.event.ChangeEvent JavaDoc;
32 import javax.swing.event.ChangeListener JavaDoc;
33
34 import org.xml.sax.*;
35
36 import org.openide.cookies.InstanceCookie;
37 import org.openide.loaders.XMLDataObject;
38 import org.openide.nodes.*;
39 import org.openide.util.HelpCtx;
40 import org.openide.util.Lookup;
41 import org.openide.util.Utilities;
42
43 import org.netbeans.api.javahelp.Help;
44
45 /** XML processor for help context links.
46  * The associated instance makes it suitable for
47  * inclusion in a menu or toolbar.
48  * @author Jesse Glick
49  */

50 public final class HelpCtxProcessor implements XMLDataObject.Processor, InstanceCookie.Of {
51     
52     private static Help findHelp() {
53         return (Help)Lookup.getDefault().lookup(Help.class);
54     }
55     
56     /** the XML file being parsed
57      */

58     private XMLDataObject xml;
59     
60     /** the cached action
61      */

62     private Action JavaDoc p;
63     
64     /** Bind to an XML file.
65      * @param xml the file to parse
66      */

67     public void attachTo(XMLDataObject xml) {
68         this.xml = xml;
69         Installer.log.fine("processing help context ref: " + xml.getPrimaryFile());
70     }
71     
72     /** Get the class produced.
73      * @throws IOException doesn't
74      * @throws ClassNotFoundException doesn't
75      * @return the presenter class
76      */

77     public Class JavaDoc instanceClass() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
78         return ShortcutAction.class;
79     }
80     
81     /** Get the name of the class produced.
82      * @return the name of the presenter class
83      */

84     public String JavaDoc instanceName() {
85         return "org.netbeans.modules.javahelp.HelpCtxProcessor$ShortcutAction"; // NOI18N
86
}
87     
88     /** Test if this instance is of a suitable type.
89      * @param type some superclass
90      * @return true if it can be assigned to the desired superclass
91      */

92     public boolean instanceOf(Class JavaDoc type) {
93         return type == Action JavaDoc.class;
94     }
95     
96     /** Create the presenter.
97      * @throws IOException doesn't
98      * @throws ClassNotFoundException doesn't
99      * @return the new presenter
100      */

101     public Object JavaDoc instanceCreate() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
102         if (p != null)
103             return p;
104         
105         Installer.log.fine("creating help context presenter from " + xml.getPrimaryFile());
106         
107         EntityResolver resolver = new EntityResolver() {
108             public InputSource resolveEntity(String JavaDoc pubid, String JavaDoc sysid) {
109                 return new InputSource(new ByteArrayInputStream JavaDoc(new byte[0]));
110             }
111         };
112         
113         HandlerBase handler = new HandlerBase() {
114             public void startElement(String JavaDoc name, AttributeList amap) throws SAXException {
115                 if ("helpctx".equals(name)) { // NOI18N
116
String JavaDoc id = amap.getValue("id"); // NOI18N
117
String JavaDoc showmaster = amap.getValue("showmaster"); // NOI18N
118
if (id != null && !"".equals(id)) { // NOI18N
119
p = new ShortcutAction(xml, id, Boolean.valueOf(showmaster).booleanValue());
120                     }
121                 }
122             }
123         };
124         
125         Parser parser = xml.createParser();
126         parser.setEntityResolver(resolver);
127         parser.setDocumentHandler(handler);
128         
129         try {
130             parser.parse(new InputSource(xml.getPrimaryFile().getInputStream()));
131         } catch (SAXException saxe) {
132             throw (IOException JavaDoc) new IOException JavaDoc(saxe.toString()).initCause(saxe);
133         }
134         
135         if (p == null) {
136             throw new IOException JavaDoc("No <helpctx> element in " + xml.getPrimaryFile()); // NOI18N
137
}
138         
139         return p;
140     }
141     
142     /** The presenter to be shown in a menu, e.g.
143      */

144     private static final class ShortcutAction extends AbstractAction JavaDoc implements HelpCtx.Provider, NodeListener, ChangeListener JavaDoc {
145         
146         /** associated XML file representing it
147          */

148         private final XMLDataObject obj;
149         
150         /** the cached help context
151          */

152         private String JavaDoc helpID;
153         
154         /** cached flag to show the master help set
155          */

156         private boolean showmaster;
157         
158         /** Create a new presenter.
159          * @param obj XML file describing it
160          */

161         public ShortcutAction(XMLDataObject obj, String JavaDoc helpID, boolean showmaster) {
162             this.obj = obj;
163             this.helpID = helpID;
164             this.showmaster = showmaster;
165             putValue("noIconInMenu", Boolean.TRUE); // NOI18N
166
Installer.log.fine("new ShortcutAction: " + obj + " " + helpID + " showmaster=" + showmaster);
167             updateText();
168             updateIcon();
169             updateEnabled();
170             if (obj.isValid()) {
171                 Node n = obj.getNodeDelegate();
172                 n.addNodeListener(org.openide.nodes.NodeOp.weakNodeListener (this, n));
173             }
174             Help h = findHelp();
175             if (h != null) h.addChangeListener(org.openide.util.WeakListeners.change (this, h));
176         }
177         
178         /** Show the help.
179          * @param actionEvent ignored
180          */

181         public void actionPerformed(ActionEvent JavaDoc actionEvent) {
182             Help h = findHelp();
183             if (h != null) {
184                 Installer.log.fine("ShortcutAction.actionPerformed: " + helpID + " showmaster=" + showmaster);
185                 h.showHelp(new HelpCtx(helpID), showmaster);
186             } else {
187                 Toolkit.getDefaultToolkit().beep();
188             }
189         }
190         
191         /**
192          * Help for the shortcut itself is generic.
193          * @return a neutral help context - welcome page
194          */

195         public HelpCtx getHelpCtx() {
196             // #23565:
197
return new HelpCtx("ide.welcome"); // NOI18N
198
}
199         
200         /** Help sets may have changed.
201          * @param changeEvent ignore
202          */

203         public void stateChanged(ChangeEvent JavaDoc e) {
204             updateEnabled();
205         }
206         
207         /** Called when the node delegate changes somehow,
208          * @param ev event indicating whether the change
209          * was of display name, icon, or other
210          */

211         public void propertyChange(PropertyChangeEvent ev) {
212             String JavaDoc prop = ev.getPropertyName();
213             if (!obj.isValid()) return;
214             if (prop == null || prop.equals(Node.PROP_NAME) || prop.equals(Node.PROP_DISPLAY_NAME)) {
215                 updateText();
216             }
217             if (prop == null || prop.equals(Node.PROP_ICON)) {
218                 updateIcon();
219             }
220         }
221
222         /** Update the text of the button according to node's
223          * display name. Handle mnemonics sanely.
224          */

225         private void updateText() {
226             String JavaDoc text;
227             if (obj.isValid()) {
228                 text = obj.getNodeDelegate().getDisplayName();
229             } else {
230                 // #16364
231
text = "dead"; // NOI18N
232
}
233             putValue(Action.NAME, text);
234         }
235
236         /** Update the icon of the button according to the
237          * node delegate.
238          */

239         private void updateIcon() {
240             if (obj.isValid()) {
241                 Image JavaDoc icon = obj.getNodeDelegate().getIcon(BeanInfo.ICON_COLOR_16x16);
242                 if (icon != null) {
243                     putValue(Action.SMALL_ICON, new ImageIcon JavaDoc(icon));
244                 }
245             }
246         }
247
248         private void updateEnabled() {
249             Help h = findHelp();
250             Boolean JavaDoc valid = h == null ? Boolean.FALSE : h.isValidID(helpID, false);
251             if (valid != null) {
252                 setEnabled(valid.booleanValue());
253             }
254             Installer.log.fine("enabled: xml=" + obj.getPrimaryFile() + " id=" + helpID + " enabled=" + valid);
255         }
256
257         public void nodeDestroyed(NodeEvent ev) {
258             setEnabled(false);
259             updateText();
260         }
261         
262         public void childrenAdded(NodeMemberEvent ev) {}
263         public void childrenRemoved(NodeMemberEvent ev) {}
264         public void childrenReordered(NodeReorderEvent ev) {}
265         
266     }
267     
268 }
269
Popular Tags