001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.web.freeform.ui;
043:
044: import java.awt.Component;
045: import java.io.BufferedReader;
046: import java.io.File;
047: import java.io.InputStreamReader;
048: import java.util.Enumeration;
049: import java.util.HashSet;
050: import java.util.Iterator;
051: import java.util.List;
052: import java.util.Set;
053: import java.util.logging.Level;
054: import java.util.logging.Logger;
055: import javax.swing.event.ChangeEvent;
056: import javax.swing.event.ChangeListener;
057: import org.netbeans.modules.ant.freeform.spi.support.NewFreeformProjectSupport;
058: import org.netbeans.modules.java.freeform.spi.support.NewJavaFreeformProjectSupport;
059: import org.netbeans.modules.j2ee.common.FileSearchUtility;
060: import org.openide.WizardDescriptor;
061: import org.openide.filesystems.FileObject;
062: import org.openide.filesystems.FileStateInvalidException;
063: import org.openide.filesystems.FileUtil;
064: import org.openide.util.HelpCtx;
065: import org.openide.util.NbBundle;
066:
067: /**
068: * @author Radko Najman
069: */
070: public class WebLocationsWizardPanel implements WizardDescriptor.Panel {
071:
072: private WebLocationsPanel component;
073: private WizardDescriptor wizardDescriptor;
074: private File baseFolder;
075:
076: public WebLocationsWizardPanel() {
077: getComponent()
078: .setName(
079: NbBundle
080: .getMessage(
081: NewWebFreeformProjectWizardIterator.class,
082: "TXT_NewWebFreeformProjectWizardIterator_WebSources")); // NOI18N
083: }
084:
085: public Component getComponent() {
086: if (component == null) {
087: component = new WebLocationsPanel(wizardDescriptor);
088: }
089: return component;
090: }
091:
092: public HelpCtx getHelp() {
093: return new HelpCtx(WebLocationsWizardPanel.class);
094: }
095:
096: public boolean isValid() {
097: getComponent();
098: return true;
099: }
100:
101: private final Set<ChangeListener> listeners = new HashSet<ChangeListener>(
102: 1);
103:
104: public final void addChangeListener(ChangeListener l) {
105: synchronized (listeners) {
106: listeners.add(l);
107: }
108: }
109:
110: public final void removeChangeListener(ChangeListener l) {
111: synchronized (listeners) {
112: listeners.remove(l);
113: }
114: }
115:
116: protected final void fireChangeEvent() {
117: Set<ChangeListener> listenersCopy;
118: synchronized (listeners) {
119: listenersCopy = new HashSet<ChangeListener>(listeners);
120: }
121: ChangeEvent ev = new ChangeEvent(this );
122: for (ChangeListener l : listenersCopy) {
123: l.stateChanged(ev);
124: }
125: }
126:
127: public void readSettings(Object settings) {
128: wizardDescriptor = (WizardDescriptor) settings;
129: wizardDescriptor.putProperty("NewProjectWizard_Title",
130: component.getClientProperty("NewProjectWizard_Title")); // NOI18N
131:
132: //guess webapps well-known locations and preset them
133: File baseFolder = (File) wizardDescriptor
134: .getProperty(NewFreeformProjectSupport.PROP_PROJECT_LOCATION);
135: File nbProjectFolder = (File) wizardDescriptor
136: .getProperty(NewFreeformProjectSupport.PROP_PROJECT_FOLDER);
137: final String webPages;
138: final String webInf;
139: final String srcPackages;
140: if (baseFolder.equals(this .baseFolder)) {
141: webPages = component.getWebPagesLocation()
142: .getAbsolutePath();
143: webInf = component.getWebInfLocation().getAbsolutePath();
144: srcPackages = component.getSrcPackagesLocation()
145: .getAbsolutePath();
146: } else {
147: this .baseFolder = baseFolder;
148: FileObject fo = FileUtil.toFileObject(baseFolder);
149: if (fo != null) {
150: FileObject webPagesFO = FileSearchUtility
151: .guessDocBase(fo);
152: if (webPagesFO == null)
153: webPages = ""; //NOI18N
154: else
155: webPages = FileUtil.toFile(webPagesFO)
156: .getAbsolutePath();
157:
158: FileObject webInfFO = FileSearchUtility.guessWebInf(fo);
159: if (webInfFO == null)
160: webInf = ""; //NOI18N
161: else
162: webInf = FileUtil.toFile(webInfFO)
163: .getAbsolutePath();
164:
165: srcPackages = guessJavaRoot(fo);
166: } else {
167: webPages = ""; // NOI18N
168: webInf = ""; //NOI18N
169: srcPackages = ""; // NOI18N
170: }
171: }
172: component.setFolders(baseFolder, nbProjectFolder);
173: component.setWebPages(webPages);
174: component.setWebInf(webInf);
175: component.setSrcPackages(srcPackages);
176: }
177:
178: public void storeSettings(Object settings) {
179: wizardDescriptor = (WizardDescriptor) settings;
180: wizardDescriptor
181: .putProperty(
182: NewWebFreeformProjectWizardIterator.PROP_WEB_WEBMODULES,
183: component.getWebModules());
184:
185: List<String> l = component.getJavaSrcFolder();
186: wizardDescriptor
187: .putProperty(
188: NewJavaFreeformProjectSupport.PROP_EXTRA_JAVA_SOURCE_FOLDERS,
189: l);
190:
191: wizardDescriptor
192: .putProperty(
193: NewWebFreeformProjectWizardIterator.PROP_WEB_SOURCE_FOLDERS,
194: component.getWebSrcFolder());
195: wizardDescriptor
196: .putProperty(
197: NewWebFreeformProjectWizardIterator.PROP_WEB_INF_FOLDER,
198: component.getWebInfFolder());
199: wizardDescriptor.putProperty("NewProjectWizard_Title", null); // NOI18N
200: }
201:
202: private String guessJavaRoot(FileObject dir) {
203: Enumeration ch = dir.getChildren(true);
204: try {
205: while (ch.hasMoreElements()) {
206: FileObject f = (FileObject) ch.nextElement();
207: if (f.getExt().equals("java")) { // NOI18N
208: String pckg = guessPackageName(f);
209: String pkgPath = f.getParent().getPath();
210: if (pckg != null
211: && pkgPath.endsWith(pckg.replace('.', '/'))) {
212: String rootName = pkgPath.substring(0, pkgPath
213: .length()
214: - pckg.length());
215: return FileUtil.toFile(
216: f.getFileSystem()
217: .findResource(rootName))
218: .getAbsolutePath();
219: }
220: }
221: }
222: } catch (FileStateInvalidException fsie) {
223: Logger.getLogger("global").log(Level.INFO, null, fsie);
224: }
225: return ""; // NOI18N
226: }
227:
228: private String guessPackageName(FileObject f) {
229: java.io.Reader r = null;
230: try {
231: r = new BufferedReader(new InputStreamReader(f
232: .getInputStream(), "utf-8")); //NOI18N
233: boolean noPackage = false;
234: for (;;) {
235: String line = ((BufferedReader) r).readLine();
236: if (line == null) {
237: if (noPackage)
238: return "";
239: else
240: break;
241: }
242: line = line.trim();
243: //try to find package
244: if (line.trim().startsWith("package")) { // NOI18N
245: int idx = line.indexOf(";"); // NOI18N
246: if (idx >= 0)
247: return line.substring("package".length(), idx)
248: .trim(); // NOI18N
249: }
250: //an easy check if it is class
251: if (line.indexOf("class") != -1)
252: noPackage = true;
253: }
254: } catch (java.io.IOException ioe) {
255: Logger.getLogger("global").log(Level.INFO, null, ioe);
256: } finally {
257: try {
258: if (r != null)
259: r.close();
260: } catch (java.io.IOException ioe) {
261: // ignore this
262: }
263: }
264:
265: return null;
266: }
267: }
|