1 19 20 package org.apache.tools.ant.module.nodes; 21 22 23 import java.awt.Toolkit ; 24 import java.awt.event.ActionEvent ; 25 import java.io.IOException ; 26 import javax.swing.AbstractAction ; 27 import javax.swing.Action ; 28 import javax.swing.UIManager ; 29 import javax.swing.event.ChangeEvent ; 30 import javax.swing.event.ChangeListener ; 31 import javax.swing.text.StyledDocument ; 32 import javax.xml.parsers.SAXParser ; 33 import javax.xml.parsers.SAXParserFactory ; 34 import org.apache.tools.ant.module.AntModule; 35 import org.apache.tools.ant.module.api.AntProjectCookie; 36 import org.apache.tools.ant.module.api.support.TargetLister; 37 import org.apache.tools.ant.module.run.TargetExecutor; 38 import org.apache.tools.ant.module.wizards.shortcut.ShortcutWizard; 39 import org.apache.tools.ant.module.xml.AntProjectSupport; 40 import org.openide.ErrorManager; 41 import org.openide.actions.OpenAction; 42 import org.openide.actions.PropertiesAction; 43 import org.openide.cookies.EditorCookie; 44 import org.openide.cookies.LineCookie; 45 import org.openide.cookies.OpenCookie; 46 import org.openide.filesystems.FileObject; 47 import org.openide.loaders.DataObject; 48 import org.openide.loaders.DataObjectNotFoundException; 49 import org.openide.nodes.AbstractNode; 50 import org.openide.nodes.Children; 51 import org.openide.nodes.PropertySupport; 52 import org.openide.nodes.Sheet; 53 import org.openide.text.Line; 54 import org.openide.util.NbBundle; 55 import org.openide.util.WeakListeners; 56 import org.openide.util.actions.SystemAction; 57 import org.xml.sax.Attributes ; 58 import org.xml.sax.InputSource ; 59 import org.xml.sax.Locator ; 60 import org.xml.sax.SAXException ; 61 import org.xml.sax.helpers.DefaultHandler ; 62 63 final class AntTargetNode extends AbstractNode implements ChangeListener { 64 65 66 private final AntProjectCookie project; 67 private final TargetLister.Target target; 68 69 75 public AntTargetNode(AntProjectCookie project, TargetLister.Target target) { 76 super(Children.LEAF); 77 this.project = project; 78 assert !target.isOverridden() : "Cannot include overridden targets"; 79 this.target = target; 80 target.getScript().addChangeListener(WeakListeners.change(this, target.getScript())); 81 setName(target.getQualifiedName()); 82 setDisplayName(target.getName()); 83 if (target.isDescribed()) { 84 setShortDescription(target.getElement().getAttribute("description")); setIconBaseWithExtension("org/apache/tools/ant/module/resources/EmphasizedTargetIcon.gif"); 86 } else if (target.isDefault()) { 87 setIconBaseWithExtension("org/apache/tools/ant/module/resources/EmphasizedTargetIcon.gif"); 88 } else { 89 setIconBaseWithExtension("org/apache/tools/ant/module/resources/TargetIcon.gif"); 90 } 91 getCookieSet().add(new TargetOpenCookie(target)); 92 } 93 94 private static String internalTargetColor = null; 95 96 private static synchronized String getInternalTargetColor() { 97 if (internalTargetColor == null) { 98 if (UIManager.getDefaults().getColor("Tree.selectionBackground").equals(UIManager.getDefaults().getColor("controlShadow"))) { internalTargetColor = "Tree.selectionBorderColor"; } else { 101 internalTargetColor = "controlShadow"; } 103 } 104 return internalTargetColor; 105 } 106 107 @Override 108 public String getHtmlDisplayName() { 109 boolean imported = target.getScript() != project; 111 if (!imported && !target.isDefault() && !target.isInternal()) { 112 return null; 113 } 114 StringBuffer name = new StringBuffer (target.getName()); 115 if (imported) { 116 name.insert(0, "<i>"); name.append("</i>"); } 119 if (target.isDefault()) { 120 name.insert(0, "<b>"); name.append("</b>"); } 123 if (target.isInternal()) { 124 name.insert(0, "'>"); name.insert(0, getInternalTargetColor()); 126 name.insert(0, "<font color='!"); name.append("</font>"); } 129 return name.toString(); 130 } 131 132 public void stateChanged (ChangeEvent ev) { 133 firePropertyChange (null, null, null); 134 } 135 136 @Override 137 public boolean canDestroy () { 138 return false; 139 } 140 141 @Override 142 public boolean canRename () { 143 return false; 144 } 145 146 @Override 147 public boolean canCopy () { 148 return true; 149 } 150 151 @Override 152 public boolean canCut () { 153 return false; 154 } 155 156 private final Action EXECUTE = new ExecuteAction(); 157 private final Action CREATE_SHORTCUT = new CreateShortcutAction(); 158 159 @Override 160 public Action [] getActions(boolean context) { 161 if (!target.isInternal()) { 162 return new Action [] { 163 SystemAction.get(OpenAction.class), 164 null, 165 EXECUTE, 166 CREATE_SHORTCUT, 167 null, 168 SystemAction.get(PropertiesAction.class), 169 }; 170 } else { 171 return new Action [] { 172 SystemAction.get(OpenAction.class), 173 null, 174 SystemAction.get(PropertiesAction.class), 175 }; 176 } 177 } 178 179 @Override 180 public Action getPreferredAction() { 181 return SystemAction.get(OpenAction.class); 182 } 183 184 private final class ExecuteAction extends AbstractAction { 185 186 ExecuteAction() { 187 super(NbBundle.getMessage(AntTargetNode.class, "LBL_execute_target")); 188 } 189 190 public void actionPerformed(ActionEvent e) { 191 try { 192 TargetExecutor te = new TargetExecutor(project, new String [] {target.getName()}); 193 te.execute(); 194 } catch (IOException ioe) { 195 AntModule.err.notify(ioe); 196 } 197 } 198 199 } 200 201 206 private final class CreateShortcutAction extends AbstractAction { 207 208 CreateShortcutAction() { 209 super(NbBundle.getMessage(AntTargetNode.class, "LBL_create_shortcut")); 210 } 211 212 public void actionPerformed(ActionEvent e) { 213 ShortcutWizard.show(project, target.getElement()); 214 } 215 216 } 217 218 @Override 219 protected Sheet createSheet() { 220 Sheet sheet = super.createSheet (); 221 Sheet.Set props = sheet.get (Sheet.PROPERTIES); 222 if (props == null) { 223 props = Sheet.createPropertiesSet(); 224 sheet.put(props); 225 } 226 String [] attrs = new String [] {"name", "description", "depends"}; for (String attr : attrs) { 228 org.openide.nodes.Node.Property<?> prop = new AntProperty(target.getElement(), attr); 229 prop.setDisplayName (NbBundle.getMessage (AntTargetNode.class, "PROP_target_" + attr)); 230 prop.setShortDescription (NbBundle.getMessage (AntTargetNode.class, "HINT_target_" + attr)); 231 props.put (prop); 232 } 233 236 return sheet; 237 } 238 239 242 private final class BuildSequenceProperty extends PropertySupport.ReadOnly<String > { 243 244 246 public BuildSequenceProperty() { 247 super ("buildSequence", String .class, 249 NbBundle.getMessage (AntTargetNode.class, "PROP_target_sequence"), 250 NbBundle.getMessage (AntTargetNode.class, "HINT_target_sequence") 251 ); 252 } 253 254 348 @Override 349 public String getValue () { 350 353 return "XXX BuildSequenceProperty currently unimplemented"; 354 } 355 } 356 357 private static final class TargetOpenCookie implements OpenCookie { 358 359 private final TargetLister.Target target; 360 361 public TargetOpenCookie(TargetLister.Target target) { 362 this.target = target; 363 } 364 365 public void open() { 366 if (target.getScript().getParseException() != null) { 367 Toolkit.getDefaultToolkit().beep(); 368 return; 369 } 370 FileObject script = target.getScript().getFileObject(); 371 assert script != null : "No build script for " + target; 372 EditorCookie editor; 373 LineCookie lines; 374 try { 375 DataObject d = DataObject.find(script); 376 editor = d.getCookie(EditorCookie.class); 377 lines = d.getCookie(LineCookie.class); 378 assert editor != null; 379 assert lines != null; 380 } catch (DataObjectNotFoundException e) { 381 throw new AssertionError (e); 382 } 383 try { 384 StyledDocument doc = editor.openDocument(); 385 InputSource in = AntProjectSupport.createInputSource(script, doc); 386 SAXParserFactory factory = SAXParserFactory.newInstance(); 387 SAXParser parser = factory.newSAXParser(); 388 final int[] line = new int[1]; 389 final String name = target.getName(); 390 class Handler extends DefaultHandler { 391 private Locator locator; 392 @Override 393 public void setDocumentLocator(Locator l) { 394 locator = l; 395 } 396 @Override 397 public void startElement(String uri, String localname, String qname, Attributes attr) throws SAXException { 398 if (line[0] == 0) { 399 if (qname.equals("target") && name.equals(attr.getValue("name"))) { line[0] = locator.getLineNumber(); 401 } 402 } 403 } 404 } 405 parser.parse(in, new Handler ()); 406 if (line[0] < 1) { 407 Toolkit.getDefaultToolkit().beep(); 408 return; 409 } 410 lines.getLineSet().getCurrent(line[0] - 1).show(Line.SHOW_GOTO); 411 } catch (Exception e) { 412 AntModule.err.notify(ErrorManager.INFORMATIONAL, e); 413 return; 414 } 415 } 416 417 } 418 419 } 420 | Popular Tags |