KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
23
24 import javax.swing.AbstractAction JavaDoc;
25 import javax.swing.Action JavaDoc;
26 import javax.swing.ImageIcon JavaDoc;
27
28 import org.openide.ErrorManager;
29 import org.openide.cookies.SaveCookie;
30 import org.openide.filesystems.FileAttributeEvent;
31 import org.openide.filesystems.FileChangeListener;
32 import org.openide.filesystems.FileEvent;
33 import org.openide.filesystems.FileObject;
34 import org.openide.filesystems.FileRenameEvent;
35 import org.openide.filesystems.FileUtil;
36 import org.openide.loaders.DataObject;
37 import org.openide.loaders.DataObjectNotFoundException;
38 import org.openide.util.NbBundle;
39
40
41 /**
42  * The RerunAction is placed into the I/O window, allowing the user to restart
43  * a particular execution context.
44  *
45  * Based on the equivalent RerunAction in the ant support.
46  *
47  * @author Tor Norbye
48  */

49 final class RerunAction extends AbstractAction JavaDoc implements FileChangeListener {
50     private ExecutionService prototype;
51     private FileObject fileObject;
52
53     public RerunAction(ExecutionService prototype, FileObject fileObject) {
54         this.prototype = prototype;
55         setEnabled(false); // initially, until ready
56
this.fileObject = fileObject;
57
58         if (fileObject != null) {
59             fileObject.addFileChangeListener(FileUtil.weakFileChangeListener(this, fileObject));
60         }
61     }
62
63     @Override JavaDoc
64     public Object JavaDoc getValue(String JavaDoc key) {
65         if (key.equals(Action.SMALL_ICON)) {
66             return new ImageIcon JavaDoc(ExecutionService.class.getResource(
67                     "/org/netbeans/modules/ruby/rubyproject/execution/rerun.png")); // NOI18N
68
} else if (key.equals(Action.SHORT_DESCRIPTION)) {
69             return NbBundle.getMessage(ExecutionService.class, "Rerun");
70         } else {
71             return super.getValue(key);
72         }
73     }
74
75     public void actionPerformed(ActionEvent JavaDoc e) {
76         setEnabled(false);
77
78         // Save the file before running it, if any
79
if (fileObject != null) {
80             // Save the file
81
try {
82                 DataObject dobj = DataObject.find(fileObject);
83
84                 if (dobj != null) {
85                     SaveCookie saveCookie = dobj.getCookie(SaveCookie.class);
86
87                     if (saveCookie != null) {
88                         saveCookie.save();
89                     }
90                 }
91             } catch (DataObjectNotFoundException donfe) {
92                 ErrorManager.getDefault().notify(donfe);
93             } catch (IOException JavaDoc ioe) {
94                 ErrorManager.getDefault().notify(ioe);
95             }
96         }
97
98         prototype.rerun();
99     }
100
101     public void fileDeleted(FileEvent fe) {
102         firePropertyChange("enabled", null, false); // NOI18N
103
}
104
105     public void fileFolderCreated(FileEvent fe) {
106     }
107
108     public void fileDataCreated(FileEvent fe) {
109     }
110
111     public void fileChanged(FileEvent fe) {
112     }
113
114     public void fileRenamed(FileRenameEvent fe) {
115     }
116
117     public void fileAttributeChanged(FileAttributeEvent fe) {
118     }
119
120     public boolean isEnabled() {
121         // #84874: should be disabled in case the original Ant script is now gone.
122
return super.isEnabled() && ((fileObject == null) || fileObject.isValid());
123     }
124 }
125
Popular Tags