1 19 20 package org.netbeans.modules.j2ee.earproject.ui.wizards; 21 22 import java.io.File ; 23 import java.text.MessageFormat ; 24 import java.util.ResourceBundle ; 25 import javax.swing.JFileChooser ; 26 import javax.swing.event.DocumentEvent ; 27 import javax.swing.event.DocumentListener ; 28 import org.netbeans.spi.project.ui.support.ProjectChooser; 29 import org.netbeans.spi.project.ui.templates.support.Templates; 30 import org.openide.WizardDescriptor; 31 import org.openide.filesystems.FileObject; 32 import org.openide.filesystems.FileUtil; 33 34 final class PanelProjectImportVisual 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 private String lastComputedPrjName; 41 private String lastComputedPrjFolder; 42 private boolean ignoreEvent; 43 44 private boolean firstTime = true; 46 47 48 public PanelProjectImportVisual(PanelConfigureProject panel, String propNameIndex, ResourceBundle customBundle, boolean importStyle) { 49 this.customBundle = customBundle; 50 initComponents(); 51 this.panel = panel; 52 this.propNameIndex = propNameIndex; 53 54 projectNameTextField.getDocument().addDocumentListener(this); 56 projectLocationTextField.getDocument().addDocumentListener(this); 57 this.importStyle = importStyle; 58 createdFolderTextField.setEditable(importStyle); 59 if (importStyle) { 60 createdFolderTextField.getDocument().addDocumentListener(this); 61 } 62 browseFolderButton.setVisible(importStyle); 63 } 64 65 void addNameListener(DocumentListener listener) { 66 projectNameTextField.getDocument().addDocumentListener(listener); 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 browseLocationButton = new javax.swing.JButton (); 83 createdFolderLabel = new javax.swing.JLabel (); 84 createdFolderTextField = new javax.swing.JTextField (); 85 browseFolderButton = new javax.swing.JButton (); 86 instructionLabel = new javax.swing.JLabel (); 87 descriptionArea = new javax.swing.JTextArea (); 88 spaceFiller = new javax.swing.JPanel (); 89 90 setLayout(new java.awt.GridBagLayout ()); 91 92 projectNameLabel.setLabelFor(projectNameTextField); 93 org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel, org.openide.util.NbBundle.getMessage(PanelProjectImportVisual.class, "LBL_NWP1_ProjectName_Label")); gridBagConstraints = new java.awt.GridBagConstraints (); 95 gridBagConstraints.gridx = 0; 96 gridBagConstraints.gridy = 3; 97 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 98 gridBagConstraints.insets = new java.awt.Insets (6, 0, 6, 0); 99 add(projectNameLabel, gridBagConstraints); 100 101 gridBagConstraints = new java.awt.GridBagConstraints (); 102 gridBagConstraints.gridx = 1; 103 gridBagConstraints.gridy = 3; 104 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 105 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 106 gridBagConstraints.weightx = 1.0; 107 gridBagConstraints.insets = new java.awt.Insets (6, 12, 6, 0); 108 add(projectNameTextField, gridBagConstraints); 109 projectNameTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectImportVisual.class, "ACS_LBL_NWP1_ProjectName_A11YDesc")); 111 projectLocationLabel.setLabelFor(projectLocationTextField); 112 org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel, org.openide.util.NbBundle.getMessage(PanelProjectImportVisual.class, "LBL_ImportLocation_Label")); gridBagConstraints = new java.awt.GridBagConstraints (); 114 gridBagConstraints.gridx = 0; 115 gridBagConstraints.gridy = 1; 116 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 117 gridBagConstraints.insets = new java.awt.Insets (6, 0, 6, 0); 118 add(projectLocationLabel, gridBagConstraints); 119 120 gridBagConstraints = new java.awt.GridBagConstraints (); 121 gridBagConstraints.gridx = 1; 122 gridBagConstraints.gridy = 1; 123 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 124 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 125 gridBagConstraints.weightx = 1.0; 126 gridBagConstraints.insets = new java.awt.Insets (6, 12, 6, 0); 127 add(projectLocationTextField, gridBagConstraints); 128 projectLocationTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectImportVisual.class, "ACS_LBL_NPW1_ProjectLocation_A11YDesc")); 130 org.openide.awt.Mnemonics.setLocalizedText(browseLocationButton, org.openide.util.NbBundle.getMessage(PanelProjectImportVisual.class, "LBL_NWP1_BrowseLocation_Button")); browseLocationButton.setActionCommand("BROWSE"); 132 browseLocationButton.addActionListener(new java.awt.event.ActionListener () { 133 public void actionPerformed(java.awt.event.ActionEvent evt) { 134 browseLocationAction(evt); 135 } 136 }); 137 138 gridBagConstraints = new java.awt.GridBagConstraints (); 139 gridBagConstraints.gridx = 2; 140 gridBagConstraints.gridy = 1; 141 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 142 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 143 gridBagConstraints.insets = new java.awt.Insets (6, 6, 6, 0); 144 add(browseLocationButton, gridBagConstraints); 145 browseLocationButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectImportVisual.class, "ACS_LBL_NWP1_BrowseLocation_A11YDesc")); 147 createdFolderLabel.setLabelFor(createdFolderTextField); 148 org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel, org.openide.util.NbBundle.getMessage(PanelProjectImportVisual.class, "LBL_NWP1_CreatedProjectFolder_Lablel")); gridBagConstraints = new java.awt.GridBagConstraints (); 150 gridBagConstraints.gridx = 0; 151 gridBagConstraints.gridy = 4; 152 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 153 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 154 add(createdFolderLabel, gridBagConstraints); 155 156 gridBagConstraints = new java.awt.GridBagConstraints (); 157 gridBagConstraints.gridx = 1; 158 gridBagConstraints.gridy = 4; 159 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 160 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 161 gridBagConstraints.weightx = 1.0; 162 gridBagConstraints.insets = new java.awt.Insets (0, 12, 0, 0); 163 add(createdFolderTextField, gridBagConstraints); 164 createdFolderTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectImportVisual.class, "ACS_LBL_NWP1_CreatedProjectFolder_A11YDesc")); 166 org.openide.awt.Mnemonics.setLocalizedText(browseFolderButton, org.openide.util.NbBundle.getMessage(PanelProjectImportVisual.class, "LBL_NWP1_BrowseProjectFolder_Button")); browseFolderButton.setActionCommand("BROWSE"); 168 browseFolderButton.addActionListener(new java.awt.event.ActionListener () { 169 public void actionPerformed(java.awt.event.ActionEvent evt) { 170 browseFolderAction(evt); 171 } 172 }); 173 174 gridBagConstraints = new java.awt.GridBagConstraints (); 175 gridBagConstraints.gridx = 2; 176 gridBagConstraints.gridy = 4; 177 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 178 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 179 gridBagConstraints.insets = new java.awt.Insets (0, 6, 0, 0); 180 add(browseFolderButton, gridBagConstraints); 181 browseFolderButton.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelProjectImportVisual.class, "ACSN_LBL_NWP1_BrowseProjectFolder_Button")); browseFolderButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectImportVisual.class, "ACSD_LBL_NWP1_BrowseProjectFolder_Button")); 184 instructionLabel.setBackground(new java.awt.Color (255, 255, 255)); 185 org.openide.awt.Mnemonics.setLocalizedText(instructionLabel, org.openide.util.NbBundle.getMessage(PanelProjectImportVisual.class, "LBL_ImportInstructions2")); gridBagConstraints = new java.awt.GridBagConstraints (); 187 gridBagConstraints.gridx = 0; 188 gridBagConstraints.gridy = 2; 189 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 190 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 191 add(instructionLabel, gridBagConstraints); 192 193 descriptionArea.setEditable(false); 194 descriptionArea.setLineWrap(true); 195 descriptionArea.setRows(2); 196 descriptionArea.setText(org.openide.util.NbBundle.getMessage(PanelProjectImportVisual.class, "LBL_ImportInstructions1")); descriptionArea.setWrapStyleWord(true); 198 descriptionArea.setOpaque(false); 199 gridBagConstraints = new java.awt.GridBagConstraints (); 200 gridBagConstraints.gridx = 0; 201 gridBagConstraints.gridy = 0; 202 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 203 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 204 gridBagConstraints.insets = new java.awt.Insets (5, 0, 5, 0); 205 add(descriptionArea, gridBagConstraints); 206 descriptionArea.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelProjectImportVisual.class, "ACSN_LBL_ImportInstructions1")); descriptionArea.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectImportVisual.class, "ACSD_LBL_ImportInstructions1")); 209 gridBagConstraints = new java.awt.GridBagConstraints (); 210 gridBagConstraints.gridx = 0; 211 gridBagConstraints.gridy = 5; 212 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 213 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 214 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 215 gridBagConstraints.weighty = 1.0; 216 add(spaceFiller, gridBagConstraints); 217 218 } 220 private void browseFolderAction(java.awt.event.ActionEvent evt) { String command = evt.getActionCommand(); 222 223 if ("BROWSE".equals(command)) { JFileChooser chooser = new JFileChooser (); 225 chooser.setDialogTitle(customBundle.getString("LBL_NWP1_BrowseProjectFolder")); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 227 String path = getCreatedFolderText(); 228 if (path.length() > 0) { 229 File f = new File (path); 230 if (f.exists()) { 231 chooser.setSelectedFile(f); 232 } 233 } else { 234 chooser.setSelectedFile(ProjectChooser.getProjectsFolder()); 235 } 236 if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { 237 File projectDir = chooser.getSelectedFile(); 238 createdFolderTextField.setText(projectDir.getAbsolutePath()); 239 } 240 panel.fireChangeEvent(); 241 } 242 } 244 private void browseLocationAction(java.awt.event.ActionEvent evt) { String command = evt.getActionCommand(); 246 247 if ("BROWSE".equals(command)) { JFileChooser chooser = new JFileChooser (); 249 chooser.setDialogTitle(customBundle.getString("LBL_NWP1_SelectProjectLocation")); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 251 String path = projectLocationTextField.getText(); 252 if (path.length() > 0) { 253 File f = new File (path); 254 if (f.exists()) { 255 chooser.setSelectedFile(f); 256 } 257 } else { 258 chooser.setSelectedFile(ProjectChooser.getProjectsFolder()); 259 } 260 if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { 261 File projectDir = chooser.getSelectedFile(); 262 projectLocationTextField.setText(projectDir.getAbsolutePath()); 263 } 264 panel.fireChangeEvent(); 265 } 266 } 268 public void addNotify() { 269 super.addNotify(); 270 projectNameTextField.requestFocus(); 272 } 273 274 boolean valid(WizardDescriptor wizardDescriptor) { 275 String sourceLocationPath = projectLocationTextField.getText().trim(); 276 if (sourceLocationPath.length() == 0 277 || !new File (sourceLocationPath).isDirectory()) { 278 wizardDescriptor.putProperty("WizardPanel_errorMessage", customBundle.getString("MSG_ProvideExistingSourcesLocation")); return false; 281 } 282 283 if (!SettingsPanel.isValidProjectName(projectNameTextField.getText())) { 284 wizardDescriptor.putProperty("WizardPanel_errorMessage", customBundle.getString("MSG_IllegalProjectName")); return false; 287 } 288 289 if (importStyle && getCreatedFolderText().length() == 0) { 290 wizardDescriptor.putProperty("WizardPanel_errorMessage", customBundle.getString("MSG_EmptyProjectFolder")); return false; 293 } 294 295 File destParent = SettingsPanel.findExistingParent(getCreatedFolderText()); 296 297 File destDir = new File (getCreatedFolderText()); 298 if (destDir.exists() && !destDir.isDirectory()) { 299 wizardDescriptor.putProperty("WizardPanel_errorMessage", MessageFormat.format(customBundle.getString("MSG_WillOverwrite"),new Object [] {destDir.getAbsolutePath() })); return false; 302 } 303 File buildXml = new File (getCreatedFolderText()+ 304 File.separator+"build.xml"); if (buildXml.exists()) { 306 wizardDescriptor.putProperty("WizardPanel_errorMessage", MessageFormat.format(customBundle.getString("MSG_WillOverwrite"),new Object [] {buildXml.getAbsolutePath() })); return false; 309 } 310 File tFile = null; 319 tFile = new File (getCreatedFolderText()+ 320 File.separator+"nbproject"+File.separator+"project.xml"); if (tFile.exists()) { 322 wizardDescriptor.putProperty("WizardPanel_errorMessage", MessageFormat.format(customBundle.getString("MSG_AlreadyExixts"),new Object [] {tFile.getAbsolutePath()})); return false; 325 } 326 tFile = new File (getCreatedFolderText()+ 327 File.separator+"nbproject"+File.separator+"project.properties"); if (tFile.exists()) { 329 wizardDescriptor.putProperty("WizardPanel_errorMessage", MessageFormat.format(customBundle.getString("MSG_AlreadyExixts"),new Object [] {tFile.getAbsolutePath()})); return false; 332 } 333 if (null != destParent && destParent.exists() && !destParent.canWrite()) { 334 wizardDescriptor.putProperty("WizardPanel_errorMessage", MessageFormat.format(customBundle.getString("MSG_ProjectLocationRO"),new Object [] {destParent.getAbsolutePath()})); return false; 338 } 339 340 wizardDescriptor.putProperty("WizardPanel_errorMessage", ""); return true; 342 } 343 344 void store(WizardDescriptor d) { 345 String name = projectNameTextField.getText().trim(); 346 347 d.putProperty(WizardProperties.PROJECT_DIR, new File (getCreatedFolderText())); 348 d.putProperty(WizardProperties.NAME, name); 349 File srcRoot = null; 350 String srcPath = projectLocationTextField.getText(); 351 if (srcPath.length() > 0) { 352 srcRoot = FileUtil.normalizeFile(new File (srcPath)); 353 } 354 d.putProperty(WizardProperties.SOURCE_ROOT, srcRoot); 355 } 356 357 void read(WizardDescriptor settings) { 358 if (!firstTime) { 359 return; 360 } 361 firstTime = false; 362 File projectLocation = getPreferredProjectLocation(settings); 363 364 if (null != projectLocation) { 365 String projectName = (String ) settings.getProperty(WizardProperties.NAME); 367 if (projectName == null) { 368 int baseCount = 1; String formater = customBundle.getString( "TXT_DefaultProjectName"); 371 while ((projectName = validFreeProjectName(projectLocation, formater, baseCount)) == null) { 372 baseCount++; 373 } 374 settings.putProperty(propNameIndex, baseCount); 376 } 377 } 383 384 projectNameTextField.selectAll(); 385 } 386 387 private static File getPreferredProjectLocation(final WizardDescriptor settings) { 388 File projectLocation = null; 389 FileObject existingSourcesFO = Templates.getExistingSourcesFolder(settings); 390 if (existingSourcesFO != null) { 391 File existingSourcesFile = FileUtil.toFile(existingSourcesFO); 392 if (existingSourcesFile != null && existingSourcesFile.isDirectory()) { 393 projectLocation = existingSourcesFile; 394 } 395 } else { 396 projectLocation = (File ) settings.getProperty(WizardProperties.PROJECT_DIR); 397 if (projectLocation == null) { 398 projectLocation = ProjectChooser.getProjectsFolder(); 399 } else { 400 projectLocation = projectLocation.getParentFile(); 401 } 402 } 403 return projectLocation; 404 } 405 406 private javax.swing.JButton browseFolderButton; 408 private javax.swing.JButton browseLocationButton; 409 private javax.swing.JLabel createdFolderLabel; 410 private javax.swing.JTextField createdFolderTextField; 411 private javax.swing.JTextArea descriptionArea; 412 private javax.swing.JLabel instructionLabel; 413 private javax.swing.JLabel projectLocationLabel; 414 private javax.swing.JTextField projectLocationTextField; 415 private javax.swing.JLabel projectNameLabel; 416 private javax.swing.JTextField projectNameTextField; 417 private javax.swing.JPanel spaceFiller; 418 420 428 private String validFreeProjectName(final File parentFolder, final String formater, final int index) { 429 String name = MessageFormat.format(formater, index); 430 File file = new File (parentFolder, name); 431 return file.exists() ? null : name; 432 } 433 434 public void changedUpdate(final DocumentEvent e) { 436 updateTexts(e); 437 } 438 439 public void insertUpdate(final DocumentEvent e) { 440 updateTexts(e); 441 } 442 443 public void removeUpdate(final DocumentEvent e) { 444 updateTexts(e); 445 } 446 448 449 private void updateTexts(final DocumentEvent e) { 450 if (!importStyle) { 451 createdFolderTextField.setText(getCreatedFolderPath()); 452 } else { 453 if (ignoreEvent) { 454 return; 455 } 456 ignoreEvent = true; 457 if (e.getDocument() != projectNameTextField.getDocument()) { updateProjectName(); 459 } 460 if (e.getDocument() != createdFolderTextField.getDocument()) { updateProjectFolder(); 462 } 463 ignoreEvent = false; 464 } 465 panel.fireChangeEvent(); } 467 468 private void updateProjectName() { 469 String prjName = computeProjectName(); 470 if ((lastComputedPrjName != null) && (!lastComputedPrjName.equals(projectNameTextField.getText().trim()))) { 471 return; 472 } 473 lastComputedPrjName = prjName; 474 if (prjName != null) { 475 projectNameTextField.setText(prjName); 476 } 477 } 478 479 private String computeProjectName() { 480 String cPrjName = null; 481 File f = FileUtil.normalizeFile(new File (projectLocationTextField.getText())); 482 FileObject fo = FileUtil.toFileObject(f); 483 if (fo != null) { 484 cPrjName = fo.getName(); 485 } 486 return cPrjName; 487 } 488 489 private void updateProjectFolder() { 490 String prjFolder = computeProjectFolder(); 491 if ((lastComputedPrjFolder != null) && (!lastComputedPrjFolder.equals(createdFolderTextField.getText().trim()))) { 492 return; 493 } 494 lastComputedPrjFolder = prjFolder; 495 if (prjFolder != null) { 496 createdFolderTextField.setText(prjFolder); 497 } else { 498 createdFolderTextField.setText(""); } 500 } 501 502 private String computeProjectFolder() { 503 return FileUtil.normalizeFile(new File (projectLocationTextField.getText())).getAbsolutePath(); 504 } 505 506 private String getCreatedFolderPath() { 507 StringBuffer folder = new StringBuffer (projectLocationTextField.getText().trim()); 508 if (!importStyle) { 509 if (!projectLocationTextField.getText().endsWith(File.separator)) { 510 folder.append(File.separatorChar); 511 } 512 folder.append(projectNameTextField.getText().trim()); 513 } 514 515 return folder.toString(); 516 } 517 518 private String getCreatedFolderText() { 519 return createdFolderTextField.getText().trim(); 520 } 521 522 } 523 524 | Popular Tags |