1 19 20 package org.netbeans.modules.ruby.railsprojects.ui.wizards; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.text.MessageFormat ; 25 import javax.swing.JFileChooser ; 26 import javax.swing.JPanel ; 27 import javax.swing.event.DocumentEvent ; 28 import javax.swing.event.DocumentListener ; 29 import javax.swing.text.Document ; 30 import org.netbeans.modules.ruby.railsprojects.ui.FoldersListSettings; 31 import org.netbeans.modules.ruby.spi.project.support.rake.PropertyUtils; 32 import org.netbeans.spi.project.ui.support.ProjectChooser; 33 import org.openide.WizardDescriptor; 34 import org.openide.WizardValidationException; 35 import org.openide.filesystems.FileUtil; 36 import org.openide.util.NbBundle; 37 import org.openide.util.Utilities; 38 39 43 44 public class PanelProjectLocationVisual extends SettingsPanel implements DocumentListener { 45 46 public static final String PROP_PROJECT_NAME = "projectName"; 48 private PanelConfigureProject panel; 49 private int type; 50 51 52 public PanelProjectLocationVisual( PanelConfigureProject panel, int type ) { 53 initComponents(); 54 this.panel = panel; 55 this.type = type; 56 projectNameTextField.getDocument().addDocumentListener( this ); 58 projectLocationTextField.getDocument().addDocumentListener( this ); 59 } 60 61 62 public String getProjectName () { 63 return this.projectNameTextField.getText (); 64 } 65 66 71 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 73 74 projectNameLabel = new javax.swing.JLabel (); 75 projectNameTextField = new javax.swing.JTextField (); 76 projectLocationLabel = new javax.swing.JLabel (); 77 projectLocationTextField = new javax.swing.JTextField (); 78 browseButton = new javax.swing.JButton (); 79 createdFolderLabel = new javax.swing.JLabel (); 80 createdFolderTextField = new javax.swing.JTextField (); 81 82 setLayout(new java.awt.GridBagLayout ()); 83 84 getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "ACSN_PanelProjectLocationVisual")); 85 getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "ACSD_PanelProjectLocationVisual")); 86 projectNameLabel.setLabelFor(projectNameTextField); 87 org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel, org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_ProjectName_Label")); 88 gridBagConstraints = new java.awt.GridBagConstraints (); 89 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 90 gridBagConstraints.insets = new java.awt.Insets (0, 0, 12, 0); 91 add(projectNameLabel, gridBagConstraints); 92 projectNameLabel.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSN_projectNameLabel")); 93 projectNameLabel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSD_projectNameLabel")); 94 95 gridBagConstraints = new java.awt.GridBagConstraints (); 96 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 97 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 98 gridBagConstraints.weightx = 1.0; 99 gridBagConstraints.insets = new java.awt.Insets (0, 12, 12, 0); 100 add(projectNameTextField, gridBagConstraints); 101 102 projectLocationLabel.setLabelFor(projectLocationTextField); 103 org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel, org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_ProjectLocation_Label")); 104 gridBagConstraints = new java.awt.GridBagConstraints (); 105 gridBagConstraints.gridy = 1; 106 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 107 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 0); 108 add(projectLocationLabel, gridBagConstraints); 109 projectLocationLabel.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSN_projectLocationLabel")); 110 projectLocationLabel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSD_projectLocationLabel")); 111 112 gridBagConstraints = new java.awt.GridBagConstraints (); 113 gridBagConstraints.gridy = 1; 114 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 115 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 116 gridBagConstraints.weightx = 1.0; 117 gridBagConstraints.insets = new java.awt.Insets (0, 12, 5, 0); 118 add(projectLocationTextField, gridBagConstraints); 119 120 org.openide.awt.Mnemonics.setLocalizedText(browseButton, org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_BrowseLocation_Button")); 121 browseButton.setActionCommand("BROWSE"); 122 browseButton.addActionListener(new java.awt.event.ActionListener () { 123 public void actionPerformed(java.awt.event.ActionEvent evt) { 124 browseLocationAction(evt); 125 } 126 }); 127 128 gridBagConstraints = new java.awt.GridBagConstraints (); 129 gridBagConstraints.gridy = 1; 130 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 131 gridBagConstraints.insets = new java.awt.Insets (0, 6, 5, 0); 132 add(browseButton, gridBagConstraints); 133 browseButton.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSN_browseButton")); 134 browseButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSD_browseButton")); 135 136 createdFolderLabel.setLabelFor(createdFolderTextField); 137 org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel, org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_CreatedProjectFolder_Lablel")); 138 gridBagConstraints = new java.awt.GridBagConstraints (); 139 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 140 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 141 add(createdFolderLabel, gridBagConstraints); 142 createdFolderLabel.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSN_createdFolderLabel")); 143 createdFolderLabel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSD_createdFolderLabel")); 144 145 createdFolderTextField.setEditable(false); 146 gridBagConstraints = new java.awt.GridBagConstraints (); 147 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 148 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 149 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 150 gridBagConstraints.weightx = 1.0; 151 gridBagConstraints.weighty = 1.0; 152 gridBagConstraints.insets = new java.awt.Insets (0, 12, 0, 0); 153 add(createdFolderTextField, gridBagConstraints); 154 155 } 157 private void browseLocationAction(java.awt.event.ActionEvent evt) { String command = evt.getActionCommand(); 159 if ( "BROWSE".equals( command ) ) { JFileChooser chooser = new JFileChooser (); 161 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 162 chooser.setDialogTitle(NbBundle.getMessage(PanelProjectLocationVisual.class,"LBL_NWP1_SelectProjectLocation")); 163 chooser.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY); 164 String path = this.projectLocationTextField.getText(); 165 if (path.length() > 0) { 166 File f = new File (path); 167 if (f.exists ()) { 168 chooser.setSelectedFile(f); 169 } 170 } 171 if ( JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { File projectDir = chooser.getSelectedFile(); 173 projectLocationTextField.setText( FileUtil.normalizeFile(projectDir).getAbsolutePath() ); 174 } 175 panel.fireChangeEvent(); 176 } 177 } 179 180 public void addNotify() { 181 super.addNotify(); 182 projectNameTextField.requestFocus(); 184 } 185 186 boolean valid( WizardDescriptor wizardDescriptor ) { 187 188 if ( projectNameTextField.getText().length() == 0 189 || projectNameTextField.getText().indexOf('/') > 0 || projectNameTextField.getText().indexOf('\\') > 0 || projectNameTextField.getText().indexOf(':') > 0) { wizardDescriptor.putProperty( "WizardPanel_errorMessage", NbBundle.getMessage(PanelProjectLocationVisual.class,"MSG_IllegalProjectName")); 194 return false; } 196 File f = new File (projectLocationTextField.getText()).getAbsoluteFile(); 197 if (getCanonicalFile (f)==null) { 198 String message = NbBundle.getMessage (PanelProjectLocationVisual.class,"MSG_IllegalProjectLocation"); 199 wizardDescriptor.putProperty("WizardPanel_errorMessage", message); 200 return false; 201 } 202 File cfl = getCanonicalFile(new File (createdFolderTextField.getText())); 204 if (Utilities.isUnix() && cfl != null && cfl.getParentFile().getParent() == null) { 205 String message = NbBundle.getMessage (PanelProjectLocationVisual.class,"MSG_ProjectInRootNotSupported"); 206 wizardDescriptor.putProperty("WizardPanel_errorMessage", message); 207 return false; 208 } 209 210 final File destFolder = new File ( createdFolderTextField.getText() ).getAbsoluteFile(); 211 if (getCanonicalFile (destFolder) == null) { 212 String message = NbBundle.getMessage (PanelProjectLocationVisual.class,"MSG_IllegalProjectLocation"); 213 wizardDescriptor.putProperty("WizardPanel_errorMessage", message); 214 return false; 215 } 216 217 File projLoc = FileUtil.normalizeFile(destFolder); 218 while (projLoc != null && !projLoc.exists()) { 219 projLoc = projLoc.getParentFile(); 220 } 221 if (projLoc == null || !projLoc.canWrite()) { 222 wizardDescriptor.putProperty( "WizardPanel_errorMessage", NbBundle.getMessage(PanelProjectLocationVisual.class,"MSG_ProjectFolderReadOnly")); 224 return false; 225 } 226 227 if (FileUtil.toFileObject(projLoc) == null) { 228 String message = NbBundle.getMessage (PanelProjectLocationVisual.class,"MSG_IllegalProjectLocation"); 229 wizardDescriptor.putProperty("WizardPanel_errorMessage", message); 230 return false; 231 } 232 233 File [] kids = destFolder.listFiles(); 234 if ( destFolder.exists() && kids != null && kids.length > 0) { 235 wizardDescriptor.putProperty( "WizardPanel_errorMessage", NbBundle.getMessage(PanelProjectLocationVisual.class,"MSG_ProjectFolderExists")); 238 return false; 239 } 240 return true; 241 } 242 243 void store( WizardDescriptor d ) { 244 245 String name = projectNameTextField.getText().trim(); 246 String location = projectLocationTextField.getText().trim(); 247 String folder = createdFolderTextField.getText().trim(); 248 249 d.putProperty( "projdir", new File ( folder )); d.putProperty( "name", name ); } 252 253 void read (WizardDescriptor settings) { 254 File projectLocation = (File ) settings.getProperty ("projdir"); if (projectLocation == null || projectLocation.getParentFile() == null || !projectLocation.getParentFile().isDirectory ()) { 256 projectLocation = ProjectChooser.getProjectsFolder(); 257 } 258 else { 259 projectLocation = projectLocation.getParentFile(); 260 } 261 this.projectLocationTextField.setText (projectLocation.getAbsolutePath()); 262 263 String projectName = (String ) settings.getProperty ("name"); if (projectName == null) { 265 if (this.type == NewRailsProjectWizardIterator.TYPE_APP) { 266 int baseCount = FoldersListSettings.getDefault().getNewApplicationCount() + 1; 267 String formater = NbBundle.getMessage(PanelProjectLocationVisual.class,"TXT_JavaApplication"); 268 while ((projectName=validFreeProjectName(projectLocation, formater, baseCount))==null) 269 baseCount++; 270 settings.putProperty (NewRailsProjectWizardIterator.PROP_NAME_INDEX, new Integer (baseCount)); 271 } 272 } 280 this.projectNameTextField.setText (projectName); 281 this.projectNameTextField.selectAll(); 282 } 283 284 void validate (WizardDescriptor d) throws WizardValidationException { 285 } 287 288 private javax.swing.JButton browseButton; 290 private javax.swing.JLabel createdFolderLabel; 291 private javax.swing.JTextField createdFolderTextField; 292 private javax.swing.JLabel projectLocationLabel; 293 private javax.swing.JTextField projectLocationTextField; 294 private javax.swing.JLabel projectNameLabel; 295 private javax.swing.JTextField projectNameTextField; 296 298 299 301 private static JFileChooser createChooser() { 302 JFileChooser chooser = new JFileChooser (); 303 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 304 chooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY ); 305 chooser.setAcceptAllFileFilterUsed( false ); 306 chooser.setName( "Select Project Directory" ); return chooser; 308 } 309 310 private String validFreeProjectName (final File parentFolder, final String formater, final int index) { 311 String name = MessageFormat.format (formater, new Object []{new Integer (index)}); 312 File file = new File (parentFolder, name); 313 return file.exists() ? null : name; 314 } 315 316 318 public void changedUpdate( DocumentEvent e ) { 319 updateTexts( e ); 320 if (this.projectNameTextField.getDocument() == e.getDocument()) { 321 firePropertyChange (PROP_PROJECT_NAME,null,this.projectNameTextField.getText()); 322 } 323 } 324 325 public void insertUpdate( DocumentEvent e ) { 326 updateTexts( e ); 327 if (this.projectNameTextField.getDocument() == e.getDocument()) { 328 firePropertyChange (PROP_PROJECT_NAME,null,this.projectNameTextField.getText()); 329 } 330 } 331 332 public void removeUpdate( DocumentEvent e ) { 333 updateTexts( e ); 334 if (this.projectNameTextField.getDocument() == e.getDocument()) { 335 firePropertyChange (PROP_PROJECT_NAME,null,this.projectNameTextField.getText()); 336 } 337 } 338 339 340 342 private void updateTexts( DocumentEvent e ) { 343 Document doc = e.getDocument(); 344 if ( doc == projectNameTextField.getDocument() || doc == projectLocationTextField.getDocument() ) { 345 String projectName = projectNameTextField.getText(); 347 String projectFolder = projectLocationTextField.getText(); 348 String projFolderPath = getCanonicalPath(new File (projectFolder)); 349 if (projFolderPath.endsWith(File.separator)) { 350 createdFolderTextField.setText(projFolderPath + projectName); 351 } else { 352 createdFolderTextField.setText(projFolderPath + File.separator + projectName); 353 } 354 } 355 panel.fireChangeEvent(); } 357 358 static File getCanonicalFile(File file) { 359 try { 360 return file.getCanonicalFile(); 361 } catch (IOException e) { 362 return null; 363 } 364 } 365 366 static String getCanonicalPath(File f) { 367 try { 368 return f.getCanonicalPath(); 369 } catch (IOException e) { 370 return ""; 371 } 372 } 373 374 } 375 | Popular Tags |