Statistics
| Revision:

root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / controls / combobutton / ComboButton.java @ 13136

History | View | Annotate | Download (7.24 KB)

1
package org.gvsig.gui.beans.controls.combobutton;
2

    
3

    
4

    
5
import java.awt.Color;
6
import java.awt.Dimension;
7
import java.awt.Graphics;
8
import java.awt.Graphics2D;
9
import java.awt.Point;
10
import java.awt.RenderingHints;
11
import java.awt.event.ActionEvent;
12
import java.awt.event.ActionListener;
13
import java.awt.event.MouseEvent;
14
import java.awt.event.MouseListener;
15
import java.awt.event.MouseMotionListener;
16
import java.awt.geom.GeneralPath;
17
import java.util.ArrayList;
18
import java.util.Iterator;
19

    
20
import javax.swing.BoxLayout;
21
import javax.swing.Icon;
22
import javax.swing.JButton;
23
import javax.swing.JMenuItem;
24
import javax.swing.JPanel;
25
import javax.swing.JPopupMenu;
26
import javax.swing.JToggleButton;
27

    
28
import org.gvsig.gui.beans.controls.IControl;
29

    
30

    
31
public class ComboButton extends JPanel implements IControl {
32

    
33
        ModeToggleButtonGroup bg;
34
        private MyJToggleButton tbutton;
35
        private JPopupMenu popMenu;
36
        private ArrayList menuItemList;
37
        private ArrayList actionListeners = new ArrayList();
38

    
39
        private ActionListener popupMenuItemListener;
40
        private ArrayList buttons=new ArrayList();
41
        int size;
42

    
43
        public ComboButton() {
44
                //this.app = app;
45
                this.bg = new ModeToggleButtonGroup();
46

    
47
                setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
48

    
49
                tbutton = new MyJToggleButton(this);
50
                tbutton.setAlignmentY(BOTTOM_ALIGNMENT);
51
                add(tbutton);
52

    
53
                popMenu = new JPopupMenu();
54
                menuItemList = new ArrayList();
55
                popupMenuItemListener = new MenuItemListener();
56
                size = 0;
57
        }
58

    
59
        public JToggleButton getJToggleButton() {
60
                return tbutton;
61
        }
62

    
63
        public boolean selectMode(int mode) {
64
                String modeText = mode + "";
65

    
66
                for (int i=0; i < size; i++) {
67
                        JMenuItem mi = (JMenuItem) menuItemList.get(i);
68
                        // found item for mode?
69
                        if (mi.getActionCommand().equals(modeText)) {
70
                                selectItem(mi);
71
                                return true;
72
                        }
73
                }
74
                return false;
75
        }
76

    
77
        private void selectItem(JMenuItem mi) {
78
                tbutton.setIcon(mi.getIcon());
79
                tbutton.setToolTipText(mi.getText());
80
                tbutton.setActionCommand(mi.getActionCommand());
81
                tbutton.setSelected(true);
82
                tbutton.requestFocus();
83
        }
84

    
85
        public void addButton(JButton menu) {
86
                buttons.add(menu);
87
                JMenuItem mi = new JMenuItem();
88
                mi.setText(menu.getText());
89
                mi.setIcon(menu.getIcon());
90
                mi.addActionListener(popupMenuItemListener);
91
                mi.setActionCommand(menu.getActionCommand());
92
                popMenu.add(mi);
93
                menuItemList.add(mi);
94
                size++;
95

    
96
                if (size == 1) {
97
                        // init tbutton
98
                        tbutton.setIcon(menu.getIcon());
99
                        tbutton.setActionCommand(menu.getActionCommand());
100
                        tbutton.setToolTipText(mi.getText());
101
                        // add button to button group
102
                        bg.add(tbutton);
103
                }
104

    
105
                // set the action listeners
106
                Iterator actionListenerIterator = actionListeners.iterator();
107
                while (actionListenerIterator.hasNext()) {
108
                        ActionListener listener = (ActionListener) actionListenerIterator.next();
109
                        menu.addActionListener(listener);
110
                }
111
        }
112

    
113
        public void addSeparator() {
114
                popMenu.addSeparator();
115
        }
116

    
117
        // sets new mode when item in popup menu is selected
118
        private class MenuItemListener implements ActionListener {
119

    
120
                public void actionPerformed(ActionEvent e) {
121
                        JMenuItem item = (JMenuItem) e.getSource();
122
                        selectItem(item);
123
                        tbutton.doClick();
124
                }
125
        }
126

    
127
        public void mouseOver() {
128
                //        popup menu is showing
129
                JPopupMenu activeMenu = bg.getActivePopupMenu();
130
                if (activeMenu != null && activeMenu.isShowing()) {
131
                        setPopupVisible(true);
132
                }
133
        }
134

    
135
        //        shows popup menu
136
        public void setPopupVisible(boolean flag) {
137
                if (flag) {
138
                        bg.setActivePopupMenu(popMenu);
139
                        if (popMenu.isShowing()) return;
140
                        Point locButton = tbutton.getLocationOnScreen();
141
                        Point locApp = this.getLocationOnScreen();
142
                        popMenu.show(this, locButton.x - locApp.x,
143
                                                                                        locButton.y - locApp.y + tbutton.getHeight());
144
                } else {
145
                        popMenu.setVisible(false);
146
                }
147
        }
148

    
149
        public boolean isPopupShowing() {
150
                return popMenu.isShowing();
151
        }
152

    
153
        public void setAction(ActionEvent action) {
154
                JButton[] bs=(JButton[])buttons.toArray(new JButton[0]);
155
                for (int i=0;i<bs.length;i++) {
156
                        if (bs[i].getActionCommand().equals(action.getActionCommand())){
157
                                bs[i].doClick();
158
                        }
159
                }
160

    
161
        }
162

    
163
        /**
164
         * Adds an ActionListener to each button in the combo.
165
         *
166
         * @param listener
167
         */
168
        public void addActionListener(ActionListener listener) {
169
                if (!actionListeners.contains(listener)) {
170
                        actionListeners.add(listener);
171
                        Iterator buttonsIterator = buttons.iterator();
172
                        while (buttonsIterator.hasNext()) {
173
                                JButton button = (JButton) buttonsIterator.next();
174
                                button.addActionListener(listener);
175
                        }
176
                }
177
        }
178

    
179
        /**
180
         * Removes an ActionListener from each button in the combo.
181
         * @param listener
182
         */
183
        public void removeActionListener(ActionListener listener) {
184
                actionListeners.remove(listener);
185
                Iterator buttonsIterator = buttons.iterator();
186
                while (buttonsIterator.hasNext()) {
187
                        JButton button = (JButton) buttonsIterator.next();
188
                        button.removeActionListener(listener);
189
                }
190
        }
191

    
192
        /**
193
         *  Sets the value of the control. It may mean different things for
194
         *  different kinds of controls.
195
         */
196
        public Object setValue(Object value) {
197
                //TODO implement this method
198
                return value;
199
        }
200
}
201

    
202
class MyJToggleButton extends JToggleButton
203
implements MouseListener, MouseMotionListener, ActionListener {
204
        private static int BORDER = 6;
205
        private int iconWidth, iconHeight;
206
        private GeneralPath gp;
207
        private boolean showToolTipText = true;
208
        private ComboButton menu;
209
        private Color arrowColor = new Color(0,0,0,130);
210
        //private Color arrowColor = Color.darkGray;
211

    
212
        MyJToggleButton(ComboButton menu) {
213
                super();
214
                this.menu = menu;
215

    
216
                // add own listeners
217
                addMouseListener(this);
218
                addMouseMotionListener(this);
219
                addActionListener(this);
220
        }
221

    
222
        public String getToolTipText() {
223
                if (showToolTipText)
224
                        return super.getToolTipText();
225
                else
226
                        return null;
227
        }
228

    
229
        // set mode
230
        public void actionPerformed(ActionEvent e) {
231
                menu.setAction(e);
232
        }
233

    
234
        public void setIcon(Icon icon) {
235
                super.setIcon(icon);
236
                iconWidth = icon.getIconWidth();
237
                iconHeight = icon.getIconHeight();
238
                Dimension dim = new Dimension(iconWidth + 2*BORDER,
239
                                                                iconHeight + 2*BORDER);
240
                setPreferredSize(dim);
241
                setMinimumSize(dim);
242
                setMaximumSize(dim);
243
        }
244

    
245

    
246
        public void paint(Graphics g) {
247
                Graphics2D g2 = (Graphics2D) g;
248

    
249
                super.paint(g2);
250

    
251
                // draw little arrow (for popup menu)
252
                if (menu.size > 1) {
253
                        if (gp == null) initPath();
254
                        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
255
                                                                RenderingHints.VALUE_ANTIALIAS_ON);
256

    
257
                        g2.setColor(Color.white);
258
                        g2.fill(gp);
259
                        g2.setColor(arrowColor);
260
                        g2.draw(gp);
261
                }
262
        }
