1 19 20 package org.netbeans.core.execution; 21 22 import java.awt.Dialog ; 23 import java.awt.Dimension ; 24 import java.awt.GridBagConstraints ; 25 import java.awt.GridBagLayout ; 26 import java.awt.Image ; 27 import java.awt.Insets ; 28 import java.awt.event.ActionEvent ; 29 import java.awt.event.ActionListener ; 30 import java.awt.event.WindowAdapter ; 31 import java.beans.Introspector ; 32 import java.beans.PropertyChangeEvent ; 33 import java.beans.PropertyChangeListener ; 34 import java.beans.PropertyEditorManager ; 35 import java.util.ArrayList ; 36 import java.util.Arrays ; 37 import java.util.Collection ; 38 import java.util.Collections ; 39 import java.util.Iterator ; 40 import javax.swing.Action ; 41 import javax.swing.ActionMap ; 42 import javax.swing.Icon ; 43 import javax.swing.JButton ; 44 import javax.swing.JLabel ; 45 import javax.swing.JPanel ; 46 import javax.swing.UIManager ; 47 import javax.swing.event.ListDataEvent ; 48 import javax.swing.event.ListDataListener ; 49 import javax.swing.text.DefaultEditorKit ; 50 import org.netbeans.TopSecurityManager; 51 import org.netbeans.progress.module.Controller; 52 import org.netbeans.progress.spi.InternalHandle; 53 import org.openide.DialogDescriptor; 54 import org.openide.DialogDisplayer; 55 import org.openide.awt.Mnemonics; 56 import org.openide.execution.ExecutorTask; 57 import org.openide.explorer.ExplorerManager; 58 import org.openide.explorer.ExplorerUtils; 59 import org.openide.explorer.view.ListView; 60 import org.openide.modules.ModuleInstall; 61 import org.openide.nodes.AbstractNode; 62 import org.openide.nodes.Children; 63 import org.openide.nodes.Node; 64 import org.openide.nodes.NodeEvent; 65 import org.openide.nodes.NodeListener; 66 import org.openide.nodes.NodeMemberEvent; 67 import org.openide.nodes.NodeReorderEvent; 68 import org.openide.util.Lookup; 69 import org.openide.util.Mutex; 70 import org.openide.util.NbBundle; 71 import org.openide.util.Utilities; 72 import org.openide.util.actions.SystemAction; 73 74 80 public class Install extends ModuleInstall { 81 82 private static final String BEANINFO_PATH 83 = "org.netbeans.core.execution.beaninfo"; private static final String EDITOR_PATH 85 = "org.netbeans.core.execution.beaninfo.editors"; 87 public void restored() { 88 TopSecurityManager.register(SecMan.DEFAULT); 89 90 String [] sp = Introspector.getBeanInfoSearchPath(); 92 java.util.List <String > paths = Arrays.asList(sp); 93 if(!paths.contains(BEANINFO_PATH)) { 94 paths = new ArrayList <String >(paths); 95 paths.add(BEANINFO_PATH); 96 Introspector.setBeanInfoSearchPath( 97 paths.toArray(new String [0])); 98 } 99 100 sp = PropertyEditorManager.getEditorSearchPath(); 102 paths = Arrays.asList(sp); 103 if(!paths.contains(EDITOR_PATH)) { 104 paths = new ArrayList <String >(paths); 105 paths.add(EDITOR_PATH); 106 PropertyEditorManager.setEditorSearchPath( 107 paths.toArray(new String [0])); 108 } 109 } 110 111 public void uninstalled() { 112 showPendingTasks(); 113 114 TopSecurityManager.unregister(SecMan.DEFAULT); 115 116 String [] sp = Introspector.getBeanInfoSearchPath(); 118 java.util.List <String > paths = Arrays.asList(sp); 119 if(paths.contains(BEANINFO_PATH)) { 120 paths = new ArrayList <String >(paths); 121 paths.remove(BEANINFO_PATH); 122 Introspector.setBeanInfoSearchPath( 123 paths.toArray(new String [0])); 124 } 125 126 sp = PropertyEditorManager.getEditorSearchPath(); 128 paths = Arrays.asList(sp); 129 if(paths.contains(EDITOR_PATH)) { 130 paths = new ArrayList <String >(paths); 131 paths.remove(EDITOR_PATH); 132 PropertyEditorManager.setEditorSearchPath( 133 paths.toArray(new String [0])); 134 } 135 } 136 137 public boolean closing() { 138 return showPendingTasks(); 139 } 140 141 145 private static class PendingDialogCloser extends WindowAdapter implements Runnable , 146 PropertyChangeListener , ActionListener , NodeListener { 147 private Dialog [] dialogHolder; 148 private Object exitOption; 149 PendingDialogCloser(Dialog [] holder, Object exit) { 150 dialogHolder = holder; 151 exitOption = exit; 152 } 153 154 public void run() { 155 dialogHolder[0].setVisible(false); 156 } 157 158 public void propertyChange(PropertyChangeEvent evt) { 161 if(ExplorerManager.PROP_EXPLORED_CONTEXT.equals(evt.getPropertyName())) { 162 checkClose(); 163 } 164 } 165 166 public void actionPerformed(ActionEvent evt) { 168 if(evt.getSource() == exitOption) { 169 killPendingTasks(); 170 Mutex.EVENT.readAccess(this); } 172 } 173 174 public void childrenRemoved(NodeMemberEvent evt) { 175 checkClose(); 176 } 177 178 public void windowOpened(java.awt.event.WindowEvent evt) { 180 checkClose(); 181 } 182 183 185 private void checkClose() { 186 if(dialogHolder[0] != null && getPendingTasks().isEmpty()) { 187 Mutex.EVENT.readAccess(this); 188 } 189 } 190 191 public void childrenAdded(NodeMemberEvent ev) {} 193 public void childrenReordered(NodeReorderEvent ev) {} 194 public void nodeDestroyed(NodeEvent ev) {} 195 196 } 197 198 200 206 private static boolean showPendingTasks() { 207 if (Boolean.getBoolean("netbeans.full.hack") || getPendingTasks().isEmpty()) { 211 return true; 212 } 213 214 EM panel = new EM(); 215 216 Dialog [] dialog = new Dialog [1]; 217 Node root = new AbstractNode(new PendingChildren()); 218 219 220 JButton exitOption = new JButton (); 221 Mnemonics.setLocalizedText(exitOption, NbBundle.getMessage(Install.class, "LAB_EndTasks")); 222 exitOption.getAccessibleContext().setAccessibleDescription( 225 NbBundle.getMessage(Install.class, "ACSD_EndTasks")); 226 227 PendingDialogCloser closer = new PendingDialogCloser(dialog, exitOption); 228 229 panel.getExplorerManager().setRootContext(root); 230 panel.getExplorerManager().addPropertyChangeListener(closer); 232 233 DialogDescriptor dd = new DialogDescriptor( 234 panel, 235 NbBundle.getMessage(Install.class, "CTL_PendingTitle"), 236 true, new Object [] { 238 exitOption, 239 DialogDescriptor.CANCEL_OPTION 240 }, 241 exitOption, 242 DialogDescriptor.DEFAULT_ALIGN, 243 null, 244 closer 245 ); 246 dd.setHelpCtx(null); 248 249 if(!getPendingTasks().isEmpty()) { 250 root.addNodeListener(closer); 251 252 dialog[0] = DialogDisplayer.getDefault().createDialog(dd); 253 254 dialog[0].addWindowListener(closer); 255 256 dialog[0].setVisible(true); 257 dialog[0].dispose(); 258 259 if(dd.getValue() == DialogDescriptor.CANCEL_OPTION 260 || dd.getValue() == DialogDescriptor.CLOSED_OPTION) { 261 return false; 262 } 263 264 } 265 266 return true; 267 } 268 269 private static class EM extends JPanel implements ExplorerManager.Provider { 270 private ExplorerManager manager = new ExplorerManager(); 271 private org.openide.util.Lookup lookup; 272 273 public EM() { 274 manager = new ExplorerManager(); 275 ActionMap map = getActionMap(); 276 map.put(DefaultEditorKit.copyAction, ExplorerUtils.actionCopy(manager)); 277 map.put(DefaultEditorKit.cutAction, ExplorerUtils.actionCut(manager)); 278 map.put(DefaultEditorKit.pasteAction, ExplorerUtils.actionPaste(manager)); 279 map.put("delete", ExplorerUtils.actionDelete(manager, true)); 281 lookup = ExplorerUtils.createLookup(manager, map); 282 283 initComponent(); 284 } 285 286 private void initComponent() { 287 setLayout(new GridBagLayout ()); 288 289 GridBagConstraints cons = new GridBagConstraints (); 290 cons.gridx = 0; 291 cons.gridy = 0; 292 cons.weightx = 1.0D; 293 cons.fill = GridBagConstraints.HORIZONTAL; 294 cons.insets = new Insets (11, 11, 0, 12); 295 296 JLabel label = new JLabel (); 297 Mnemonics.setLocalizedText(label, NbBundle.getMessage(Install.class, "LAB_PendingTasks")); 298 299 add(label, cons); 300 301 cons.gridy = 1; 302 cons.weighty = 1.0D; 303 cons.fill = GridBagConstraints.BOTH; 304 cons.insets = new Insets (2, 11, 0, 12); 305 306 ListView view = new ListView(); 307 view.setBorder(UIManager.getBorder("Nb.ScrollPane.border")); 309 label.setLabelFor(view); 310 311 add(view, cons); 312 313 view.getAccessibleContext().setAccessibleDescription( 314 NbBundle.getMessage(Install.class, "ACSD_PendingTasks")); 315 getAccessibleContext().setAccessibleDescription( 316 NbBundle.getMessage(Install.class, "ACSD_PendingTitle")); 317 318 Dimension origSize = getPreferredSize(); 320 setPreferredSize(new Dimension (origSize.width * 5 / 4, origSize.height)); 321 } 322 323 public ExplorerManager getExplorerManager() { 324 return manager; 325 } 326 public Lookup getLookup() { 327 return lookup; 328 } 329 public void addNotify() { 330 super.addNotify(); 331 ExplorerUtils.activateActions(manager, true); 332 } 333 public void removeNotify() { 334 ExplorerUtils.activateActions(manager, false); 335 super.removeNotify(); 336 } 337 } 338 339 342 static Collection <?> getPendingTasks() { 343 344 ArrayList <Object > pendingTasks = new ArrayList <Object >( 10 ); pendingTasks.addAll(org.netbeans.core.ModuleActions.getDefaultInstance().getRunningActions()); 348 349 ExecutionEngine ee = ExecutionEngine.getExecutionEngine(); 350 if (ee != null) { 351 pendingTasks.addAll(ee.getRunningTasks()); 352 } 353 354 pendingTasks.addAll(Arrays.asList(Controller.getDefault().getModel().getHandles())); 355 356 360 return pendingTasks; 361 } 362 363 364 private static void killPendingTasks() { 365 killRunningExecutors(); 368 369 } 372 373 374 private static void killRunningExecutors() { 375 ExecutionEngine ee = ExecutionEngine.getExecutionEngine(); 376 if (ee == null) { 377 return; 378 } 379 ArrayList <ExecutorTask> tasks = new ArrayList <ExecutorTask>(ee.getRunningTasks()); 380 381 for ( Iterator <ExecutorTask> it = tasks.iterator(); it.hasNext(); ) { 382 ExecutorTask et = it.next(); 383 if ( !et.isFinished() ) { 384 et.stop(); 385 } 386 } 387 388 } 389 390 391 392 static class PendingChildren extends Children.Keys<Object > 393 implements ExecutionListener, ListDataListener { 394 395 397 399 400 401 public PendingChildren() { 402 415 416 ExecutionEngine ee = ExecutionEngine.getExecutionEngine(); 417 if (ee != null) { 418 ee.addExecutionListener(this); 419 } 420 Controller.getDefault().getModel().addListDataListener(this); 421 } 422 423 426 protected Node[] createNodes(Object key) { 427 Node n = null; 428 if(key instanceof Action ) { 429 Action action = (Action )key; 430 Icon icon = (action instanceof SystemAction) ? 431 ((SystemAction)action).getIcon() : null; 432 433 String actionName = (String )action.getValue(Action.NAME); 434 if (actionName == null) actionName = ""; actionName = org.openide.awt.Actions.cutAmpersand(actionName); 436 n = new NoActionNode(icon, actionName, NbBundle.getMessage( 437 Install.class, "CTL_ActionInProgress", actionName)); 438 } else if (key instanceof ExecutorTask) { 439 n = new NoActionNode(null, key.toString(), 440 NbBundle.getMessage(Install.class, "CTL_PendingExternalProcess2", 441 ExecutionEngine.getExecutionEngine().getRunningTaskName((ExecutorTask) key)) 443 ); 444 } else if (key instanceof InternalHandle) { 445 n = new NoActionNode(null, ((InternalHandle)key).getDisplayName(), null); 446 } 447 return n == null ? null : new Node[] { n }; 448 } 449 450 451 protected void addNotify() { 452 setKeys(getPendingTasks()); 453 super.addNotify(); 454 } 455 456 457 protected void removeNotify() { 458 setKeys(Collections.emptySet()); 459 super.removeNotify(); 460 ExecutionEngine ee = ExecutionEngine.getExecutionEngine(); 461 if (ee != null) { 462 ee.removeExecutionListener(this); 463 } 464 Controller.getDefault().getModel().removeListDataListener(this); 465 } 466 467 469 public void startedExecution( ExecutionEvent ev ) { 470 setKeys(getPendingTasks()); 471 } 472 473 public void finishedExecution( ExecutionEvent ev ) { 474 setKeys(getPendingTasks()); 475 } 476 477 public void intervalAdded(ListDataEvent e) { 478 setKeys(getPendingTasks()); 479 } 480 481 public void intervalRemoved(ListDataEvent e) { 482 setKeys(getPendingTasks()); 483 } 484 485 486 public void contentsChanged(ListDataEvent e) { 487 setKeys(getPendingTasks()); 488 } 489 490 } 492 493 private static class NoActionNode extends AbstractNode { 494 private Image img; 495 496 497 public NoActionNode(Icon icon, String name, String display) { 498 super(Children.LEAF); 499 if (icon != null) img = Utilities.icon2Image(icon); 500 501 setName(name); 502 if (display != null) setDisplayName(display); 503 } 504 505 506 public Action [] getActions(boolean context) { 507 return new Action [0]; 508 } 509 510 512 public Image getIcon(int type) { 513 return img == null ? super.getIcon(type) : img; 514 } 515 } 516 } 517 | Popular Tags |