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 @ 1743
History | View | Annotate | Download (8.73 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 |
static private int eventId = Integer.MIN_VALUE; |
56 |
|
57 |
private JComponent parent = null; |
58 |
|
59 |
/**
|
60 |
* Crea un ButtonsPanel con un Layout por defecto.
|
61 |
*/
|
62 |
public ButtonsPanel(JComponent parent) { |
63 |
this.parent = parent;
|
64 |
initialize(); |
65 |
} |
66 |
|
67 |
private void initialize() { |
68 |
setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT, 2, 0)); |
69 |
setBorder(javax.swing.BorderFactory.createEmptyBorder(4, 0, 0, 0)); |
70 |
} |
71 |
/**
|
72 |
* Crea un ButtonsPanel con un Layout por defecto.
|
73 |
* @param items Que botones vamos a usar en la creaci?n.
|
74 |
*/
|
75 |
public ButtonsPanel(int items, JComponent parent) { |
76 |
this.parent = parent;
|
77 |
initialize(); |
78 |
switch (items) {
|
79 |
case BUTTONS_ACCEPT:
|
80 |
addAccept(); |
81 |
break;
|
82 |
case BUTTONS_ACCEPTCANCEL:
|
83 |
addAccept(); |
84 |
addCancel(); |
85 |
break;
|
86 |
case BUTTONS_ACCEPTCANCELAPPLY:
|
87 |
addApply(); |
88 |
addAccept(); |
89 |
addCancel(); |
90 |
break;
|
91 |
case BUTTONS_APPLYCLOSE:
|
92 |
addApply(); |
93 |
addClose(); |
94 |
break;
|
95 |
case BUTTONS_CANCEL:
|
96 |
addCancel(); |
97 |
break;
|
98 |
case BUTTONS_YESNO:
|
99 |
addYes(); |
100 |
addNo(); |
101 |
break;
|
102 |
case BUTTONS_CLOSE:
|
103 |
addClose(); |
104 |
break;
|
105 |
case BUTTONS_EXIT:
|
106 |
addExit(); |
107 |
break;
|
108 |
case BUTTONS_NONE:
|
109 |
break;
|
110 |
} |
111 |
} |
112 |
|
113 |
/**
|
114 |
* A?adir el disparador de cuando se pulsa un bot?n.
|
115 |
* @param listener
|
116 |
*/
|
117 |
public void addButtonPressedListener(ButtonsPanelListener listener) { |
118 |
if (!actionCommandListeners.contains(listener))
|
119 |
actionCommandListeners.add(listener); |
120 |
} |
121 |
|
122 |
/**
|
123 |
* Devuelve el array de listeners del componente
|
124 |
* @return
|
125 |
*/
|
126 |
public Object[] getButtonPressedListeners() { |
127 |
return actionCommandListeners.toArray();
|
128 |
} |
129 |
|
130 |
/**
|
131 |
* Borrar el disparador de eventos de los botones.
|
132 |
* @param listener
|
133 |
*/
|
134 |
public void removeButtonPressedListener(ButtonsPanelListener listener) { |
135 |
actionCommandListeners.remove(listener); |
136 |
} |
137 |
|
138 |
private void callActionCommandListeners(int buttonID) { |
139 |
Iterator<ButtonsPanelListener> acIterator = actionCommandListeners.iterator();
|
140 |
while (acIterator.hasNext()) {
|
141 |
ButtonsPanelListener listener = (ButtonsPanelListener) acIterator.next(); |
142 |
listener.actionButtonPressed(new ButtonsPanelEvent(parent, buttonID));
|
143 |
} |
144 |
eventId++; |
145 |
} |
146 |
|
147 |
/**
|
148 |
* A?adir el boton Aceptar.
|
149 |
*/
|
150 |
public void addAccept() { |
151 |
addButton(Messages.getText("aceptar"), BUTTON_ACCEPT);
|
152 |
} |
153 |
|
154 |
/**
|
155 |
* A?adir el boton Guardar.
|
156 |
*/
|
157 |
public void addSave() { |
158 |
addButton(Messages.getText("guardar"), BUTTON_SAVE);
|
159 |
} |
160 |
|
161 |
/**
|
162 |
* A?adir el boton Cancelar.
|
163 |
*/
|
164 |
public void addCancel() { |
165 |
addButton(Messages.getText("cancelar"), BUTTON_CANCEL);
|
166 |
} |
167 |
|
168 |
/**
|
169 |
* A?adir el boton S?.
|
170 |
*/
|
171 |
public void addYes() { |
172 |
addButton(Messages.getText("si"), BUTTON_YES);
|
173 |
} |
174 |
|
175 |
/**
|
176 |
* A?adir el boton No.
|
177 |
*/
|
178 |
public void addNo() { |
179 |
addButton(Messages.getText("no"), BUTTON_NO);
|
180 |
} |
181 |
|
182 |
/**
|
183 |
* A?adir el boton Aplicar.
|
184 |
*/
|
185 |
public void addApply() { |
186 |
addButton(Messages.getText("aplicar"), BUTTON_APPLY);
|
187 |
} |
188 |
|
189 |
/**
|
190 |
* A?adir el boton Cerrar.
|
191 |
*/
|
192 |
public void addClose() { |
193 |
addButton(Messages.getText("cerrar"), BUTTON_CLOSE);
|
194 |
} |
195 |
|
196 |
/**
|
197 |
* A?adir el boton Salir.
|
198 |
*/
|
199 |
public void addExit() { |
200 |
addButton(Messages.getText("salir"), BUTTON_EXIT);
|
201 |
} |
202 |
|
203 |
/**
|
204 |
* A?adir el boton Ver detalles.
|
205 |
*/
|
206 |
public void addSeeDetails() { |
207 |
addButton(Messages.getText("verdetalles"), BUTTON_SEEDETAILS);
|
208 |
} |
209 |
|
210 |
/**
|
211 |
* A?adir el boton Ocultar detalles.
|
212 |
*/
|
213 |
public void addHideDetails() { |
214 |
addButton(Messages.getText("ocultardetalles"), BUTTON_HIDEDETAILS);
|
215 |
} |
216 |
|
217 |
/**
|
218 |
* A?adir el boton Pausar.
|
219 |
*/
|
220 |
public void addPause() { |
221 |
addButton(Messages.getText("pausar"), BUTTON_PAUSE);
|
222 |
} |
223 |
|
224 |
/**
|
225 |
* A?adir el boton Reanudar.
|
226 |
*/
|
227 |
public void addRestart() { |
228 |
addButton(Messages.getText("reanudar"), BUTTON_RESTART);
|
229 |
} |
230 |
|
231 |
/**
|
232 |
* A?adimos un bot?n definido por el usuario.
|
233 |
*
|
234 |
* @param text Texto que contendr? el bot?n
|
235 |
* @param id Entero para identificar los eventos del bot?n
|
236 |
*/
|
237 |
public void addButton(String text, int id) { |
238 |
JButton button = new JButton(); |
239 |
button.setText(text); |
240 |
|
241 |
buttonsList.add(button); |
242 |
button.setActionCommand(id + "");
|
243 |
button.addActionListener(new ActionListener() { |
244 |
public void actionPerformed(ActionEvent e) { |
245 |
callActionCommandListeners(Integer.parseInt(e.getActionCommand()));
|
246 |
} |
247 |
}); |
248 |
|
249 |
add(button); |
250 |
} |
251 |
|
252 |
/**
|
253 |
* Obtener un bot?n por su Entero
|
254 |
* @param id N?mero del disparador del bot?n
|
255 |
* @return El bot?n especificado o <code>null</code> si no se encontr? el bot?n.
|
256 |
*/
|
257 |
public JButton getButton(int id) { |
258 |
Iterator<JButton> acIterator = buttonsList.iterator(); |
259 |
while (acIterator.hasNext()) {
|
260 |
JButton button = (JButton) acIterator.next(); |
261 |
if (Integer.parseInt(button.getActionCommand()) == id) |
262 |
return button;
|
263 |
} |
264 |
return null; |
265 |
} |
266 |
|
267 |
/**
|
268 |
* <p>Removes the button identified by <code>id</code>.</p>
|
269 |
*
|
270 |
* @param id identifier of the button
|
271 |
* @return <code>true</code> if has removed the button; otherwise <code>false</code>
|
272 |
*/
|
273 |
public boolean removeButton(int id) { |
274 |
String b_text = getButtonText(id);
|
275 |
|
276 |
Iterator<JButton> acIterator = buttonsList.iterator(); |
277 |
while (acIterator.hasNext()) {
|
278 |
JButton button = (JButton) acIterator.next(); |
279 |
if (button.getText().compareTo(b_text) == 0) { |
280 |
buttonsList.remove(button); |
281 |
return true; |
282 |
} |
283 |
} |
284 |
|
285 |
return false; |
286 |
} |
287 |
|
288 |
/**
|
289 |
* <p>Returns the text of the button identified by <code>id</code>.</p>
|
290 |
*
|
291 |
* @param id identifier of the button
|
292 |
*
|
293 |
* @return text of the identified button
|
294 |
*/
|
295 |
public String getButtonText(int id) { |
296 |
switch (id) {
|
297 |
case BUTTON_ACCEPT:
|
298 |
return Messages.getText("aceptar"); |
299 |
case BUTTON_CANCEL:
|
300 |
return Messages.getText("cancelar"); |
301 |
case BUTTON_APPLY:
|
302 |
return Messages.getText("aplicar"); |
303 |
case BUTTON_YES:
|
304 |
return Messages.getText("si"); |
305 |
case BUTTON_NO:
|
306 |
return Messages.getText("no"); |
307 |
case BUTTON_CLOSE:
|
308 |
return Messages.getText("cerrar"); |
309 |
case BUTTON_EXIT:
|
310 |
return Messages.getText("salir"); |
311 |
case BUTTON_SEEDETAILS:
|
312 |
return Messages.getText("verdetalles"); |
313 |
case BUTTON_HIDEDETAILS:
|
314 |
return Messages.getText("ocultardetalles"); |
315 |
case BUTTON_PAUSE:
|
316 |
return Messages.getText("pausar"); |
317 |
case BUTTON_RESTART:
|
318 |
return Messages.getText("reanudar"); |
319 |
case BUTTON_SAVE:
|
320 |
return Messages.getText("guardar"); |
321 |
} |
322 |
|
323 |
return null; |
324 |
} |
325 |
|
326 |
/**
|
327 |
* <p>Enables (or disables) the button identified by <code>id</code>.</p>
|
328 |
*
|
329 |
* @param id identifier of the button
|
330 |
* @param b <code>true</code> to enable the button, otherwise <code>false</code>
|
331 |
*
|
332 |
* @return <code>true</code> if there was a button of that kind in this group, otherwise <code>false</code>
|
333 |
*/
|
334 |
public boolean setEnabled(int id, boolean b) { |
335 |
String b_text = getButtonText(id);
|
336 |
|
337 |
Iterator<JButton> acIterator = buttonsList.iterator(); |
338 |
while (acIterator.hasNext()) {
|
339 |
JButton button = (JButton) acIterator.next(); |
340 |
if (button.getText().compareTo(b_text) == 0) { |
341 |
button.setEnabled(b); |
342 |
return true; |
343 |
} |
344 |
} |
345 |
|
346 |
return false; |
347 |
} |
348 |
} |