KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > railsprojects > GenerateAction


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.railsprojects;
20
21 import java.awt.Dialog JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.swing.JButton JavaDoc;
27 import javax.swing.event.ChangeEvent JavaDoc;
28 import javax.swing.event.ChangeListener JavaDoc;
29 import javax.swing.event.ChangeListener JavaDoc;
30 import org.netbeans.api.project.FileOwnerQuery;
31 import org.netbeans.api.project.Project;
32 import org.netbeans.modules.ruby.rubyproject.execution.DirectoryFileLocator;
33 import org.netbeans.modules.ruby.rubyproject.execution.ExecutionDescriptor;
34 import org.netbeans.modules.ruby.rubyproject.execution.ExecutionService;
35 import org.openide.DialogDescriptor;
36 import org.openide.DialogDisplayer;
37 import org.openide.ErrorManager;
38 import org.openide.filesystems.FileObject;
39 import org.openide.filesystems.FileSystem;
40 import org.openide.filesystems.FileUtil;
41 import org.openide.loaders.DataObject;
42 import org.openide.nodes.Node;
43 import org.openide.util.HelpCtx;
44 import org.openide.util.Lookup;
45 import org.openide.util.NbBundle;
46 import org.openide.util.Task;
47 import org.openide.util.actions.NodeAction;
48
49
50 public final class GenerateAction extends NodeAction {
51     private boolean forcing;
52     private boolean preview;
53
54     protected void performAction(Node[] activatedNodes) {
55         Lookup lookup = activatedNodes[0].getLookup();
56         Project project = (Project)lookup.lookup(Project.class);
57
58         if (project == null) {
59             DataObject dataObject = (DataObject)lookup.lookup(DataObject.class);
60
61             if (dataObject == null) {
62                 return;
63             }
64
65             project = FileOwnerQuery.getOwner(dataObject.getPrimaryFile());
66         }
67
68         if (project == null) {
69             return;
70         }
71
72         Generator generator = (Generator)activatedNodes[0].getLookup().lookup(Generator.class);
73
74         if (generator == null) {
75             generator = Generator.controller;
76         }
77
78         boolean cancelled;
79         final JButton JavaDoc okButton = new JButton JavaDoc(NbBundle.getMessage(GenerateAction.class, "Ok"));
80         okButton.getAccessibleContext()
81                 .setAccessibleDescription(NbBundle.getMessage(GenerateAction.class, "AD_Ok"));
82
83         final GeneratorPanel panel = new GeneratorPanel(generator);
84         panel.setForcing(forcing);
85         panel.setPretend(preview);
86
87         Object JavaDoc[] options = new Object JavaDoc[] { okButton, DialogDescriptor.CANCEL_OPTION };
88
89         panel.setChangeListener(new ChangeListener JavaDoc() {
90                 public void stateChanged(ChangeEvent JavaDoc e) {
91                     String JavaDoc name = panel.getName();
92                     String JavaDoc type = panel.getType();
93                     boolean enabled = ((name.length() > 0) && (type.length() > 0));
94                     okButton.setEnabled(enabled);
95                 }
96             });
97
98         okButton.setEnabled(false);
99
100         DialogDescriptor desc =
101             new DialogDescriptor(panel,
102                 NbBundle.getMessage(GenerateAction.class, "GeneratorTitle"), true, options,
103                 options[0], DialogDescriptor.DEFAULT_ALIGN, null, null);
104         desc.setMessageType(DialogDescriptor.PLAIN_MESSAGE);
105
106         Dialog JavaDoc dlg = DialogDisplayer.getDefault().createDialog(desc);
107         dlg.setVisible(true);
108
109         if (desc.getValue() != options[0]) {
110             cancelled = true;
111         } else {
112             cancelled = false;
113         }
114
115         dlg.dispose();
116         forcing = panel.isForce(); // Persist state for next invocation (this session only)
117
preview = panel.isPretend();
118
119         if (!cancelled) {
120             final String JavaDoc name = panel.getName();
121             final String JavaDoc type = panel.getType();
122
123             if ((name.length() > 0) && (type.length() > 0)) { // TODO: Toggle OK state based on valid entry
124

125                 final FileObject dir = project.getProjectDirectory();
126                 final File JavaDoc pwd = FileUtil.toFile(project.getProjectDirectory());
127                 List JavaDoc<String JavaDoc> argvList = new ArrayList JavaDoc<String JavaDoc>();
128                 argvList.add("script" + File.separator + "generate"); // NOI18N
129
argvList.add(type);
130
131                 if (panel.isForce()) {
132                     argvList.add("--force"); // NOI18N
133
} else {
134                     argvList.add("--skip"); // NOI18N
135
}
136
137                 if (panel.isPretend()) {
138                     argvList.add("--pretend"); // NOI18N
139
}
140
141                 argvList.add(name);
142
143                 String JavaDoc[] firstParameterList = panel.getFirstParameterList();
144
145                 if (firstParameterList != null) {
146                     for (String JavaDoc parameter : firstParameterList) {
147                         argvList.add(parameter);
148                     }
149
150                     String JavaDoc[] remainingParameters = panel.getSecondParameterList();
151
152                     if (remainingParameters != null) {
153                         for (String JavaDoc parameter : remainingParameters) {
154                             argvList.add(parameter);
155                         }
156                     }
157                 }
158
159                 final String JavaDoc[] argv = argvList.toArray(new String JavaDoc[argvList.size()]);
160
161                 try {
162                     project.getProjectDirectory().getFileSystem().runAtomicAction(new FileSystem.AtomicAction() {
163                             public void run() throws IOException JavaDoc {
164                                 Task task =
165                                     new ExecutionService(new ExecutionDescriptor(
166                                             "Rails Generator", pwd, argv).fileLocator(
167                                             new DirectoryFileLocator(dir)).
168                                             addOutputRecognizer(RailsProjectGenerator.RAILS_GENERATOR)).run();
169
170                                 task.waitFinished();
171                             }
172                         });
173                     project.getProjectDirectory().getFileSystem().refresh(true);
174                 } catch (IOException JavaDoc ioe) {
175                     ErrorManager.getDefault().notify(ioe);
176                 }
177             }
178         }
179     }
180
181     public String JavaDoc getName() {
182         return NbBundle.getMessage(GenerateAction.class, "CTL_GenerateAction");
183     }
184
185     protected boolean enable(Node[] activatedNodes) {
186         if ((activatedNodes == null) || (activatedNodes.length != 1)) {
187             return false;
188         }
189
190         Lookup lookup = activatedNodes[0].getLookup();
191         Project project = (Project)lookup.lookup(Project.class);
192
193         if (project != null) {
194             return true;
195         }
196
197         DataObject dataObject = (DataObject)lookup.lookup(DataObject.class);
198
199         return dataObject != null;
200     }
201
202     protected void initialize() {
203         super.initialize();
204         // see org.openide.util.actions.SystemAction.iconResource() javadoc for more details
205
putValue("noIconInMenu", Boolean.TRUE);
206     }
207
208     public HelpCtx getHelpCtx() {
209         return HelpCtx.DEFAULT_HELP;
210     }
211
212     protected boolean asynchronous() {
213         return true;
214     }
215 }
216
Popular Tags