1 19 20 package org.apache.tools.ant.module.run; 21 22 import java.awt.EventQueue ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 import java.util.WeakHashMap ; 26 import javax.swing.JComponent ; 27 import javax.swing.JMenuItem ; 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 41 public final class StopBuildingAction extends CallableSystemAction { 42 43 46 private static final Map <Thread ,String > activeProcesses = new WeakHashMap <Thread ,String >(); 47 48 static void registerProcess(Thread t, String displayName) { 49 synchronized (activeProcesses) { 50 assert !activeProcesses.containsKey(t); 51 activeProcesses.put(t, displayName); 52 } 53 EventQueue.invokeLater(new Runnable () { 54 public void run() { 55 SystemAction.get(StopBuildingAction.class).setEnabled(true); 56 } 57 }); 58 } 59 60 static void unregisterProcess(Thread 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 () { 68 public void run() { 69 SystemAction.get(StopBuildingAction.class).setEnabled(enable); 70 } 71 }); 72 } 73 74 @Override 75 public void performAction() { 76 Thread [] toStop = null; 77 synchronized (activeProcesses) { 78 assert !activeProcesses.isEmpty(); 79 if (activeProcesses.size() == 1) { 80 toStop = activeProcesses.keySet().toArray(new Thread [1]); 81 } 82 } 83 if (toStop == null) { 84 Map <Thread ,String > activeProcessesClone; 86 synchronized (activeProcesses) { 87 activeProcessesClone = new HashMap <Thread ,String >(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 toStop[i] = null; 95 } 96 } 97 } 98 } 99 for (Thread t : toStop) { 100 if (t != null) { 101 TargetExecutor.stopProcess(t); 102 } 103 } 104 } 105 106 @Override 107 public String getName() { 108 return NbBundle.getMessage(StopBuildingAction.class, "LBL_stop_building"); 109 } 110 111 @Override 112 public HelpCtx getHelpCtx() { 113 return null; 114 } 115 116 @Override 117 protected void initialize() { 118 super.initialize(); 119 setEnabled(false); } 121 122 @Override 123 protected boolean asynchronous() { 124 return false; 125 } 126 127 @Override 128 public JMenuItem getMenuPresenter() { 129 class SpecialMenuItem extends JMenuItem implements DynamicMenuContent { 130 public SpecialMenuItem() { 131 super(StopBuildingAction.this); 132 } 133 public JComponent [] getMenuPresenters() { 134 String 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 [] {this}; 150 } 151 public JComponent [] synchMenuPresenters(JComponent [] items) { 152 return getMenuPresenters(); 153 } 154 } 155 return new SpecialMenuItem(); 156 } 157 158 } 159 | Popular Tags |