KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mc4j > console > connection > create > CreateWizardIterator


1 /*
2  * Copyright 2002-2004 Greg Hinkle
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.mc4j.console.connection.create;
18
19 import org.openide.WizardDescriptor;
20 import org.openide.util.NbBundle;
21
22 import javax.swing.event.ChangeEvent JavaDoc;
23 import javax.swing.event.ChangeListener JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.ObjectInputStream JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.NoSuchElementException JavaDoc;
29 import java.util.Set JavaDoc;
30
31 /**
32  *
33  * @author Greg Hinkle (ghinkle@users.sourceforge.net), January 2002
34  * @version $Revision: 570 $($Author: ghinkl $ / $Date: 2006-04-12 15:14:16 -0400 (Wed, 12 Apr 2006) $)
35  */

36 public class CreateWizardIterator implements WizardDescriptor.Iterator {
37
38     private CreateWizardDescriptor descriptor;
39
40
41     // You should define what panels you want to use here:
42
public CreateWizardIterator() {
43     }
44
45     public void setDescriptor(CreateWizardDescriptor descriptor) {
46         this.descriptor = descriptor;
47
48     }
49
50     protected WizardDescriptor.Panel[] createPanels() {
51         return new WizardDescriptor.Panel[] {
52             new CreateVisualPanel(descriptor),
53             new CallCreateVisualPanel(descriptor)
54         };
55     }
56
57     // And the list of step names:
58

59     protected String JavaDoc[] createSteps() {
60         return new String JavaDoc[] {
61             "Enter MBean Parameters",
62             "Create MBean"
63         };
64     }
65
66     // --- The rest probably does not need to be touched. ---
67

68     // Keep track of the panels and selected panel:
69

70     private transient int index = 0;
71
72     protected final int getIndex() {
73         return index;
74     }
75     private transient WizardDescriptor.Panel[] panels = null;
76     protected final WizardDescriptor.Panel[] getPanels() {
77         if (panels == null) {
78             panels = createPanels();
79         }
80         return panels;
81     }
82
83     // Also the list of steps in the left pane:
84

85     private transient String JavaDoc[] steps = null;
86         protected final String JavaDoc[] getSteps() {
87         if (steps == null) {
88             steps = createSteps();
89         }
90         return steps;
91     }
92
93     // --- WizardDescriptor.Iterator METHODS: ---
94
// Note that this is very similar to WizardDescriptor.Iterator, but with a
95
// few more options for customization. If you e.g. want to make panels appear
96
// or disappear dynamically, go ahead.
97

98     public String JavaDoc name() {
99         return NbBundle.getMessage(CreateWizardIterator.class, "TITLE_x_of_y",
100         new Integer JavaDoc(index + 1), new Integer JavaDoc(getPanels().length));
101     }
102
103     private boolean showing (int index) throws NoSuchElementException JavaDoc {
104         switch (index) {
105         case 0:
106             return true;
107         case 1:
108             return true;
109         default:
110             throw new NoSuchElementException JavaDoc ();
111         }
112     }
113     public boolean hasNext () {
114         for (int i = index + 1; i < panels.length; i++) {
115             if (showing (i)) {
116                 return true;
117             }
118         }
119         return false;
120     }
121     public boolean hasPrevious () {
122         return index > 0;
123     }
124     public void nextPanel () {
125         index++;
126         while (! showing (index)) index++;
127         if (index == 1) {
128             // User finished intro panel, list of panels may have changed:
129
fireChangeEvent ();
130         }
131     }
132     public void previousPanel () {
133         index--;
134         while (! showing (index)) index--;
135     }
136
137     public WizardDescriptor.Panel current() {
138         return getPanels()[index];
139     }
140
141     // If nothing unusual changes in the middle of the wizard, simply:
142
//public final void addChangeListener(ChangeListener l) {}
143
//public final void removeChangeListener(ChangeListener l) {}
144
// If something changes dynamically (besides moving between panels),
145
// e.g. the number of panels changes in response to user input, then
146
// uncomment the following and call when needed:
147
// fireChangeEvent();
148

149     private transient Set JavaDoc listeners = new HashSet JavaDoc(1); // Set<ChangeListener>
150
public final void addChangeListener(ChangeListener JavaDoc l) {
151         synchronized (listeners) {
152             listeners.add(l);
153         }
154     }
155     public final void removeChangeListener(ChangeListener JavaDoc l) {
156         synchronized (listeners) {
157             listeners.remove(l);
158         }
159     }
160     protected final void fireChangeEvent() {
161         Iterator JavaDoc it;
162         synchronized (listeners) {
163             it = new HashSet JavaDoc(listeners).iterator();
164         }
165         ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(this);
166         while (it.hasNext()) {
167             ((ChangeListener JavaDoc)it.next()).stateChanged(ev);
168         }
169     }
170     private void readObject(ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
171         in.defaultReadObject();
172         listeners = new HashSet JavaDoc(1);
173     }
174
175
176 }
177
Popular Tags