KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > module > loader > AntActionInstance


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.apache.tools.ant.module.loader;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.beans.PropertyChangeSupport JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.ObjectInputStream JavaDoc;
30 import java.net.MalformedURLException JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.util.Arrays JavaDoc;
33 import javax.swing.Action JavaDoc;
34 import javax.swing.ImageIcon JavaDoc;
35 import javax.swing.JButton JavaDoc;
36 import javax.swing.JComponent JavaDoc;
37 import javax.swing.JMenuItem JavaDoc;
38 import javax.swing.event.ChangeEvent JavaDoc;
39 import javax.swing.event.ChangeListener JavaDoc;
40 import org.apache.tools.ant.module.AntModule;
41 import org.apache.tools.ant.module.api.AntProjectCookie;
42 import org.apache.tools.ant.module.run.TargetExecutor;
43 import org.netbeans.api.project.FileOwnerQuery;
44 import org.netbeans.api.project.Project;
45 import org.netbeans.api.project.ui.OpenProjects;
46 import org.openide.awt.Actions;
47 import org.openide.awt.DynamicMenuContent;
48 import org.openide.awt.Mnemonics;
49 import org.openide.cookies.InstanceCookie;
50 import org.openide.filesystems.FileObject;
51 import org.openide.filesystems.FileUtil;
52 import org.openide.util.actions.Presenter;
53 import org.openide.util.RequestProcessor;
54 import org.openide.util.WeakListeners;
55 import org.w3c.dom.Element JavaDoc;
56 import org.w3c.dom.NodeList JavaDoc;
57
58 /** An instance cookie providing an action running a script.
59  * The action provides the standard presenters, so may be used
60  * e.g. in menu items.
61  */

