root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / buttonspanel / ButtonsPanel.java @ 10962
History | View | Annotate | Download (6.23 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.FlowLayout; |
22 |
import java.awt.event.ActionEvent; |
23 |
import java.awt.event.ActionListener; |
24 |
import java.util.ArrayList; |
25 |
import java.util.Iterator; |
26 |
|
27 |
import javax.swing.JButton; |
28 |
import javax.swing.JPanel; |
29 |
|
30 |
import org.gvsig.gui.beans.messages.Messages; |
31 |
/**
|
32 |
* <code>ButtonsPanel</code> ofrece un widget con un conjunto de botones
|
33 |
* preestablecidos, aunque tambi?n se pueden a?adir botones con el m?todo
|
34 |
* {@link #addButton(String, int)}
|
35 |
*
|
36 |
* @version 15/03/2007
|
37 |
* @author Borja Sanchez Zamorano (borja.sanchez@iver.es)
|
38 |
*/
|
39 |
public class ButtonsPanel extends JPanel { |
40 |
private static final long serialVersionUID = -7264041442217894577L; |
41 |
private ArrayList buttonsList = new ArrayList(); |
42 |
static private int eventId = Integer.MIN_VALUE; |
43 |
private ArrayList actionCommandListeners = new ArrayList(); |
44 |
|
45 |
public static final int BUTTON_ACCEPT = 1; |
46 |
public static final int BUTTON_CANCEL = 2; |
47 |
public static final int BUTTON_APPLY = 3; |
48 |
public static final int BUTTON_YES = 4; |
49 |
public static final int BUTTON_NO = 5; |
50 |
public static final int BUTTON_CLOSE = 6; |
51 |
public static final int BUTTON_EXIT = 7; |
52 |
public static final int BUTTON_SEEDETAILS = 8; |
53 |
public static final int BUTTON_HIDEDETAILS = 9; |
54 |
public static final int BUTTON_PAUSE = 10; |
55 |
public static final int BUTTON_RESTART = 11; |
56 |
|
57 |
public static final int BUTTONS_ACCEPT = 1; |
58 |
public static final int BUTTONS_ACCEPTCANCEL = 2; |
59 |
public static final int BUTTONS_ACCEPTCANCELAPPLY = 3; |
60 |
public static final int BUTTONS_CANCEL = 4; |
61 |
public static final int BUTTONS_YESNO = 5; |
62 |
public static final int BUTTONS_CLOSE = 6; |
63 |
public static final int BUTTONS_EXIT = 7; |
64 |
public static final int BUTTONS_NONE = 8; |
65 |
|
66 |
/**
|
67 |
* Crea un ButtonsPanel con un Layout por defecto.
|
68 |
*/
|
69 |
public ButtonsPanel() {
|
70 |
setLayout(new java.awt.FlowLayout(FlowLayout.RIGHT)); |
71 |
} |
72 |
|
73 |
/**
|
74 |
* Crea un ButtonsPanel con un Layout por defecto.
|
75 |
*
|
76 |
* @param items Que botones vamos a usar en la creaci?n.
|
77 |
*/
|
78 |
public ButtonsPanel(int items) { |
79 |
setLayout(new java.awt.FlowLayout(FlowLayout.RIGHT)); |
80 |
switch (items) {
|
81 |
case BUTTONS_ACCEPT:
|
82 |
addAccept(); |
83 |
break;
|
84 |
case BUTTONS_ACCEPTCANCEL:
|
85 |
addAccept(); |
86 |
addCancel(); |
87 |
break;
|
88 |
case BUTTONS_ACCEPTCANCELAPPLY:
|
89 |
addAccept(); |
90 |
addCancel(); |
91 |
addApply(); |
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 |
public void addActionListener(ActionListener listener) { |
112 |
if (!actionCommandListeners.contains(listener))
|
113 |
actionCommandListeners.add(listener); |
114 |
} |
115 |
|
116 |
public void removeActionListener(ActionListener listener) { |
117 |
actionCommandListeners.remove(listener); |
118 |
} |
119 |
|
120 |
private void callActionCommandListeners(String buttonID) { |
121 |
Iterator acIterator = actionCommandListeners.iterator();
|
122 |
while (acIterator.hasNext()) {
|
123 |
ActionListener listener = (ActionListener) acIterator.next(); |
124 |
listener.actionPerformed(new ActionEvent(this, eventId, buttonID)); |
125 |
} |
126 |
eventId++; |
127 |
} |
128 |
|
129 |
/**
|
130 |
* A?adir el boton Aceptar.
|
131 |
*/
|
132 |
public void addAccept() { |
133 |
addButton(Messages.getText("aceptar"), BUTTON_ACCEPT);
|
134 |
} |
135 |
|
136 |
/**
|
137 |
* A?adir el boton Cancelar.
|
138 |
*/
|
139 |
public void addCancel() { |
140 |
addButton(Messages.getText("cancelar"), BUTTON_CANCEL);
|
141 |
} |
142 |
|
143 |
/**
|
144 |
* A?adir el boton S?.
|
145 |
*/
|
146 |
public void addYes() { |
147 |
addButton(Messages.getText("si"), BUTTON_YES);
|
148 |
} |
149 |
|
150 |
/**
|
151 |
* A?adir el boton No.
|
152 |
*/
|
153 |
public void addNo() { |
154 |
addButton(Messages.getText("no"), BUTTON_NO);
|
155 |
} |
156 |
|
157 |
/**
|
158 |
* A?adir el boton Aplicar.
|
159 |
*/
|
160 |
public void addApply() { |
161 |
addButton(Messages.getText("aplicar"), BUTTON_APPLY);
|
162 |
} |
163 |
|
164 |
/**
|
165 |
* A?adir el boton Cerrar.
|
166 |
*/
|
167 |
public void addClose() { |
168 |
addButton(Messages.getText("cerrar"), BUTTON_CLOSE);
|
169 |
} |
170 |
|
171 |
/**
|
172 |
* A?adir el boton Salir.
|
173 |
*/
|
174 |
public void addExit() { |
175 |
addButton(Messages.getText("salir"), BUTTON_EXIT);
|
176 |
} |
177 |
|
178 |
/**
|
179 |
* A?adir el boton Ver detalles.
|
180 |
*/
|
181 |
public void addSeeDetails() { |
182 |
addButton(Messages.getText("verdetalles"), BUTTON_SEEDETAILS);
|
183 |
} |
184 |
|
185 |
/**
|
186 |
* A?adir el boton Ocultar detalles.
|
187 |
*/
|
188 |
public void addHideDetails() { |
189 |
addButton(Messages.getText("ocultardetalles"), BUTTON_HIDEDETAILS);
|
190 |
} |
191 |
|
192 |
/**
|
193 |
* A?adir el boton Pausar.
|
194 |
*/
|
195 |
public void addPause() { |
196 |
addButton(Messages.getText("pausar"), BUTTON_PAUSE);
|
197 |
} |
198 |
|
199 |
/**
|
200 |
* A?adir el boton Reanudar.
|
201 |
*/
|
202 |
public void addRestart() { |
203 |
addButton(Messages.getText("reanudar"), BUTTON_RESTART);
|
204 |
} |
205 |
|
206 |
/**
|
207 |
* A?adimos un bot?n definido por el usuario.
|
208 |
*
|
209 |
* @param text Texto que contendr? el bot?n
|
210 |
* @param id Entero para identificar los eventos del bot?n
|
211 |
*/
|
212 |
public void addButton(String text, int id) { |
213 |
JButton button = new JButton(); |
214 |
buttonsList.add(button); |
215 |
button.setText(text); |
216 |
button.setActionCommand(id + "");
|
217 |
button.addActionListener(new ActionListener() { |
218 |
public void actionPerformed(ActionEvent e) { |
219 |
callActionCommandListeners(e.getActionCommand()); |
220 |
} |
221 |
}); |
222 |
|
223 |
add(button); |
224 |
} |
225 |
|
226 |
/**
|
227 |
* Obtener un bot?n por su Entero
|
228 |
* @param id N?mero del disparador del bot?n
|
229 |
* @return El bot?n especificado o <code>null</code> si no se encontr? el bot?n.
|
230 |
*/
|
231 |
public JButton getButton(int id) { |
232 |
Iterator acIterator = buttonsList.iterator();
|
233 |
while (acIterator.hasNext()) {
|
234 |
JButton button = (JButton) acIterator.next(); |
235 |
if (button.getActionCommand().compareTo(id + "") == 0) |
236 |
return button;
|
237 |
} |
238 |
return null; |
239 |
} |
240 |
} |