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