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-2006 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.wizards;
043:
044: import java.awt.Component;
045:
046: import javax.swing.event.ChangeListener;
047: import org.netbeans.api.java.project.JavaProjectConstants;
048: import org.netbeans.api.project.Project;
049: import org.netbeans.api.project.ProjectUtils;
050:
051: import org.openide.WizardDescriptor;
052: import org.openide.loaders.TemplateWizard;
053: import org.openide.util.HelpCtx;
054: import org.netbeans.spi.project.ui.templates.support.Templates;
055: import org.netbeans.modules.web.api.webmodule.WebModule;
056: import org.netbeans.api.project.SourceGroup;
057: import org.netbeans.api.project.Sources;
058: import org.openide.filesystems.FileObject;
059:
060: /* Wizard panel that collects data for the Servlet and Filter
061: * wizards.
062: *
063: * @author Ana von Klopp, Milan Kuchtiak
064: */
065: public class ServletPanel implements WizardDescriptor.FinishPanel {
066:
067: /** The visual component that displays this panel.
068: * If you need to access the component from this class,
069: * just use getComponent().
070: */
071: private transient BaseWizardPanel wizardPanel;
072: private transient TemplateWizard wizard;
073: /** listener to changes in the wizard */
074: private ChangeListener listener;
075: private DeployData deployData;
076: private transient TargetEvaluator evaluator;
077:
078: /** Create the wizard panel descriptor. */
079: private ServletPanel(TargetEvaluator evaluator,
080: TemplateWizard wizard, boolean first) {
081: this .evaluator = evaluator;
082: this .wizard = wizard;
083: this .deployData = evaluator.getDeployData();
084: if (first)
085: this .wizardPanel = new DeployDataPanel(evaluator);
086: else
087: this .wizardPanel = new DeployDataExtraPanel(evaluator);
088: }
089:
090: /** Create the wizard panel descriptor. */
091: public static ServletPanel createServletPanel(
092: TargetEvaluator evaluator, TemplateWizard wizard) {
093: return new ServletPanel(evaluator, wizard, true);
094: }
095:
096: /** Create the wizard panel descriptor. */
097: public static ServletPanel createFilterPanel(
098: TargetEvaluator evaluator, TemplateWizard wizard) {
099: return new ServletPanel(evaluator, wizard, false);
100: }
101:
102: public Component getComponent() {
103: return wizardPanel;
104: }
105:
106: public boolean isValid() {
107: if (deployData.isValid()) {
108: wizard.putProperty("WizardPanel_errorMessage", ""); //NOI18N
109: return true;
110: }
111: wizard.putProperty("WizardPanel_errorMessage", //NOI18N
112: deployData.getErrorMessage());
113: return false;
114: }
115:
116: public HelpCtx getHelp() {
117: // #114487
118: if (evaluator.getFileType() == FileType.SERVLET) {
119: return wizardPanel.getHelp();
120: }
121: return null;
122: }
123:
124: /** Add a listener to changes of the panel's validity.
125: * @param l the listener to add
126: * @see #isValid
127: */
128: public void addChangeListener(ChangeListener l) {
129: if (listener != null)
130: throw new IllegalStateException();
131: if (wizardPanel != null)
132: wizardPanel.addChangeListener(l);
133: listener = l;
134: }
135:
136: /** Remove a listener to changes of the panel's validity.
137: * @param l the listener to remove
138: */
139: public void removeChangeListener(ChangeListener l) {
140: listener = null;
141: if (wizardPanel != null)
142: wizardPanel.removeChangeListener(l);
143: }
144:
145: public void readSettings(Object settings) {
146: if (settings instanceof TemplateWizard) {
147: TemplateWizard w = (TemplateWizard) settings;
148: //Project project = Templates.getProject(w);
149: String targetName = w.getTargetName();
150: org.openide.filesystems.FileObject targetFolder = Templates
151: .getTargetFolder(w);
152: Project project = Templates.getProject(w);
153: Sources sources = ProjectUtils.getSources(project);
154: SourceGroup[] groups = sources
155: .getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
156: String packageName = null;
157: for (int i = 0; i < groups.length && packageName == null; i++) {
158: if (WebModule.getWebModule(groups[i].getRootFolder()) != null) {
159: packageName = org.openide.filesystems.FileUtil
160: .getRelativePath(groups[i].getRootFolder(),
161: targetFolder);
162: }
163: }
164: if (packageName != null)
165: packageName = packageName.replace('/', '.');
166: else
167: packageName = "";
168: if (targetName == null)
169: evaluator.setClassName(w.getTemplate().getName(),
170: packageName);
171: else
172: evaluator.setClassName(targetName, packageName);
173: }
174: wizardPanel.setData();
175: }
176:
177: public void storeSettings(Object settings) {
178: // do nothing
179: }
180: }
|