KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > freeform > ui > WebLocationsWizardPanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.web.freeform.ui;
21
22 import java.awt.Component JavaDoc;
23 import java.io.BufferedReader JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Set JavaDoc;
31 import javax.swing.event.ChangeEvent JavaDoc;
32 import javax.swing.event.ChangeListener JavaDoc;
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  * @author Radko Najman
45  */

46 public class WebLocationsWizardPanel implements WizardDescriptor.Panel {
47
48     private WebLocationsPanel component;
49     private WizardDescriptor wizardDescriptor;
50     private File JavaDoc baseFolder;
51
52     public WebLocationsWizardPanel() {
53         getComponent().setName(NbBundle.getMessage(NewWebFreeformProjectWizardIterator.class, "TXT_NewWebFreeformProjectWizardIterator_WebSources")); // NOI18N
54
}
55
56     public Component JavaDoc 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 JavaDoc/*<ChangeListener>*/ listeners = new HashSet JavaDoc(1);
73     public final void addChangeListener(ChangeListener JavaDoc l) {
74         synchronized (listeners) {
75             listeners.add(l);
76         }
77     }
78     public final void removeChangeListener(ChangeListener JavaDoc l) {
79         synchronized (listeners) {
80             listeners.remove(l);
81         }
82     }
83     protected final void fireChangeEvent() {
84         Iterator JavaDoc it;
85         synchronized (listeners) {
86             it = new HashSet JavaDoc(listeners).iterator();
87         }
88         ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(this);
89         while (it.hasNext()) {
90             ((ChangeListener JavaDoc)it.next()).stateChanged(ev);
91         }
92     }
93
94     public void readSettings(Object JavaDoc settings) {
95         wizardDescriptor = (WizardDescriptor) settings;
96         wizardDescriptor.putProperty("NewProjectWizard_Title", component.getClientProperty("NewProjectWizard_Title")); // NOI18N
97

98         //guess webapps well-known locations and preset them
99
File JavaDoc baseFolder = (File JavaDoc)wizardDescriptor.getProperty(NewFreeformProjectSupport.PROP_PROJECT_LOCATION);
100         File JavaDoc nbProjectFolder = (File JavaDoc)wizardDescriptor.getProperty(NewFreeformProjectSupport.PROP_PROJECT_FOLDER);
101         final String JavaDoc webPages;
102         final String JavaDoc 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 = ""; // NOI18N
114
srcPackages = ""; // NOI18N
115
}
116         }
117         component.setFolders(baseFolder, nbProjectFolder);
118         component.setWebPages(webPages);
119         component.setSrcPackages(srcPackages);
120     }
121
122     public void storeSettings(Object JavaDoc settings) {
123         wizardDescriptor = (WizardDescriptor) settings;
124         wizardDescriptor.putProperty(NewWebFreeformProjectWizardIterator.PROP_WEB_WEBMODULES, component.getWebModules());
125         
126         List JavaDoc 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); // NOI18N
131
}
132
133     private String JavaDoc guessDocBase (FileObject dir) {
134         Enumeration JavaDoc ch = dir.getChildren (true);
135         while (ch.hasMoreElements ()) {
136             FileObject f = (FileObject) ch.nextElement ();
137             if (f.isFolder () && f.getName ().equals ("WEB-INF")) { // NOI18N
138
final FileObject webXmlFleObject = f.getFileObject ("web.xml"); // NOI18N
139
if (webXmlFleObject!= null && webXmlFleObject.isData ()) {
140                     return FileUtil.toFile(f.getParent()).getAbsolutePath();
141                 }
142             }
143         }
144         return ""; // NOI18N
145
}
146
147     private String JavaDoc guessJavaRoot (FileObject dir) {
148         Enumeration JavaDoc ch = dir.getChildren (true);
149         try {
150             while (ch.hasMoreElements ()) {
151                 FileObject f = (FileObject) ch.nextElement ();
152                 if (f.getExt ().equals ("java")) { // NOI18N
153
String JavaDoc pckg = guessPackageName (f);
154                     String JavaDoc pkgPath = f.getParent ().getPath ();
155                     if (pckg != null && pkgPath.endsWith (pckg.replace ('.', '/'))) {
156                         String JavaDoc 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 ""; // NOI18N
165
}
166
167     private String JavaDoc guessPackageName(FileObject f) {
168         java.io.Reader JavaDoc r = null;
169         try {
170             r = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(f.getInputStream (), "utf-8")); //NOI18N
171
boolean noPackage = false;
172             for (;;) {
173                 String JavaDoc line = ((BufferedReader JavaDoc) r).readLine();
174                 if (line == null) {
175                     if (noPackage)
176                         return "";
177                     else
178                         break;
179                 }
180                 line = line.trim();
181                 //try to find package
182
if (line.trim().startsWith("package")) { // NOI18N
183
int idx = line.indexOf(";"); // NOI18N
184
if (idx >= 0)
185                         return line.substring("package".length(), idx).trim(); // NOI18N
186
}
187                 //an easy check if it is class
188
if (line.indexOf("class") != -1)
189                     noPackage = true;
190             }
191         } catch (java.io.IOException JavaDoc ioe) {
192             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
193         } finally {
194             try {
195                 if (r != null)
196                     r.close ();
197             } catch (java.io.IOException JavaDoc ioe) {
198                 // ignore this
199
}
200         }
201         
202         return null;
203     }
204 }
205
Popular Tags