Statistics
| Revision:

root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / buttonspanel / ButtonsPanel.java @ 12121

History | View | Annotate | Download (7.28 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2007 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 */
19
package org.gvsig.gui.beans.buttonspanel;
20

    
21
import java.awt.event.ActionEvent;
22
import java.awt.event.ActionListener;
23
import java.util.ArrayList;
24
import java.util.Iterator;
25

    
26
import javax.swing.JButton;
27
import javax.swing.JPanel;
28

    
29
import org.gvsig.gui.beans.messages.Messages;
30
/**
31
 * <code>ButtonsPanel</code> ofrece un widget con un conjunto de botones
32
 * preestablecidos, aunque tambi?n se pueden a?adir botones con el m?todo
33
 * {@link #addButton(String, int)}
34
 * 
35
 * @version 15/03/2007
36
 * @author Borja Sanchez Zamorano (borja.sanchez@iver.es)
37
 */
38
public class ButtonsPanel extends JPanel {
39
        private static final long serialVersionUID        = -7264041442217894577L;
40

    
41
        private ArrayList       buttonsList               = new ArrayList();
42
        private ArrayList       actionCommandListeners    = new ArrayList();
43

    
44
        static private int      eventId                   = Integer.MIN_VALUE;
45

    
46
        public static final int BUTTON_ACCEPT             = 1;
47
        public static final int BUTTON_CANCEL             = 2;
48
        public static final int BUTTON_APPLY              = 3;
49
        public static final int BUTTON_YES                = 4;
50
        public static final int BUTTON_NO                 = 5;
51
        public static final int BUTTON_CLOSE              = 6;
52
        public static final int BUTTON_EXIT               = 7;
53
        public static final int BUTTON_SEEDETAILS         = 8;
54
        public static final int BUTTON_HIDEDETAILS        = 9;
55
        public static final int BUTTON_PAUSE              = 10;
56
        public static final int BUTTON_RESTART            = 11;
57
        public static final int BUTTON_SAVE               = 12;
58
        /**
59
         * Sirve para cuando se crean botones nuevos, saber el ?ltimo n?mero usado
60
         * internamente, as? '<code>new_id = BUTTON_LAST + 1;</code>' podr?a ser
61
         * el ?ndice del nuevo bot?n.
62
         */
63
        public static final int BUTTON_LAST               = 12;
64
        public static final int BUTTONS_ACCEPT            = 1;
65
        public static final int BUTTONS_ACCEPTCANCEL      = 2;
66
        public static final int BUTTONS_ACCEPTCANCELAPPLY = 3;
67
        public static final int BUTTONS_CANCEL            = 4;
68
        public static final int BUTTONS_YESNO             = 5;
69
        public static final int BUTTONS_CLOSE             = 6;
70
        public static final int BUTTONS_EXIT              = 7;
71
        public static final int BUTTONS_NONE              = 8;
72

    
73
        /**
74
         * Crea un ButtonsPanel con un Layout por defecto.
75
         */
76
        public ButtonsPanel() {
77
                initialize();
78
        }
79

    
80
        private void initialize() {
81
                setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT, 7, 0));
82
                setBorder(javax.swing.BorderFactory.createEmptyBorder(8, 0, 0, 0));
83
// jpanel1 = new JPanel();
84
//                jpanel1.setLayout(new java.awt.GridLayout(1, 0, 7, 0));
85
//    this.add(jpanel1);
86
        }
87
        /**
88
         * Crea un ButtonsPanel con un Layout por defecto.
89
         * @param items Que botones vamos a usar en la creaci?n.
90
         */
91
        public ButtonsPanel(int items) {
92
                initialize();
93
                switch (items) {
94
                        case BUTTONS_ACCEPT:
95
                                addAccept();
96
                                break;
97
                        case BUTTONS_ACCEPTCANCEL:
98
                                addAccept();
99
                                addCancel();
100
                                break;
101
                        case BUTTONS_ACCEPTCANCELAPPLY:
102
                                addAccept();
103
                                addCancel();
104
                                addApply();
105
                                break;
106
                        case BUTTONS_CANCEL:
107
                                addCancel();
108
                                break;
109
                        case BUTTONS_YESNO:
110
                                addYes();
111
                                addNo();
112
                                break;
113
                        case BUTTONS_CLOSE:
114
                                addClose();
115
                                break;
116
                        case BUTTONS_EXIT:
117
                                addExit();
118
                                break;
119
                        case BUTTONS_NONE:
120
                                break;
121
                }
122
        }
