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.versioning.system.cvss.ui.wizards;
043:
044: import org.openide.WizardDescriptor;
045: import org.openide.WizardValidationException;
046: import org.openide.ErrorManager;
047: import org.openide.util.HelpCtx;
048:
049: import javax.swing.*;
050: import javax.swing.event.ChangeEvent;
051: import javax.swing.event.ChangeListener;
052: import java.util.List;
053: import java.util.LinkedList;
054: import java.util.ArrayList;
055: import java.util.Iterator;
056: import java.awt.*;
057:
058: /**
059: * Abstract wizard panel with <codE>valid</code>
060: * and <codE>errorMessage</code> bound properties.
061: *
062: * <p>Components use 3:2 (60x25 chars) size mode
063: * to avoid wizard resizing after [next>].
064: *
065: * @author Petr Kuzel
066: */
067: public abstract class AbstractStep implements
068: WizardDescriptor.ValidatingPanel {
069:
070: private List listeners = new LinkedList();
071: private boolean valid;
072: private JComponent panel;
073: private volatile boolean underConstruction;
074: private String errorMessage;
075: private boolean applyStandaloneLayout;
076:
077: /**
078: * If called before getComponent it disables 3:2 size mode.
079: */
080: public void applyStandaloneLayout() {
081: applyStandaloneLayout = true;
082: }
083:
084: /**
085: * Calls to createComponent. Noramalizes size nad assigns
086: * helpId based on subclass name.
087: */
088: public final synchronized Component getComponent() {
089: if (panel == null) {
090: try {
091: underConstruction = true;
092: panel = createComponent();
093: HelpCtx.setHelpIDString(panel, getClass().getName());
094: if (applyStandaloneLayout == false) {
095: JTextArea template = new JTextArea();
096: template.setColumns(60);
097: template.setRows(25);
098: panel.setPreferredSize(template.getPreferredSize());
099: }
100: } catch (RuntimeException ex) {
101: ErrorManager.getDefault().notify(ex);
102: } finally {
103: assert panel != null;
104: underConstruction = false;
105: fireChange();
106: }
107: }
108: return panel;
109: }
110:
111: /**
112: * @return must not return null
113: */
114: protected abstract JComponent createComponent();
115:
116: public HelpCtx getHelp() {
117: return null;
118: }
119:
120: public void readSettings(Object settings) {
121: }
122:
123: public void storeSettings(Object settings) {
124: }
125:
126: protected final void valid() {
127: setValid(true, null);
128: }
129:
130: /**
131: * Valid with error message that can be corrected
132: * by external change.
133: */
134: protected final void valid(String extErrorMessage) {
135: setValid(true, extErrorMessage);
136: }
137:
138: protected final void invalid(String message) {
139: setValid(false, message);
140: }
141:
142: public final boolean isValid() {
143: return valid;
144: }
145:
146: public final String getErrorMessage() {
147: return errorMessage;
148: }
149:
150: // comes on next or finish
151: public final void validate() throws WizardValidationException {
152: validateBeforeNext();
153: if (isValid() == false || errorMessage != null) {
154: throw new WizardValidationException(panel, errorMessage,
155: errorMessage);
156: }
157: }
158:
159: /**
160: * Perform heavy validation reporting results
161: * using {@link #valid} and {@link #invalid}.
162: */
163: protected abstract void validateBeforeNext();
164:
165: public final void addChangeListener(ChangeListener l) {
166: synchronized (listeners) {
167: listeners.add(l);
168: }
169: }
170:
171: public final void removeChangeListener(ChangeListener l) {
172: synchronized (listeners) {
173: listeners.remove(l);
174: }
175: }
176:
177: private void setValid(boolean valid, String errorMessage) {
178: boolean fire = AbstractStep.this .valid != valid;
179: fire |= errorMessage != null
180: && (errorMessage.equals(this .errorMessage) == false);
181: AbstractStep.this .valid = valid;
182: this .errorMessage = errorMessage;
183: if (fire) {
184: fireChange();
185: }
186: }
187:
188: private void fireChange() {
189: if (underConstruction)
190: return;
191: List clone;
192: synchronized (listeners) {
193: clone = new ArrayList(listeners);
194: }
195: Iterator it = clone.iterator();
196: ChangeEvent event = new ChangeEvent(this );
197: while (it.hasNext()) {
198: ChangeListener listener = (ChangeListener) it.next();
199: listener.stateChanged(event);
200: }
201: }
202:
203: }
|