Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / controls / combobutton / ComboButton.java @ 40561

History | View | Annotate | Download (9.61 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 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 3
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.gui.beans.controls.combobutton;
25

    
26
import java.awt.Color;
27
import java.awt.Graphics;
28
import java.awt.Graphics2D;
29
import java.awt.RenderingHints;
30
import java.awt.event.ActionEvent;
31
import java.awt.event.ActionListener;
32
import java.awt.event.MouseEvent;
33
import java.awt.event.MouseListener;
34
import java.awt.event.MouseMotionListener;
35
import java.awt.geom.GeneralPath;
36
import java.util.ArrayList;
37
import java.util.Iterator;
38

    
39
import javax.swing.JButton;
40
import javax.swing.JMenuItem;
41
import javax.swing.JPopupMenu;
42

    
43
import org.gvsig.gui.beans.controls.IControl;
44
/**
45
 * Boton destinado a ser usado en un JToolBar que ofrece un desplegable de 
46
 * items de menu.
47
 *
48
 * @version 06/02/2008
49
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
50
 */
51
public class ComboButton extends JButton implements IControl, MouseListener, MouseMotionListener, ActionListener {
52
        private static final long serialVersionUID = -1412453774004951410L;
53
        private JPopupMenu popMenu         = new JPopupMenu();
54
        private static int BORDER          = 6;
55
        private int        offsetTrianglex = -4;
56
        private int        offsetTriangley = -4;
57
        private int        triangleWidth   = 8;
58
        private int        triangleHeight  = 7;
59
        private boolean    showToolTipText = true;
60
        private boolean    showMenuAlways  = true;
61
        private boolean    alwaysMenuOnClick = false;
62
        
63
        private ArrayList<ComboButtonListener> actionCommandListeners = new ArrayList<ComboButtonListener>();
64
        
65
        /**
66
         * Indica si en la siguiente agregacion de un item ha de llevar un separador
67
         * previo
68
         */
69
        private boolean nextSeparator = false;
70

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

    
85
                for (int i = 0; i < popMenu.getSubElements().length; i++) {
86
                        JMenuItem mi = (JMenuItem) popMenu.getSubElements()[i];
87
                        // found item for mode?
88
                        if (mi.getActionCommand().equals(modeText)) {
89
                                selectItem(mi);
90
                                return true;
91
                        }
92
                }
93
                return false;
94
        }
95

    
96
        private void selectItem(JMenuItem mi) {
97
                setIcon(mi.getIcon());
98
                setToolTipText(mi.getText());
99
                setActionCommand(mi.getActionCommand());
100
//                requestFocus();
101
        }
102
        
103
        public void setSelectedItem(String actionCommand) {
104
                for (int i = 0; i < popMenu.getSubElements().length; i++) {
105
                        if (((JMenuItem) popMenu.getSubElements()[i].getComponent()).getActionCommand().equals(actionCommand)) {
106
                                selectItem((JMenuItem) popMenu.getSubElements()[i].getComponent());
107
                                break;
108
                        }
109
                }
110
//                popMenu.requestFocus();
111
        }
112

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

    
137
                popMenu.add(mi);
138

    
139
                if (popMenu.getSubElements().length == 1) {
140
                        // init tbutton
141
                        setIcon(menu.getIcon());
142
                        setActionCommand(menu.getActionCommand());
143
                        setToolTipText(menu.getText());
144
                }
145
        }
146

    
147
        /**
148
         * Indica que en la siguiente agregaci?n de un item al menu ha de llevar un
149
         * separador
150
         */
151
        public void addSeparator() {
152
                nextSeparator = true;
153
        }
154

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

    
169
        public void setAction(ActionEvent action) {
170
                for (int i = 0; i < popMenu.getSubElements().length; i++) {
171
                        if (((JMenuItem) popMenu.getSubElements()[i].getComponent()).getActionCommand().equals(action.getActionCommand())) {
172
                                ((JMenuItem) popMenu.getSubElements()[i].getComponent()).doClick();
173
                        }
174
                }
175
        }
176

    
177
        /*
178
         * (non-Javadoc)
179
         * @see org.gvsig.gui.beans.controls.IControl#setValue(java.lang.Object)
180
         */
181
        public Object setValue(Object value) {
182
                return value;
183
        }
184

    
185
        /*
186
         * (non-Javadoc)
187
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
188
         */
189
        public void actionPerformed(ActionEvent e) {
190
                for (int i = 0; i < popMenu.getSubElements().length; i++) {
191
                        if (e.getSource() instanceof JMenuItem && popMenu.getSubElements()[i] == e.getSource()) {
192
                                selectItem((JMenuItem) e.getSource());
193
                                callComboButtonClickedListeners();
194
                                return;
195
                        }
196
                }
197
        }
198

    
199
        /*
200
         * (non-Javadoc)
201
         * @see javax.swing.JComponent#paint(java.awt.Graphics)
202
         */
203
        public void paint(Graphics g) {
204
                super.paint(g);
205

    
206
                Graphics2D g2 = (Graphics2D) g;
207
                if (showMenuAlways || popMenu.getSubElements().length > 1) {
208
                        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
209
                        // draw little arrow (for popup menu)
210
                        drawTriangle(g2);
211
                }
212
        }