263

    
264
        private void initPath() {
265
                gp = new GeneralPath();
266
                int x = BORDER + iconWidth + 2;
267
                int y = BORDER + iconHeight + 1;
268
                gp.moveTo(x-6, y-5);
269
                gp.lineTo(x, y-5);
270
                gp.lineTo(x-3,y);
271
                gp.closePath();
272
        }
273

    
274

    
275
        private boolean popupTriangleClicked(int x, int y) {
276
                return (menu.size > 1 && y > iconHeight);
277
        }
278

    
279
        public void mouseClicked(MouseEvent e) {
280
        }
281

    
282
        public void mouseEntered(MouseEvent arg0) {
283
        }
284

    
285
        public void mouseExited(MouseEvent arg0) {
286
        }
287

    
288
        public void mousePressed(MouseEvent e) {
289
                boolean showPopup=popupTriangleClicked(e.getX(), e.getY());
290
                menu.setPopupVisible(showPopup);
291
                requestFocus();
292
                if (!showPopup)
293
                        doClick();
294
        }
295

    
296
        public void mouseReleased(MouseEvent arg0) {
297
        }
298

    
299
        public void mouseDragged(MouseEvent e) {
300
                if (popupTriangleClicked(e.getX(), e.getY()))
301
                        menu.setPopupVisible(true);
302
        }
303

    
304
        public void mouseMoved(MouseEvent e) {
305
                menu.mouseOver();
306
                showToolTipText = !menu.isPopupShowing();
307
        }
308
}
309