root / tags / v2_0_0_Build_2020 / libraries / libUIComponent / src / org / gvsig / gui / beans / controls / combobutton / ComboButton.java @ 33964
History | View | Annotate | Download (9.46 KB)
1 | 18831 | bsanchez | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2005 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 | 13136 | evercher | package org.gvsig.gui.beans.controls.combobutton; |
20 | |||
21 | import java.awt.Color; |
||
22 | import java.awt.Graphics; |
||
23 | import java.awt.Graphics2D; |
||
24 | import java.awt.RenderingHints; |
||
25 | import java.awt.event.ActionEvent; |
||
26 | import java.awt.event.ActionListener; |
||
27 | import java.awt.event.MouseEvent; |
||
28 | import java.awt.event.MouseListener; |
||
29 | import java.awt.event.MouseMotionListener; |
||
30 | import java.awt.geom.GeneralPath; |
||
31 | 26787 | vcaballero | import java.util.ArrayList; |
32 | import java.util.Iterator; |
||
33 | 13136 | evercher | |
34 | import javax.swing.JButton; |
||
35 | import javax.swing.JMenuItem; |
||
36 | import javax.swing.JPopupMenu; |
||
37 | |||
38 | import org.gvsig.gui.beans.controls.IControl; |
||
39 | 18831 | bsanchez | /**
|
40 | 18976 | bsanchez | * Boton destinado a ser usado en un JToolBar que ofrece un desplegable de
|
41 | * items de menu.
|
||
42 | 18831 | bsanchez | *
|
43 | * @version 06/02/2008
|
||
44 | * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
||
45 | */
|
||
46 | public class ComboButton extends JButton implements IControl, MouseListener, MouseMotionListener, ActionListener { |
||
47 | private static final long serialVersionUID = -1412453774004951410L; |
||
48 | 18976 | bsanchez | private JPopupMenu popMenu = new JPopupMenu(); |
49 | private static int BORDER = 6; |
||
50 | private int offsetTrianglex = -4; |
||
51 | private int offsetTriangley = -4; |
||
52 | 26787 | vcaballero | private int triangleWidth = 8; |
53 | private int triangleHeight = 7; |
||
54 | 18976 | bsanchez | private boolean showToolTipText = true; |
55 | private boolean showMenuAlways = true; |
||
56 | 18994 | bsanchez | private boolean alwaysMenuOnClick = false; |
57 | 18976 | bsanchez | |
58 | 26787 | vcaballero | private ArrayList<ComboButtonListener> actionCommandListeners = new ArrayList<ComboButtonListener>(); |
59 | |||
60 | 18976 | bsanchez | /**
|
61 | * Indica si en la siguiente agregacion de un item ha de llevar un separador
|
||
62 | * previo
|
||
63 | */
|
||
64 | private boolean nextSeparator = false; |
||
65 | 13136 | evercher | |
66 | public ComboButton() {
|
||
67 | 18831 | bsanchez | addMouseListener(this);
|
68 | addMouseMotionListener(this);
|
||
69 | addActionListener(this);
|
||
70 | 13136 | evercher | } |
71 | 18831 | bsanchez | |
72 | 18976 | bsanchez | /**
|
73 | * @deprecated Mantego el metodo para posibles compatibilidades
|
||
74 | * @param mode
|
||
75 | * @return
|
||
76 | */
|
||
77 | 13136 | evercher | public boolean selectMode(int mode) { |
78 | String modeText = mode + ""; |
||
79 | |||
80 | 18831 | bsanchez | for (int i = 0; i < popMenu.getSubElements().length; i++) { |
81 | JMenuItem mi = (JMenuItem) popMenu.getSubElements()[i]; |
||
82 | 13136 | evercher | // found item for mode?
|
83 | if (mi.getActionCommand().equals(modeText)) {
|
||
84 | selectItem(mi); |
||
85 | return true; |
||
86 | } |
||
87 | } |
||
88 | return false; |
||
89 | } |
||
90 | |||
91 | private void selectItem(JMenuItem mi) { |
||
92 | 18831 | bsanchez | setIcon(mi.getIcon()); |
93 | setToolTipText(mi.getText()); |
||
94 | setActionCommand(mi.getActionCommand()); |
||
95 | 26787 | vcaballero | // requestFocus();
|
96 | 13136 | evercher | } |
97 | 18831 | bsanchez | |
98 | public void setSelectedItem(String actionCommand) { |
||
99 | for (int i = 0; i < popMenu.getSubElements().length; i++) { |
||
100 | if (((JMenuItem) popMenu.getSubElements()[i].getComponent()).getActionCommand().equals(actionCommand)) { |
||
101 | selectItem((JMenuItem) popMenu.getSubElements()[i].getComponent());
|
||
102 | break;
|
||
103 | } |
||
104 | } |
||
105 | 26787 | vcaballero | // popMenu.requestFocus();
|
106 | 18831 | bsanchez | } |
107 | 13136 | evercher | |
108 | 18976 | bsanchez | /**
|
109 | * Borra todos los items de la lista desplegable
|
||
110 | */
|
||
111 | 18831 | bsanchez | public void clearButtons() { |
112 | popMenu.removeAll(); |
||
113 | } |
||
114 | |||
115 | 18976 | bsanchez | /**
|
116 | * A?ade un JMenuItem al menu desplegable
|
||
117 | * @param menu
|
||
118 | */
|
||
119 | 13136 | evercher | public void addButton(JButton menu) { |
120 | JMenuItem mi = new JMenuItem(); |
||
121 | mi.setText(menu.getText()); |
||
122 | mi.setIcon(menu.getIcon()); |
||
123 | 18976 | bsanchez | mi.setEnabled(menu.isEnabled()); |
124 | 13136 | evercher | mi.setActionCommand(menu.getActionCommand()); |
125 | 18831 | bsanchez | mi.addActionListener(this);
|
126 | 18976 | bsanchez | |
127 | if (nextSeparator) {
|
||
128 | popMenu.addSeparator(); |
||
129 | nextSeparator = false;
|
||
130 | } |
||
131 | |||
132 | 13136 | evercher | popMenu.add(mi); |
133 | |||
134 | 18831 | bsanchez | if (popMenu.getSubElements().length == 1) { |
135 | 13136 | evercher | // init tbutton
|
136 | 18831 | bsanchez | setIcon(menu.getIcon()); |
137 | setActionCommand(menu.getActionCommand()); |
||
138 | setToolTipText(menu.getText()); |
||
139 | 13136 | evercher | } |
140 | } |
||
141 | |||
142 | 18976 | bsanchez | /**
|
143 | * Indica que en la siguiente agregaci?n de un item al menu ha de llevar un
|
||
144 | * separador
|
||
145 | */
|
||
146 | 13136 | evercher | public void addSeparator() { |
147 | 18976 | bsanchez | nextSeparator = true;
|
148 | 13136 | evercher | } |
149 | |||
150 | 18976 | bsanchez | /**
|
151 | * Muestra/Oculta el menu
|
||
152 | * @param flag
|
||
153 | */
|
||
154 | 13136 | evercher | public void setPopupVisible(boolean flag) { |
155 | if (flag) {
|
||
156 | 18831 | bsanchez | if (popMenu.isShowing())
|
157 | return;
|
||
158 | popMenu.show(this, 0, getHeight()); |
||
159 | 13136 | evercher | } else {
|
160 | popMenu.setVisible(false);
|
||
161 | } |
||
162 | } |
||
163 | |||
164 | public void setAction(ActionEvent action) { |
||
165 | 18831 | bsanchez | for (int i = 0; i < popMenu.getSubElements().length; i++) { |
166 | if (((JMenuItem) popMenu.getSubElements()[i].getComponent()).getActionCommand().equals(action.getActionCommand())) { |
||
167 | ((JMenuItem) popMenu.getSubElements()[i].getComponent()).doClick();
|
||
168 | 13136 | evercher | } |
169 | } |
||
170 | } |
||
171 | |||
172 | 18831 | bsanchez | /*
|
173 | * (non-Javadoc)
|
||
174 | * @see org.gvsig.gui.beans.controls.IControl#setValue(java.lang.Object)
|
||
175 | 13136 | evercher | */
|
176 | public Object setValue(Object value) { |
||
177 | return value;
|
||
178 | } |
||
179 | |||
180 | 18831 | bsanchez | /*
|
181 | * (non-Javadoc)
|
||
182 | * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
|
||
183 | */
|
||
184 | 13136 | evercher | public void actionPerformed(ActionEvent e) { |
185 | 18831 | bsanchez | for (int i = 0; i < popMenu.getSubElements().length; i++) { |
186 | if (e.getSource() instanceof JMenuItem && popMenu.getSubElements()[i] == e.getSource()) { |
||
187 | selectItem((JMenuItem) e.getSource());
|
||
188 | 26787 | vcaballero | callComboButtonClickedListeners(); |
189 | 18831 | bsanchez | return;
|
190 | } |
||
191 | } |
||
192 | 13136 | evercher | } |
193 | |||
194 | 18831 | bsanchez | /*
|
195 | * (non-Javadoc)
|
||
196 | * @see javax.swing.JComponent#paint(java.awt.Graphics)
|
||
197 | */
|
||
198 | 13136 | evercher | public void paint(Graphics g) { |
199 | 18831 | bsanchez | super.paint(g);
|
200 | |||
201 | 13136 | evercher | Graphics2D g2 = (Graphics2D) g; |
202 | 18976 | bsanchez | if (showMenuAlways || popMenu.getSubElements().length > 1) { |
203 | 18831 | bsanchez | g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
204 | // draw little arrow (for popup menu)
|
||
205 | drawTriangle(g2); |
||
206 | } |
||
207 | } |
||
208 | 13136 | evercher | |
209 | 18831 | bsanchez | private void drawTriangle(Graphics2D g2) { |
210 | GeneralPath gp = new GeneralPath(); |
||
211 | 13136 | evercher | |
212 | 18831 | bsanchez | int x = 0; |
213 | if (offsetTrianglex < 0) |
||
214 | x = this.getWidth() + offsetTrianglex - BORDER;
|
||
215 | else
|
||
216 | x = offsetTrianglex + BORDER; |
||
217 | 13136 | evercher | |
218 | 18831 | bsanchez | int y = 0; |
219 | if (offsetTriangley < 0) |
||
220 | y = this.getHeight() + offsetTriangley - BORDER;
|
||
221 | else
|
||
222 | y = offsetTriangley + BORDER; |
||
223 | 13136 | evercher | |
224 | 18831 | bsanchez | gp.moveTo(x, y); |
225 | gp.lineTo(x + triangleWidth, y); |
||
226 | gp.lineTo(x + (triangleWidth / 2), y + triangleHeight);
|
||
227 | 13136 | evercher | gp.closePath(); |
228 | 18831 | bsanchez | |
229 | g2.setColor(Color.white);
|
||
230 | g2.fill(gp); |
||
231 | g2.setColor(new Color(0, 0, 0, 130)); |
||
232 | g2.draw(gp); |
||
233 | 13136 | evercher | } |
234 | |||
235 | private boolean popupTriangleClicked(int x, int y) { |
||
236 | 18994 | bsanchez | if (alwaysMenuOnClick)
|
237 | return true; |
||
238 | |||
239 | 18976 | bsanchez | if (!showMenuAlways && (popMenu.getSubElements().length <= 1)) |
240 | 18831 | bsanchez | return false; |
241 | 13136 | evercher | |
242 | 18831 | bsanchez | if (offsetTrianglex < 0) { |
243 | if (x < (this.getWidth() - BORDER + offsetTrianglex)) |
||
244 | return false; |
||
245 | } else {
|
||
246 | if (x > (BORDER + offsetTrianglex + triangleWidth))
|
||
247 | return false; |
||
248 | } |
||
249 | |||
250 | if (offsetTriangley < 0) { |
||
251 | if (y < (this.getHeight() - BORDER + offsetTriangley)) |
||
252 | return false; |
||
253 | } else {
|
||
254 | if (y > (BORDER + offsetTriangley + triangleHeight))
|
||
255 | return false; |
||
256 | } |
||
257 | 13136 | evercher | |
258 | 18831 | bsanchez | return true; |
259 | 13136 | evercher | } |
260 | |||
261 | 18831 | bsanchez | /*
|
262 | * (non-Javadoc)
|
||
263 | * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
|
||
264 | */
|
||
265 | 13136 | evercher | public void mousePressed(MouseEvent e) { |
266 | 18831 | bsanchez | boolean showPopup = popupTriangleClicked(e.getX(), e.getY());
|
267 | 26787 | vcaballero | if (showPopup)
|
268 | setPopupVisible(showPopup); |
||
269 | else
|
||
270 | callComboButtonClickedListeners(); |
||
271 | 13136 | evercher | } |
272 | 26787 | vcaballero | |
273 | /**
|
||
274 | * A?adir un listener a la lista de eventos
|
||
275 | * @param listener
|
||
276 | */
|
||
277 | public void addComboButtonClickedListener(ComboButtonListener listener) { |
||
278 | if (!actionCommandListeners.contains(listener))
|
||
279 | actionCommandListeners.add(listener); |
||
280 | } |
||
281 | 13136 | evercher | |
282 | 26787 | vcaballero | /**
|
283 | * Borrar un listener de la lista de eventos
|
||
284 | * @param listener
|
||
285 | */
|
||
286 | public void removeComboButtonClickedListener(ComboButtonListener listener) { |
||
287 | actionCommandListeners.remove(listener); |
||
288 | } |
||
289 | |||
290 | /**
|
||
291 | * Invocar a los eventos asociados al componente
|
||
292 | */
|
||
293 | private void callComboButtonClickedListeners() { |
||
294 | Iterator<ComboButtonListener> acIterator = actionCommandListeners.iterator();
|
||
295 | while (acIterator.hasNext()) {
|
||
296 | ComboButtonListener listener = acIterator.next(); |
||
297 | listener.actionComboButtonClicked(new ComboButtonEvent(this)); |
||
298 | } |
||
299 | } |
||
300 | |||
301 | 18831 | bsanchez | /*
|
302 | * (non-Javadoc)
|
||
303 | * @see javax.swing.JComponent#getToolTipText()
|
||
304 | */
|
||
305 | public String getToolTipText() { |
||
306 | if (showToolTipText)
|
||
307 | return super.getToolTipText(); |
||
308 | return null; |
||
309 | 13136 | evercher | } |
310 | 18831 | bsanchez | |
311 | /*
|
||
312 | * (non-Javadoc)
|
||
313 | * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
|
||
314 | */
|
||
315 | 13136 | evercher | public void mouseDragged(MouseEvent e) { |
316 | if (popupTriangleClicked(e.getX(), e.getY()))
|
||
317 | 18831 | bsanchez | setPopupVisible(true);
|
318 | 13136 | evercher | } |
319 | |||
320 | 18831 | bsanchez | /*
|
321 | * (non-Javadoc)
|
||
322 | * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
|
||
323 | */
|
||
324 | 13136 | evercher | public void mouseMoved(MouseEvent e) { |
325 | 18831 | bsanchez | showToolTipText = !popMenu.isShowing(); |
326 | 13136 | evercher | } |
327 | |||
328 | 18976 | bsanchez | /**
|
329 | * Devuelve si esta visible siempre
|
||
330 | * @return the showMenuAlways
|
||
331 | */
|
||
332 | public boolean isShowMenuAlways() { |
||
333 | return showMenuAlways;
|
||
334 | } |
||
335 | |||
336 | /**
|
||
337 | * Especifica si el menu se ha de visualizar siempre o solo cuando haya mas de
|
||
338 | * un item
|
||
339 | * @param showMenuAlways the showMenuAlways to set
|
||
340 | */
|
||
341 | public void setShowMenuAlways(boolean showMenuAlways) { |
||
342 | this.showMenuAlways = showMenuAlways;
|
||
343 | } |
||
344 | |||
345 | 26787 | vcaballero | public void mouseReleased(MouseEvent e) { |
346 | |||
347 | } |
||
348 | 18831 | bsanchez | public void mouseClicked(MouseEvent e) {} |
349 | public void mouseEntered(MouseEvent arg0) {} |
||
350 | public void mouseExited(MouseEvent arg0) {} |
||
351 | 18994 | bsanchez | |
352 | /**
|
||
353 | * @return the alwaysMenuOnClick
|
||
354 | */
|
||
355 | public boolean isAlwaysMenuOnClick() { |
||
356 | return alwaysMenuOnClick;
|
||
357 | } |
||
358 | |||
359 | /**
|
||
360 | * @param alwaysMenuOnClick the alwaysMenuOnClick to set
|
||
361 | */
|
||
362 | public void setAlwaysMenuOnClick(boolean alwaysMenuOnClick) { |
||
363 | this.alwaysMenuOnClick = alwaysMenuOnClick;
|
||
364 | } |
||
365 | 18831 | bsanchez | } |