1 19 20 package org.netbeans.modules.j2ee.clientproject.ui.wizards; 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeEvent ; 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.text.MessageFormat ; 27 import java.util.ArrayList ; 28 import java.util.Enumeration ; 29 import java.util.Iterator ; 30 import javax.swing.JFileChooser ; 31 import javax.swing.event.ChangeEvent ; 32 import javax.swing.event.ChangeListener ; 33 import javax.swing.event.DocumentEvent ; 34 import javax.swing.event.DocumentListener ; 35 import org.openide.DialogDisplayer; 36 import org.openide.ErrorManager; 37 import org.openide.NotifyDescriptor; 38 import org.openide.WizardDescriptor; 39 import org.openide.WizardValidationException; 40 import org.openide.filesystems.FileObject; 41 import org.openide.filesystems.FileUtil; 42 import org.openide.util.HelpCtx; 43 import org.openide.util.NbBundle; 44 45 47 51 public class PanelSourceFolders extends SettingsPanel implements PropertyChangeListener { 52 53 private final Panel firer; 54 private WizardDescriptor wizardDescriptor; 55 private File oldProjectLocation; 56 57 private final DocumentListener configFilesDocumentListener = new DocumentListener () { 58 public void changedUpdate(DocumentEvent e) { 59 configFilesChanged(); 60 } 61 62 public void insertUpdate(DocumentEvent e) { 63 configFilesChanged(); 64 } 65 66 public void removeUpdate(DocumentEvent e) { 67 configFilesChanged(); 68 } 69 }; 70 71 72 public PanelSourceFolders (Panel panel) { 73 this.firer = panel; 74 initComponents(); 75 this.setName(NbBundle.getMessage(PanelSourceFolders.class,"LAB_ConfigureSourceRoots")); 76 this.putClientProperty ("NewProjectWizard_Title", NbBundle.getMessage(PanelSourceFolders.class,"TXT_ImportAppClientModule")); this.getAccessibleContext().setAccessibleName(NbBundle.getMessage(PanelSourceFolders.class,"AN_PanelSourceFolders")); 78 this.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(PanelSourceFolders.class,"AD_PanelSourceFolders")); 79 this.sourcePanel.addPropertyChangeListener (this); 80 this.testsPanel.addPropertyChangeListener(this); 81 ((FolderList)this.sourcePanel).setRelatedFolderList((FolderList)this.testsPanel); 82 ((FolderList)this.testsPanel).setRelatedFolderList((FolderList)this.sourcePanel); 83 this.jTextFieldConfigFiles.getDocument().addDocumentListener(configFilesDocumentListener); 84 } 85 86 public void initValues(FileObject fo) { 87 ((FolderList) this.sourcePanel).setLastUsedDir(FileUtil.toFile(fo)); 88 ((FolderList) this.testsPanel).setLastUsedDir(FileUtil.toFile(fo)); 89 90 FileObject confFO = FileSearchUtility.guessConfigFilesPath(fo); 91 if (confFO == null) { } else { 94 String configFiles = FileUtil.toFile(confFO).getAbsolutePath(); 95 jTextFieldConfigFiles.setText(configFiles); 96 } 97 FileObject librariesFO = FileSearchUtility.guessLibrariesFolder(fo); 98 if (librariesFO != null) { 99 String libraries = FileUtil.toFile(librariesFO).getAbsolutePath(); 100 jTextFieldLibraries.setText(libraries); 101 } 102 } 103 104 105 public void propertyChange(PropertyChangeEvent evt) { 106 if (FolderList.PROP_FILES.equals(evt.getPropertyName())) { 107 this.dataChanged(); 108 } else if (FolderList.PROP_LAST_USED_DIR.equals (evt.getPropertyName())) { 109 if (evt.getSource() == this.sourcePanel) { 110 ((FolderList)this.testsPanel).setLastUsedDir 111 ((File )evt.getNewValue()); 112 } 113 else if (evt.getSource() == this.testsPanel) { 114 ((FolderList)this.sourcePanel).setLastUsedDir 115 ((File )evt.getNewValue()); 116 } 117 } 118 } 119 120 private void dataChanged () { 121 this.firer.fireChangeEvent(); 122 } 123 124 125 void read (WizardDescriptor settings) { 126 this.wizardDescriptor = settings; 127 128 File projectLocation = (File ) settings.getProperty(WizardProperties.SOURCE_ROOT); 130 ((FolderList)this.sourcePanel).setProjectFolder(projectLocation); 131 ((FolderList)this.testsPanel).setProjectFolder(projectLocation); 132 if (!projectLocation.equals(oldProjectLocation)) { 133 File [] srcRoot = (File []) settings.getProperty (WizardProperties.JAVA_ROOT); if (srcRoot!=null) { 135 ((FolderList)this.sourcePanel).setFiles(srcRoot); 136 } 137 File [] testRoot = (File []) settings.getProperty (WizardProperties.TEST_ROOT); if (testRoot != null) { 139 ((FolderList)this.testsPanel).setFiles (testRoot); 140 } 141 initValues(FileUtil.toFileObject(projectLocation)); 142 oldProjectLocation = projectLocation; 143 } 144 } 145 146 void store (WizardDescriptor settings) { 147 File [] sourceRoots = ((FolderList)this.sourcePanel).getFiles(); 148 File [] testRoots = ((FolderList)this.testsPanel).getFiles(); 149 settings.putProperty (WizardProperties.JAVA_ROOT,sourceRoots); settings.putProperty(WizardProperties.TEST_ROOT,testRoots); String configFiles = jTextFieldConfigFiles.getText().trim(); 152 if (configFiles.length() > 0) { 153 settings.putProperty(WizardProperties.CONFIG_FILES_FOLDER, new File (configFiles)); 154 } else { 155 settings.putProperty(WizardProperties.CONFIG_FILES_FOLDER, null); 156 } 157 String libPath = jTextFieldLibraries.getText().trim(); 158 if (libPath != null && !libPath.equals("")) { 159 settings.putProperty(WizardProperties.LIB_FOLDER, new File (libPath)); 160 } 161 } 162 163 boolean valid (WizardDescriptor settings) { 164 File projectLocation = (File ) settings.getProperty (WizardProperties.PROJECT_DIR); String confFolder = jTextFieldConfigFiles.getText().trim(); 166 if (confFolder.length() == 0) { 167 wizardDescriptor.putProperty("WizardPanel_errorMessage", NbBundle.getMessage(PanelSourceFolders.class, "MSG_BlankConfigurationFilesFolder")); 169 return false; 170 } 171 File [] sourceRoots = ((FolderList)this.sourcePanel).getFiles(); 172 File [] testRoots = ((FolderList)this.testsPanel).getFiles(); 173 String result = checkValidity (projectLocation, getConfigFiles(), sourceRoots, testRoots); 174 if (result == null) { 175 wizardDescriptor.putProperty( "WizardPanel_errorMessage"," "); return true; 177 } 178 else { 179 wizardDescriptor.putProperty( "WizardPanel_errorMessage",result); return false; 181 } 182 } 183 184 static String checkValidity (final File projectLocation, final File configFilesLocation, final File [] sources, final File [] tests ) { 185 String ploc = projectLocation.getAbsolutePath (); 186 if (configFilesLocation != null) { 187 FileObject fo = FileUtil.toFileObject(FileUtil.normalizeFile(configFilesLocation)); 188 if (fo == null || !fo.isFolder()) { 189 return NbBundle.getMessage(PanelSourceFolders.class, "MSG_IllegalConfigurationFilesFolder"); 190 } 191 } 192 if (sources.length ==0) { 193 return " "; } 195 for (int i=0; i<sources.length;i++) { 196 if (!sources[i].isDirectory() || !sources[i].canRead()) { 197 return MessageFormat.format(NbBundle.getMessage(PanelSourceFolders.class,"MSG_IllegalSources"), 198 new Object [] {sources[i].getAbsolutePath()}); 199 } 200 String sloc = sources[i].getAbsolutePath (); 201 if (ploc.equals (sloc) || ploc.startsWith (sloc + File.separatorChar)) { 202 return NbBundle.getMessage(PanelSourceFolders.class,"MSG_IllegalProjectFolder"); 203 } 204 } 205 for (int i=0; i<tests.length; i++) { 206 if (!tests[i].isDirectory() || !tests[i].canRead()) { 207 return MessageFormat.format(NbBundle.getMessage(PanelSourceFolders.class,"MSG_IllegalTests"), 208 new Object [] {sources[i].getAbsolutePath()}); 209 } 210 String tloc = tests[i].getAbsolutePath(); 211 if (ploc.equals(tloc) || ploc.startsWith(tloc + File.separatorChar)) { 212 return NbBundle.getMessage(PanelSourceFolders.class,"MSG_IllegalProjectFolder"); 213 } 214 } 215 return null; 216 } 217 218 void validate (WizardDescriptor d) throws WizardValidationException { 219 searchClassFiles (((FolderList)this.sourcePanel).getFiles()); 221 } 224 225 private void searchClassFiles (File [] folders) throws WizardValidationException { 226 boolean found = false; 227 for (int i=0; i<folders.length; i++) { 228 FileObject folder = FileUtil.toFileObject(folders[i]); 229 if (folder != null) { 230 Enumeration en = folder.getData (true); 231 while (!found && en.hasMoreElements ()) { 232 Object obj = en.nextElement (); 233 assert obj instanceof FileObject : "Instance of FileObject: " + obj; FileObject fo = (FileObject) obj; 235 found = "class".equals (fo.getExt ()); } 237 } 238 } 239 if (found) { 240 Object DELETE_OPTION = NbBundle.getMessage (PanelSourceFolders.class, "TXT_DeleteOption"); Object KEEP_OPTION = NbBundle.getMessage (PanelSourceFolders.class, "TXT_KeepOption"); Object CANCEL_OPTION = NbBundle.getMessage (PanelSourceFolders.class, "TXT_CancelOption"); NotifyDescriptor desc = new NotifyDescriptor ( 244 NbBundle.getMessage (PanelSourceFolders.class, "MSG_FoundClassFiles"), NbBundle.getMessage (PanelSourceFolders.class, "MSG_FoundClassFiles_Title"), NotifyDescriptor.YES_NO_CANCEL_OPTION, 247 NotifyDescriptor.QUESTION_MESSAGE, 248 new Object [] {DELETE_OPTION, KEEP_OPTION, CANCEL_OPTION}, 249 null 250 ); 251 Object result = DialogDisplayer.getDefault().notify(desc); 252 if (DELETE_OPTION.equals (result)) { 253 deleteClassFiles (folders); 254 } else if (!KEEP_OPTION.equals (result)) { 255 throw new WizardValidationException (this.sourcePanel, "", ""); } 258 } 259 } 260 261 private void deleteClassFiles (File [] folders) { 262 for (int i=0; i<folders.length; i++) { 263 FileObject folder = FileUtil.toFileObject(folders[i]); 264 Enumeration en = folder.getData (true); 265 while (en.hasMoreElements ()) { 266 Object obj = en.nextElement (); 267 assert obj instanceof FileObject : "Instance of FileObject: " + obj; 268 FileObject fo = (FileObject) obj; 269 try { 270 if ("class".equals (fo.getExt ())) { fo.delete (); 272 } 273 } catch (IOException ioe) { 274 ErrorManager.getDefault ().notify (ioe); 275 } 276 } 277 } 278 } 279 280 285 private void initComponents() { 287 java.awt.GridBagConstraints gridBagConstraints; 288 289 jLabel3 = new javax.swing.JLabel (); 290 sourcePanel = new FolderList (NbBundle.getMessage(PanelSourceFolders.class,"CTL_SourceRoots"), NbBundle.getMessage(PanelSourceFolders.class,"MNE_SourceRoots").charAt(0),NbBundle.getMessage(PanelSourceFolders.class,"AD_SourceRoots"), NbBundle.getMessage(PanelSourceFolders.class,"CTL_AddSourceRoot"), 291 NbBundle.getMessage(PanelSourceFolders.class,"MNE_AddSourceFolder").charAt(0), NbBundle.getMessage(PanelSourceFolders.class,"AD_AddSourceFolder"),NbBundle.getMessage(PanelSourceFolders.class,"MNE_RemoveSourceFolder").charAt(0), NbBundle.getMessage(PanelSourceFolders.class,"AD_RemoveSourceFolder")); 292 testsPanel = new FolderList (NbBundle.getMessage(PanelSourceFolders.class,"CTL_TestRoots"), NbBundle.getMessage(PanelSourceFolders.class,"MNE_TestRoots").charAt(0),NbBundle.getMessage(PanelSourceFolders.class,"AD_TestRoots"), NbBundle.getMessage(PanelSourceFolders.class,"CTL_AddTestRoot"), 293 NbBundle.getMessage(PanelSourceFolders.class,"MNE_AddTestFolder").charAt(0), NbBundle.getMessage(PanelSourceFolders.class,"AD_AddTestFolder"),NbBundle.getMessage(PanelSourceFolders.class,"MNE_RemoveTestFolder").charAt(0), NbBundle.getMessage(PanelSourceFolders.class,"AD_RemoveTestFolder")); 294 jLabel1 = new javax.swing.JLabel (); 295 jTextFieldConfigFiles = new javax.swing.JTextField (); 296 jButtonConfigFilesLocation = new javax.swing.JButton (); 297 jLabel2 = new javax.swing.JLabel (); 298 jTextFieldLibraries = new javax.swing.JTextField (); 299 jButtonLibraries = new javax.swing.JButton (); 300 301 setLayout(new java.awt.GridBagLayout ()); 302 303 getAccessibleContext().setAccessibleName(null); 304 getAccessibleContext().setAccessibleDescription(null); 305 org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(PanelSourceFolders.class, "LBL_SourceDirectoriesLabel")); 306 gridBagConstraints = new java.awt.GridBagConstraints (); 307 gridBagConstraints.gridy = 0; 308 gridBagConstraints.gridwidth = 3; 309 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 310 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 311 gridBagConstraints.insets = new java.awt.Insets (0, 0, 11, 0); 312 add(jLabel3, gridBagConstraints); 313 jLabel3.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getBundle(PanelSourceFolders.class).getString("ACSN_jLabel3")); 314 jLabel3.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(PanelSourceFolders.class).getString("ACSD_jLabel3")); 315 316 gridBagConstraints = new java.awt.GridBagConstraints (); 317 gridBagConstraints.gridy = 3; 318 gridBagConstraints.gridwidth = 3; 319 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 320 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 321 gridBagConstraints.weightx = 1.0; 322 gridBagConstraints.weighty = 0.45; 323 gridBagConstraints.insets = new java.awt.Insets (12, 0, 0, 0); 324 add(sourcePanel, gridBagConstraints); 325 326 gridBagConstraints = new java.awt.GridBagConstraints (); 327 gridBagConstraints.gridy = 4; 328 gridBagConstraints.gridwidth = 3; 329 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 330 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 331 gridBagConstraints.weightx = 1.0; 332 gridBagConstraints.weighty = 0.45; 333 gridBagConstraints.insets = new java.awt.Insets (12, 0, 0, 0); 334 add(testsPanel, gridBagConstraints); 335 336 jLabel1.setLabelFor(jTextFieldConfigFiles); 337 org.openide.awt.Mnemonics.setLocalizedText(jLabel1, java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/clientproject/ui/wizards/Bundle").getString("LBL_IW_ConfigFilesFolder_Label")); 338 gridBagConstraints = new java.awt.GridBagConstraints (); 339 gridBagConstraints.gridx = 0; 340 gridBagConstraints.gridy = 1; 341 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 342 gridBagConstraints.insets = new java.awt.Insets (0, 0, 11, 11); 343 add(jLabel1, gridBagConstraints); 344 345 gridBagConstraints = new java.awt.GridBagConstraints (); 346 gridBagConstraints.gridx = 1; 347 gridBagConstraints.gridy = 1; 348 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 349 gridBagConstraints.weightx = 1.0; 350 gridBagConstraints.insets = new java.awt.Insets (0, 0, 11, 11); 351 add(jTextFieldConfigFiles, gridBagConstraints); 352 353 org.openide.awt.Mnemonics.setLocalizedText(jButtonConfigFilesLocation, java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/clientproject/ui/wizards/Bundle").getString("LBL_NWP1_BrowseLocation_Button_w")); 354 jButtonConfigFilesLocation.addActionListener(new java.awt.event.ActionListener () { 355 public void actionPerformed(java.awt.event.ActionEvent evt) { 356 jButtonConfigFilesLocationActionPerformed(evt); 357 } 358 }); 359 360 gridBagConstraints = new java.awt.GridBagConstraints (); 361 gridBagConstraints.gridx = 2; 362 gridBagConstraints.gridy = 1; 363 gridBagConstraints.insets = new java.awt.Insets (0, 0, 11, 0); 364 add(jButtonConfigFilesLocation, gridBagConstraints); 365 366 jLabel2.setLabelFor(jTextFieldLibraries); 367 org.openide.awt.Mnemonics.setLocalizedText(jLabel2, java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/clientproject/ui/wizards/Bundle").getString("LBL_IW_LibrariesLocation_Label")); 368 gridBagConstraints = new java.awt.GridBagConstraints (); 369 gridBagConstraints.gridx = 0; 370 gridBagConstraints.gridy = 2; 371 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 372 gridBagConstraints.insets = new java.awt.Insets (0, 0, 11, 11); 373 add(jLabel2, gridBagConstraints); 374 375 gridBagConstraints = new java.awt.GridBagConstraints (); 376 gridBagConstraints.gridx = 1; 377 gridBagConstraints.gridy = 2; 378 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 379 gridBagConstraints.weightx = 1.0; 380 gridBagConstraints.insets = new java.awt.Insets (0, 0, 11, 11); 381 add(jTextFieldLibraries, gridBagConstraints); 382 383 org.openide.awt.Mnemonics.setLocalizedText(jButtonLibraries, java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/clientproject/ui/wizards/Bundle").getString("LBL_NWP1_BrowseLocation_Button")); 384 jButtonLibraries.addActionListener(new java.awt.event.ActionListener () { 385 public void actionPerformed(java.awt.event.ActionEvent evt) { 386 jButtonLibrariesActionPerformed(evt); 387 } 388 }); 389 390 gridBagConstraints = new java.awt.GridBagConstraints (); 391 gridBagConstraints.gridx = 2; 392 gridBagConstraints.gridy = 2; 393 gridBagConstraints.insets = new java.awt.Insets (0, 0, 11, 0); 394 add(jButtonLibraries, gridBagConstraints); 395 396 } 398 private void jButtonLibrariesActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser (); 400 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 401 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 402 if (jTextFieldLibraries.getText().length() > 0 && getLibraries().exists()) { 403 chooser.setSelectedFile(getLibraries()); 404 } else { 405 chooser.setCurrentDirectory((File ) wizardDescriptor.getProperty(WizardProperties.PROJECT_DIR)); 406 } 407 if ( JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { 408 File configFilesDir = FileUtil.normalizeFile(chooser.getSelectedFile()); 409 jTextFieldLibraries.setText(configFilesDir.getAbsolutePath()); 410 } 411 } 413 private void jButtonConfigFilesLocationActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser (); 415 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 416 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 417 if (jTextFieldConfigFiles.getText().length() > 0 && getConfigFiles().exists()) { 418 chooser.setSelectedFile(getConfigFiles()); 419 } else { 420 chooser.setCurrentDirectory((File ) wizardDescriptor.getProperty(WizardProperties.PROJECT_DIR)); 421 } 422 if ( JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { 423 File configFilesDir = FileUtil.normalizeFile(chooser.getSelectedFile()); 424 jTextFieldConfigFiles.setText(configFilesDir.getAbsolutePath()); 425 } 426 } 428 429 430 private javax.swing.JButton jButtonConfigFilesLocation; 432 private javax.swing.JButton jButtonLibraries; 433 private javax.swing.JLabel jLabel1; 434 private javax.swing.JLabel jLabel2; 435 private javax.swing.JLabel jLabel3; 436 private javax.swing.JTextField jTextFieldConfigFiles; 437 private javax.swing.JTextField jTextFieldLibraries; 438 private javax.swing.JPanel sourcePanel; 439 private javax.swing.JPanel testsPanel; 440 442 443 static class Panel implements WizardDescriptor.ValidatingPanel { 444 445 private ArrayList <ChangeListener > listeners; 446 private PanelSourceFolders component; 447 private WizardDescriptor settings; 448 449 public synchronized void removeChangeListener(ChangeListener l) { 450 if (this.listeners == null) { 451 return; 452 } 453 this.listeners.remove(l); 454 } 455 456 public void addChangeListener(ChangeListener l) { 457 if (this.listeners == null) { 458 this.listeners = new ArrayList <ChangeListener >(); 459 } 460 this.listeners.add (l); 461 } 462 463 public void readSettings(Object settings) { 464 this.settings = (WizardDescriptor) settings; 465 this.component.read (this.settings); 466 Object substitute = component.getClientProperty ("NewProjectWizard_Title"); if (substitute != null) { 470 this.settings.putProperty ("NewProjectWizard_Title", substitute); } 472 } 473 474 public void storeSettings(Object settings) { 475 this.component.store (this.settings); 476 } 477 478 public void validate() throws WizardValidationException { 479 this.component.validate(this.settings); 480 } 481 482 public boolean isValid() { 483 return this.component.valid (this.settings); 484 } 485 486 public synchronized java.awt.Component getComponent() { 487 if (this.component == null) { 488 this.component = new PanelSourceFolders (this); 489 } 490 return this.component; 491 } 492 493 public HelpCtx getHelp() { 494 return new HelpCtx (PanelSourceFolders.class); 495 } 496 497 private void fireChangeEvent () { 498 Iterator it = null; 499 synchronized (this) { 500 if (this.listeners == null) { 501 return; 502 } 503 it = ((ArrayList )this.listeners.clone()).iterator(); 504 } 505 ChangeEvent event = new ChangeEvent (this); 506 while (it.hasNext()) { 507 ((ChangeListener )it.next()).stateChanged(event); 508 } 509 } 510 511 } 512 513 private File getAsFile(String filename) { 514 return FileUtil.normalizeFile(new File (filename)); 515 } 516 517 public File getConfigFiles() { 518 return getAsFile(jTextFieldConfigFiles.getText()); 519 } 520 521 public File getLibraries() { 522 return getAsFile(jTextFieldLibraries.getText()); 523 } 524 525 private void configFilesChanged() { 526 dataChanged(); 527 } 528 529 } 530 | Popular Tags |