1 19 20 package org.netbeans.modules.java.j2seproject.ui.wizards; 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeEvent ; 24 import java.io.File ; 25 import java.text.MessageFormat ; 26 import java.util.ArrayList ; 27 import java.util.List ; 28 import javax.swing.JButton ; 29 import javax.swing.event.ChangeEvent ; 30 import javax.swing.event.ChangeListener ; 31 import org.netbeans.spi.project.ui.templates.support.Templates; 32 import org.openide.DialogDisplayer; 33 import org.openide.NotifyDescriptor; 34 import org.openide.WizardDescriptor; 35 import org.openide.WizardValidationException; 36 import org.openide.filesystems.FileObject; 37 import org.openide.filesystems.FileUtil; 38 import org.openide.util.HelpCtx; 39 import org.openide.util.NbBundle; 40 41 43 47 public class PanelSourceFolders extends SettingsPanel implements PropertyChangeListener { 48 49 private Panel firer; 50 private WizardDescriptor wizardDescriptor; 51 52 public static final String INITIAL_SOURCE_ROOT = "EXISTING_SOURCES_CURRENT_DIRECTORY"; 55 56 public PanelSourceFolders (Panel panel) { 57 this.firer = panel; 58 initComponents(); 59 this.setName(NbBundle.getMessage(PanelConfigureProjectVisual.class,"LAB_ConfigureSourceRoots")); 60 this.putClientProperty ("NewProjectWizard_Title", NbBundle.getMessage(PanelSourceFolders.class,"TXT_JavaExtSourcesProjectLocation")); this.getAccessibleContext().setAccessibleName(NbBundle.getMessage(PanelSourceFolders.class,"AN_PanelSourceFolders")); 62 this.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(PanelSourceFolders.class,"AD_PanelSourceFolders")); 63 this.sourcePanel.addPropertyChangeListener (this); 64 this.testsPanel.addPropertyChangeListener(this); 65 ((FolderList)this.sourcePanel).setRelatedFolderList((FolderList)this.testsPanel); 66 ((FolderList)this.testsPanel).setRelatedFolderList((FolderList)this.sourcePanel); 67 } 68 69 public void propertyChange(PropertyChangeEvent evt) { 70 if (FolderList.PROP_FILES.equals(evt.getPropertyName())) { 71 this.dataChanged(); 72 } 73 else if (FolderList.PROP_LAST_USED_DIR.equals (evt.getPropertyName())) { 74 if (evt.getSource() == this.sourcePanel) { 75 ((FolderList)this.testsPanel).setLastUsedDir 76 ((File )evt.getNewValue()); 77 } 78 else if (evt.getSource() == this.testsPanel) { 79 ((FolderList)this.sourcePanel).setLastUsedDir 80 ((File )evt.getNewValue()); 81 } 82 } 83 } 84 85 private void dataChanged () { 86 this.firer.fireChangeEvent(); 87 } 88 89 90 void read (WizardDescriptor settings) { 91 this.wizardDescriptor = settings; 92 File projectLocation = (File ) settings.getProperty ("projdir"); ((FolderList)this.sourcePanel).setProjectFolder(projectLocation); 94 ((FolderList)this.testsPanel).setProjectFolder(projectLocation); 95 File [] srcRoot = (File []) settings.getProperty ("sourceRoot"); assert srcRoot != null : "sourceRoot property must be initialized!" ; ((FolderList)this.sourcePanel).setFiles(srcRoot); 98 File [] testRoot = (File []) settings.getProperty ("testRoot"); assert testRoot != null : "testRoot property must be initialized!"; ((FolderList)this.testsPanel).setFiles (testRoot); 101 102 File currentDirectory = null; 104 FileObject folder = Templates.getExistingSourcesFolder(wizardDescriptor); 105 if (folder != null) { 106 currentDirectory = FileUtil.toFile(folder); 107 } 108 if (currentDirectory != null && currentDirectory.isDirectory()) { 109 ((FolderList)sourcePanel).setLastUsedDir(currentDirectory); 110 ((FolderList)testsPanel).setLastUsedDir(currentDirectory); 111 } 112 } 113 114 void store (WizardDescriptor settings) { 115 File [] sourceRoots = ((FolderList)this.sourcePanel).getFiles(); 116 File [] testRoots = ((FolderList)this.testsPanel).getFiles(); 117 settings.putProperty ("sourceRoot",sourceRoots); settings.putProperty("testRoot",testRoots); } 120 121 boolean valid (WizardDescriptor settings) { 122 File projectLocation = (File ) settings.getProperty ("projdir"); File [] sourceRoots = ((FolderList)this.sourcePanel).getFiles(); 124 File [] testRoots = ((FolderList)this.testsPanel).getFiles(); 125 String result = checkValidity (projectLocation, sourceRoots, testRoots); 126 if (result == null) { 127 wizardDescriptor.putProperty( "WizardPanel_errorMessage",""); return true; 129 } 130 else { 131 wizardDescriptor.putProperty( "WizardPanel_errorMessage",result); return false; 133 } 134 } 135 136 static String checkValidity (final File projectLocation, final File [] sources, final File [] tests ) { 137 String ploc = projectLocation.getAbsolutePath (); 138 for (int i=0; i<sources.length;i++) { 139 if (!sources[i].isDirectory() || !sources[i].canRead()) { 140 return MessageFormat.format(NbBundle.getMessage(PanelSourceFolders.class,"MSG_IllegalSources"), 141 new Object [] {sources[i].getAbsolutePath()}); 142 } 143 String sloc = sources[i].getAbsolutePath (); 144 if (ploc.equals (sloc) || ploc.startsWith (sloc + File.separatorChar)) { 145 return NbBundle.getMessage(PanelSourceFolders.class,"MSG_IllegalProjectFolder"); 146 } 147 } 148 for (int i=0; i<tests.length; i++) { 149 if (!tests[i].isDirectory() || !tests[i].canRead()) { 150 return MessageFormat.format(NbBundle.getMessage(PanelSourceFolders.class,"MSG_IllegalTests"), 151 new Object [] {sources[i].getAbsolutePath()}); 152 } 153 String tloc = tests[i].getAbsolutePath(); 154 if (ploc.equals(tloc) || ploc.startsWith(tloc + File.separatorChar)) { 155 return NbBundle.getMessage(PanelSourceFolders.class,"MSG_IllegalProjectFolder"); 156 } 157 } 158 return null; 159 } 160 161 void validate (WizardDescriptor d) throws WizardValidationException { 162 searchClassFiles (((FolderList)this.sourcePanel).getFiles()); 164 } 167 168 private static void findClassFiles(File folder, List <File > files) { 169 File [] kids = folder.listFiles(); 170 if (kids == null) { 171 return; 172 } 173 for (File kid : kids) { 174 if (kid.isFile() && kid.getName().endsWith(".class")) { 175 files.add(kid); 176 } else if (kid.isDirectory()) { 177 findClassFiles(kid, files); 178 } 179 } 180 } 181 private void searchClassFiles (File [] folders) throws WizardValidationException { 182 List <File > classFiles = new ArrayList <File >(); 183 for (File folder : folders) { 184 findClassFiles(folder, classFiles); 185 } 186 if (!classFiles.isEmpty()) { 187 JButton DELETE_OPTION = new JButton (NbBundle.getMessage (PanelSourceFolders.class, "TXT_DeleteOption")); JButton KEEP_OPTION = new JButton (NbBundle.getMessage (PanelSourceFolders.class, "TXT_KeepOption")); JButton CANCEL_OPTION = new JButton (NbBundle.getMessage (PanelSourceFolders.class, "TXT_CancelOption")); KEEP_OPTION.setMnemonic(NbBundle.getMessage (PanelSourceFolders.class, "MNE_KeepOption").charAt(0)); 191 DELETE_OPTION.getAccessibleContext().setAccessibleDescription (NbBundle.getMessage (PanelSourceFolders.class, "AD_DeleteOption")); 192 KEEP_OPTION.getAccessibleContext().setAccessibleDescription (NbBundle.getMessage (PanelSourceFolders.class, "AD_KeepOption")); 193 CANCEL_OPTION.getAccessibleContext().setAccessibleDescription (NbBundle.getMessage (PanelSourceFolders.class, "AD_CancelOption")); 194 NotifyDescriptor desc = new NotifyDescriptor ( 195 NbBundle.getMessage (PanelSourceFolders.class, "MSG_FoundClassFiles"), NbBundle.getMessage (PanelSourceFolders.class, "MSG_FoundClassFiles_Title"), NotifyDescriptor.YES_NO_CANCEL_OPTION, 198 NotifyDescriptor.QUESTION_MESSAGE, 199 new Object [] {DELETE_OPTION, KEEP_OPTION, CANCEL_OPTION}, 200 DELETE_OPTION 201 ); 202 Object result = DialogDisplayer.getDefault().notify(desc); 203 if (DELETE_OPTION.equals (result)) { 204 for (File f : classFiles) { 205 f.delete(); } 207 } else if (!KEEP_OPTION.equals (result)) { 208 throw new WizardValidationException (this.sourcePanel, "", ""); } 211 } 212 } 213 214 219 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 221 222 jLabel3 = new javax.swing.JLabel (); 223 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"), 224 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")); 225 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"), 226 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")); 227 228 setLayout(new java.awt.GridBagLayout ()); 229 230 getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelSourceFolders.class, "ACSN_PanelSourceFolders")); 231 getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelSourceFolders.class, "ACSD_PanelSourceFolders")); 232 org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(PanelSourceFolders.class, "LBL_SourceDirectoriesLabel")); 233 gridBagConstraints = new java.awt.GridBagConstraints (); 234 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 235 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 236 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 237 add(jLabel3, gridBagConstraints); 238 jLabel3.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getBundle(PanelSourceFolders.class).getString("ACSN_jLabel3")); 239 jLabel3.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(PanelSourceFolders.class).getString("ACSD_jLabel3")); 240 241 gridBagConstraints = new java.awt.GridBagConstraints (); 242 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 243 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 244 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 245 gridBagConstraints.weightx = 1.0; 246 gridBagConstraints.weighty = 0.45; 247 gridBagConstraints.insets = new java.awt.Insets (12, 0, 0, 0); 248 add(sourcePanel, gridBagConstraints); 249 250 gridBagConstraints = new java.awt.GridBagConstraints (); 251 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 252 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 253 gridBagConstraints.weightx = 1.0; 254 gridBagConstraints.weighty = 0.45; 255 gridBagConstraints.insets = new java.awt.Insets (12, 0, 0, 0); 256 add(testsPanel, gridBagConstraints); 257 258 } 260 261 262 private javax.swing.JLabel jLabel3; 264 private javax.swing.JPanel sourcePanel; 265 private javax.swing.JPanel testsPanel; 266 268 269 static class Panel implements WizardDescriptor.ValidatingPanel, WizardDescriptor.FinishablePanel { 270 271 private List <ChangeListener > listeners; 272 private PanelSourceFolders component; 273 private WizardDescriptor settings; 274 275 public synchronized void removeChangeListener(ChangeListener l) { 276 if (this.listeners == null) { 277 return; 278 } 279 this.listeners.remove(l); 280 } 281 282 public void addChangeListener(ChangeListener l) { 283 if (this.listeners == null) { 284 this.listeners = new ArrayList <ChangeListener >(); 285 } 286 this.listeners.add (l); 287 } 288 289 public void readSettings(Object settings) { 290 this.settings = (WizardDescriptor) settings; 291 this.component.read (this.settings); 292 Object substitute = component.getClientProperty ("NewProjectWizard_Title"); if (substitute != null) { 296 this.settings.putProperty ("NewProjectWizard_Title", substitute); } 298 } 299 300 public void storeSettings(Object settings) { 301 this.component.store (this.settings); 302 } 303 304 public void validate() throws WizardValidationException { 305 this.component.validate(this.settings); 306 } 307 308 public boolean isValid() { 309 return this.component.valid (this.settings); 310 } 311 312 public synchronized java.awt.Component getComponent() { 313 if (this.component == null) { 314 this.component = new PanelSourceFolders (this); 315 } 316 return this.component; 317 } 318 319 public HelpCtx getHelp() { 320 return new HelpCtx (PanelSourceFolders.class); 321 } 322 323 private void fireChangeEvent () { 324 ChangeListener [] _listeners; 325 synchronized (this) { 326 if (listeners == null) { 327 return; 328 } 329 _listeners = listeners.toArray(new ChangeListener [listeners.size()]); 330 } 331 ChangeEvent event = new ChangeEvent (this); 332 for (ChangeListener l : _listeners) { 333 l.stateChanged(event); 334 } 335 } 336 337 public boolean isFinishPanel() { 338 return true; 339 } 340 341 } 342 343 } 344 | Popular Tags |