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