KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > tools > generator > SAXGeneratorAbstractPanel


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.tools.generator;
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 customizer 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  *
36  * @author Petr Kuzel
37  * @version
38  */

39 public abstract class SAXGeneratorAbstractPanel extends JPanel implements Customizer {
40
41     /** Serial Version UID */
42     private static final long serialVersionUID =5089896677680825691L;
43     
44     // associated wizard step or null
45
private WizardStep step;
46
47     /**
48      * After a setObject() call contains current model driving wizard.
49      */

50     protected SAXGeneratorModel model;
51     
52     /** Creates new SAXGeneratorAbstractPanel */
53     public SAXGeneratorAbstractPanel() {
54     }
55
56     public static final class WizardStep implements WizardDescriptor.Panel {
57
58         private SAXGeneratorAbstractPanel peer;
59         private Class JavaDoc peerClass;
60         private Object JavaDoc bean;
61         private Integer JavaDoc index;
62         
63         private Vector listeners = new Vector();
64         private final ChangeEvent EVENT = new ChangeEvent(this);
65         private boolean valid = true;
66         
67         /**
68          * Create wizard step that uses instance of passed class as its component.
69          */

70         public WizardStep(Class JavaDoc peerClass) {
71             if (SAXGeneratorAbstractPanel.class.isAssignableFrom(peerClass) == false) {
72                 throw new IllegalArgumentException JavaDoc("SAXGeneratorAbstractPanel required. Got " + peerClass);
73             }
74             this.peerClass = peerClass;
75         }
76         
77         public java.awt.Component JavaDoc getComponent() {
78             return getPeer();
79         }
80         
81         private SAXGeneratorAbstractPanel getPeer() {
82             if (peer == null) {
83                 try {
84                     // unfortunately constructor does not initialize this
85
// object properly, client need to call setIndex and setBean
86
if (bean == null) throw new IllegalStateException JavaDoc();
87                     if (index == null) throw new IllegalStateException JavaDoc();
88                     peer = (SAXGeneratorAbstractPanel) peerClass.newInstance();
89                     peer.step = this;
90                     peer.setObject(bean);
91                     peer.putClientProperty("WizardPanel_contentSelectedIndex", index); // NOI18N
92
} catch (InstantiationException JavaDoc ex) {
93                     throw new IllegalStateException JavaDoc();
94                 } catch (IllegalAccessException JavaDoc ex) {
95                     throw new IllegalStateException JavaDoc();
96                 }
97             }
98             return peer;
99         }
100
101         void setBean(Object JavaDoc bean) {
102             this.bean = bean;
103         }
104         
105         void setIndex(int index) {
106             this.index = new Integer JavaDoc(index);
107         }
108         
109         public void readSettings(java.lang.Object JavaDoc p1) {
110             getPeer().updateView();
111         }
112         
113         /**
114          * Cunstruct help ctx from WizardPanel_helpURL property.
115          */

116         public HelpCtx getHelp() {
117             //return new HelpCtx(getPeer().getClass());
118
return null;
119         }
120
121         public void addChangeListener(javax.swing.event.ChangeListener JavaDoc l) {
122             listeners.add(l);
123         }
124
125         public void storeSettings(java.lang.Object JavaDoc p1) {
126             getPeer().updateModel();
127         }
128
129         public boolean isValid() {
130             return valid;
131         }
132
133         void setValid(boolean valid) {
134
135             if (this.valid == valid) return;
136
137             this.valid = valid;
138
139             synchronized (listeners) {
140                 Iterator it = listeners.iterator();
141                 while (it.hasNext()) {
142                     ChangeListener next = (ChangeListener) it.next();
143                     next.stateChanged(EVENT);
144                 }
145             }
146         }
147
148         public void removeChangeListener(javax.swing.event.ChangeListener JavaDoc l) {
149             listeners.remove(l);
150         }
151     }
152         
153
154     /**
155      * Update validity of associted wizard step or void.
156      */

157     protected final void setValid(boolean valid) {
158         if (step != null) step.setValid(valid);
159     }
160
161     /**
162      * User just leaved the panel, update model
163      */

164     protected abstract void updateModel();
165     
166     /**
167      * User just entered the panel, init view by model values
168      */

169     protected abstract void initView();
170     
171     /**
172      * User just reentered the panel.
173      */

174     protected abstract void updateView();
175     
176     
177     public void setObject(java.lang.Object JavaDoc peer) {
178         if ( not(peer instanceof SAXGeneratorModel) ) {
179             throw new IllegalArgumentException JavaDoc("SAXGeneratorModel class expected."); // NOI18N
180
}
181         
182         model = (SAXGeneratorModel) peer;
183         initView();
184     }
185         
186     public void addPropertyChangeListener(java.beans.PropertyChangeListener JavaDoc p1) {
187     }
188     
189     public void removePropertyChangeListener(java.beans.PropertyChangeListener JavaDoc p1) {
190     }
191
192     protected final boolean not (boolean expr) {
193         return ! expr;
194     }
195     
196 }
197
Popular Tags