1 19 20 package org.netbeans.modules.web.project.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.web.project.ui.FoldersListSettings; 30 import org.netbeans.spi.project.ui.support.ProjectChooser; 31 import org.openide.WizardDescriptor; 32 import org.openide.WizardValidationException; 33 import org.openide.filesystems.FileUtil; 34 import org.openide.util.NbBundle; 35 36 40 41 public class PanelProjectLocationVisual extends SettingsPanel implements DocumentListener { 42 43 public static final String PROP_PROJECT_NAME = "projectName"; private static final String PROJECT_NAME_FORMATER = NbBundle.getMessage(PanelProjectLocationVisual.class,"LBL_NPW1_DefaultProjectName"); 46 private String generatedProjectName = ""; 47 private int generatedProjectNameIndex = 0; 48 49 private PanelConfigureProject panel; 50 51 52 public PanelProjectLocationVisual( PanelConfigureProject panel ) { 53 initComponents(); 54 this.panel = panel; 55 56 projectNameTextField.getDocument().addDocumentListener( this ); 58 projectLocationTextField.getDocument().addDocumentListener( this ); 59 } 60 61 protected int computeHeight() { 62 return createdFolderTextField.getFontMetrics(createdFolderTextField.getFont()).getHeight() * 3 + 40; 63 } 64 65 public String getProjectName () { 66 return this.projectNameTextField.getText (); 67 } 68 69 74 private void initComponents() { 76 java.awt.GridBagConstraints gridBagConstraints; 77 78 projectNameLabel = new javax.swing.JLabel (); 79 projectNameTextField = new javax.swing.JTextField (); 80 projectLocationLabel = new javax.swing.JLabel (); 81 projectLocationTextField = new javax.swing.JTextField (); 82 browseButton = new javax.swing.JButton (); 83 createdFolderLabel = new javax.swing.JLabel (); 84 createdFolderTextField = new javax.swing.JTextField (); 85 86 setLayout(new java.awt.GridBagLayout ()); 87 88 getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "ACSN_PanelProjectLocationVisual")); 89 getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "ACSD_PanelProjectLocationVisual")); 90 projectNameLabel.setDisplayedMnemonic(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_ProjectName_LabelMnemonic").charAt(0)); 91 projectNameLabel.setLabelFor(projectNameTextField); 92 projectNameLabel.setText(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_ProjectName_Label")); 93 gridBagConstraints = new java.awt.GridBagConstraints (); 94 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 95 gridBagConstraints.insets = new java.awt.Insets (0, 0, 12, 0); 96 add(projectNameLabel, gridBagConstraints); 97 projectNameLabel.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSN_projectNameLabel")); 98 projectNameLabel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSD_projectNameLabel")); 99 100 gridBagConstraints = new java.awt.GridBagConstraints (); 101 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 102 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 103 gridBagConstraints.weightx = 1.0; 104 gridBagConstraints.insets = new java.awt.Insets (0, 12, 12, 0); 105 add(projectNameTextField, gridBagConstraints); 106 107 projectLocationLabel.setDisplayedMnemonic(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_ProjectLocation_LabelMnemonic").charAt(0)); 108 projectLocationLabel.setLabelFor(projectLocationTextField); 109 projectLocationLabel.setText(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_ProjectLocation_Label")); 110 gridBagConstraints = new java.awt.GridBagConstraints (); 111 gridBagConstraints.gridy = 1; 112 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 113 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 0); 114 add(projectLocationLabel, gridBagConstraints); 115 projectLocationLabel.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSN_projectLocationLabel")); 116 projectLocationLabel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSD_projectLocationLabel")); 117 118 gridBagConstraints = new java.awt.GridBagConstraints (); 119 gridBagConstraints.gridy = 1; 120 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 121 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 122 gridBagConstraints.weightx = 1.0; 123 gridBagConstraints.insets = new java.awt.Insets (0, 12, 5, 0); 124 add(projectLocationTextField, gridBagConstraints); 125 126 browseButton.setMnemonic(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_Browse_LablelMnemonic").charAt(0)); 127 browseButton.setText(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_BrowseLocation_Button")); 128 browseButton.setActionCommand("BROWSE"); 129 browseButton.addActionListener(new java.awt.event.ActionListener () { 130 public void actionPerformed(java.awt.event.ActionEvent evt) { 131 browseLocationAction(evt); 132 } 133 }); 134 135 gridBagConstraints = new java.awt.GridBagConstraints (); 136 gridBagConstraints.gridy = 1; 137 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 138 gridBagConstraints.insets = new java.awt.Insets (0, 6, 5, 0); 139 add(browseButton, gridBagConstraints); 140 browseButton.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSN_browseButton")); 141 browseButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSD_browseButton")); 142 143 createdFolderLabel.setDisplayedMnemonic(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_CreatedProjectFolder_LablelMnemonic").charAt(0)); 144 createdFolderLabel.setLabelFor(createdFolderTextField); 145 createdFolderLabel.setText(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_CreatedProjectFolder_Lablel")); 146 gridBagConstraints = new java.awt.GridBagConstraints (); 147 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 148 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 149 add(createdFolderLabel, gridBagConstraints); 150 createdFolderLabel.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSN_createdFolderLabel")); 151 createdFolderLabel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSD_createdFolderLabel")); 152 153 createdFolderTextField.setEditable(false); 154 gridBagConstraints = new java.awt.GridBagConstraints (); 155 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 156 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 157 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 158 gridBagConstraints.weightx = 1.0; 159 gridBagConstraints.weighty = 1.0; 160 gridBagConstraints.insets = new java.awt.Insets (0, 12, 0, 0); 161 add(createdFolderTextField, gridBagConstraints); 162 163 } 164 166 private void browseLocationAction(java.awt.event.ActionEvent evt) { String command = evt.getActionCommand(); 168 if ( "BROWSE".equals( command ) ) { JFileChooser chooser = new JFileChooser (); 170 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 171 chooser.setDialogTitle(NbBundle.getMessage(PanelProjectLocationVisual.class,"LBL_NWP1_SelectProjectLocation")); 172 chooser.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY); 173 String path = this.projectLocationTextField.getText(); 174 if (path.length() > 0) { 175 File f = new File (path); 176 if (f.exists ()) { 177 chooser.setSelectedFile(f); 178 } 179 } 180 if ( JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { File projectDir = chooser.getSelectedFile(); 182 projectLocationTextField.setText( projectDir.getAbsolutePath() ); 183 } 184 panel.fireChangeEvent(); 185 } 186 } 188 189 public void addNotify() { 190 super.addNotify(); 191 projectNameTextField.requestFocus(); 193 } 194 195 boolean valid( WizardDescriptor wizardDescriptor ) { 196 197 if ( projectNameTextField.getText().length() == 0 ) { 198 wizardDescriptor.putProperty( "WizardPanel_errorMessage", NbBundle.getMessage(PanelProjectLocationVisual.class,"MSG_IllegalProjectName")); 200 return false; } 202 File f = new File (projectLocationTextField.getText()).getAbsoluteFile(); 203 if (getCanonicalFile (f)==null) { 204 String message = NbBundle.getMessage (PanelProjectLocationVisual.class,"MSG_IllegalProjectLocation"); 205 wizardDescriptor.putProperty("WizardPanel_errorMessage", message); 206 return false; 207 } 208 final File destFolder = getCanonicalFile(new File ( createdFolderTextField.getText() ).getAbsoluteFile()); 209 if (destFolder == null) { 210 String message = NbBundle.getMessage (PanelProjectLocationVisual.class,"MSG_IllegalProjectName"); 211 wizardDescriptor.putProperty("WizardPanel_errorMessage", message); 212 return false; 213 } 214 215 File projLoc = destFolder; 216 while (projLoc != null && !projLoc.exists()) { 217 projLoc = projLoc.getParentFile(); 218 } 219 if (projLoc == null || !projLoc.canWrite()) { 220 wizardDescriptor.putProperty( "WizardPanel_errorMessage", NbBundle.getMessage(PanelProjectLocationVisual.class,"MSG_ProjectFolderReadOnly")); 222 return false; 223 } 224 225 if (FileUtil.toFileObject(projLoc) == null) { 226 String message = NbBundle.getMessage (PanelProjectLocationVisual.class,"MSG_IllegalProjectLocation"); 227 wizardDescriptor.putProperty("WizardPanel_errorMessage", message); 228 return false; 229 } 230 231 File [] kids = destFolder.listFiles(); 232 if ( destFolder.exists() && kids != null && kids.length > 0) { 233 wizardDescriptor.putProperty( "WizardPanel_errorMessage", NbBundle.getMessage(PanelProjectLocationVisual.class,"MSG_ProjectFolderExists")); 236 return false; 237 } 238 239 wizardDescriptor.putProperty( "WizardPanel_errorMessage", ""); 240 return true; 241 } 242 243 void store( WizardDescriptor d ) { 244 String name = projectNameTextField.getText().trim(); 245 String folder = createdFolderTextField.getText().trim(); 246 247 d.putProperty( "projdir", new File ( folder )); d.putProperty( "name", name ); 250 final Integer nameIndex = projectNameTextField.getText().equals(generatedProjectName) ? Integer.valueOf(generatedProjectNameIndex) : null; 251 d.putProperty(NewWebProjectWizardIterator.PROP_NAME_INDEX, nameIndex); 252 } 253 254 void read (WizardDescriptor settings) { 255 File projectLocation = (File ) settings.getProperty ("projdir"); if (projectLocation == null || projectLocation.getParentFile() == null || !projectLocation.getParentFile().isDirectory ()) { 257 projectLocation = ProjectChooser.getProjectsFolder(); 258 } 259 else { 260 projectLocation = projectLocation.getParentFile(); 261 } 262 this.projectLocationTextField.setText (projectLocation.getAbsolutePath()); 263 264 String projectName = (String ) settings.getProperty ("name"); if (projectName == null) { 266 int baseCount = FoldersListSettings.getDefault().getNewProjectCount() + 1; 267 String formater = NbBundle.getMessage(PanelProjectLocationVisual.class,"LBL_NPW1_DefaultProjectName"); 268 while ((projectName=validFreeProjectName(projectLocation, formater, baseCount))==null) 269 baseCount++; 270 settings.putProperty (NewWebProjectWizardIterator.PROP_NAME_INDEX, Integer.valueOf(baseCount)); 271 } 272 this.projectNameTextField.setText (projectName); 273 this.projectNameTextField.selectAll(); 274 } 275 276 void validate (WizardDescriptor d) throws WizardValidationException { 277 } 279 280 private javax.swing.JButton browseButton; 282 private javax.swing.JLabel createdFolderLabel; 283 private javax.swing.JTextField createdFolderTextField; 284 private javax.swing.JLabel projectLocationLabel; 285 private javax.swing.JTextField projectLocationTextField; 286 private javax.swing.JLabel projectNameLabel; 287 protected javax.swing.JTextField projectNameTextField; 288 290 291 293 private String validFreeProjectName (final File parentFolder, final String formater, final int index) { 294 String name = MessageFormat.format (formater, new Object []{Integer.valueOf(index)}); 295 File file = new File (parentFolder, name); 296 return file.exists() ? null : name; 297 } 298 299 301 public void changedUpdate( DocumentEvent e ) { 302 updateTexts( e ); 303 if (this.projectNameTextField.getDocument() == e.getDocument()) { 304 firePropertyChange (PROP_PROJECT_NAME,null,this.projectNameTextField.getText()); 305 } 306 } 307 308 public void insertUpdate( DocumentEvent e ) { 309 updateTexts( e ); 310 if (this.projectNameTextField.getDocument() == e.getDocument()) { 311 firePropertyChange (PROP_PROJECT_NAME,null,this.projectNameTextField.getText()); 312 } 313 } 314 315 public void removeUpdate( DocumentEvent e ) { 316 updateTexts( e ); 317 if (this.projectNameTextField.getDocument() == e.getDocument()) { 318 firePropertyChange (PROP_PROJECT_NAME,null,this.projectNameTextField.getText()); 319 } 320 } 321 322 324 private void updateTexts( DocumentEvent e ) { 325 Document doc = e.getDocument(); 326 if (doc == projectNameTextField.getDocument() || doc == projectLocationTextField.getDocument()) { 327 String projectName = projectNameTextField.getText(); 328 329 if (doc == projectLocationTextField.getDocument()) { 330 if (projectName.equals(generatedProjectName)) { 331 File f = new File (projectLocationTextField.getText().trim()); 332 generatedProjectNameIndex = getValidProjectNameIndex(generatedProjectNameIndex, f); 333 } else { 334 generatedProjectNameIndex = 0; 335 } 336 generatedProjectName = generatedProjectNameIndex > 0 ? getProjectName(generatedProjectNameIndex) : null; 337 if(generatedProjectNameIndex > 0) { 338 projectName = generatedProjectName; 339 projectNameTextField.setText(generatedProjectName); 340 projectNameTextField.selectAll(); 341 } 342 } 343 344 String projectFolder = projectLocationTextField.getText(); 345 createdFolderTextField.setText(projectFolder + File.separatorChar + projectName); 346 } 347 panel.fireChangeEvent(); } 349 350 static File getCanonicalFile(File file) { 351 try { 352 return file.getCanonicalFile(); 353 } catch (IOException e) { 354 return null; 355 } 356 } 357 358 public static String getProjectName(int index) { 359 return MessageFormat.format(PROJECT_NAME_FORMATER, new Object [] {String.valueOf(index)}); 360 } 361 362 private static int getValidProjectNameIndex(int currentIndex, File projectLocation) { 363 int index = currentIndex > 0 ? currentIndex : FoldersListSettings.getDefault().getNewProjectCount() + 1; 364 if(projectLocation != null) { 365 while (new File (projectLocation, getProjectName(index)).exists()) { 366 index++; 367 } 368 } 369 return index; 370 } 371 372 } 373 | Popular Tags |