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: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2008 Sun Microsystems, Inc.
038: */
039:
040: package org.netbeans.modules.j2ee.common.project.ui;
041:
042: import javax.swing.event.ChangeEvent;
043: import java.awt.Component;
044: import java.io.File;
045: import javax.swing.SwingUtilities;
046: import javax.swing.event.ChangeListener;
047: import org.netbeans.api.queries.CollocationQuery;
048: import org.netbeans.spi.project.support.ant.PropertyUtils;
049: import org.openide.WizardDescriptor;
050: import org.openide.filesystems.FileUtil;
051: import org.openide.util.ChangeSupport;
052: import org.openide.util.HelpCtx;
053: import org.openide.util.NbBundle;
054:
055: /**
056: *
057: * @author Petr Hejl
058: * @since 1.21
059: */
060: public class PanelSharability implements WizardDescriptor.Panel,
061: WizardDescriptor.FinishablePanel {
062:
063: public static final String WIZARD_SHARED_LIBRARIES = "sharedLibraries"; // NOI18N
064:
065: public static final String WIZARD_SERVER_LIBRARY = "serverLibrary"; // NOI18N
066:
067: private static final String PROP_ERROR_MESSAGE = "WizardPanel_errorMessage"; // NOI18N
068:
069: private final ChangeSupport changeSupport = new ChangeSupport(this );
070:
071: private final String projectLocationProperty;
072:
073: private final String serverIdProperty;
074:
075: private final boolean finish;
076:
077: private PanelSharabilityVisual panel;
078:
079: private WizardDescriptor descriptor;
080:
081: private File projectLocation;
082:
083: public PanelSharability(String projectLocationProperty,
084: String serverIdProperty, boolean finish) {
085: this .projectLocationProperty = projectLocationProperty;
086: this .serverIdProperty = serverIdProperty;
087: this .finish = finish;
088: }
089:
090: public boolean isFinishPanel() {
091: return finish;
092: }
093:
094: public synchronized Component getComponent() {
095: if (panel == null) {
096: assert SwingUtilities.isEventDispatchThread() : "Not called in EDT api bug 122184"; //NOI18N
097: panel = new PanelSharabilityVisual(this );
098:
099: panel.addChangeListener(new PanelListener());
100: }
101: return panel;
102: }
103:
104: public void addChangeListener(ChangeListener l) {
105: changeSupport.addChangeListener(l);
106: }
107:
108: public void removeChangeListener(ChangeListener l) {
109: changeSupport.removeChangeListener(l);
110: }
111:
112: public HelpCtx getHelp() {
113: return new HelpCtx(PanelSharability.class);
114: }
115:
116: public boolean isValid() {
117: descriptor.putProperty(PROP_ERROR_MESSAGE, "");
118:
119: if (panel != null && panel.getLibraryRadioButton().isSelected()
120: && panel.getLibraryRadioButton().isEnabled()) {
121:
122: Object name = panel.getLibraryNameComboBox().getEditor()
123: .getItem();
124: if (!PropertyUtils.isUsablePropertyName((name == null) ? ""
125: : name.toString())) { // NOI18N
126: descriptor
127: .putProperty(
128: PROP_ERROR_MESSAGE,
129: decorateMessage(NbBundle
130: .getMessage(
131: PanelSharability.class,
132: "PanelSharability.libraryNameError.text")));
133: return false;
134: }
135:
136: if (panel.getSharableProject().isSelected()) {
137: String location = panel.getSharedLibarariesLocation();
138: if (new File(location).isAbsolute()) {
139: descriptor
140: .putProperty(
141: PROP_ERROR_MESSAGE,
142: decorateMessage(NbBundle
143: .getMessage(
144: PanelSharability.class,
145: "PanelSharability.absolutePathWarning.text")));
146:
147: } else {
148: if (projectLocation != null) {
149: File projectLoc = FileUtil
150: .normalizeFile(projectLocation);
151: File libLoc = PropertyUtils.resolveFile(
152: projectLoc, location);
153: if (!CollocationQuery.areCollocated(projectLoc,
154: libLoc)) {
155: descriptor
156: .putProperty(
157: PROP_ERROR_MESSAGE,
158: decorateMessage(NbBundle
159: .getMessage(
160: PanelSharability.class,
161: "PanelSharability.relativePathWarning.text")));
162: }
163: }
164: }
165: }
166: descriptor.putProperty(PROP_ERROR_MESSAGE,
167: decorateMessage(NbBundle.getMessage(
168: PanelSharability.class,
169: "PanelSharability.licenseWarning.text")));
170: }
171:
172: return true;
173: }
174:
175: public void readSettings(Object settings) {
176: descriptor = (WizardDescriptor) settings;
177:
178: projectLocation = (File) descriptor
179: .getProperty(projectLocationProperty);
180: panel.setProjectLocation(projectLocation);
181: panel.setServerInstance((String) descriptor
182: .getProperty(serverIdProperty));
183: }
184:
185: public void storeSettings(Object settings) {
186: WizardDescriptor d = (WizardDescriptor) settings;
187:
188: d.putProperty(WIZARD_SHARED_LIBRARIES, panel
189: .getSharedLibarariesLocation());
190: d.putProperty(WIZARD_SERVER_LIBRARY, panel
191: .getServerLibraryName());
192: }
193:
194: private String decorateMessage(String message) {
195: if (message != null) {
196: return "<html>"
197: + message.replaceAll("<", "<").replaceAll(">",
198: ">") + "</html>"; // NIO18N
199: }
200: return null;
201: }
202:
203: private class PanelListener implements ChangeListener {
204:
205: public void stateChanged(ChangeEvent e) {
206: changeSupport.fireChange();
207: }
208: }
209: }
|