Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.swing / org.gvsig.raster.swing.impl / src / main / java / org / gvsig / raster / swing / impl / basepanel / ButtonsPanel.java @ 2106

History | View | Annotate | Download (8.7 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2012 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 2
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.raster.swing.impl.basepanel;
25

    
26
import java.awt.event.ActionEvent;
27
import java.awt.event.ActionListener;
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.i18n.Messages;
36
import org.gvsig.raster.swing.basepanel.ButtonsPanelEvent;
37
import org.gvsig.raster.swing.basepanel.ButtonsPanelListener;
38
import org.gvsig.raster.swing.basepanel.IButtonsPanel;
39

    
40
/**
41
 * <code>ButtonsPanel</code> ofrece un widget con un conjunto de botones
42
 * preestablecidos, aunque tambi?n se pueden a?adir botones con el m?todo
43
 * {@link #addButton(String, int)}
44
 *
45
 * @version 09/05/2008
46
 *
47
 * @author BorSanZa - Borja Sanchez Zamorano 
48
 */
49
public class ButtonsPanel extends JPanel implements IButtonsPanel {
50
        private static final long serialVersionUID = -1660559792086305063L;
51

    
52
        private ArrayList<ButtonsPanelListener> actionCommandListeners = new ArrayList<ButtonsPanelListener>();
53
        private ArrayList<JButton> buttonsList = new ArrayList<JButton>();
54

    
55
        private JComponent      parent                    = null;
56

    
57
        /**
58
         * Crea un ButtonsPanel con un Layout por defecto.
59
         */
60
        public ButtonsPanel(JComponent parent) {
61
                this.parent = parent;
62
                initialize();
63
        }
64

    
65
        private void initialize() {
66
                setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT, 2, 0));
67
                setBorder(javax.swing.BorderFactory.createEmptyBorder(4, 0, 0, 0));
68
        }
69
        /**
70
         * Crea un ButtonsPanel con un Layout por defecto.
71
         * @param items Que botones vamos a usar en la creaci?n.
72
         */
73
        public ButtonsPanel(int items, JComponent parent) {
74
                this.parent = parent;
75
                initialize();
76
                switch (items) {
77
                        case BUTTONS_ACCEPT:
78
                                addAccept();
79
                                break;
80
                        case BUTTONS_ACCEPTCANCEL:
81
                                addAccept();
82
                                addCancel();
83
                                break;
84
                        case BUTTONS_ACCEPTCANCELAPPLY:
85
                                addApply();
86
                                addAccept();
87
                                addCancel();
88
                                break;
89
                        case BUTTONS_APPLYCLOSE:
90
                                addApply();
91
                                addClose();
92
                                break;
93
                        case BUTTONS_CANCEL:
94
                                addCancel();
95
                                break;
96
                        case BUTTONS_YESNO:
97
                                addYes();
98
                                addNo();
99
                                break;
100
                        case BUTTONS_CLOSE:
101
                                addClose();
102
                                break;
103
                        case BUTTONS_EXIT:
104
                                addExit();
105
                                break;
106
                        case BUTTONS_NONE:
107
                                break;
108
                }
109
        }
110

    
111
        /**
112
         * A?adir el disparador de cuando se pulsa un bot?n.
113
         * @param listener
114
         */
115
        public void addButtonPressedListener(ButtonsPanelListener listener) {
116
                if (!actionCommandListeners.contains(listener))
117
                        actionCommandListeners.add(listener);
118
        }
119

    
120
        /**
121
         * Devuelve el array de listeners del componente
122
         * @return
123
         */
124
        public Object[] getButtonPressedListeners() {
125
                return actionCommandListeners.toArray();
126
        }
127

    
128
        /**
129
         * Borrar el disparador de eventos de los botones.
130
         * @param listener
131
         */
132
        public void removeButtonPressedListener(ButtonsPanelListener listener) {
133
                actionCommandListeners.remove(listener);
134
        }
135

    
136
        private void callActionCommandListeners(int buttonID) {
137
                Iterator<ButtonsPanelListener> acIterator = actionCommandListeners.iterator();
138
                while (acIterator.hasNext()) {
139
                        ButtonsPanelListener listener = (ButtonsPanelListener) acIterator.next();
140
                        listener.actionButtonPressed(new ButtonsPanelEvent(parent, buttonID));
141
                }
142
        }
143

    
144
        /**
145
         * A?adir el boton Aceptar.
146
         */
147
        public void addAccept() {
148
                addButton(Messages.getText("aceptar"), BUTTON_ACCEPT);
149
        }
150

    
151
        /**
152
         * A?adir el boton Guardar.
153
         */
154
        public void addSave() {
155
                addButton(Messages.getText("guardar"), BUTTON_SAVE);
156
        }
157

    
158
        /**
159
         * A?adir el boton Cancelar.
160
         */
161
        public void addCancel() {
162
                addButton(Messages.getText("cancelar"), BUTTON_CANCEL);
163
        }
164

    
165
        /**
166
         * A?adir el boton S?.
167
         */
168
        public void addYes() {
169
                addButton(Messages.getText("si"), BUTTON_YES);
170
        }
171

    
172
        /**
173
         * A?adir el boton No.
174
         */
175
        public void addNo() {
176
                addButton(Messages.getText("no"), BUTTON_NO);
177
        }
178

    
179
        /**
180
         * A?adir el boton Aplicar.
181
         */
182
        public void addApply() {
183
                addButton(Messages.getText("aplicar"), BUTTON_APPLY);
184
        }
