KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > core > wizard > AbstractPanel


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 package org.netbeans.modules.xml.core.wizard;
20
21 import java.util.*;
22 import java.beans.*;
23
24 import javax.swing.*;
25 import javax.swing.event.*;
26
27 import org.openide.*;
28 import org.openide.util.HelpCtx;
29 import java.net.URL JavaDoc;
30
31 /**
32  * Base class of wizardable panels. <code>updateModel</code>
33  * and <code>initView</code> methods need to be implemented. They are called as user goes
34  * over wizard steps and it must (re)store current state.
35  * <p>
36  * For proper functionality it must be wrapped by {@link WizardStep}.
37  *
38  * @author Petr Kuzel
39  * @version
40  */

41 public abstract class AbstractPanel extends JPanel implements Customizer {
42
43     /** Serial Version UID */
44     private static final long serialVersionUID =508989667995691L;
45     
46     /**
47      * After a setObject() call contains current model driving wizard.
48      */

49     protected DocumentModel model;
50
51     // associated wizard step wrapper (initialized by step.
52
private WizardStep step;
53             
54     /**
55      * User just leaved the panel, update model
56      */

57     protected abstract void updateModel();
58     
59     /**
60      * User just entered the panel, init view by model values
61      */

62     protected abstract void initView();
63     
64     /**
65      * User just reentered the panel.
66      */

67     protected abstract void updateView();
68     
69     
70     
71     // customizer impl ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72

73     public void setObject(Object JavaDoc model) {
74         if ( not(model instanceof DocumentModel) ) {
75             throw new IllegalArgumentException JavaDoc("DocumentModel class expected."); // NOI18N
76
}
77         
78         this.model = (DocumentModel) model;
79         initView();
80     }
81         
82     public void addPropertyChangeListener(PropertyChangeListener p1) {
83     }
84     
85     public void removePropertyChangeListener(PropertyChangeListener p1) {
86     }
87
88     protected static boolean not(boolean expr) {
89         return ! expr;
90     }
91
92     /**
93      * Gives access to WizardStep wrapper.
94      * It is supported only for AbstractPanel with associated WizardStep.
95      * @return step or <code>IllegalStateException</code> exception.
96      */

97     protected final WizardStep getStep() {
98         if (step == null) throw new IllegalStateException JavaDoc("new WizardStep(this) have not been called!");
99         return step;
100     }
101     
102     /**
103      * WizardDescriptor.Panel adapter for AbstractPanel.
104      * It solved isValid() clash between Component and WizardDescriptor.Panel.
105      */

106     public static class WizardStep implements WizardDescriptor.Panel {
107         
108         private final AbstractPanel peer;
109         private Vector listeners = new Vector();
110         private final ChangeEvent EVENT = new ChangeEvent(this);
111         private boolean valid = true;
112         
113         public WizardStep(AbstractPanel peer) {
114             if (peer == null) throw new NullPointerException JavaDoc();
115             this.peer = peer;
116             peer.step = this;
117         }
118     
119         public java.awt.Component JavaDoc getComponent() {
120             return peer;
121         }
122
123         public void readSettings(Object JavaDoc settings) {
124             peer.updateView();
125         }
126
127         /**
128          * Cunstruct help ctx from WizardPanel_helpURL property.
129          */

130         public final HelpCtx getHelp() {
131     // URL url = (URL) getClientProperty("WizardPanel_helpURL");
132
// if (url != null) {
133
// return new HelpCtx(peer.getClass()); // warning getClass(0 returns a subclass
134
// }
135
return HelpCtx.DEFAULT_HELP;
136         }
137     
138         public void addChangeListener(javax.swing.event.ChangeListener JavaDoc l) {
139             listeners.add(l);
140         }
141
142         public void storeSettings(Object JavaDoc settings) {
143             peer.updateModel();
144         }
145
146         public boolean isValid() {
147             return valid;
148         }
149
150         protected final void setValid(boolean valid) {
151
152             if (this.valid == valid) return;
153
154             this.valid = valid;
155
156             synchronized (listeners) {
157                 Iterator it = listeners.iterator();
158                 while (it.hasNext()) {
159                     ChangeListener next = (ChangeListener) it.next();
160                     next.stateChanged(EVENT);
161                 }
162             }
163         }
164
165         public void removeChangeListener(ChangeListener l) {
166             listeners.remove(l);
167         }
168     }
169     
170 }
171
Popular Tags