Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libIverUtiles / src / com / iver / utiles / swing / wizard / Wizard.java @ 4033

History | View | Annotate | Download (8.56 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.FontRenderContext;
8
import java.awt.font.TextLayout;
9

    
10
import java.util.ArrayList;
11
import java.util.Iterator;
12

    
13
import javax.swing.JButton;
14
import javax.swing.JComponent;
15
import javax.swing.JPanel;
16

    
17

    
18
/**
19
 * Clase wizard con la gesti?n de los botones y los paneles a?adidos al mismo.
20
 * Al avanzar o retrasar un paso el asistente se desactivan todos los botones
21
 * menos el de cancelar, que est? activo siempre por defecto
22
 *
23
 * @author Fernando Gonz?lez Cort?s
24
 */
25
public class Wizard extends JPanel implements WizardControl {
26
        private JPanel jPanel = null;
27
        private JButton btnBack = null;
28
        private JButton btnNext = null;
29
        private JButton btnFinish = null;
30
        private JButton btnCancel = null;
31
        private ArrayList steps = new ArrayList();
32
        private int currentStep = 0;
33
        private JPanel pnlSteps = null;
34
        private ArrayList listeners = new ArrayList();
35

    
36
        /**
37
         * This is the default constructor
38
         */
39
        public Wizard(String backText, String nextText, String finishText, String cancelText) {
40
                super();
41
                initialize(backText, nextText, finishText, cancelText);
42
        }
43

    
44
        /**
45
         * A?ade un l?stener de eventos del wizard
46
         *
47
         * @param listener
48
         */
49
        public void addWizardListener(WizardListener listener) {
50
                listeners.add(listener);
51
        }
52

    
53
        /**
54
         * Elimina un listener de eventos del wizard
55
         *
56
         * @param listener
57
         */
58
        public void removeWizardListener(WizardListener listener) {
59
                listeners.remove(listener);
60
        }
61

    
62
        /**
63
         * Invoca en los listeners el evento cancel
64
         */
65
        private void callCancelListener() {
66
                for (Iterator iter = listeners.iterator(); iter.hasNext();) {
67
                        WizardListener element = (WizardListener) iter.next();
68
                        element.cancel(new WizardEvent(this, currentStep));
69
                }
70
        }
71

    
72
        /**
73
         * Invoca en los listeners el evento finish
74
         */
75
        private void callFinishListener() {
76
                for (Iterator iter = listeners.iterator(); iter.hasNext();) {
77
                        WizardListener element = (WizardListener) iter.next();
78
                        element.finished(new WizardEvent(this, currentStep));
79
                }
80
        }
81

    
82
        /**
83
         * Invoca en los listeners el evento next
84
         */
85
        private void callNextListener() {
86
                for (Iterator iter = listeners.iterator(); iter.hasNext();) {
87
                        WizardListener element = (WizardListener) iter.next();
88
                        element.next(new WizardEvent(this, currentStep));
89
                }
90
        }
91

    
92
        /**
93
         * Invoca en los listeners el evento back
94
         */
95
        private void callBackListener() {
96
                for (Iterator iter = listeners.iterator(); iter.hasNext();) {
97
                        WizardListener element = (WizardListener) iter.next();
98
                        element.back(new WizardEvent(this, currentStep));
99
                }
100
        }
101

    
102
        /**
103
         * This method initializes this
104
         */
105
        private void initialize(String backText, String nextText, String finishText, String cancelText) {
106
                this.setLayout(new BorderLayout());
107
                this.setSize(300, 200);
108
                this.add(getJPanel(backText, nextText, finishText, cancelText), java.awt.BorderLayout.SOUTH);
109
                this.add(getPnlSteps(), java.awt.BorderLayout.CENTER);
110
        }
111

    
112
        /**
113
         * A?ade un paso al asistente. Inicializa el paso
114
         *
115
         * @param s Paso a a?adir
116
         *
117
         * @throws RuntimeException DOCUMENT ME!
118
         */
119
        public void addStep(Step s) {
120
                if (!(s instanceof JComponent)) {
121
                        throw new RuntimeException(
122
                                "Step must be a visual component (descend from JComponent)");
123
                }
124

    
125
                getPnlSteps().add((JComponent) s, BorderLayout.CENTER);
126
                steps.add(s);
127
                disableButtons();
128
                s.init(this);
129
        }
130

    
131
        /**
132
         * Habilita o deshabilita los botones en funci?n del paso en el que se
133
         * encuentre el asistente
134
         */
135
        private void disableButtons() {
136
                btnNext.setEnabled(false);
137
                btnBack.setEnabled(false);
138
                btnFinish.setEnabled(false);
139
        }
140

    
141
        /**
142
         * Activa el paso al siguiente paso del asistente
143
         */
144
        public void enableNext(boolean enabled) {
145
                if (currentStep == (steps.size() - 1)) {
146
                        btnFinish.setEnabled(enabled);
147
                } else {
148
                        btnNext.setEnabled(enabled);
149
                }
150
        }
151

    
152
        /**
153
         * Activa el paso al paso anterior del asistente
154
         */
155
        public void enableBack(boolean enabled) {
156
                if (currentStep != 0) {
157
                        btnBack.setEnabled(enabled);
158
                }
159
        }
160

    
161
        /**
162
         * This method initializes jPanel
163
         *
164
         * @return javax.swing.JPanel
165
         */
166
        private JPanel getJPanel(String backText, String nextText, String finishText, String cancelText) {
167
                if (jPanel == null) {
168
                        jPanel = new JPanel();
169
                        jPanel.add(getBtnBack(backText), null);
170
                        jPanel.add(getBtnNext(nextText), null);
171
                        jPanel.add(getBtnFinish(finishText), null);
172
                        jPanel.add(getBtnCancel(cancelText), null);
173
                }
174

    
175
                return jPanel;
176
        }
177

    
178
        private JButton newJButton(String text){
179
                return new JButton() {
180
            protected void paintComponent(Graphics g) {
181
                            TextLayout tl = new TextLayout(getText(), getFont(), ((Graphics2D )getGraphics()).getFontRenderContext());
182
                            setPreferredSize(new java.awt.Dimension((int) tl.getBounds().getWidth(), 18));
183
                super.paintComponent(g);
184
            }
185
        };
186
            
187
        }
188
        
189
        /**
190
         * Obtiene una referencia al bot?n de dar un paso atr?s
191
         * @param text
192
         *
193
         * @return javax.swing.JButton
194
         */
195
        public JButton getBtnBack(String text) {
196
                if (btnBack == null) {
197
                        btnBack = newJButton(text);
198
                        btnBack.setText(text);
199
                        btnBack.setMargin(new java.awt.Insets(2, 2, 2, 2));
200
                        btnBack.setEnabled(false);
201
                        btnBack.addActionListener(new java.awt.event.ActionListener() {
202
                                        public void actionPerformed(java.awt.event.ActionEvent e) {
203
                                                backStep();
204
                                        }
205
                                });
206
                }
207

    
208
                return btnBack;
209
        }
210

    
211
        /**
212
         * Obtiene una referencia al bot?n de dar un paso adelante
213
         * @param text
214
         *
215
         * @return javax.swing.JButton
216
         */
217
        public JButton getBtnNext(String text) {
218
                if (btnNext == null) {
219
                        btnNext = newJButton(text);
220
                        btnNext.setText(text);
221
                        btnNext.setMargin(new java.awt.Insets(2, 2, 2, 2));
222
                        btnNext.setEnabled(false);
223
                        btnNext.addActionListener(new java.awt.event.ActionListener() {
224
                                        public void actionPerformed(java.awt.event.ActionEvent e) {
225
                                                nextStep();
226
                                        }
227
                                });
228
                }
229

    
230
                return btnNext;
231
        }
232

    
233
        /**
234
         * Obtiene una referencia al bot?n de finalizar
235
         * @param finishText
236
         *
237
         * @return javax.swing.JButton
238
         */
239
        public JButton getBtnFinish(String text) {
240
                if (btnFinish == null) {
241
                        btnFinish = newJButton(text);
242
                        btnFinish.setMargin(new java.awt.Insets(2, 2, 2, 2));
243
                        btnFinish.setText(text);
244
                        btnFinish.setEnabled(false);
245
                        btnFinish.addActionListener(new java.awt.event.ActionListener() {
246
                                        public void actionPerformed(java.awt.event.ActionEvent e) {
247
                                                callFinishListener();
248
                                        }
249
                                });
250
                }
251

    
252
                return btnFinish;
253
        }
254

    
255
        /**
256
         * Obtiene una referencia al bot?n de cancelar
257
         * @param cancelText
258
         *
259
         * @return javax.swing.JButton
260
         */
261
        public JButton getBtnCancel(String text) {
262
                if (btnCancel == null) {
263
                        btnCancel = newJButton(text);
264
                        btnCancel.setMargin(new java.awt.Insets(2, 2, 2, 2));
265
                        btnCancel.setText(text);
266
                        btnCancel.addActionListener(new java.awt.event.ActionListener() {
267
                                        public void actionPerformed(java.awt.event.ActionEvent e) {
268
                                                callCancelListener();
269
                                        }
270
                                });
271
                }
272

    
273
                return btnCancel;
274
        }
275

    
276
        /**
277
         * This method initializes pnlSteps
278
         *
279
         * @return javax.swing.JPanel
280
         */
281
        private JPanel getPnlSteps() {
282
                if (pnlSteps == null) {
283
                        pnlSteps = new JPanel();
284
                        pnlSteps.setLayout(new CardLayout());
285
                }
286

    
287
                return pnlSteps;
288
        }
289

    
290
        /**
291
         * Muestra el panel del siguiente paso del asistente
292
         */
293
        public void nextStep() {
294
                currentStep++;
295
                ((CardLayout) getPnlSteps().getLayout()).next(getPnlSteps());
296
                disableButtons();
297
                callNextListener();
298
        }
299

    
300
        /**
301
         * Muestra el panel del paso anterior del asistente
302
         */
303
        public void backStep() {
304
                currentStep--;
305
                ((CardLayout) getPnlSteps().getLayout()).previous(getPnlSteps());
306
                disableButtons();
307
                callBackListener();
308
        }
309

    
310
        /**
311
         * Se cancela el asistente. Esta operaci?n no tiene ning?n efecto, salvo
312
         * que se disparar? el evento de cancelado. El resultado de esto depender?
313
         * de las implementaciones que haya escuchando el evento. Generalmente
314
         * deber? haber un objeto que al escuchar este evento cerrar? el
315
         * asistente.
316
         */
317
        public void cancel() {
318
                callCancelListener();
319
        }
320

    
321
        /**
322
         * Se finaliza el asistente. Esta operaci?n no tiene ning?n efecto, salvo
323
         * que se disparar? el evento de finalizaci?n. El resultado de esto
324
         * depender? de las implementaciones que haya escuchando el evento.
325
         * Generalmente deber? haber un objeto que al escuchar este evento cerrar?
326
         * el asistente.
327
         */
328
        public void finish() {
329
                callFinishListener();
330
        }
331

    
332
        /**
333
         * Obtiene un array con los pasos del asistente
334
         *
335
         * @return array de pasos
336
         */
337
        public Step[] getSteps() {
338
                return (Step[]) steps.toArray(new Step[0]);
339
        }
340

    
341
        /**
342
         * Obtiene el paso actual del asistente
343
         *
344
         * @return Paso actual del asistente
345
         */
346
        public Step getCurrentStep() {
347
                return getSteps()[currentStep];
348
        }
349
}