svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / buttonspanel / ButtonsPanel.java @ 42246
History | View | Annotate | Download (10.7 KB)
1 | 40561 | jjdelcerro | /**
|
---|---|---|---|
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 | 40435 | jjdelcerro | package org.gvsig.gui.beans.buttonspanel; |
25 | |||
26 | /* gvSIG. Geographic Information System of the Valencian Government
|
||
27 | *
|
||
28 | * Copyright (C) 2007-2008 Infrastructures and Transports Department
|
||
29 | * of the Valencian Government (CIT)
|
||
30 | *
|
||
31 | * This program is free software; you can redistribute it and/or
|
||
32 | * modify it under the terms of the GNU General Public License
|
||
33 | * as published by the Free Software Foundation; either version 2
|
||
34 | * of the License, or (at your option) any later version.
|
||
35 | *
|
||
36 | * This program is distributed in the hope that it will be useful,
|
||
37 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
38 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
39 | * GNU General Public License for more details.
|
||
40 | *
|
||
41 | * You should have received a copy of the GNU General Public License
|
||
42 | * along with this program; if not, write to the Free Software
|
||
43 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
44 | * MA 02110-1301, USA.
|
||
45 | *
|
||
46 | */
|
||
47 | |||
48 | import java.awt.event.ActionEvent; |
||
49 | import java.awt.event.ActionListener; |
||
50 | import java.util.ArrayList; |
||
51 | import java.util.Iterator; |
||
52 | |||
53 | import javax.swing.JButton; |
||
54 | import javax.swing.JPanel; |
||
55 | |||
56 | import org.gvsig.gui.beans.Messages; |
||
57 | |||
58 | /**
|
||
59 | * <code>ButtonsPanel</code> ofrece un widget con un conjunto de botones
|
||
60 | * preestablecidos, aunque tambi?n se pueden a?adir botones con el m?todo
|
||
61 | * {@link #addButton(String, int)}
|
||
62 | *
|
||
63 | * @version 09/05/2008
|
||
64 | *
|
||
65 | * @author BorSanZa - Borja Sanchez Zamorano (borja.sanchez@iver.es)
|
||
66 | */
|
||
67 | public class ButtonsPanel extends JPanel { |
||
68 | private static final long serialVersionUID = -1660559792086305063L; |
||
69 | |||
70 | private ArrayList<ButtonsPanelListener> actionCommandListeners = new ArrayList<ButtonsPanelListener>(); |
||
71 | private ArrayList<JButton> buttonsList = new ArrayList<JButton>(); |
||
72 | |||
73 | static private int eventId = Integer.MIN_VALUE; |
||
74 | |||
75 | public static final int BUTTON_ACCEPT = 1; |
||
76 | public static final int BUTTON_CANCEL = 2; |
||
77 | public static final int BUTTON_APPLY = 3; |
||
78 | public static final int BUTTON_YES = 4; |
||
79 | public static final int BUTTON_NO = 5; |
||
80 | public static final int BUTTON_CLOSE = 6; |
||
81 | public static final int BUTTON_EXIT = 7; |
||
82 | public static final int BUTTON_SEEDETAILS = 8; |
||
83 | public static final int BUTTON_HIDEDETAILS = 9; |
||
84 | public static final int BUTTON_PAUSE = 10; |
||
85 | public static final int BUTTON_RESTART = 11; |
||
86 | public static final int BUTTON_SAVE = 12; |
||
87 | /**
|
||
88 | * Sirve para cuando se crean botones nuevos, saber el ?ltimo n?mero usado
|
||
89 | * internamente, as? '<code>new_id = BUTTON_LAST + 1;</code>' podr?a ser
|
||
90 | * el ?ndice del nuevo bot?n.
|
||
91 | */
|
||
92 | public static final int BUTTON_LAST = 12; |
||
93 | public static final int BUTTONS_ACCEPT = 1; |
||
94 | public static final int BUTTONS_ACCEPTCANCEL = 2; |
||
95 | public static final int BUTTONS_ACCEPTCANCELAPPLY = 3; |
||
96 | public static final int BUTTONS_CANCEL = 4; |
||
97 | public static final int BUTTONS_YESNO = 5; |
||
98 | public static final int BUTTONS_CLOSE = 6; |
||
99 | public static final int BUTTONS_EXIT = 7; |
||
100 | public static final int BUTTONS_NONE = 8; |
||
101 | public static final int BUTTONS_APPLYCLOSE = 9; |
||
102 | |||
103 | /**
|
||
104 | * Crea un ButtonsPanel con un Layout por defecto.
|
||
105 | */
|
||
106 | public ButtonsPanel() {
|
||
107 | initialize(); |
||
108 | } |
||
109 | |||
110 | private void initialize() { |
||
111 | setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT, 2, 0)); |
||
112 | setBorder(javax.swing.BorderFactory.createEmptyBorder(4, 0, 0, 0)); |
||
113 | } |
||
114 | /**
|
||
115 | * Crea un ButtonsPanel con un Layout por defecto.
|
||
116 | * @param items Que botones vamos a usar en la creaci?n.
|
||
117 | */
|
||
118 | public ButtonsPanel(int items) { |
||
119 | initialize(); |
||
120 | switch (items) {
|
||
121 | case BUTTONS_ACCEPT:
|
||
122 | addAccept(); |
||
123 | break;
|
||
124 | case BUTTONS_ACCEPTCANCEL:
|
||
125 | addAccept(); |
||
126 | addCancel(); |
||
127 | break;
|
||
128 | case BUTTONS_ACCEPTCANCELAPPLY:
|
||
129 | addApply(); |
||
130 | addAccept(); |
||
131 | addCancel(); |
||
132 | break;
|
||
133 | case BUTTONS_APPLYCLOSE:
|
||
134 | addApply(); |
||
135 | addClose(); |
||
136 | break;
|
||
137 | case BUTTONS_CANCEL:
|
||
138 | addCancel(); |
||
139 | break;
|
||
140 | case BUTTONS_YESNO:
|
||
141 | addYes(); |
||
142 | addNo(); |
||
143 | break;
|
||
144 | case BUTTONS_CLOSE:
|
||
145 | addClose(); |
||
146 | break;
|
||
147 | case BUTTONS_EXIT:
|
||
148 | addExit(); |
||
149 | break;
|
||
150 | case BUTTONS_NONE:
|
||
151 | break;
|
||
152 | } |
||
153 | } |
||
154 | |||
155 | /**
|
||
156 | * A?adir el disparador de cuando se pulsa un bot?n.
|
||
157 | * @param listener
|
||
158 | */
|
||
159 | public void addButtonPressedListener(ButtonsPanelListener listener) { |
||
160 | if (!actionCommandListeners.contains(listener))
|
||
161 | actionCommandListeners.add(listener); |
||
162 | } |
||
163 | |||
164 | /**
|
||
165 | * Devuelve el array de listeners del componente
|
||
166 | * @return
|
||
167 | */
|
||
168 | public Object[] getButtonPressedListeners() { |
||
169 | return actionCommandListeners.toArray();
|
||
170 | } |
||
171 | |||
172 | /**
|
||
173 | * Borrar el disparador de eventos de los botones.
|
||
174 | * @param listener
|
||
175 | */
|
||
176 | public void removeButtonPressedListener(ButtonsPanelListener listener) { |
||
177 | actionCommandListeners.remove(listener); |
||
178 | } |
||
179 | |||
180 | private void callActionCommandListeners(int buttonID) { |
||
181 | Iterator<ButtonsPanelListener> acIterator = actionCommandListeners.iterator();
|
||
182 | while (acIterator.hasNext()) {
|
||
183 | ButtonsPanelListener listener = (ButtonsPanelListener) acIterator.next(); |
||
184 | listener.actionButtonPressed(new ButtonsPanelEvent(this, buttonID)); |
||
185 | } |
||
186 | eventId++; |
||
187 | } |
||
188 | |||
189 | /**
|
||
190 | * A?adir el boton Aceptar.
|
||
191 | */
|
||
192 | public void addAccept() { |
||
193 | addButton(Messages.getText("aceptar"), BUTTON_ACCEPT);
|
||
194 | } |
||
195 | |||
196 | /**
|
||
197 | * A?adir el boton Guardar.
|
||
198 | */
|
||
199 | public void addSave() { |
||
200 | addButton(Messages.getText("guardar"), BUTTON_SAVE);
|
||
201 | } |
||
202 | |||
203 | /**
|
||
204 | * A?adir el boton Cancelar.
|
||
205 | */
|
||
206 | public void addCancel() { |
||
207 | addButton(Messages.getText("cancelar"), BUTTON_CANCEL);
|
||
208 | } |
||
209 | |||
210 | /**
|
||
211 | * A?adir el boton S?.
|
||
212 | */
|
||
213 | public void addYes() { |
||
214 | addButton(Messages.getText("si"), BUTTON_YES);
|
||
215 | } |
||
216 | |||
217 | /**
|
||
218 | * A?adir el boton No.
|
||
219 | */
|
||
220 | public void addNo() { |
||
221 | addButton(Messages.getText("no"), BUTTON_NO);
|
||
222 | } |
||
223 | |||
224 | /**
|
||
225 | * A?adir el boton Aplicar.
|
||
226 | */
|
||
227 | public void addApply() { |
||
228 | addButton(Messages.getText("aplicar"), BUTTON_APPLY);
|
||
229 | } |
||
230 | |||
231 | /**
|
||
232 | * A?adir el boton Cerrar.
|
||
233 | */
|
||
234 | public void addClose() { |
||
235 | addButton(Messages.getText("cerrar"), BUTTON_CLOSE);
|
||
236 | } |
||
237 | |||
238 | /**
|
||
239 | * A?adir el boton Salir.
|
||
240 | */
|
||
241 | public void addExit() { |
||
242 | addButton(Messages.getText("salir"), BUTTON_EXIT);
|
||
243 | } |
||
244 | |||
245 | /**
|
||
246 | * A?adir el boton Ver detalles.
|
||
247 | */
|
||
248 | public void addSeeDetails() { |
||
249 | addButton(Messages.getText("verdetalles"), BUTTON_SEEDETAILS);
|
||
250 | } |
||
251 | |||
252 | /**
|
||
253 | * A?adir el boton Ocultar detalles.
|
||
254 | */
|
||
255 | public void addHideDetails() { |
||
256 | addButton(Messages.getText("ocultardetalles"), BUTTON_HIDEDETAILS);
|
||
257 | } |
||
258 | |||
259 | /**
|
||
260 | * A?adir el boton Pausar.
|
||
261 | */
|
||
262 | public void addPause() { |
||
263 | addButton(Messages.getText("pausar"), BUTTON_PAUSE);
|
||
264 | } |
||
265 | |||
266 | /**
|
||
267 | * A?adir el boton Reanudar.
|
||
268 | */
|
||
269 | public void addRestart() { |
||
270 | addButton(Messages.getText("reanudar"), BUTTON_RESTART);
|
||
271 | } |
||
272 | |||
273 | /**
|
||
274 | * A?adimos un bot?n definido por el usuario.
|
||
275 | *
|
||
276 | * @param text Texto que contendr? el bot?n
|
||
277 | * @param id Entero para identificar los eventos del bot?n
|
||
278 | */
|
||
279 | public void addButton(String text, int id) { |
||
280 | JButton button = new JButton(); |
||
281 | button.setText(text); |
||
282 | |||
283 | buttonsList.add(button); |
||
284 | button.setActionCommand(id + "");
|
||
285 | button.addActionListener(new ActionListener() { |
||
286 | public void actionPerformed(ActionEvent e) { |
||
287 | callActionCommandListeners(Integer.parseInt(e.getActionCommand()));
|
||
288 | } |
||
289 | }); |
||
290 | |||
291 | add(button); |
||
292 | } |
||
293 | |||
294 | /**
|
||
295 | * Obtener un bot?n por su Entero
|
||
296 | * @param id N?mero del disparador del bot?n
|
||
297 | * @return El bot?n especificado o <code>null</code> si no se encontr? el bot?n.
|
||
298 | */
|
||
299 | public JButton getButton(int id) { |
||
300 | Iterator<JButton> acIterator = buttonsList.iterator(); |
||
301 | while (acIterator.hasNext()) {
|
||
302 | JButton button = (JButton) acIterator.next(); |
||
303 | if (Integer.parseInt(button.getActionCommand()) == id) |
||
304 | return button;
|
||
305 | } |
||
306 | return null; |
||
307 | } |
||
308 | |||
309 | /**
|
||
310 | * <p>Removes the button identified by <code>id</code>.</p>
|
||
311 | *
|
||
312 | * @param id identifier of the button
|
||
313 | * @return <code>true</code> if has removed the button; otherwise <code>false</code>
|
||
314 | */
|
||
315 | public boolean removeButton(int id) { |
||
316 | String b_text = getButtonText(id);
|
||
317 | |||
318 | Iterator<JButton> acIterator = buttonsList.iterator(); |
||
319 | while (acIterator.hasNext()) {
|
||
320 | JButton button = (JButton) acIterator.next(); |
||
321 | if (button.getText().compareTo(b_text) == 0) { |
||
322 | buttonsList.remove(button); |
||
323 | return true; |
||
324 | } |
||
325 | } |
||
326 | |||
327 | return false; |
||
328 | } |
||
329 | |||
330 | /**
|
||
331 | * <p>Returns the text of the button identified by <code>id</code>.</p>
|
||
332 | *
|
||
333 | * @param id identifier of the button
|
||
334 | *
|
||
335 | * @return text of the identified button
|
||
336 | */
|
||
337 | protected String getButtonText(int id) { |
||
338 | switch (id) {
|
||
339 | case BUTTON_ACCEPT:
|
||
340 | return Messages.getText("aceptar"); |
||
341 | case BUTTON_CANCEL:
|
||
342 | return Messages.getText("cancelar"); |
||
343 | case BUTTON_APPLY:
|
||
344 | return Messages.getText("aplicar"); |
||
345 | case BUTTON_YES:
|
||
346 | return Messages.getText("si"); |
||
347 | case BUTTON_NO:
|
||
348 | return Messages.getText("no"); |
||
349 | case BUTTON_CLOSE:
|
||
350 | return Messages.getText("cerrar"); |
||
351 | case BUTTON_EXIT:
|
||
352 | return Messages.getText("salir"); |
||
353 | case BUTTON_SEEDETAILS:
|
||
354 | return Messages.getText("verdetalles"); |
||
355 | case BUTTON_HIDEDETAILS:
|
||
356 | return Messages.getText("ocultardetalles"); |
||
357 | case BUTTON_PAUSE:
|
||
358 | return Messages.getText("pausar"); |
||
359 | case BUTTON_RESTART:
|
||
360 | return Messages.getText("reanudar"); |
||
361 | case BUTTON_SAVE:
|
||
362 | return Messages.getText("guardar"); |
||
363 | } |
||
364 | |||
365 | return null; |
||
366 | } |
||
367 | |||
368 | /**
|
||
369 | * <p>Enables (or disables) the button identified by <code>id</code>.</p>
|
||
370 | *
|
||
371 | * @param id identifier of the button
|
||
372 | * @param b <code>true</code> to enable the button, otherwise <code>false</code>
|
||
373 | *
|
||
374 | * @return <code>true</code> if there was a button of that kind in this group, otherwise <code>false</code>
|
||
375 | */
|
||
376 | public boolean setEnabled(int id, boolean b) { |
||
377 | String b_text = getButtonText(id);
|
||
378 | |||
379 | Iterator<JButton> acIterator = buttonsList.iterator(); |
||
380 | while (acIterator.hasNext()) {
|
||
381 | JButton button = (JButton) acIterator.next(); |
||
382 | if (button.getText().compareTo(b_text) == 0) { |
||
383 | button.setEnabled(b); |
||
384 | return true; |
||
385 | } |
||
386 | } |
||
387 | |||
388 | return false; |
||
389 | } |
||
390 | } |