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