KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > rubyproject > execution > StopAction


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 package org.netbeans.modules.ruby.rubyproject.execution;
20
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.io.Reader JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25 import javax.swing.AbstractAction JavaDoc;
26 import javax.swing.Action JavaDoc;
27 import javax.swing.ImageIcon JavaDoc;
28 import org.openide.util.NbBundle;
29 import org.openide.util.RequestProcessor;
30
31 /**
32  * The StopAction is placed into the I/O window, allowing the user to halt
33  * execution.
34  *
35  * Based on the equivalent StopAction in the ant support.
36  *
37  * @author Tor Norbye
38  */

39 final class StopAction extends AbstractAction JavaDoc {
40     public List JavaDoc<RequestProcessor.Task> processorTasks = new ArrayList JavaDoc<RequestProcessor.Task>();
41     private List JavaDoc<Reader JavaDoc> readers = new ArrayList JavaDoc<Reader JavaDoc>();
42     public Process JavaDoc process;
43
44     public StopAction() {
45         setEnabled(false); // initially, until ready
46
}
47
48     public void addReader(Reader JavaDoc reader) {
49         readers.add(reader);
50     }
51
52     @Override JavaDoc
53     public Object JavaDoc getValue(String JavaDoc key) {
54         if (key.equals(Action.SMALL_ICON)) {
55             return new ImageIcon JavaDoc(ExecutionService.class.getResource(
56                     "/org/netbeans/modules/ruby/rubyproject/execution/stop.gif")); // NOI18N
57
} else if (key.equals(Action.SHORT_DESCRIPTION)) {
58             return NbBundle.getMessage(ExecutionService.class, "Stop");
59         } else {
60             return super.getValue(key);
61         }
62     }
63
64     public void notifyDone(RequestProcessor.Task task) {
65         if (processorTasks.contains(task)) {
66             processorTasks.remove(task);
67
68             // Done
69
if (processorTasks.size() == 0) {
70                 if (process != null) {
71                     process.destroy();
72                     process = null;
73                 }
74             }
75         }
76     }
77
78     public void actionPerformed(ActionEvent JavaDoc e) {
79         setEnabled(false); // discourage repeated clicking
80

81         for (RequestProcessor.Task task : processorTasks) {
82             task.cancel();
83         }
84     }
85 }
86
Popular Tags