KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > ui > wizards > AbstractStep


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.subversion.ui.wizards;
21
22 import org.openide.WizardDescriptor;
23 import org.openide.WizardValidationException;
24 import org.openide.ErrorManager;
25 import org.openide.util.HelpCtx;
26
27 import javax.swing.*;
28 import javax.swing.event.ChangeEvent JavaDoc;
29 import javax.swing.event.ChangeListener JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.LinkedList JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.awt.*;
35
36 /**
37  * Abstract wizard panel with <codE>valid</code>
38  * and <codE>errorMessage</code> bound properties.
39  *
40  * <p>Components use 3:2 (60x25 chars) size mode
41  * to avoid wizard resizing after [next>].
42  *
43  * @author Petr Kuzel
44  */

45 public abstract class AbstractStep implements WizardDescriptor.ValidatingPanel {
46
47     private List JavaDoc<ChangeListener JavaDoc> listeners = new LinkedList JavaDoc<ChangeListener JavaDoc>();
48     private boolean valid;
49     private JComponent panel;
50     private volatile boolean underConstruction;
51     private String JavaDoc errorMessage;
52     private boolean applyStandaloneLayout;
53
54     /**
55      * If called before getComponent it disables 3:2 size mode.
56      */

57     public void applyStandaloneLayout() {
58         applyStandaloneLayout = true;
59     }
60
61     /**
62      * Calls to createComponent. Noramalizes size nad assigns
63      * helpId based on subclass name.
64      */

65     public final synchronized Component getComponent() {
66         if (panel == null) {
67             try {
68                 underConstruction = true;
69                 panel = createComponent();
70                 HelpCtx.setHelpIDString(panel, getClass().getName());
71                 if (applyStandaloneLayout == false) {
72                     JTextArea template = new JTextArea();
73                     template.setColumns(60);
74                     template.setRows(25);
75                     panel.setPreferredSize(template.getPreferredSize());
76                 }
77             } catch (RuntimeException JavaDoc ex) {
78                 ErrorManager.getDefault().notify(ex);
79             } finally {
80                 assert panel != null;
81                 underConstruction = false;
82                 fireChange();
83             }
84         }
85         return panel;
86     }
87
88     /**
89      * @return must not return null
90      */

91     protected abstract JComponent createComponent();
92
93     public HelpCtx getHelp() {
94         return null;
95     }
96
97     public void readSettings(Object JavaDoc settings) {
98     }
99
100     public void storeSettings(Object JavaDoc settings) {
101     }
102
103     protected final void valid() {
104         setValid(true, null);
105     }
106
107     /**
108      * Valid with error message that can be corrected
109      * by external change.
110      */

111     protected final void valid(String JavaDoc extErrorMessage) {
112         setValid(true, extErrorMessage);
113     }
114
115     protected final void invalid(String JavaDoc message) {
116         setValid(false, message);
117     }
118
119     public final boolean isValid() {
120         return valid;
121     }
122
123     public final String JavaDoc getErrorMessage() {
124         return errorMessage;
125     }
126
127     // comes on next or finish
128
public final void validate () throws WizardValidationException {
129         validateBeforeNext();
130         if (isValid() == false || errorMessage != null) {
131             throw new WizardValidationException (
132                 panel,
133                 errorMessage,
134                 errorMessage
135             );
136         }
137     }
138
139     /**
140      * Perform heavy validation reporting results
141      * using {@link #valid} and {@link #invalid}.
142      */

143     protected abstract void validateBeforeNext();
144
145     public final void addChangeListener(ChangeListener JavaDoc l) {
146         synchronized(listeners) {
147             listeners.add(l);
148         }
149     }
150
151     public final void removeChangeListener(ChangeListener JavaDoc l) {
152         synchronized(listeners) {
153             listeners.remove(l);
154         }
155     }
156
157     private void setValid(boolean valid, String JavaDoc errorMessage) {
158         boolean fire = AbstractStep.this.valid != valid;
159         fire |= errorMessage != null && (errorMessage.equals(this.errorMessage) == false);
160         AbstractStep.this.valid = valid;
161         this.errorMessage = errorMessage;
162         if (fire) {
163             fireChange();
164         }
165     }
166
167     private void fireChange() {
168         if (underConstruction) return;
169         List JavaDoc<ChangeListener JavaDoc> clone;
170         synchronized(listeners) {
171             clone = new ArrayList JavaDoc<ChangeListener JavaDoc>(listeners);
172         }
173         Iterator JavaDoc<ChangeListener JavaDoc> it = clone.iterator();
174         ChangeEvent JavaDoc event = new ChangeEvent JavaDoc(this);
175         while (it.hasNext()) {
176             ChangeListener JavaDoc listener = it.next();
177             listener.stateChanged(event);
178         }
179     }
180
181 }
182
Popular Tags