1 19 20 package org.netbeans.modules.j2ee.clientproject.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.j2ee.clientproject.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 public class PanelProjectLocationVisual extends SettingsPanel implements DocumentListener { 41 42 public static final String PROP_PROJECT_NAME = "projectName"; 44 private PanelConfigureProject panel; 45 46 47 public PanelProjectLocationVisual( PanelConfigureProject panel ) { 48 initComponents(); 49 this.panel = panel; 50 projectNameTextField.getDocument().addDocumentListener( this ); 52 projectLocationTextField.getDocument().addDocumentListener( this ); 53 } 54 55 56 public String getProjectName() { 57 return this.projectNameTextField.getText(); 58 } 59 60 65 private void initComponents() { 67 java.awt.GridBagConstraints gridBagConstraints; 68 69 projectNameLabel = new javax.swing.JLabel (); 70 projectNameTextField = new javax.swing.JTextField (); 71 projectLocationLabel = new javax.swing.JLabel (); 72 projectLocationTextField = new javax.swing.JTextField (); 73 browseButton = new javax.swing.JButton (); 74 createdFolderLabel = new javax.swing.JLabel (); 75 createdFolderTextField = new javax.swing.JTextField (); 76 77 setLayout(new java.awt.GridBagLayout ()); 78 79 projectNameLabel.setLabelFor(projectNameTextField); 80 org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel, org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_ProjectName_Label")); gridBagConstraints = new java.awt.GridBagConstraints (); 82 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 83 gridBagConstraints.insets = new java.awt.Insets (0, 0, 12, 0); 84 add(projectNameLabel, gridBagConstraints); 85 projectNameLabel.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSN_projectNameLabel")); projectNameLabel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSD_projectNameLabel")); 88 gridBagConstraints = new java.awt.GridBagConstraints (); 89 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 90 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 91 gridBagConstraints.weightx = 1.0; 92 gridBagConstraints.insets = new java.awt.Insets (0, 12, 12, 0); 93 add(projectNameTextField, gridBagConstraints); 94 95 projectLocationLabel.setLabelFor(projectLocationTextField); 96 org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel, org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_ProjectLocation_Label")); gridBagConstraints = new java.awt.GridBagConstraints (); 98 gridBagConstraints.gridy = 1; 99 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 100 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 0); 101 add(projectLocationLabel, gridBagConstraints); 102 projectLocationLabel.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSN_projectLocationLabel")); projectLocationLabel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSD_projectLocationLabel")); 105 gridBagConstraints = new java.awt.GridBagConstraints (); 106 gridBagConstraints.gridy = 1; 107 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 108 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 109 gridBagConstraints.weightx = 1.0; 110 gridBagConstraints.insets = new java.awt.Insets (0, 12, 5, 0); 111 add(projectLocationTextField, gridBagConstraints); 112 113 org.openide.awt.Mnemonics.setLocalizedText(browseButton, org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_BrowseLocation_Button")); browseButton.setActionCommand("BROWSE"); 115 browseButton.addActionListener(new java.awt.event.ActionListener () { 116 public void actionPerformed(java.awt.event.ActionEvent evt) { 117 browseLocationAction(evt); 118 } 119 }); 120 gridBagConstraints = new java.awt.GridBagConstraints (); 121 gridBagConstraints.gridy = 1; 122 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 123 gridBagConstraints.insets = new java.awt.Insets (0, 6, 5, 0); 124 add(browseButton, gridBagConstraints); 125 browseButton.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSN_browseButton")); browseButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSD_browseButton")); 128 createdFolderLabel.setLabelFor(createdFolderTextField); 129 org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel, org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_CreatedProjectFolder_Label")); gridBagConstraints = new java.awt.GridBagConstraints (); 131 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 132 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 133 add(createdFolderLabel, gridBagConstraints); 134 createdFolderLabel.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSN_createdFolderLabel")); createdFolderLabel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSD_createdFolderLabel")); 137 createdFolderTextField.setEditable(false); 138 gridBagConstraints = new java.awt.GridBagConstraints (); 139 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 140 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 141 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 142 gridBagConstraints.weightx = 1.0; 143 gridBagConstraints.weighty = 1.0; 144 gridBagConstraints.insets = new java.awt.Insets (0, 12, 0, 0); 145 add(createdFolderTextField, gridBagConstraints); 146 147 getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "ACSN_PanelProjectLocationVisual")); getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "ACSD_PanelProjectLocationVisual")); } 151 private void browseLocationAction(java.awt.event.ActionEvent evt) { String command = evt.getActionCommand(); 153 if ( "BROWSE".equals( command ) ) { JFileChooser chooser = new JFileChooser (); 155 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 156 chooser.setDialogTitle(NbBundle.getMessage(PanelSourceFolders.class,"LBL_NWP1_SelectProjectLocation")); 157 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 158 String path = this.projectLocationTextField.getText(); 159 if (path.length() > 0) { 160 File f = new File (path); 161 if (f.exists()) { 162 chooser.setSelectedFile(f); 163 } 164 } 165 if ( JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { File projectDir = chooser.getSelectedFile(); 167 projectLocationTextField.setText( FileUtil.normalizeFile(projectDir).getAbsolutePath() ); 168 } 169 panel.fireChangeEvent(); 170 } 171 } 173 174 public void addNotify() { 175 super.addNotify(); 176 projectNameTextField.requestFocus(); 178 } 179 180 boolean valid( WizardDescriptor wizardDescriptor ) { 181 182 if ( projectNameTextField.getText().length() == 0 183 || projectNameTextField.getText().indexOf('/') > 0 || projectNameTextField.getText().indexOf('\\') > 0 || projectNameTextField.getText().indexOf(':') > 0) { wizardDescriptor.putProperty( "WizardPanel_errorMessage", NbBundle.getMessage(PanelProjectLocationVisual.class,"MSG_IllegalProjectName")); 188 return false; } 190 File f = new File (projectLocationTextField.getText()).getAbsoluteFile(); 191 if (getCanonicalFile(f)==null) { 192 String message = NbBundle.getMessage(PanelProjectLocationVisual.class,"MSG_IllegalProjectLocation"); 193 wizardDescriptor.putProperty("WizardPanel_errorMessage", message); 194 return false; 195 } 196 final File destFolder = new File ( createdFolderTextField.getText() ).getAbsoluteFile(); 197 if (getCanonicalFile(destFolder) == null) { 198 String message = NbBundle.getMessage(PanelProjectLocationVisual.class,"MSG_IllegalProjectName"); 199 wizardDescriptor.putProperty("WizardPanel_errorMessage", message); 200 return false; 201 } 202 203 File projLoc = FileUtil.normalizeFile(destFolder); 204 while (projLoc != null && !projLoc.exists()) { 205 projLoc = projLoc.getParentFile(); 206 } 207 if (projLoc == null || !projLoc.canWrite()) { 208 wizardDescriptor.putProperty( "WizardPanel_errorMessage", NbBundle.getMessage(PanelProjectLocationVisual.class,"MSG_ProjectFolderReadOnly")); 210 return false; 211 } 212 213 if (FileUtil.toFileObject(projLoc) == null) { 214 String message = NbBundle.getMessage(PanelProjectLocationVisual.class,"MSG_IllegalProjectLocation"); 215 wizardDescriptor.putProperty("WizardPanel_errorMessage", message); 216 return false; 217 } 218 219 File [] kids = destFolder.listFiles(); 220 if ( destFolder.exists() && kids != null && kids.length > 0) { 221 wizardDescriptor.putProperty( "WizardPanel_errorMessage", NbBundle.getMessage(PanelProjectLocationVisual.class,"MSG_ProjectFolderExists")); 224 return false; 225 } 226 return true; 227 } 228 229 void store( WizardDescriptor d ) { 230 231 String name = projectNameTextField.getText().trim(); 232 String location = projectLocationTextField.getText().trim(); 233 String folder = createdFolderTextField.getText().trim(); 234 235 d.putProperty(WizardProperties.PROJECT_DIR, new File ( folder )); d.putProperty(WizardProperties.NAME, name ); } 238 239 void read(WizardDescriptor settings) { 240 File projectLocation = (File ) settings.getProperty(WizardProperties.PROJECT_DIR); if (projectLocation == null || projectLocation.getParentFile() == null || !projectLocation.getParentFile().isDirectory()) { 242 projectLocation = ProjectChooser.getProjectsFolder(); 243 } else { 244 projectLocation = projectLocation.getParentFile(); 245 } 246 this.projectLocationTextField.setText(projectLocation.getAbsolutePath()); 247 248 String projectName = (String ) settings.getProperty(WizardProperties.NAME); if (projectName == null) { 250 int baseCount = FoldersListSettings.getDefault().getNewApplicationCount() + 1; 251 String formater = NbBundle.getMessage(PanelSourceFolders.class, "TXT_JavaApplication"); 252 while ((projectName=validFreeProjectName(projectLocation, formater, baseCount))==null) 253 baseCount++; 254 settings.putProperty(NewAppClientProjectWizardIterator.PROP_NAME_INDEX, new Integer (baseCount)); 255 } 256 this.projectNameTextField.setText(projectName); 257 this.projectNameTextField.selectAll(); 258 } 259 260 void validate(WizardDescriptor d) throws WizardValidationException { 261 } 263 264 private javax.swing.JButton browseButton; 266 private javax.swing.JLabel createdFolderLabel; 267 private javax.swing.JTextField createdFolderTextField; 268 private javax.swing.JLabel projectLocationLabel; 269 private javax.swing.JTextField projectLocationTextField; 270 private javax.swing.JLabel projectNameLabel; 271 private javax.swing.JTextField projectNameTextField; 272 274 275 277 private static JFileChooser createChooser() { 278 JFileChooser chooser = new JFileChooser (); 279 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 280 chooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY ); 281 chooser.setAcceptAllFileFilterUsed( false ); 282 chooser.setName( "Select Project Directory" ); return chooser; 284 } 285 286 private String validFreeProjectName(final File parentFolder, final String formater, final int index) { 287 String name = MessageFormat.format(formater, new Object []{new Integer (index)}); 288 File file = new File (parentFolder, name); 289 return file.exists() ? null : name; 290 } 291 292 294 public void changedUpdate( DocumentEvent e ) { 295 updateTexts( e ); 296 if (this.projectNameTextField.getDocument() == e.getDocument()) { 297 firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText()); 298 } 299 } 300 301 public void insertUpdate( 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 removeUpdate( 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 316 318 private void updateTexts( DocumentEvent e ) { 319 320 Document doc = e.getDocument(); 321 322 if ( doc == projectNameTextField.getDocument() || doc == projectLocationTextField.getDocument() ) { 323 325 String projectName = projectNameTextField.getText(); 326 String projectFolder = projectLocationTextField.getText(); 327 328 createdFolderTextField.setText( projectFolder + File.separatorChar + projectName ); 330 332 } 333 panel.fireChangeEvent(); } 335 336 static File getCanonicalFile(File file) { 337 try { 338 return file.getCanonicalFile(); 339 } catch (IOException e) { 340 return null; 341 } 342 } 343 } 344 | Popular Tags |