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