1 19 20 package org.netbeans.modules.j2ee.earproject.ui.wizards; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.text.MessageFormat ; 25 import java.util.ResourceBundle ; 26 import javax.swing.JFileChooser ; 27 import javax.swing.event.DocumentEvent ; 28 import javax.swing.event.DocumentListener ; 29 import org.netbeans.modules.j2ee.earproject.ProjectEar; 30 import org.netbeans.modules.j2ee.earproject.ui.FoldersListSettings; 31 import org.netbeans.spi.project.ui.support.ProjectChooser; 32 import org.openide.WizardDescriptor; 33 34 final class PanelProjectLocationVisual extends SettingsPanel implements DocumentListener { 35 36 private final PanelConfigureProject panel; 37 private final String propNameIndex; 38 private final ResourceBundle customBundle; 39 private final boolean importStyle; 40 41 42 public PanelProjectLocationVisual(PanelConfigureProject panel, String propNameIndex, ResourceBundle customBundle, boolean importStyle) { 43 this.customBundle = customBundle; 44 initComponents(); 45 this.panel = panel; 46 this.propNameIndex = propNameIndex; 47 48 projectNameTextField.getDocument().addDocumentListener(this); 50 projectLocationTextField.getDocument().addDocumentListener(this); 51 this.importStyle = importStyle; 52 createdFolderTextField.setEditable(importStyle); 53 if (importStyle) { 54 createdFolderTextField.getDocument().addDocumentListener(this); 55 } 56 browseFolderButton.setVisible(importStyle); 57 } 58 59 void addNameListener(DocumentListener listener) { 60 projectNameTextField.getDocument().addDocumentListener(listener); 61 } 62 63 68 private void initComponents() { 70 java.awt.GridBagConstraints gridBagConstraints; 71 72 projectNameLabel = new javax.swing.JLabel (); 73 projectNameTextField = new javax.swing.JTextField (); 74 projectLocationLabel = new javax.swing.JLabel (); 75 projectLocationTextField = new javax.swing.JTextField (); 76 browseLocationButton = new javax.swing.JButton (); 77 createdFolderLabel = new javax.swing.JLabel (); 78 createdFolderTextField = new javax.swing.JTextField (); 79 browseFolderButton = new javax.swing.JButton (); 80 spaceFiller = new javax.swing.JPanel (); 81 82 setLayout(new java.awt.GridBagLayout ()); 83 84 projectNameLabel.setLabelFor(projectNameTextField); 85 org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel, org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_ProjectName_Label")); gridBagConstraints = new java.awt.GridBagConstraints (); 87 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 88 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 89 gridBagConstraints.insets = new java.awt.Insets (3, 0, 3, 0); 90 add(projectNameLabel, gridBagConstraints); 91 92 gridBagConstraints = new java.awt.GridBagConstraints (); 93 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 94 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 95 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 96 gridBagConstraints.weightx = 1.0; 97 gridBagConstraints.insets = new java.awt.Insets (3, 12, 3, 5); 98 add(projectNameTextField, gridBagConstraints); 99 projectNameTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "ACS_LBL_NWP1_ProjectName_A11YDesc")); 101 projectLocationLabel.setLabelFor(projectLocationTextField); 102 org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel, org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_ProjectLocation_Label")); gridBagConstraints = new java.awt.GridBagConstraints (); 104 gridBagConstraints.gridy = 1; 105 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 106 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 107 gridBagConstraints.insets = new java.awt.Insets (3, 0, 3, 0); 108 add(projectLocationLabel, gridBagConstraints); 109 110 gridBagConstraints = new java.awt.GridBagConstraints (); 111 gridBagConstraints.gridx = 1; 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 (3, 12, 3, 0); 117 add(projectLocationTextField, gridBagConstraints); 118 projectLocationTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "ACS_LBL_NPW1_ProjectLocation_A11YDesc")); 120 org.openide.awt.Mnemonics.setLocalizedText(browseLocationButton, org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_BrowseLocation_Button")); browseLocationButton.setActionCommand("BROWSE"); browseLocationButton.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.gridx = 2; 130 gridBagConstraints.gridy = 1; 131 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 132 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 133 gridBagConstraints.insets = new java.awt.Insets (3, 6, 3, 5); 134 add(browseLocationButton, gridBagConstraints); 135 browseLocationButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "ACS_LBL_NWP1_BrowseLocation_A11YDesc")); 137 createdFolderLabel.setLabelFor(createdFolderTextField); 138 org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel, org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_CreatedProjectFolder_Lablel")); gridBagConstraints = new java.awt.GridBagConstraints (); 140 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 141 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 142 gridBagConstraints.insets = new java.awt.Insets (3, 0, 3, 0); 143 add(createdFolderLabel, gridBagConstraints); 144 145 createdFolderTextField.setEditable(false); 146 gridBagConstraints = new java.awt.GridBagConstraints (); 147 gridBagConstraints.gridx = 1; 148 gridBagConstraints.gridy = 2; 149 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 150 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 151 gridBagConstraints.insets = new java.awt.Insets (3, 12, 3, 0); 152 add(createdFolderTextField, gridBagConstraints); 153 createdFolderTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "ACS_LBL_NWP1_CreatedProjectFolder_A11YDesc")); 155 org.openide.awt.Mnemonics.setLocalizedText(browseFolderButton, org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_BrowseLocation_Button")); browseFolderButton.setActionCommand("BROWSE"); browseFolderButton.addActionListener(new java.awt.event.ActionListener () { 158 public void actionPerformed(java.awt.event.ActionEvent evt) { 159 browseLocationAction2(evt); 160 } 161 }); 162 163 gridBagConstraints = new java.awt.GridBagConstraints (); 164 gridBagConstraints.gridx = 2; 165 gridBagConstraints.gridy = 2; 166 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 167 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 168 gridBagConstraints.insets = new java.awt.Insets (3, 5, 3, 5); 169 add(browseFolderButton, gridBagConstraints); 170 browseFolderButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "ACSD_LBL_NWP1_BrowseFolder")); 172 gridBagConstraints = new java.awt.GridBagConstraints (); 173 gridBagConstraints.gridx = 0; 174 gridBagConstraints.gridy = 3; 175 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 176 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 177 gridBagConstraints.weightx = 1.0; 178 gridBagConstraints.weighty = 1.0; 179 add(spaceFiller, gridBagConstraints); 180 181 } 183 private void browseLocationAction2(java.awt.event.ActionEvent evt) { String command = evt.getActionCommand(); 185 186 if ("BROWSE".equals(command)) { JFileChooser chooser = new JFileChooser (); 188 chooser.setDialogTitle(customBundle.getString("LBL_NWP1_SelectProjectLocation")); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 190 String path = getCreatedFolderText(); 191 if (path.length() > 0) { 192 File f = new File (path); 193 if (f.exists()) { 194 chooser.setSelectedFile(f); 195 } 196 } 197 if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { 198 File projectDir = chooser.getSelectedFile(); 199 createdFolderTextField.setText(projectDir.getAbsolutePath()); 200 } 201 panel.fireChangeEvent(); 202 } 203 } 205 private void browseLocationAction(java.awt.event.ActionEvent evt) { String command = evt.getActionCommand(); 207 208 if ("BROWSE".equals(command)) { JFileChooser chooser = new JFileChooser (); 210 chooser.setDialogTitle(customBundle.getString("LBL_NWP1_SelectProjectLocation")); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 212 String path = projectLocationTextField.getText(); 213 if (path.length() > 0) { 214 File f = new File (path); 215 if (f.exists()) { 216 chooser.setSelectedFile(f); 217 } 218 } 219 if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { 220 File projectDir = chooser.getSelectedFile(); 221 projectLocationTextField.setText(projectDir.getAbsolutePath()); 222 } 223 panel.fireChangeEvent(); 224 } 225 } 227 public void addNotify() { 228 super.addNotify(); 229 projectNameTextField.requestFocus(); 231 } 232 233 boolean valid(WizardDescriptor wizardDescriptor) { 234 if("\\".equals(getCreatedFolderText())) { return false; 239 } 240 241 if (!SettingsPanel.isValidProjectName(projectNameTextField.getText())) { 242 wizardDescriptor.putProperty("WizardPanel_errorMessage", customBundle.getString("MSG_IllegalProjectName")); return false; } 246 247 final String relativeDDPath = "src" + File.separator + "conf" + File.separator + ProjectEar.FILE_DD; 249 250 if (importStyle) { 251 File appXmlSrc = new File (projectLocationTextField.getText().trim() + 252 File.separator + relativeDDPath); 253 if (!appXmlSrc.exists()) { 254 wizardDescriptor.putProperty("WizardPanel_errorMessage", MessageFormat.format(customBundle.getString("MSG_Requires"),new Object [] {appXmlSrc.getAbsolutePath()})); return false; 257 } 258 if (getCreatedFolderText().length() == 0) { 259 wizardDescriptor.putProperty("WizardPanel_errorMessage", customBundle.getString("MSG_EmptyProjectFolder")); return false; 262 } 263 } 264 265 File destParent = SettingsPanel.findExistingParent(getCreatedFolderText()); 266 267 File destDir = new File (getCreatedFolderText()); 268 if (destDir.exists() && !destDir.isDirectory()) { 269 wizardDescriptor.putProperty("WizardPanel_errorMessage", MessageFormat.format(customBundle.getString("MSG_WillOverwrite"),new Object [] {destDir.getAbsolutePath() })); return false; 272 } 273 File buildXml = new File (getCreatedFolderText() + File.separator + "build.xml"); if (buildXml.exists()) { 275 wizardDescriptor.putProperty("WizardPanel_errorMessage", MessageFormat.format(customBundle.getString("MSG_WillOverwrite"),new Object [] {buildXml.getAbsolutePath() })); return false; 278 } 279 File tFile = new File (getCreatedFolderText() + File.separator + relativeDDPath); 280 if (tFile.exists()) { 281 wizardDescriptor.putProperty("WizardPanel_errorMessage", MessageFormat.format(customBundle.getString("MSG_WillOverwrite"),new Object [] {tFile.getAbsolutePath()})); return false; 284 } 285 286 tFile = new File (getCreatedFolderText() + File.separator + "nbproject" + File.separator + "project.xml"); if (tFile.exists()) { 288 wizardDescriptor.putProperty("WizardPanel_errorMessage", MessageFormat.format(customBundle.getString("MSG_WillOverwrite"),new Object [] {tFile.getAbsolutePath()})); return false; 291 } 292 tFile = new File (getCreatedFolderText() + 293 File.separator+"nbproject"+File.separator+"project.properties"); if (tFile.exists()) { 295 wizardDescriptor.putProperty("WizardPanel_errorMessage", MessageFormat.format(customBundle.getString("MSG_WillOverwrite"),new Object [] {tFile.getAbsolutePath()})); return false; 298 } 299 if (null != destParent && destParent.exists() && !destParent.canWrite()) { 300 wizardDescriptor.putProperty("WizardPanel_errorMessage", MessageFormat.format(customBundle.getString("MSG_ProjectLocationRO"),new Object [] {destParent.getAbsolutePath()})); return false; 304 } 305 306 wizardDescriptor.putProperty("WizardPanel_errorMessage", ""); return true; 308 } 309 310 void store( WizardDescriptor d ) { 311 String name = projectNameTextField.getText().trim(); 312 String folder = getCreatedFolderText() ; 313 314 d.putProperty( "projdir", new File ( folder )); d.putProperty( "name", name ); 317 final Integer nameIndex = projectNameTextField.getText().equals(generatedProjectName) ? generatedProjectNameIndex : null; 318 d.putProperty(propNameIndex, nameIndex); 319 } 320 321 void read (WizardDescriptor settings) { 322 File projectLocation = (File ) settings.getProperty ("projdir"); if (projectLocation == null || projectLocation.getParentFile() == null || !projectLocation.getParentFile().isDirectory ()) { 324 projectLocation = ProjectChooser.getProjectsFolder(); 325 } 326 else { 327 projectLocation = projectLocation.getParentFile(); 328 } 329 this.projectLocationTextField.setText (projectLocation.getAbsolutePath()); 330 331 String projectName = (String ) settings.getProperty ("name"); if (projectName == null) { 333 int baseCount = FoldersListSettings.getDefault().getNewProjectCount() + 1; 334 String formater = customBundle.getString("LBL_NPW1_DefaultProjectName"); 335 while ((projectName=validFreeProjectName(projectLocation, formater, baseCount))==null) { 336 baseCount++; 337 } 338 settings.putProperty (propNameIndex, baseCount); 339 } 340 this.projectNameTextField.setText (projectName); 341 this.projectNameTextField.selectAll(); 342 } 343 344 private javax.swing.JButton browseFolderButton; 346 private javax.swing.JButton browseLocationButton; 347 private javax.swing.JLabel createdFolderLabel; 348 private javax.swing.JTextField createdFolderTextField; 349 private javax.swing.JLabel projectLocationLabel; 350 private javax.swing.JTextField projectLocationTextField; 351 private javax.swing.JLabel projectNameLabel; 352 private javax.swing.JTextField projectNameTextField; 353 private javax.swing.JPanel spaceFiller; 354 356 358 private String validFreeProjectName (final File parentFolder, final String formater, final int index) { 359 String name = MessageFormat.format(formater, index); 360 File file = new File (parentFolder, name); 361 return file.exists() ? null : name; 362 } 363 364 366 public static final String PROP_PROJECT_NAME = "projectName"; 368 public void changedUpdate( DocumentEvent e ) { 369 updateTexts( e ); 370 if (this.projectNameTextField.getDocument() == e.getDocument()) { 371 firePropertyChange (PROP_PROJECT_NAME,null,this.projectNameTextField.getText()); 372 } 373 } 374 375 public void insertUpdate( DocumentEvent e ) { 376 updateTexts( e ); 377 if (this.projectNameTextField.getDocument() == e.getDocument()) { 378 firePropertyChange (PROP_PROJECT_NAME,null,this.projectNameTextField.getText()); 379 } 380 } 381 382 public void removeUpdate( DocumentEvent e ) { 383 updateTexts( e ); 384 if (this.projectNameTextField.getDocument() == e.getDocument()) { 385 firePropertyChange (PROP_PROJECT_NAME,null,this.projectNameTextField.getText()); 386 } 387 } 388 389 private String generatedProjectName = ""; 390 private int generatedProjectNameIndex = 0; 391 392 394 private void updateTexts( DocumentEvent e ) { 395 javax.swing.text.Document doc = e.getDocument(); 396 if (doc == projectNameTextField.getDocument() || doc == projectLocationTextField.getDocument()) { 397 String projectName = projectNameTextField.getText(); 398 399 if (doc == projectLocationTextField.getDocument()) { 400 if (projectName.equals(generatedProjectName)) { 401 File f = new File (projectLocationTextField.getText().trim()); 402 generatedProjectNameIndex = getValidProjectNameIndex(generatedProjectNameIndex, f, customBundle); 403 } else { 404 generatedProjectNameIndex = 0; 405 } 406 generatedProjectName = generatedProjectNameIndex > 0 ? getProjectName(generatedProjectNameIndex,customBundle) : null; 407 if(generatedProjectNameIndex > 0) { 408 projectName = generatedProjectName; 409 projectNameTextField.setText(generatedProjectName); 410 projectNameTextField.selectAll(); 411 } 412 } 413 414 String projectFolder = projectLocationTextField.getText(); 415 createdFolderTextField.setText(projectFolder + File.separatorChar + projectName); 416 } 417 panel.fireChangeEvent(); } 419 420 static File getCanonicalFile(File file) { 421 try { 422 return file.getCanonicalFile(); 423 } catch (IOException e) { 424 return null; 425 } 426 } 427 428 public static String getProjectName(int index, ResourceBundle rb) { 429 return MessageFormat.format(rb.getString("LBL_NPW1_DefaultProjectName"), new Object [] {String.valueOf(index)}); 430 } 431 432 private static int getValidProjectNameIndex(int currentIndex, File projectLocation, ResourceBundle rb) { 433 int index = currentIndex > 0 ? currentIndex : FoldersListSettings.getDefault().getNewProjectCount() + 1; 434 if(projectLocation != null) { 435 while (new File (projectLocation, getProjectName(index,rb)).exists()) { 436 index++; 437 } 438 } 439 return index; 440 } 441 442 private String getCreatedFolderText() { 443 return createdFolderTextField.getText().trim(); 444 } 445 446 } 447 448 | Popular Tags |