KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > module > run > LastTargetExecuted


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.run;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import javax.swing.event.ChangeEvent JavaDoc;
29 import javax.swing.event.ChangeListener JavaDoc;
30 import org.apache.tools.ant.module.api.AntProjectCookie;
31 import org.openide.execution.ExecutorTask;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.loaders.DataObject;
35 import org.openide.loaders.DataObjectNotFoundException;
36
37 /**
38  * Records the last Ant target(s) that was executed.
39  * @author Jesse Glick
40  */

41 public class LastTargetExecuted {
42     
43     private LastTargetExecuted() {}
44     
45     private static File JavaDoc buildScript;
46     private static int verbosity;
47     private static String JavaDoc[] targets;
48     private static Map JavaDoc<String JavaDoc,String JavaDoc> properties;
49     
50     /** Called from {@link TargetExecutor}. */
51     static void record(File JavaDoc buildScript, int verbosity, String JavaDoc[] targets, Map JavaDoc<String JavaDoc,String JavaDoc> properties) {
52         LastTargetExecuted.buildScript = buildScript;
53         LastTargetExecuted.verbosity = verbosity;
54         LastTargetExecuted.targets = targets;
55         LastTargetExecuted.properties = properties;
56         fireChange();
57     }
58     
59     /**
60      * Get the last build script to be run.
61      * @return the last-run build script, or null if nothing has been run yet (or the build script disappeared etc.)
62      */

63     public static AntProjectCookie getLastBuildScript() {
64         if (buildScript != null && buildScript.isFile()) {
65             FileObject fo = FileUtil.toFileObject(buildScript);
66             assert fo != null;
67             try {
68                 return DataObject.find(fo).getCookie(AntProjectCookie.class);
69             } catch (DataObjectNotFoundException e) {
70                 assert false : e;
71             }
72         }
73         return null;
74     }
75     
76     /**
77      * Get the last target names to be run.
78      * @return a list of one or more targets, or null for the default target
79      */

80     public static String JavaDoc[] getLastTargets() {
81         return targets;
82     }
83     
84     /**
85      * Get a display name (as it would appear in the Output Window) for the last process.
86      * @return a process display name, or null if nothing has been run yet
87      */

88     public static String JavaDoc getProcessDisplayName() {
89         AntProjectCookie apc = getLastBuildScript();
90         if (apc != null) {
91             return TargetExecutor.getProcessDisplayName(apc, targets != null ? Arrays.asList(targets) : null);
92         } else {
93             return null;
94         }
95     }
96     
97     /**
98      * Try to rerun the last task.
99      */

100     public static ExecutorTask rerun() throws IOException JavaDoc {
101         AntProjectCookie apc = getLastBuildScript();
102         if (apc == null) {
103             // Can happen in case the build script was deleted (similar to #84874).
104
// Also make sure to disable RunLastTargetAction.
105
fireChange();
106             return null;
107         }
108         TargetExecutor t = new TargetExecutor(apc, targets);
109         t.setVerbosity(verbosity);
110         t.setProperties(properties);
111         return t.execute();
112     }
113     
114     private static final List JavaDoc<ChangeListener JavaDoc> listeners = new ArrayList JavaDoc<ChangeListener JavaDoc>();
115     
116     public static void addChangeListener(ChangeListener JavaDoc l) {
117         synchronized (listeners) {
118             listeners.add(l);
119         }
120     }
121     
122     public static void removeChangeListener(ChangeListener JavaDoc l) {
123         synchronized (listeners) {
124             listeners.remove(l);
125         }
126     }
127     
128     private static void fireChange() {
129         ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(LastTargetExecuted.class);
130         ChangeListener JavaDoc[] ls;
131         synchronized (listeners) {
132             ls = listeners.toArray(new ChangeListener JavaDoc[listeners.size()]);
133         }
134         for (ChangeListener JavaDoc l : ls) {
135             l.stateChanged(ev);
136         }
137     }
138     
139 }
140
Popular Tags