1 19 package org.netbeans.modules.ruby.railsprojects; 20 21 import java.awt.Dialog ; 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 import javax.swing.JButton ; 27 import javax.swing.event.ChangeEvent ; 28 import javax.swing.event.ChangeListener ; 29 import javax.swing.event.ChangeListener ; 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 okButton = new JButton (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 [] options = new Object [] { okButton, DialogDescriptor.CANCEL_OPTION }; 88 89 panel.setChangeListener(new ChangeListener () { 90 public void stateChanged(ChangeEvent e) { 91 String name = panel.getName(); 92 String 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 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(); preview = panel.isPretend(); 118 119 if (!cancelled) { 120 final String name = panel.getName(); 121 final String type = panel.getType(); 122 123 if ((name.length() > 0) && (type.length() > 0)) { 125 final FileObject dir = project.getProjectDirectory(); 126 final File pwd = FileUtil.toFile(project.getProjectDirectory()); 127 List <String > argvList = new ArrayList <String >(); 128 argvList.add("script" + File.separator + "generate"); argvList.add(type); 130 131 if (panel.isForce()) { 132 argvList.add("--force"); } else { 134 argvList.add("--skip"); } 136 137 if (panel.isPretend()) { 138 argvList.add("--pretend"); } 140 141 argvList.add(name); 142 143 String [] firstParameterList = panel.getFirstParameterList(); 144 145 if (firstParameterList != null) { 146 for (String parameter : firstParameterList) { 147 argvList.add(parameter); 148 } 149 150 String [] remainingParameters = panel.getSecondParameterList(); 151 152 if (remainingParameters != null) { 153 for (String parameter : remainingParameters) { 154 argvList.add(parameter); 155 } 156 } 157 } 158 159 final String [] argv = argvList.toArray(new String [argvList.size()]); 160 161 try { 162 project.getProjectDirectory().getFileSystem().runAtomicAction(new FileSystem.AtomicAction() { 163 public void run() throws IOException { 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 ioe) { 175 ErrorManager.getDefault().notify(ioe); 176 } 177 } 178 } 179 } 180 181 public String 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 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 |