1 19 20 package org.netbeans.modules.j2ee.ejbfreeform.ui; 21 22 import java.awt.Component ; 23 import java.io.BufferedReader ; 24 import java.io.File ; 25 import java.io.InputStreamReader ; 26 import java.util.Enumeration ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Set ; 31 import javax.swing.event.ChangeEvent ; 32 import javax.swing.event.ChangeListener ; 33 import org.netbeans.modules.ant.freeform.spi.support.NewFreeformProjectSupport; 34 import org.netbeans.modules.java.freeform.spi.support.NewJavaFreeformProjectSupport; 35 import org.openide.ErrorManager; 36 import org.openide.WizardDescriptor; 37 import org.openide.filesystems.FileObject; 38 import org.openide.filesystems.FileStateInvalidException; 39 import org.openide.filesystems.FileUtil; 40 import org.openide.util.HelpCtx; 41 import org.openide.util.NbBundle; 42 43 44 47 public class EJBLocationsWizardPanel implements WizardDescriptor.Panel, ChangeListener { 48 49 private EJBLocationsPanel component; 50 private WizardDescriptor wizardDescriptor; 51 private File baseFolder; 52 53 public EJBLocationsWizardPanel() { 54 getComponent().setName(NbBundle.getMessage(NewEJBFreeformProjectWizardIterator.class, "TXT_NewEJBFreeformProjectWizardIterator_EJBSources")); } 56 57 public Component getComponent() { 58 if (component == null) { 59 component = new EJBLocationsPanel(this); 60 } 61 return component; 62 } 63 64 public HelpCtx getHelp() { 65 return new HelpCtx( EJBLocationsWizardPanel.class ); 66 } 67 68 public boolean isValid() { 69 getComponent(); 70 return component.valid(wizardDescriptor); 71 } 72 73 private final Set listeners = new HashSet (1); 74 public final void addChangeListener(ChangeListener l) { 75 synchronized (listeners) { 76 listeners.add(l); 77 } 78 } 79 public final void removeChangeListener(ChangeListener l) { 80 synchronized (listeners) { 81 listeners.remove(l); 82 } 83 } 84 protected final void fireChangeEvent() { 85 Iterator it; 86 synchronized (listeners) { 87 it = new HashSet (listeners).iterator(); 88 } 89 ChangeEvent ev = new ChangeEvent (this); 90 while (it.hasNext()) { 91 ((ChangeListener )it.next()).stateChanged(ev); 92 } 93 } 94 95 public void readSettings(Object settings) { 96 wizardDescriptor = (WizardDescriptor) settings; 97 wizardDescriptor.putProperty("NewProjectWizard_Title", component.getClientProperty("NewProjectWizard_Title")); 99 File baseFolder = (File )wizardDescriptor.getProperty(NewFreeformProjectSupport.PROP_PROJECT_LOCATION); 101 File nbProjectFolder = (File )wizardDescriptor.getProperty(NewFreeformProjectSupport.PROP_PROJECT_FOLDER); 102 final String configFiles; 103 final String srcPackages; 104 if(baseFolder.equals(this.baseFolder)) { 105 configFiles = component.getConfigFilesLocation().getAbsolutePath(); 106 srcPackages = component.getSrcPackagesLocation().getAbsolutePath(); 107 } else { 108 this.baseFolder = baseFolder; 109 FileObject fo = FileUtil.toFileObject(baseFolder); 110 if (fo != null) { 111 configFiles = guessConfigFiles(fo); 112 srcPackages = guessJavaRoot(fo); 113 } else { 114 configFiles = ""; srcPackages = ""; } 117 } 118 component.setFolders(baseFolder, nbProjectFolder); 119 component.setConfigFilesField(configFiles); 120 component.setSrcPackages(srcPackages); 121 } 122 123 public void storeSettings(Object settings) { 124 wizardDescriptor = (WizardDescriptor) settings; 125 wizardDescriptor.putProperty(NewEJBFreeformProjectWizardIterator.PROP_EJB_EJBMODULES, component.getEJBModules()); 126 127 List l = component.getJavaSrcFolder(); 128 wizardDescriptor.putProperty(NewJavaFreeformProjectSupport.PROP_EXTRA_JAVA_SOURCE_FOLDERS, l); 129 130 wizardDescriptor.putProperty(NewEJBFreeformProjectWizardIterator.PROP_EJB_SOURCE_FOLDERS, component.getEJBSrcFolder()); 131 wizardDescriptor.putProperty(NewEJBFreeformProjectWizardIterator.PROP_EJB_RESOURCE_FOLDERS, component.getResourcesFolder()); 132 wizardDescriptor.putProperty(NewEJBFreeformProjectWizardIterator.SERVER_ID, component.getSelectedServerID()); 133 wizardDescriptor.putProperty(NewEJBFreeformProjectWizardIterator.J2EE_LEVEL, component.getSelectedJ2eeSpec()); 134 wizardDescriptor.putProperty("NewProjectWizard_Title", null); } 136 137 private String guessConfigFiles (FileObject dir) { 139 Enumeration ch = dir.getChildren (true); 140 while (ch.hasMoreElements ()) { 141 FileObject f = (FileObject) ch.nextElement (); 142 if (f.getNameExt().equals ("ejb-jar.xml")) { return FileUtil.toFile(f.getParent()).getAbsolutePath(); 144 } 145 } 146 return ""; } 148 149 private String guessJavaRoot (FileObject dir) { 150 Enumeration ch = dir.getChildren (true); 151 try { 152 while (ch.hasMoreElements ()) { 153 FileObject f = (FileObject) ch.nextElement (); 154 if (f.getExt ().equals ("java") && !f.isFolder()) { String pckg = guessPackageName (f); 156 String pkgPath = f.getParent ().getPath (); 157 if (pckg != null && pkgPath.endsWith (pckg.replace ('.', '/'))) { 158 String rootName = pkgPath.substring (0, pkgPath.length () - pckg.length ()); 159 return FileUtil.toFile(f.getFileSystem().findResource(rootName)).getAbsolutePath(); 160 } 161 } 162 } 163 } catch (FileStateInvalidException fsie) { 164 ErrorManager.getDefault ().notify (ErrorManager.INFORMATIONAL, fsie); 165 } 166 return ""; } 168 169 private String guessPackageName(FileObject f) { 170 java.io.Reader r = null; 171 try { 172 r = new BufferedReader (new InputStreamReader (f.getInputStream (), "utf-8")); boolean noPackage = false; 174 for (;;) { 175 String line = ((BufferedReader ) r).readLine(); 176 if (line == null) { 177 if (noPackage) 178 return ""; 179 else 180 break; 181 } 182 line = line.trim(); 183 if (line.trim().startsWith("package")) { int idx = line.indexOf(";"); if (idx >= 0) 187 return line.substring("package".length(), idx).trim(); } 189 if (line.indexOf("class") != -1) 191 noPackage = true; 192 } 193 } catch (java.io.IOException ioe) { 194 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 195 } finally { 196 try { 197 if (r != null) 198 r.close (); 199 } catch (java.io.IOException ioe) { 200 } 202 } 203 204 return null; 205 } 206 207 public void stateChanged(ChangeEvent e) { 208 fireChangeEvent(); 209 } 210 211 } 212 | Popular Tags |