Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libUIComponent / src / org / gvsig / gui / beans / controls / combobutton / ComboButton.java @ 38564

History | View | Annotate | Download (9.51 KB)

1
/* 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
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
import java.util.ArrayList;
32
import java.util.Iterator;
33

    
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
/**
40
 * Boton destinado a ser usado en un JToolBar que ofrece un desplegable de 
41
 * items de menu.
42
 *
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
        private JPopupMenu popMenu         = new JPopupMenu();
49
        private static int BORDER          = 6;
50
        private int        offsetTrianglex = -4;
51
        private int        offsetTriangley = -4;
52
        private int        triangleWidth   = 8;
53
        private int        triangleHeight  = 7;
54
        private boolean    showToolTipText = true;
55
        private boolean    showMenuAlways  = true;
56
        private boolean    alwaysMenuOnClick = false;
57
        
58
        private ArrayList<ComboButtonListener> actionCommandListeners = new ArrayList<ComboButtonListener>();
59
        
60
        /**
61
         * Indica si en la siguiente agregacion de un item ha de llevar un separador
62
         * previo
63
         */
64
        private boolean nextSeparator = false;
65

    
66
        public ComboButton() {
67
                addMouseListener(this);
68
                addMouseMotionListener(this);
69
                addActionListener(this);
70
        }
71
        
72
        /**
73
         * @deprecated Mantego el metodo para posibles compatibilidades
74
         * @param mode
75
         * @return
76
         */
77
        public boolean selectMode(int mode) {
78
                String modeText = mode + "";
79

    
80
                for (int i = 0; i < popMenu.getSubElements().length; i++) {
81
                        JMenuItem mi = (JMenuItem) popMenu.getSubElements()[i];
82
                        // 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
                setIcon(mi.getIcon());
93
                setToolTipText(mi.getText());
94
                setActionCommand(mi.getActionCommand());
95
//                requestFocus();
96
        }
97
        
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
//                popMenu.requestFocus();
106
        }
107

    
108
        /**
109
         * Borra todos los items de la lista desplegable
110
         */
111
        public void clearButtons() {
112
                popMenu.removeAll();
113
        }
114
        
115
        /**
116
         * A?ade un JMenuItem al menu desplegable
117
         * @param menu
118
         */
119
        public void addButton(JButton menu) {
120
                JMenuItem mi = new JMenuItem();
121
                mi.setText(menu.getText());
122
                mi.setIcon(menu.getIcon());
123
                mi.setEnabled(menu.isEnabled());
124
                mi.setActionCommand(menu.getActionCommand());
125
                mi.addActionListener(this);
126
                
127
                if (nextSeparator) {
128
                        popMenu.addSeparator();
129
                        nextSeparator = false;
130
                }
131

    
132
                popMenu.add(mi);
133

    
134
                if (popMenu.getSubElements().length == 1) {
135
                        // init tbutton
136
                        setIcon(menu.getIcon());
137
                        setActionCommand(menu.getActionCommand());
138
                        setToolTipText(menu.getText());
139
                }
140
        }
141

    
142
        /**
143
         * Indica que en la siguiente agregaci?n de un item al menu ha de llevar un
144
         * separador
145
         */
146
        public void addSeparator() {
147
                nextSeparator = true;
148
        }
149

    
150
        /**
151
         * Muestra/Oculta el menu
152
         * @param flag
153
         */
154
        public void setPopupVisible(boolean flag) {
155
                if (flag) {
156
                        if (popMenu.isShowing())
157
                                return;
158
                        popMenu.show(this, 0, getHeight());
159
                } else {
160
                        popMenu.setVisible(false);
161
                }
162
        }
163

    
164
        public void setAction(ActionEvent action) {
165
                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
                        }
169
                }
170
        }
171

    
172
        /*
173
         * (non-Javadoc)
174
         * @see org.gvsig.gui.beans.controls.IControl#setValue(java.lang.Object)
175
         */
176
        public Object setValue(Object value) {
177
                return value;
178
        }
179

    
180
        /*
181
         * (non-Javadoc)
182
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
183
         */
184
        public void actionPerformed(ActionEvent e) {
185
                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
                                callComboButtonClickedListeners();
189
                                return;
190
                        }
191
                }
192
        }
193

    
194
        /*
195
         * (non-Javadoc)
196
         * @see javax.swing.JComponent#paint(java.awt.Graphics)
197
         */
198
        public void paint(Graphics g) {
199
                super.paint(g);
200

    
201
                Graphics2D g2 = (Graphics2D) g;
202
                if (showMenuAlways || popMenu.getSubElements().length > 1) {
203
                        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
204
                        // draw little arrow (for popup menu)
205
                        drawTriangle(g2);
206
                }
207
        }
208

    
209
        private void drawTriangle(Graphics2D g2) {
210
                GeneralPath gp = new GeneralPath();
211

    
212
                int x = 0;
213
                if (offsetTrianglex < 0)
214
                        x = this.getWidth() + offsetTrianglex - BORDER;
215
                else
216
                        x = offsetTrianglex + BORDER;
217

    
218
                int y = 0;
219
                if (offsetTriangley < 0)
220
                        y = this.getHeight() + offsetTriangley - BORDER;
221
                else
222
                        y = offsetTriangley + BORDER;
223

    
224
                gp.moveTo(x, y);
225
                gp.lineTo(x + triangleWidth, y);
226
                gp.lineTo(x + (triangleWidth / 2), y + triangleHeight);
227
                gp.closePath();
228

    
229
                g2.setColor(Color.white);
230
                g2.fill(gp);
231
                g2.setColor(new Color(0, 0, 0, 130));
232
                g2.draw(gp);
233
        }
234

    
235
        private boolean popupTriangleClicked(int x, int y) {
236
                if (alwaysMenuOnClick)
237
                        return true;
238

    
239
                if (!showMenuAlways && (popMenu.getSubElements().length <= 1))
240
                        return false;
241

    
242
                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

    
258
                return true;
259
        }
260

    
261
        /*
262
         * (non-Javadoc)
263
         * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
264
         */
265
        public void mousePressed(MouseEvent e) {
266
                boolean showPopup = popupTriangleClicked(e.getX(), e.getY());
267
                if (showPopup)
268
                        setPopupVisible(showPopup);
269
                else
270
                        callComboButtonClickedListeners();
271
        }
272
        
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

    
282
        /**
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
        /*
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
        }
310
        
311
        /*
312
         * (non-Javadoc)
313
         * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
314
         */
315
        public void mouseDragged(MouseEvent e) {
316
                if (popupTriangleClicked(e.getX(), e.getY()))
317
                        setPopupVisible(true);
318
        }
319

    
320
        /*
321
         * (non-Javadoc)
322
         * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
323
         */
324
        public void mouseMoved(MouseEvent e) {
325
                showToolTipText = !popMenu.isShowing();
326
        }
327

    
328
        /**
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
        public void mouseReleased(MouseEvent e) {
346
                
347
        }
348
        public void mouseClicked(MouseEvent e) {}
349
        public void mouseEntered(MouseEvent arg0) {}
350
        public void mouseExited(MouseEvent arg0) {}
351

    
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
        
366
        public Object getValue() {
367
                return null;
368
        }
369
}