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