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