123

    
124
        /**
125
         * A?adir el disparador de cuando se pulsa un bot?n.
126
         * @param listener
127
         */
128
        public void addButtonPressedListener(ButtonsPanelListener listener) {
129
                if (!actionCommandListeners.contains(listener))
130
                        actionCommandListeners.add(listener);
131
        }
132

    
133
        /**
134
         * Borrar el disparador de eventos de los botones.
135
         * @param listener
136
         */
137
        public void removeButtonPressedListener(ButtonsPanelListener listener) {
138
                actionCommandListeners.remove(listener);
139
        }
140

    
141
        private void callActionCommandListeners(int buttonID) {
142
                Iterator acIterator = actionCommandListeners.iterator();
143
                while (acIterator.hasNext()) {
144
                        ButtonsPanelListener listener = (ButtonsPanelListener) acIterator.next();
145
                        listener.actionButtonPressed(new ButtonsPanelEvent(this, buttonID));
146
                }
147
                eventId++;
148
        }
149

    
150
        /**
151
         * A?adir el boton Aceptar.
152
         */
153
        public void addAccept() {
154
                addButton(Messages.getText("aceptar"), BUTTON_ACCEPT);
155
        }
156

    
157
        /**
158
         * A?adir el boton Guardar.
159
         */
160
        public void addSave() {
161
                addButton(Messages.getText("guardar"), BUTTON_SAVE);
162
        }
163

    
164
        /**
165
         * A?adir el boton Cancelar.
166
         */
167
        public void addCancel() {
168
                addButton(Messages.getText("cancelar"), BUTTON_CANCEL);
169
        }
170

    
171
        /**
172
         * A?adir el boton S?.
173
         */
174
        public void addYes() {
175
                addButton(Messages.getText("si"), BUTTON_YES);
176
        }
177

    
178
        /**
179
         * A?adir el boton No.
180
         */
181
        public void addNo() {
182
                addButton(Messages.getText("no"), BUTTON_NO);
183
        }
184

    
185
        /**
186
         * A?adir el boton Aplicar.
187
         */
188
        public void addApply() {
189
                addButton(Messages.getText("aplicar"), BUTTON_APPLY);
190
        }
191

    
192
        /**
193
         * A?adir el boton Cerrar.
194
         */
195
        public void addClose() {
196
                addButton(Messages.getText("cerrar"), BUTTON_CLOSE);
197
        }
198

    
199
        /**
200
         * A?adir el boton Salir.
201
         */
202
        public void addExit() {
203
                addButton(Messages.getText("salir"), BUTTON_EXIT);
204
        }
205

    
206
        /**
207
         * A?adir el boton Ver detalles.
208
         */
209
        public void addSeeDetails() {
210
                addButton(Messages.getText("verdetalles"), BUTTON_SEEDETAILS);
211
        }
212

    
213
        /**
214
         * A?adir el boton Ocultar detalles.
215
         */
216
        public void addHideDetails() {
217
                addButton(Messages.getText("ocultardetalles"), BUTTON_HIDEDETAILS);
218
        }
219

    
220
        /**
221
         * A?adir el boton Pausar.
222
         */
223
        public void addPause() {
224
                addButton(Messages.getText("pausar"), BUTTON_PAUSE);
225
        }
226

    
227
        /**
228
         * A?adir el boton Reanudar.
229
         */
230
        public void addRestart() {
231
                addButton(Messages.getText("reanudar"), BUTTON_RESTART);
232
        }
233

    
234
        /**
235
         * A?adimos un bot?n definido por el usuario.
236
         * 
237
         * @param text Texto que contendr? el bot?n
238
         * @param id Entero para identificar los eventos del bot?n
239
         */
240
        public void addButton(String text, int id) {
241
                JButton button = new JButton();
242
                buttonsList.add(button);
243
                button.setText(text);
244
                button.setActionCommand(id + "");
245
                button.addActionListener(new ActionListener() {
246
                        public void actionPerformed(ActionEvent e) {
247
                                callActionCommandListeners(Integer.parseInt(e.getActionCommand()));
248
                        }
249
                });
250

    
251
//                jpanel1.add(button);
252
                add(button);
253
        }
254

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