1 19 20 package org.netbeans.modules.javahelp; 21 22 import java.awt.Image ; 23 import java.awt.Toolkit ; 24 import java.awt.event.ActionEvent ; 25 import java.beans.*; 26 import java.io.ByteArrayInputStream ; 27 import java.io.IOException ; 28 import javax.swing.AbstractAction ; 29 import javax.swing.Action ; 30 import javax.swing.ImageIcon ; 31 import javax.swing.event.ChangeEvent ; 32 import javax.swing.event.ChangeListener ; 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 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 58 private XMLDataObject xml; 59 60 62 private Action p; 63 64 67 public void attachTo(XMLDataObject xml) { 68 this.xml = xml; 69 Installer.log.fine("processing help context ref: " + xml.getPrimaryFile()); 70 } 71 72 77 public Class instanceClass() throws IOException , ClassNotFoundException { 78 return ShortcutAction.class; 79 } 80 81 84 public String instanceName() { 85 return "org.netbeans.modules.javahelp.HelpCtxProcessor$ShortcutAction"; } 87 88 92 public boolean instanceOf(Class type) { 93 return type == Action .class; 94 } 95 96 101 public Object instanceCreate() throws IOException , ClassNotFoundException { 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 pubid, String sysid) { 109 return new InputSource(new ByteArrayInputStream (new byte[0])); 110 } 111 }; 112 113 HandlerBase handler = new HandlerBase() { 114 public void startElement(String name, AttributeList amap) throws SAXException { 115 if ("helpctx".equals(name)) { String id = amap.getValue("id"); String showmaster = amap.getValue("showmaster"); if (id != null && !"".equals(id)) { 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 ) new IOException (saxe.toString()).initCause(saxe); 133 } 134 135 if (p == null) { 136 throw new IOException ("No <helpctx> element in " + xml.getPrimaryFile()); } 138 139 return p; 140 } 141 142 144 private static final class ShortcutAction extends AbstractAction implements HelpCtx.Provider, NodeListener, ChangeListener { 145 146 148 private final XMLDataObject obj; 149 150 152 private String helpID; 153 154 156 private boolean showmaster; 157 158 161 public ShortcutAction(XMLDataObject obj, String helpID, boolean showmaster) { 162 this.obj = obj; 163 this.helpID = helpID; 164 this.showmaster = showmaster; 165 putValue("noIconInMenu", Boolean.TRUE); 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 181 public void actionPerformed(ActionEvent 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 195 public HelpCtx getHelpCtx() { 196 return new HelpCtx("ide.welcome"); } 199 200 203 public void stateChanged(ChangeEvent e) { 204 updateEnabled(); 205 } 206 207 211 public void propertyChange(PropertyChangeEvent ev) { 212 String 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 225 private void updateText() { 226 String text; 227 if (obj.isValid()) { 228 text = obj.getNodeDelegate().getDisplayName(); 229 } else { 230 text = "dead"; } 233 putValue(Action.NAME, text); 234 } 235 236 239 private void updateIcon() { 240 if (obj.isValid()) { 241 Image icon = obj.getNodeDelegate().getIcon(BeanInfo.ICON_COLOR_16x16); 242 if (icon != null) { 243 putValue(Action.SMALL_ICON, new ImageIcon (icon)); 244 } 245 } 246 } 247 248 private void updateEnabled() { 249 Help h = findHelp(); 250 Boolean 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 |