svn-gvsig-desktop / trunk / libraries / libIverUtiles / src / com / iver / utiles / swing / wizard / Wizard.java @ 1836
History | View | Annotate | Download (7.84 KB)
1 |
package com.iver.utiles.swing.wizard; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.CardLayout; |
5 |
|
6 |
import java.util.ArrayList; |
7 |
import java.util.Iterator; |
8 |
|
9 |
import javax.swing.JButton; |
10 |
import javax.swing.JComponent; |
11 |
import javax.swing.JPanel; |
12 |
|
13 |
|
14 |
/**
|
15 |
* Clase wizard con la gesti?n de los botones y los paneles a?adidos al mismo.
|
16 |
* Al avanzar o retrasar un paso el asistente se desactivan todos los botones
|
17 |
* menos el de cancelar, que est? activo siempre por defecto
|
18 |
*
|
19 |
* @author Fernando Gonz?lez Cort?s
|
20 |
*/
|
21 |
public class Wizard extends JPanel implements WizardControl { |
22 |
private JPanel jPanel = null; |
23 |
private JButton btnBack = null; |
24 |
private JButton btnNext = null; |
25 |
private JButton btnFinish = null; |
26 |
private JButton btnCancel = null; |
27 |
private ArrayList steps = new ArrayList(); |
28 |
private int currentStep = 0; |
29 |
private JPanel pnlSteps = null; |
30 |
private ArrayList listeners = new ArrayList(); |
31 |
|
32 |
/**
|
33 |
* This is the default constructor
|
34 |
*/
|
35 |
public Wizard() {
|
36 |
super();
|
37 |
initialize(); |
38 |
} |
39 |
|
40 |
/**
|
41 |
* A?ade un l?stener de eventos del wizard
|
42 |
*
|
43 |
* @param listener
|
44 |
*/
|
45 |
public void addWizardListener(WizardListener listener) { |
46 |
listeners.add(listener); |
47 |
} |
48 |
|
49 |
/**
|
50 |
* Elimina un listener de eventos del wizard
|
51 |
*
|
52 |
* @param listener
|
53 |
*/
|
54 |
public void removeWizardListener(WizardListener listener) { |
55 |
listeners.remove(listener); |
56 |
} |
57 |
|
58 |
/**
|
59 |
* Invoca en los listeners el evento cancel
|
60 |
*/
|
61 |
private void callCancelListener() { |
62 |
for (Iterator iter = listeners.iterator(); iter.hasNext();) { |
63 |
WizardListener element = (WizardListener) iter.next(); |
64 |
element.cancel(new WizardEvent(this, currentStep)); |
65 |
} |
66 |
} |
67 |
|
68 |
/**
|
69 |
* Invoca en los listeners el evento finish
|
70 |
*/
|
71 |
private void callFinishListener() { |
72 |
for (Iterator iter = listeners.iterator(); iter.hasNext();) { |
73 |
WizardListener element = (WizardListener) iter.next(); |
74 |
element.finished(new WizardEvent(this, currentStep)); |
75 |
} |
76 |
} |
77 |
|
78 |
/**
|
79 |
* Invoca en los listeners el evento next
|
80 |
*/
|
81 |
private void callNextListener() { |
82 |
for (Iterator iter = listeners.iterator(); iter.hasNext();) { |
83 |
WizardListener element = (WizardListener) iter.next(); |
84 |
element.next(new WizardEvent(this, currentStep)); |
85 |
} |
86 |
} |
87 |
|
88 |
/**
|
89 |
* Invoca en los listeners el evento back
|
90 |
*/
|
91 |
private void callBackListener() { |
92 |
for (Iterator iter = listeners.iterator(); iter.hasNext();) { |
93 |
WizardListener element = (WizardListener) iter.next(); |
94 |
element.back(new WizardEvent(this, currentStep)); |
95 |
} |
96 |
} |
97 |
|
98 |
/**
|
99 |
* This method initializes this
|
100 |
*/
|
101 |
private void initialize() { |
102 |
this.setLayout(new BorderLayout()); |
103 |
this.setSize(300, 200); |
104 |
this.add(getJPanel(), java.awt.BorderLayout.SOUTH);
|
105 |
this.add(getPnlSteps(), java.awt.BorderLayout.CENTER);
|
106 |
} |
107 |
|
108 |
/**
|
109 |
* A?ade un paso al asistente. Inicializa el paso
|
110 |
*
|
111 |
* @param s Paso a a?adir
|
112 |
*
|
113 |
* @throws RuntimeException DOCUMENT ME!
|
114 |
*/
|
115 |
public void addStep(Step s) { |
116 |
if (!(s instanceof JComponent)) { |
117 |
throw new RuntimeException( |
118 |
"Step must be a visual component (descend from JComponent)");
|
119 |
} |
120 |
|
121 |
getPnlSteps().add((JComponent) s, BorderLayout.CENTER); |
122 |
steps.add(s); |
123 |
disableButtons(); |
124 |
s.init(this);
|
125 |
} |
126 |
|
127 |
/**
|
128 |
* Habilita o deshabilita los botones en funci?n del paso en el que se
|
129 |
* encuentre el asistente
|
130 |
*/
|
131 |
private void disableButtons() { |
132 |
btnNext.setEnabled(false);
|
133 |
btnBack.setEnabled(false);
|
134 |
btnFinish.setEnabled(false);
|
135 |
} |
136 |
|
137 |
/**
|
138 |
* Activa el paso al siguiente paso del asistente
|
139 |
*/
|
140 |
public void enableNext(boolean enabled) { |
141 |
if (currentStep == (steps.size() - 1)) { |
142 |
btnFinish.setEnabled(enabled); |
143 |
} else {
|
144 |
btnNext.setEnabled(enabled); |
145 |
} |
146 |
} |
147 |
|
148 |
/**
|
149 |
* Activa el paso al paso anterior del asistente
|
150 |
*/
|
151 |
public void enableBack(boolean enabled) { |
152 |
if (currentStep != 0) { |
153 |
btnBack.setEnabled(enabled); |
154 |
} |
155 |
} |
156 |
|
157 |
/**
|
158 |
* This method initializes jPanel
|
159 |
*
|
160 |
* @return javax.swing.JPanel
|
161 |
*/
|
162 |
private JPanel getJPanel() { |
163 |
if (jPanel == null) { |
164 |
jPanel = new JPanel(); |
165 |
jPanel.add(getBtnBack(), null);
|
166 |
jPanel.add(getBtnNext(), null);
|
167 |
jPanel.add(getBtnFinish(), null);
|
168 |
jPanel.add(getBtnCancel(), null);
|
169 |
} |
170 |
|
171 |
return jPanel;
|
172 |
} |
173 |
|
174 |
/**
|
175 |
* Obtiene una referencia al bot?n de dar un paso atr?s
|
176 |
*
|
177 |
* @return javax.swing.JButton
|
178 |
*/
|
179 |
public JButton getBtnBack() { |
180 |
if (btnBack == null) { |
181 |
btnBack = new JButton(); |
182 |
btnBack.setPreferredSize(new java.awt.Dimension(54, 18)); |
183 |
btnBack.setText("Back");
|
184 |
btnBack.setMargin(new java.awt.Insets(2, 2, 2, 2)); |
185 |
btnBack.setEnabled(false);
|
186 |
btnBack.addActionListener(new java.awt.event.ActionListener() {
|
187 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
188 |
backStep(); |
189 |
} |
190 |
}); |
191 |
} |
192 |
|
193 |
return btnBack;
|
194 |
} |
195 |
|
196 |
/**
|
197 |
* Obtiene una referencia al bot?n de dar un paso adelante
|
198 |
*
|
199 |
* @return javax.swing.JButton
|
200 |
*/
|
201 |
public JButton getBtnNext() { |
202 |
if (btnNext == null) { |
203 |
btnNext = new JButton(); |
204 |
btnNext.setText("Next");
|
205 |
btnNext.setMargin(new java.awt.Insets(2, 2, 2, 2)); |
206 |
btnNext.setPreferredSize(new java.awt.Dimension(54, 18)); |
207 |
btnNext.setEnabled(false);
|
208 |
btnNext.addActionListener(new java.awt.event.ActionListener() {
|
209 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
210 |
nextStep(); |
211 |
} |
212 |
}); |
213 |
} |
214 |
|
215 |
return btnNext;
|
216 |
} |
217 |
|
218 |
/**
|
219 |
* Obtiene una referencia al bot?n de finalizar
|
220 |
*
|
221 |
* @return javax.swing.JButton
|
222 |
*/
|
223 |
public JButton getBtnFinish() { |
224 |
if (btnFinish == null) { |
225 |
btnFinish = new JButton(); |
226 |
btnFinish.setMargin(new java.awt.Insets(2, 2, 2, 2)); |
227 |
btnFinish.setPreferredSize(new java.awt.Dimension(54, 18)); |
228 |
btnFinish.setText("Finish");
|
229 |
btnFinish.setEnabled(false);
|
230 |
btnFinish.addActionListener(new java.awt.event.ActionListener() {
|
231 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
232 |
callFinishListener(); |
233 |
} |
234 |
}); |
235 |
} |
236 |
|
237 |
return btnFinish;
|
238 |
} |
239 |
|
240 |
/**
|
241 |
* Obtiene una referencia al bot?n de cancelar
|
242 |
*
|
243 |
* @return javax.swing.JButton
|
244 |
*/
|
245 |
public JButton getBtnCancel() { |
246 |
if (btnCancel == null) { |
247 |
btnCancel = new JButton(); |
248 |
btnCancel.setMargin(new java.awt.Insets(2, 2, 2, 2)); |
249 |
btnCancel.setPreferredSize(new java.awt.Dimension(54, 18)); |
250 |
btnCancel.setText("Cancel");
|
251 |
btnCancel.addActionListener(new java.awt.event.ActionListener() {
|
252 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
253 |
callCancelListener(); |
254 |
} |
255 |
}); |
256 |
} |
257 |
|
258 |
return btnCancel;
|
259 |
} |
260 |
|
261 |
/**
|
262 |
* This method initializes pnlSteps
|
263 |
*
|
264 |
* @return javax.swing.JPanel
|
265 |
*/
|
266 |
private JPanel getPnlSteps() { |
267 |
if (pnlSteps == null) { |
268 |
pnlSteps = new JPanel(); |
269 |
pnlSteps.setLayout(new CardLayout()); |
270 |
} |
271 |
|
272 |
return pnlSteps;
|
273 |
} |
274 |
|
275 |
/**
|
276 |
* Muestra el panel del siguiente paso del asistente
|
277 |
*/
|
278 |
public void nextStep() { |
279 |
currentStep++; |
280 |
((CardLayout) getPnlSteps().getLayout()).next(getPnlSteps());
|
281 |
disableButtons(); |
282 |
callNextListener(); |
283 |
} |
284 |
|
285 |
/**
|
286 |
* Muestra el panel del paso anterior del asistente
|
287 |
*/
|
288 |
public void backStep() { |
289 |
currentStep--; |
290 |
((CardLayout) getPnlSteps().getLayout()).previous(getPnlSteps());
|
291 |
disableButtons(); |
292 |
callBackListener(); |
293 |
} |
294 |
|
295 |
/**
|
296 |
* Se cancela el asistente. Esta operaci?n no tiene ning?n efecto, salvo
|
297 |
* que se disparar? el evento de cancelado. El resultado de esto depender?
|
298 |
* de las implementaciones que haya escuchando el evento. Generalmente
|
299 |
* deber? haber un objeto que al escuchar este evento cerrar? el
|
300 |
* asistente.
|
301 |
*/
|
302 |
public void cancel() { |
303 |
callCancelListener(); |
304 |
} |
305 |
|
306 |
/**
|
307 |
* Se finaliza el asistente. Esta operaci?n no tiene ning?n efecto, salvo
|
308 |
* que se disparar? el evento de finalizaci?n. El resultado de esto
|
309 |
* depender? de las implementaciones que haya escuchando el evento.
|
310 |
* Generalmente deber? haber un objeto que al escuchar este evento cerrar?
|
311 |
* el asistente.
|
312 |
*/
|
313 |
public void finish() { |
314 |
callFinishListener(); |
315 |
} |
316 |
|
317 |
/**
|
318 |
* Obtiene un array con los pasos del asistente
|
319 |
*
|
320 |
* @return array de pasos
|
321 |
*/
|
322 |
public Step[] getSteps() { |
323 |
return (Step[]) steps.toArray(new Step[0]); |
324 |
} |
325 |
|
326 |
/**
|
327 |
* Obtiene el paso actual del asistente
|
328 |
*
|
329 |
* @return Paso actual del asistente
|
330 |
*/
|
331 |
public Step getCurrentStep() {
|
332 |
return getSteps()[currentStep];
|
333 |
} |
334 |
} |