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