KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mc4j > console > connection > wizard > ConnectionIterator


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.wizard;
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 ConnectionIterator implements WizardDescriptor.Iterator {
37
38     private ConnectionDescriptor connectionDescriptor;
39
40     // You should define what panels you want to use here:
41
public ConnectionIterator() {
42     }
43
44     public void setDescriptor(ConnectionDescriptor connectionDescriptor) {
45         this.connectionDescriptor = connectionDescriptor;
46     }
47
48     protected WizardDescriptor.Panel[] createPanels() {
49         return new WizardDescriptor.Panel[] {
50             new ConnectionVisualPanel(this.connectionDescriptor),
51             new ServerInstallPanel(this.connectionDescriptor),
52             new ClassPathListPanel(this.connectionDescriptor)
53         };
54     }
55
56     // And the list of step names:
57

58     protected String JavaDoc[] createSteps() {
59         return new String JavaDoc[] {
60             "Connect to Server",
61             "Select Server Installation",
62             "Customize classpath"
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(ConnectionIterator.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             if (this.connectionDescriptor.getSettings().getConnectionType() == null)
109                 return false;
110             return (this.connectionDescriptor.getSettings().getConnectionType().getConnectionClasspathEntries() != null);
111         case 2:
112             return true;
113         default:
114             throw new NoSuchElementException JavaDoc ();
115         }
116     }
117     public boolean hasNext () {
118         for (int i = index + 1; i < panels.length; i++) {
119             if (showing (i)) {
120                 return true;
121             }
122         }
123         return false;
124     }
125     public boolean hasPrevious () {
126         return index > 0;
127     }
128     public void nextPanel () {
129         index++;
130         while (! showing (index)) index++;
131         if (index == 1) {
132             // User finished intro panel, list of panels may have changed:
133
fireChangeEvent ();
134         }
135     }
136     public void previousPanel () {
137         index--;
138         while (! showing (index)) index--;
139     }
140
141     public WizardDescriptor.Panel current() {
142         return getPanels()[index];
143     }
144
145     // If nothing unusual changes in the middle of the wizard, simply:
146
//public final void addChangeListener(ChangeListener l) {}
147
//public final void removeChangeListener(ChangeListener l) {}
148
// If something changes dynamically (besides moving between panels),
149
// e.g. the number of panels changes in response to user input, then
150
// uncomment the following and call when needed:
151
// fireChangeEvent();
152

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