213

    
214
        private void drawTriangle(Graphics2D g2) {
215
                GeneralPath gp = new GeneralPath();
216

    
217
                int x = 0;
218
                if (offsetTrianglex < 0)
219
                        x = this.getWidth() + offsetTrianglex - BORDER;
220
                else
221
                        x = offsetTrianglex + BORDER;
222

    
223
                int y = 0;
224
                if (offsetTriangley < 0)
225
                        y = this.getHeight() + offsetTriangley - BORDER;
226
                else
227
                        y = offsetTriangley + BORDER;
228

    
229
                gp.moveTo(x, y);
230
                gp.lineTo(x + triangleWidth, y);
231
                gp.lineTo(x + (triangleWidth / 2), y + triangleHeight);
232
                gp.closePath();
233

    
234
                g2.setColor(Color.white);
235
                g2.fill(gp);
236
                g2.setColor(new Color(0, 0, 0, 130));
237
                g2.draw(gp);
238
        }
239

    
240
        private boolean popupTriangleClicked(int x, int y) {
241
                if (alwaysMenuOnClick)
242
                        return true;
243

    
244
                if (!showMenuAlways && (popMenu.getSubElements().length <= 1))
245
                        return false;
246

    
247
                if (offsetTrianglex < 0) {
248
                        if (x < (this.getWidth() - BORDER + offsetTrianglex))
249
                                return false;
250
                } else {
251
                        if (x > (BORDER + offsetTrianglex + triangleWidth))
252
                                return false;
253
                }
254
        
255
                if (offsetTriangley < 0) {
256
                        if (y < (this.getHeight() - BORDER + offsetTriangley))
257
                                return false;
258
                } else {
259
                        if (y > (BORDER + offsetTriangley + triangleHeight))
260
                                return false;
261
                }
262

    
263
                return true;
264
        }
265

    
266
        /*
267
         * (non-Javadoc)
268
         * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
269
         */
270
        public void mousePressed(MouseEvent e) {
271
                boolean showPopup = popupTriangleClicked(e.getX(), e.getY());
272
                if (showPopup)
273
                        setPopupVisible(showPopup);
274
                else
275
                        callComboButtonClickedListeners();
276
        }
277
        
278
        /**
279
         * A?adir un listener a la lista de eventos
280
         * @param listener
281
         */
282
        public void addComboButtonClickedListener(ComboButtonListener listener) {
283
                if (!actionCommandListeners.contains(listener))
284
                        actionCommandListeners.add(listener);
285
        }
286

    
287
        /**
288
         * Borrar un listener de la lista de eventos
289
         * @param listener
290
         */
291
        public void removeComboButtonClickedListener(ComboButtonListener listener) {
292
                actionCommandListeners.remove(listener);
293
        }
294
        
295
        /**
296
         * Invocar a los eventos asociados al componente
297
         */
298
        private void callComboButtonClickedListeners() {
299
                Iterator<ComboButtonListener> acIterator = actionCommandListeners.iterator();
300
                while (acIterator.hasNext()) {
301
                        ComboButtonListener listener = acIterator.next();
302
                        listener.actionComboButtonClicked(new ComboButtonEvent(this));
303
                }
304
        }
305

    
306
        /*
307
         * (non-Javadoc)
308
         * @see javax.swing.JComponent#getToolTipText()
309
         */
310
        public String getToolTipText() {
311
                if (showToolTipText)
312
                        return super.getToolTipText();
313
                return null;
314
        }
315
        
316
        /*
317
         * (non-Javadoc)
318
         * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
319
         */
320
        public void mouseDragged(MouseEvent e) {
321
                if (popupTriangleClicked(e.getX(), e.getY()))
322
                        setPopupVisible(true);
323
        }
324

    
325
        /*
326
         * (non-Javadoc)
327
         * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
328
         */
329
        public void mouseMoved(MouseEvent e) {
330
                showToolTipText = !popMenu.isShowing();
331
        }
332

    
333
        /**
334
         * Devuelve si esta visible siempre
335
         * @return the showMenuAlways
336
         */
337
        public boolean isShowMenuAlways() {
338
                return showMenuAlways;
339
        }
340

    
341
        /**
342
         * Especifica si el menu se ha de visualizar siempre o solo cuando haya mas de
343
         * un item
344
         * @param showMenuAlways the showMenuAlways to set
345
         */
346
        public void setShowMenuAlways(boolean showMenuAlways) {
347
                this.showMenuAlways = showMenuAlways;
348
        }
349
        
350
        public void mouseReleased(MouseEvent e) {
351
                
352
        }
353
        public void mouseClicked(MouseEvent e) {}
354
        public void mouseEntered(MouseEvent arg0) {}
355
        public void mouseExited(MouseEvent arg0) {}
356

    
357
        /**
358
         * @return the alwaysMenuOnClick
359
         */
360
        public boolean isAlwaysMenuOnClick() {
361
                return alwaysMenuOnClick;
362
        }
363

    
364
        /**
365
         * @param alwaysMenuOnClick the alwaysMenuOnClick to set
366
         */
367
        public void setAlwaysMenuOnClick(boolean alwaysMenuOnClick) {
368
                this.alwaysMenuOnClick = alwaysMenuOnClick;
369
        }
370
        
371
        public Object getValue() {
372
                return null;
373
        }
374
}