001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.apache.tools.ant.module.loader;
043:
044: import java.awt.Component;
045: import java.awt.event.ActionEvent;
046: import java.beans.PropertyChangeEvent;
047: import java.beans.PropertyChangeListener;
048: import java.beans.PropertyChangeSupport;
049: import java.io.File;
050: import java.io.IOException;
051: import java.io.ObjectInputStream;
052: import java.net.MalformedURLException;
053: import java.net.URL;
054: import java.util.Arrays;
055: import javax.swing.Action;
056: import javax.swing.ImageIcon;
057: import javax.swing.JButton;
058: import javax.swing.JComponent;
059: import javax.swing.JMenuItem;
060: import javax.swing.event.ChangeEvent;
061: import javax.swing.event.ChangeListener;
062: import org.apache.tools.ant.module.AntModule;
063: import org.apache.tools.ant.module.api.AntProjectCookie;
064: import org.apache.tools.ant.module.run.TargetExecutor;
065: import org.netbeans.api.project.FileOwnerQuery;
066: import org.netbeans.api.project.Project;
067: import org.netbeans.api.project.ui.OpenProjects;
068: import org.openide.awt.Actions;
069: import org.openide.awt.DynamicMenuContent;
070: import org.openide.awt.Mnemonics;
071: import org.openide.cookies.InstanceCookie;
072: import org.openide.filesystems.FileObject;
073: import org.openide.filesystems.FileUtil;
074: import org.openide.util.actions.Presenter;
075: import org.openide.util.RequestProcessor;
076: import org.openide.util.WeakListeners;
077: import org.w3c.dom.Element;
078: import org.w3c.dom.NodeList;
079:
080: /** An instance cookie providing an action running a script.
081: * The action provides the standard presenters, so may be used
082: * e.g. in menu items.
083: */
084: public class AntActionInstance implements InstanceCookie, Action,
085: Presenter.Menu, Presenter.Toolbar, ChangeListener,
086: PropertyChangeListener {
087:
088: private boolean inited;
089: private final AntProjectCookie proj;
090: private transient PropertyChangeSupport changeSupport;
091:
092: public AntActionInstance(AntProjectCookie proj) {
093: this .proj = proj;
094: }
095:
096: private void readObject(ObjectInputStream in) throws IOException,
097: ClassNotFoundException {
098: in.defaultReadObject();
099: init();
100: }
101:
102: public Class instanceClass() {
103: return AntActionInstance.class;
104: }
105:
106: public String instanceName() {
107: FileObject fo = proj.getFileObject();
108: if (fo != null) {
109: return fo.getName(); // without .xml extension
110: } else {
111: // ???
112: return ""; // NOI18N
113: }
114: }
115:
116: public Object instanceCreate() {
117: init();
118: return this ;
119: }
120:
121: private void init() {
122: if (inited) {
123: return;
124: }
125: inited = true;
126: proj.addChangeListener(WeakListeners.change(this , proj));
127: OpenProjects.getDefault().addPropertyChangeListener(
128: WeakListeners.propertyChange(this , OpenProjects
129: .getDefault()));
130: changeSupport = new PropertyChangeSupport(this );
131: }
132:
133: public void actionPerformed(ActionEvent ignore) {
134: // #21355 similar to fix of #16720 - don't do this in the event thread...
135: RequestProcessor.getDefault().post(new Runnable() {
136: public void run() {
137: TargetExecutor exec = new TargetExecutor(proj, null);
138: try {
139: exec.execute();
140: } catch (IOException ioe) {
141: AntModule.err.notify(ioe);
142: }
143: }
144: });
145: }
146:
147: public boolean isEnabled() {
148: if (proj.getFile() == null) {
149: return false; // cannot run script not on disk
150: }
151: // #21249: if it delegates to a script in a project, enable only if that project is open
152: Element root = proj.getProjectElement();
153: if (root == null) {
154: return false; // misparse
155: }
156: NodeList nl = root.getElementsByTagName("ant"); // NOI18N
157: if (nl.getLength() == 1) {
158: Element ant = (Element) nl.item(0);
159: String antfile = ant.getAttribute("antfile"); // NOI18N
160: if (antfile.length() == 0) {
161: String dir = ant.getAttribute("dir"); // NOI18N
162: if (dir.length() > 0) {
163: antfile = dir + File.separatorChar + "build.xml"; // NOI18N
164: }
165: }
166: if (antfile.length() > 0) {
167: FileObject fo = FileUtil
168: .toFileObject(new File(antfile));
169: if (fo != null) {
170: Project owner = FileOwnerQuery.getOwner(fo);
171: if (owner != null) {
172: return Arrays.asList(
173: OpenProjects.getDefault()
174: .getOpenProjects()).contains(
175: owner);
176: }
177: }
178: }
179: }
180: return true;
181: }
182:
183: public void setEnabled(boolean b) {
184: assert false;
185: }
186:
187: public Object getValue(String key) {
188: if (Action.NAME.equals(key)) {
189: Element el = proj.getProjectElement();
190: if (el != null) {
191: String pname = el.getAttribute("name"); // NOI18N
192: return Actions.cutAmpersand(pname);
193: }
194: } else if (Action.SMALL_ICON.equals(key)) {
195: try {
196: return new ImageIcon(
197: new URL(
198: "nbresloc:/org/apache/tools/ant/module/resources/AntIcon.gif"));
199: } catch (MalformedURLException e) {
200: throw new AssertionError(e);
201: }
202: } else if (Action.MNEMONIC_KEY.equals(key)) {
203: Element el = proj.getProjectElement();
204: if (el != null) {
205: String pname = el.getAttribute("name"); // NOI18N
206: int idx = Mnemonics.findMnemonicAmpersand(pname);
207: if (idx != -1) {
208: // XXX this is wrong, should use some method in Mnemonics...
209: return pname.charAt(idx + 1);
210: }
211: }
212: return 0; // #: 13084
213: }
214: return null;
215: }
216:
217: public void putValue(String key, Object value) {
218: // ignore
219: }
220:
221: public final void addPropertyChangeListener(
222: PropertyChangeListener listener) {
223: changeSupport.addPropertyChangeListener(listener);
224: }
225:
226: public final void removePropertyChangeListener(
227: PropertyChangeListener listener) {
228: changeSupport.removePropertyChangeListener(listener);
229: }
230:
231: public JMenuItem getMenuPresenter() {
232: class AntMenuItem extends JMenuItem implements
233: DynamicMenuContent {
234: public AntMenuItem() {
235: super (AntActionInstance.this );
236: }
237:
238: public JComponent[] getMenuPresenters() {
239: return isEnabled() ? new JComponent[] { this }
240: : new JComponent[0];
241: }
242:
243: public JComponent[] synchMenuPresenters(JComponent[] items) {
244: return getMenuPresenters();
245: }
246: }
247: return new AntMenuItem();
248: }
249:
250: public Component getToolbarPresenter() {
251: class AntButton extends JButton implements
252: PropertyChangeListener {
253: public AntButton() {
254: super (AntActionInstance.this );
255: // XXX setVisible(false) said to be poor on GTK L&F; consider using #26338 instead
256: setVisible(isEnabled());
257: AntActionInstance.this
258: .addPropertyChangeListener(WeakListeners
259: .propertyChange(this ,
260: AntActionInstance.this ));
261: }
262:
263: public void propertyChange(PropertyChangeEvent evt) {
264: if ("enabled".equals(evt.getPropertyName())) { // NOI18N
265: setVisible(isEnabled());
266: }
267: }
268: }
269: return new AntButton();
270: }
271:
272: public void stateChanged(ChangeEvent ignore) {
273: // Ant script changed; maybe the project name changed with it.
274: // Or maybe it is now misparsed.
275: changeSupport.firePropertyChange(Action.NAME, null,
276: getValue(Action.NAME));
277: changeSupport.firePropertyChange("enabled", null,
278: isEnabled() ? Boolean.TRUE : Boolean.FALSE); // NOI18N
279: changeSupport.firePropertyChange(Action.MNEMONIC_KEY, null,
280: getValue(Action.MNEMONIC_KEY));
281: }
282:
283: public void propertyChange(PropertyChangeEvent evt) {
284: // Open projects list may have changed.
285: changeSupport.firePropertyChange("enabled", null,
286: isEnabled() ? Boolean.TRUE : Boolean.FALSE); // NOI18N
287: }
288:
289: }
|