62 public class AntActionInstance implements
63         InstanceCookie, Action JavaDoc,
64         Presenter.Menu, Presenter.Toolbar,
65         ChangeListener JavaDoc, PropertyChangeListener JavaDoc
66 {
67     
68     private boolean inited;
69     private final AntProjectCookie proj;
70     private transient PropertyChangeSupport JavaDoc changeSupport;
71     
72     public AntActionInstance (AntProjectCookie proj) {
73         this.proj = proj;
74     }
75
76     private void readObject (ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
77         in.defaultReadObject ();
78         init();
79     }
80     
81     public Class JavaDoc instanceClass () {
82         return AntActionInstance.class;
83     }
84     
85     public String JavaDoc instanceName () {
86         FileObject fo = proj.getFileObject();
87         if (fo != null) {
88             return fo.getName(); // without .xml extension
89
} else {
90             // ???
91
return ""; // NOI18N
92
}
93     }
94     
95     public Object JavaDoc instanceCreate () {
96         init();
97         return this;
98     }
99     
100     private void init() {
101         if (inited) {
102             return;
103         }
104         inited = true;
105         proj.addChangeListener(WeakListeners.change(this, proj));
106         OpenProjects.getDefault().addPropertyChangeListener(WeakListeners.propertyChange(this, OpenProjects.getDefault()));
107         changeSupport = new PropertyChangeSupport JavaDoc(this);
108     }
109     
110     public void actionPerformed (ActionEvent JavaDoc ignore) {
111         // #21355 similar to fix of #16720 - don't do this in the event thread...
112
RequestProcessor.getDefault().post(new Runnable JavaDoc() {
113             public void run() {
114                 TargetExecutor exec = new TargetExecutor (proj, null);
115                 try {
116                     exec.execute ();
117                 } catch (IOException JavaDoc ioe) {
118                     AntModule.err.notify (ioe);
119                 }
120             }
121         });
122     }
123     
124     public boolean isEnabled () {
125         if (proj.getFile() == null) {
126             return false; // cannot run script not on disk
127
}
128         // #21249: if it delegates to a script in a project, enable only if that project is open
129
Element JavaDoc root = proj.getProjectElement();
130         if (root == null) {
131             return false; // misparse
132
}
133         NodeList JavaDoc nl = root.getElementsByTagName("ant"); // NOI18N
134
if (nl.getLength() == 1) {
135             Element JavaDoc ant = (Element JavaDoc) nl.item(0);
136             String JavaDoc antfile = ant.getAttribute("antfile"); // NOI18N
137
if (antfile.length() == 0) {
138                 String JavaDoc dir = ant.getAttribute("dir"); // NOI18N
139
if (dir.length() > 0) {
140                     antfile = dir + File.separatorChar + "build.xml"; // NOI18N
141
}
142             }
143             if (antfile.length() > 0) {
144                 FileObject fo = FileUtil.toFileObject(new File JavaDoc(antfile));
145                 if (fo != null) {
146                     Project owner = FileOwnerQuery.getOwner(fo);
147                     if (owner != null) {
148                         return Arrays.asList(OpenProjects.getDefault().getOpenProjects()).contains(owner);
149                     }
150                 }
151             }
152         }
153         return true;
154     }
155     
156     public void setEnabled (boolean b) {
157         assert false;
158     }
159     
160     public Object JavaDoc getValue (String JavaDoc key) {
161         if (Action.NAME.equals (key)) {
162             Element JavaDoc el = proj.getProjectElement ();
163             if (el != null) {
164                 String JavaDoc pname = el.getAttribute ("name"); // NOI18N
165
return Actions.cutAmpersand(pname);
166             }
167         } else if (Action.SMALL_ICON.equals (key)) {
168             try {
169                 return new ImageIcon JavaDoc(new URL JavaDoc("nbresloc:/org/apache/tools/ant/module/resources/AntIcon.gif"));
170             } catch (MalformedURLException JavaDoc e) {
171                 throw new AssertionError JavaDoc(e);
172             }
173         } else if (Action.MNEMONIC_KEY.equals (key)) {
174             Element JavaDoc el = proj.getProjectElement ();
175             if (el != null) {
176                 String JavaDoc pname = el.getAttribute ("name"); // NOI18N
177
int idx = Mnemonics.findMnemonicAmpersand(pname);
178                 if (idx != -1) {
179                     // XXX this is wrong, should use some method in Mnemonics...
180
return new Integer JavaDoc (pname.charAt (idx + 1));
181                 }
182             }
183             return new Integer JavaDoc (0); // #: 13084
184
}
185         return null;
186     }
187     
188     public void putValue (String JavaDoc key, Object JavaDoc value) {
189         // ignore
190
}
191     
192     public final void addPropertyChangeListener (PropertyChangeListener JavaDoc listener) {
193         changeSupport.addPropertyChangeListener(listener);
194     }
195     
196     public final void removePropertyChangeListener (PropertyChangeListener JavaDoc listener) {
197         changeSupport.removePropertyChangeListener(listener);
198     }
199     
200     public JMenuItem JavaDoc getMenuPresenter () {
201         class AntMenuItem extends JMenuItem JavaDoc implements DynamicMenuContent {
202             public AntMenuItem() {
203                 super(AntActionInstance.this);
204             }
205             public JComponent JavaDoc[] getMenuPresenters() {
206                 return isEnabled() ? new JComponent JavaDoc[] {this} : new JComponent JavaDoc[0];
207             }
208             public JComponent JavaDoc[] synchMenuPresenters(JComponent JavaDoc[] items) {
209                 return getMenuPresenters();
210             }
211         }
212         return new AntMenuItem();
213     }
214
215     public Component JavaDoc getToolbarPresenter () {
216         class AntButton extends JButton JavaDoc implements PropertyChangeListener JavaDoc {
217             public AntButton() {
218                 super(AntActionInstance.this);
219                 setVisible(isEnabled());
220                 AntActionInstance.this.addPropertyChangeListener(WeakListeners.propertyChange(this, AntActionInstance.this));
221             }
222             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
223                 if ("enabled".equals(evt.getPropertyName())) { // NOI18N
224
setVisible(isEnabled());
225                 }
226             }
227         }
228         return new AntButton();
229     }
230     
231     public void stateChanged (ChangeEvent JavaDoc ignore) {
232         // Ant script changed; maybe the project name changed with it.
233
// Or maybe it is now misparsed.
234
changeSupport.firePropertyChange(Action.NAME, null, getValue (Action.NAME));
235         changeSupport.firePropertyChange("enabled", null, isEnabled () ? Boolean.TRUE : Boolean.FALSE); // NOI18N
236
changeSupport.firePropertyChange(Action.MNEMONIC_KEY, null, getValue (Action.MNEMONIC_KEY));
237     }
238
239     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
240         // Open projects list may have changed.
241
changeSupport.firePropertyChange("enabled", null, isEnabled() ? Boolean.TRUE : Boolean.FALSE); // NOI18N
242
}
243     
244 }
245
Popular Tags