KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.EventQueue JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.WeakHashMap JavaDoc;
26 import javax.swing.JComponent JavaDoc;
27 import javax.swing.JMenuItem JavaDoc;
28 import org.openide.awt.DynamicMenuContent;
29 import org.openide.awt.Mnemonics;
30 import org.openide.util.HelpCtx;
31 import org.openide.util.NbBundle;
32 import org.openide.util.actions.CallableSystemAction;
33 import org.openide.util.actions.SystemAction;
34
35 /**
36  * Action which stops the currently running Ant process.
37  * If there is more than one, a dialog appears asking you to select one.
38  * @author Jesse Glick
39  * @see "issue #43143"
40  */

41 public final class StopBuildingAction extends CallableSystemAction {
42     
43     /**
44      * Map from active processing threads to their process display names.
45      */

46     private static final Map JavaDoc<Thread JavaDoc,String JavaDoc> activeProcesses = new WeakHashMap JavaDoc<Thread JavaDoc,String JavaDoc>();
47     
48     static void registerProcess(Thread JavaDoc t, String JavaDoc displayName) {
49         synchronized (activeProcesses) {
50             assert !activeProcesses.containsKey(t);
51             activeProcesses.put(t, displayName);
52         }
53         EventQueue.invokeLater(new Runnable JavaDoc() {
54             public void run() {
55                 SystemAction.get(StopBuildingAction.class).setEnabled(true);
56             }
57         });
58     }
59     
60     static void unregisterProcess(Thread JavaDoc t) {
61         final boolean enable;
62         synchronized (activeProcesses) {
63             assert activeProcesses.containsKey(t);
64             activeProcesses.remove(t);
65             enable = !activeProcesses.isEmpty();
66         }
67         EventQueue.invokeLater(new Runnable JavaDoc() {
68             public void run() {
69                 SystemAction.get(StopBuildingAction.class).setEnabled(enable);
70             }
71         });
72     }
73     
74     @Override JavaDoc
75     public void performAction() {
76         Thread JavaDoc[] toStop = null;
77         synchronized (activeProcesses) {
78             assert !activeProcesses.isEmpty();
79             if (activeProcesses.size() == 1) {
80                 toStop = activeProcesses.keySet().toArray(new Thread JavaDoc[1]);
81             }
82         }
83         if (toStop == null) {
84             // More than one, need to select one.
85
Map JavaDoc<Thread JavaDoc,String JavaDoc> activeProcessesClone;
86             synchronized (activeProcesses) {
87                 activeProcessesClone = new HashMap JavaDoc<Thread JavaDoc,String JavaDoc>(activeProcesses);
88             }
89             toStop = StopBuildingAlert.selectProcessToKill(activeProcessesClone);
90             synchronized (activeProcesses) {
91                 for (int i = 0; i < toStop.length; i++) {
92                     if (!activeProcesses.containsKey(toStop[i])) {
93                         // Oops, process ended while it was being selected... just ignore.
94
toStop[i] = null;
95                     }
96                 }
97             }
98         }
99         for (Thread JavaDoc t : toStop) {
100             if (t != null) {
101                 TargetExecutor.stopProcess(t);
102             }
103         }
104     }
105
106     @Override JavaDoc
107     public String JavaDoc getName() {
108         return NbBundle.getMessage(StopBuildingAction.class, "LBL_stop_building");
109     }
110
111     @Override JavaDoc
112     public HelpCtx getHelpCtx() {
113         return null;
114     }
115
116     @Override JavaDoc
117     protected void initialize() {
118         super.initialize();
119         setEnabled(false); // no processes initially
120
}
121
122     @Override JavaDoc
123     protected boolean asynchronous() {
124         return false;
125     }
126     
127     @Override JavaDoc
128     public JMenuItem JavaDoc getMenuPresenter() {
129         class SpecialMenuItem extends JMenuItem JavaDoc implements DynamicMenuContent {
130             public SpecialMenuItem() {
131                 super(StopBuildingAction.this);
132             }
133             public JComponent JavaDoc[] getMenuPresenters() {
134                 String JavaDoc label;
135                 synchronized (activeProcesses) {
136                     switch (activeProcesses.size()) {
137                         case 0:
138                             label = NbBundle.getMessage(StopBuildingAction.class, "LBL_stop_building");
139                             break;
140                         case 1:
141                             label = NbBundle.getMessage(StopBuildingAction.class, "LBL_stop_building_one",
142                                     activeProcesses.values().iterator().next());
143                             break;
144                         default:
145                             label = NbBundle.getMessage(StopBuildingAction.class, "LBL_stop_building_many");
146                     }
147                 }
148                 Mnemonics.setLocalizedText(this, label);
149                 return new JComponent JavaDoc[] {this};
150             }
151             public JComponent JavaDoc[] synchMenuPresenters(JComponent JavaDoc[] items) {
152                 return getMenuPresenters();
153             }
154         }
155         return new SpecialMenuItem();
156     }
157     
158 }
159
Popular Tags