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: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028:
029: package org.netbeans.modules.web.webmodule;
030:
031: import java.awt.Component;
032: import java.beans.PropertyChangeEvent;
033: import java.beans.PropertyChangeListener;
034: import java.util.Map;
035: import java.util.Set;
036: import javax.swing.JComponent;
037: import javax.swing.JPanel;
038: import javax.swing.event.ChangeListener;
039: import org.netbeans.modules.web.api.webmodule.ExtenderController;
040: import org.netbeans.modules.web.api.webmodule.WebModule;
041: import org.netbeans.modules.web.spi.webmodule.FrameworkConfigurationPanel;
042: import org.netbeans.modules.web.spi.webmodule.WebFrameworkProvider;
043: import org.netbeans.modules.web.spi.webmodule.WebModuleExtender;
044: import org.openide.WizardDescriptor;
045: import org.openide.filesystems.FileObject;
046: import org.openide.util.HelpCtx;
047:
048: /**
049: * This class is a bridge between the deprecated {@link WebFrameworkProvider#getConfigurationPanel}
050: * and {@link WebFrameworkProvider#extend} methods and the new {@link WebModuleExtender} class
051: * which replaces them.
052: *
053: * @author Andrei Badea
054: */
055: @SuppressWarnings("deprecation")
056: public class WebModuleExtenderBridge extends WebModuleExtender {
057:
058: private final WebFrameworkProvider framework;
059: private final WebModule webModule;
060: private final ExtenderController controller;
061: private final FrameworkConfigurationPanel configPanel;
062: private final WizardDescriptor wizard;
063:
064: private Map<String, Object> oldProps;
065:
066: public static WebModuleExtenderBridge create(
067: WebFrameworkProvider framework, WebModule webModule,
068: ExtenderController controller) {
069: WebModuleExtenderBridge result = new WebModuleExtenderBridge(
070: framework, webModule, controller);
071: result.initialize();
072: return result;
073: }
074:
075: private WebModuleExtenderBridge(WebFrameworkProvider framework,
076: WebModule webModule, final ExtenderController controller) {
077: this .framework = framework;
078: this .webModule = webModule;
079: this .controller = controller;
080: FrameworkConfigurationPanel tmpPanel = framework
081: .getConfigurationPanel(webModule);
082: configPanel = (tmpPanel != null) ? tmpPanel
083: : new EmptyConfigPanel();
084: // we don't want to send configPanel to the wizard descriptor in order to
085: // keep full control over the methods called on configPanel,
086: // so we create a special panel for the wizard
087: @SuppressWarnings("unchecked")
088: // NOI18N
089: WizardDescriptor tmpWizard = new WizardDescriptor(
090: new WizardDescriptor.Panel[] { new EmptyPanel() });
091: wizard = tmpWizard;
092: }
093:
094: /**
095: * Not done in the constructor to avoid escaping "this" before the constructor
096: * has finished executing.
097: */
098: private void initialize() {
099: wizard.addPropertyChangeListener(new PropertyChangeListener() {
100: public void propertyChange(PropertyChangeEvent evt) {
101: String message = (String) wizard
102: .getProperty("WizardPanel_errorMessage");
103: if (message != null && message.trim().length() == 0) {
104: // many WizardDescriptor clients pass " " for no error message to ensure
105: // that it still takes up vertical space
106: message = null;
107: }
108: controller.setErrorMessage(message); // NOI18N
109: }
110: });
111: }
112:
113: public void addChangeListener(ChangeListener listener) {
114: configPanel.addChangeListener(listener);
115: }
116:
117: @SuppressWarnings("unchecked")
118: public Set<FileObject> extend(WebModule webModule) {
119: configPanel.storeSettings(wizard);
120: return (Set<FileObject>) framework.extend(webModule);
121: }
122:
123: public JComponent getComponent() {
124: return (JComponent) configPanel.getComponent();
125: }
126:
127: public HelpCtx getHelp() {
128: return configPanel.getHelp();
129: }
130:
131: public boolean isValid() {
132: return configPanel.isValid();
133: }
134:
135: public void removeChangeListener(ChangeListener listener) {
136: configPanel.removeChangeListener(listener);
137: }
138:
139: @SuppressWarnings("unchecked")
140: public void update() {
141: if (oldProps != null) {
142: for (Map.Entry<String, Object> entry : oldProps.entrySet()) {
143: wizard.putProperty(entry.getKey(), null);
144: }
145: }
146: Map<String, Object> newProps = controller.getProperties()
147: .getProperties();
148: for (Map.Entry<String, Object> entry : newProps.entrySet()) {
149: wizard.putProperty(entry.getKey(), entry.getValue());
150: }
151: oldProps = newProps;
152: configPanel.readSettings(wizard);
153: }
154:
155: private static class EmptyPanel implements WizardDescriptor.Panel {
156:
157: private JPanel component;
158:
159: public void addChangeListener(ChangeListener l) {
160: }
161:
162: public Component getComponent() {
163: if (component == null) {
164: component = new JPanel();
165: }
166: return component;
167: }
168:
169: public HelpCtx getHelp() {
170: return null;
171: }
172:
173: public boolean isValid() {
174: return true;
175: }
176:
177: public void readSettings(Object settings) {
178: }
179:
180: public void removeChangeListener(ChangeListener l) {
181: }
182:
183: public void storeSettings(Object settings) {
184: }
185: }
186:
187: /**
188: * An empty framework configuration panel to which WebModuleExtenderBridge will delegate
189: * when the framework doesn't have a framework configuration panel.
190: */
191: private final static class EmptyConfigPanel extends EmptyPanel
192: implements FrameworkConfigurationPanel {
193:
194: @Override
195: public Component getComponent() {
196: return null;
197: }
198:
199: public void enableComponents(boolean enable) {
200: }
201: }
202: }
|