185

    
186
        /**
187
         * A?adir el boton Cerrar.
188
         */
189
        public void addClose() {
190
                addButton(Messages.getText("cerrar"), BUTTON_CLOSE);
191
        }
192

    
193
        /**
194
         * A?adir el boton Salir.
195
         */
196
        public void addExit() {
197
                addButton(Messages.getText("salir"), BUTTON_EXIT);
198
        }
199

    
200
        /**
201
         * A?adir el boton Ver detalles.
202
         */
203
        public void addSeeDetails() {
204
                addButton(Messages.getText("verdetalles"), BUTTON_SEEDETAILS);
205
        }
206

    
207
        /**
208
         * A?adir el boton Ocultar detalles.
209
         */
210
        public void addHideDetails() {
211
                addButton(Messages.getText("ocultardetalles"), BUTTON_HIDEDETAILS);
212
        }
213

    
214
        /**
215
         * A?adir el boton Pausar.
216
         */
217
        public void addPause() {
218
                addButton(Messages.getText("pausar"), BUTTON_PAUSE);
219
        }
220

    
221
        /**
222
         * A?adir el boton Reanudar.
223
         */
224
        public void addRestart() {
225
                addButton(Messages.getText("reanudar"), BUTTON_RESTART);
226
        }
227

    
228
        /**
229
         * A?adimos un bot?n definido por el usuario.
230
         *
231
         * @param text Texto que contendr? el bot?n
232
         * @param id Entero para identificar los eventos del bot?n
233
         */
234
        public void addButton(String text, int id) {
235
                JButton button = new JButton();
236
                button.setText(text);
237

    
238
                buttonsList.add(button);
239
                button.setActionCommand(id + "");
240
                button.addActionListener(new ActionListener() {
241
                        public void actionPerformed(ActionEvent e) {
242
                                callActionCommandListeners(Integer.parseInt(e.getActionCommand()));
243
                        }
244
                });
245

    
246
                add(button);
247
        }
248

    
249
        /**
250
         * Obtener un bot?n por su Entero
251
         * @param id N?mero del disparador del bot?n
252
         * @return El bot?n especificado o <code>null</code> si no se encontr? el bot?n.
253
         */
254
        public JButton getButton(int id) {
255
                Iterator<JButton> acIterator = buttonsList.iterator();
256
                while (acIterator.hasNext()) {
257
                        JButton button = (JButton) acIterator.next();
258
                        if (Integer.parseInt(button.getActionCommand()) == id)
259
                                return button;
260
                }
261
                return null;
262
        }
263

    
264
        /**
265
         * <p>Removes the button identified by <code>id</code>.</p>
266
         * 
267
         * @param id identifier of the button
268
         * @return <code>true</code> if has removed the button; otherwise <code>false</code>
269
         */
270
        public boolean removeButton(int id) {
271
                String b_text = getButtonText(id);
272
                
273
                Iterator<JButton> acIterator = buttonsList.iterator();
274
                while (acIterator.hasNext()) {
275
                        JButton button = (JButton) acIterator.next();
276
                        if (button.getText().compareTo(b_text) == 0) {
277
                                buttonsList.remove(button);
278
                                return true;
279
                        }
280
                }
281
                
282
                return false;
283
        }
284
        
285
        /**
286
         * <p>Returns the text of the button identified by <code>id</code>.</p>
287
         * 
288
         * @param id identifier of the button
289
         * 
290
         * @return text of the identified button
291
         */
292
        public String getButtonText(int id) {
293
                switch (id) {
294
                        case BUTTON_ACCEPT:
295
                                return Messages.getText("aceptar");
296
                        case BUTTON_CANCEL:
297
                                return Messages.getText("cancelar");
298
                        case BUTTON_APPLY:
299
                                return Messages.getText("aplicar");
300
                        case BUTTON_YES:
301
                                return Messages.getText("si");
302
                        case BUTTON_NO:
303
                                return Messages.getText("no");
304
                        case BUTTON_CLOSE:
305
                                return Messages.getText("cerrar");
306
                        case BUTTON_EXIT:
307
                                return Messages.getText("salir");
308
                        case BUTTON_SEEDETAILS:
309
                                return Messages.getText("verdetalles");
310
                        case BUTTON_HIDEDETAILS:
311
                                return Messages.getText("ocultardetalles");
312
                        case BUTTON_PAUSE:
313
                                return Messages.getText("pausar");
314
                        case BUTTON_RESTART:
315
                                return Messages.getText("reanudar");
316
                        case BUTTON_SAVE:
317
                                return Messages.getText("guardar");
318
                }
319

    
320
                return null;
321
        }
322

    
323
        /**
324
         * <p>Enables (or disables) the button identified by <code>id</code>.</p>
325
         * 
326
         * @param id identifier of the button
327
         * @param b <code>true</code> to enable the button, otherwise <code>false</code>
328
         * 
329
         * @return <code>true</code> if there was a button of that kind in this group, otherwise <code>false</code>
330
         */
331
        public boolean setEnabled(int id, boolean b) {
332
                String b_text = getButtonText(id);
333
                
334
                Iterator<JButton> acIterator = buttonsList.iterator();
335
                while (acIterator.hasNext()) {
336
                        JButton button = (JButton) acIterator.next();
337
                        if (button.getText().compareTo(b_text) == 0) {
338
                                button.setEnabled(b);
339
                                return true;
340
                        }
341
                }
342
                
343
                return false;
344
        }
345

    
346
        public JComponent getComponent() {
347
                return this;
348
        }
349
}