1 19 20 package org.netbeans.modules.web.freeform.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 46 public class WebLocationsWizardPanel implements WizardDescriptor.Panel { 47 48 private WebLocationsPanel component; 49 private WizardDescriptor wizardDescriptor; 50 private File baseFolder; 51 52 public WebLocationsWizardPanel() { 53 getComponent().setName(NbBundle.getMessage(NewWebFreeformProjectWizardIterator.class, "TXT_NewWebFreeformProjectWizardIterator_WebSources")); } 55 56 public Component getComponent() { 57 if (component == null) { 58 component = new WebLocationsPanel(wizardDescriptor); 59 } 60 return component; 61 } 62 63 public HelpCtx getHelp() { 64 return new HelpCtx( WebLocationsWizardPanel.class ); 65 } 66 67 public boolean isValid() { 68 getComponent(); 69 return true; 70 } 71 72 private final Set listeners = new HashSet (1); 73 public final void addChangeListener(ChangeListener l) { 74 synchronized (listeners) { 75 listeners.add(l); 76 } 77 } 78 public final void removeChangeListener(ChangeListener l) { 79 synchronized (listeners) { 80 listeners.remove(l); 81 } 82 } 83 protected final void fireChangeEvent() { 84 Iterator it; 85 synchronized (listeners) { 86 it = new HashSet (listeners).iterator(); 87 } 88 ChangeEvent ev = new ChangeEvent (this); 89 while (it.hasNext()) { 90 ((ChangeListener )it.next()).stateChanged(ev); 91 } 92 } 93 94 public void readSettings(Object settings) { 95 wizardDescriptor = (WizardDescriptor) settings; 96 wizardDescriptor.putProperty("NewProjectWizard_Title", component.getClientProperty("NewProjectWizard_Title")); 98 File baseFolder = (File )wizardDescriptor.getProperty(NewFreeformProjectSupport.PROP_PROJECT_LOCATION); 100 File nbProjectFolder = (File )wizardDescriptor.getProperty(NewFreeformProjectSupport.PROP_PROJECT_FOLDER); 101 final String webPages; 102 final String srcPackages; 103 if(baseFolder.equals(this.baseFolder)) { 104 webPages = component.getWebPagesLocation().getAbsolutePath(); 105 srcPackages = component.getSrcPackagesLocation().getAbsolutePath(); 106 } else { 107 this.baseFolder = baseFolder; 108 FileObject fo = FileUtil.toFileObject(baseFolder); 109 if (fo != null) { 110 webPages = guessDocBase(fo); 111 srcPackages = guessJavaRoot(fo); 112 } else { 113 webPages = ""; srcPackages = ""; } 116 } 117 component.setFolders(baseFolder, nbProjectFolder); 118 component.setWebPages(webPages); 119 component.setSrcPackages(srcPackages); 120 } 121 122 public void storeSettings(Object settings) { 123 wizardDescriptor = (WizardDescriptor) settings; 124 wizardDescriptor.putProperty(NewWebFreeformProjectWizardIterator.PROP_WEB_WEBMODULES, component.getWebModules()); 125 126 List l = component.getJavaSrcFolder(); 127 wizardDescriptor.putProperty(NewJavaFreeformProjectSupport.PROP_EXTRA_JAVA_SOURCE_FOLDERS, l); 128 129 wizardDescriptor.putProperty(NewWebFreeformProjectWizardIterator.PROP_WEB_SOURCE_FOLDERS, component.getWebSrcFolder()); 130 wizardDescriptor.putProperty("NewProjectWizard_Title", null); } 132 133 private String guessDocBase (FileObject dir) { 134 Enumeration ch = dir.getChildren (true); 135 while (ch.hasMoreElements ()) { 136 FileObject f = (FileObject) ch.nextElement (); 137 if (f.isFolder () && f.getName ().equals ("WEB-INF")) { final FileObject webXmlFleObject = f.getFileObject ("web.xml"); if (webXmlFleObject!= null && webXmlFleObject.isData ()) { 140 return FileUtil.toFile(f.getParent()).getAbsolutePath(); 141 } 142 } 143 } 144 return ""; } 146 147 private String guessJavaRoot (FileObject dir) { 148 Enumeration ch = dir.getChildren (true); 149 try { 150 while (ch.hasMoreElements ()) { 151 FileObject f = (FileObject) ch.nextElement (); 152 if (f.getExt ().equals ("java")) { String pckg = guessPackageName (f); 154 String pkgPath = f.getParent ().getPath (); 155 if (pckg != null && pkgPath.endsWith (pckg.replace ('.', '/'))) { 156 String rootName = pkgPath.substring (0, pkgPath.length () - pckg.length ()); 157 return FileUtil.toFile(f.getFileSystem().findResource(rootName)).getAbsolutePath(); 158 } 159 } 160 } 161 } catch (FileStateInvalidException fsie) { 162 ErrorManager.getDefault ().notify (ErrorManager.INFORMATIONAL, fsie); 163 } 164 return ""; } 166 167 private String guessPackageName(FileObject f) { 168 java.io.Reader r = null; 169 try { 170 r = new BufferedReader (new InputStreamReader (f.getInputStream (), "utf-8")); boolean noPackage = false; 172 for (;;) { 173 String line = ((BufferedReader ) r).readLine(); 174 if (line == null) { 175 if (noPackage) 176 return ""; 177 else 178 break; 179 } 180 line = line.trim(); 181 if (line.trim().startsWith("package")) { int idx = line.indexOf(";"); if (idx >= 0) 185 return line.substring("package".length(), idx).trim(); } 187 if (line.indexOf("class") != -1) 189 noPackage = true; 190 } 191 } catch (java.io.IOException ioe) { 192 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 193 } finally { 194 try { 195 if (r != null) 196 r.close (); 197 } catch (java.io.IOException ioe) { 198 } 200 } 201 202 return null; 203 } 204 } 205 | Popular Tags |