Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.util / org.gvsig.tools.util.impl / src / main / java / org / gvsig / configurableactions / ActionsComponent.java @ 1714

History | View | Annotate | Download (2.75 KB)

1

    
2
package org.gvsig.configurableactions;
3

    
4
import java.awt.event.ActionEvent;
5
import java.awt.event.ActionListener;
6
import java.awt.event.MouseAdapter;
7
import java.awt.event.MouseEvent;
8
import java.net.URL;
9
import java.util.Collection;
10
import javax.swing.AbstractAction;
11
import javax.swing.Action;
12
import javax.swing.ImageIcon;
13
import javax.swing.JLabel;
14
import javax.swing.JMenuItem;
15
import javax.swing.JPopupMenu;
16

    
17

    
18
public class ActionsComponent extends JLabel {
19

    
20
    private final Object source;
21
    private Collection<Action> actions;
22

    
23
    private class ActionListenerAdapter extends AbstractAction {
24

    
25
        private final Action action;
26

    
27
        public ActionListenerAdapter(Action action) {
28
            this.action = action;
29
        }
30
        
31
        @Override
32
        public void actionPerformed(ActionEvent e) {
33
            e.setSource(source);
34
            action.actionPerformed(e);
35
        }
36

    
37
        @Override
38
        public Object getValue(String key) {
39
            return action.getValue(key);
40
        }
41

    
42
        @Override
43
        public void putValue(String key, Object newValue) {
44
            action.putValue(key, newValue);
45
        }
46

    
47
        @Override
48
        public boolean isEnabled() {
49
            return action.isEnabled(); 
50
        }
51

    
52
        @Override
53
        public void setEnabled(boolean newValue) {
54
            action.setEnabled(newValue); 
55
        }
56
    }
57
    
58
    public ActionsComponent(Collection<Action> actions, Object source)  {
59
        this.source = source;
60
        this.update(actions);
61
        this.initComponent();
62
    }
63
    
64
    private void initComponent() {
65
        URL url = this.getClass().getResource("/org/gvsig/configurableactions/images/configurableactions.png");
66
        this.setIcon(new ImageIcon(url));
67
        this.setBorder(null);
68
        this.addMouseListener(new MouseAdapter() {
69
            @Override
70
            public void mouseClicked(MouseEvent e) {
71
                if( e.getClickCount()==1 && e.getButton()==MouseEvent.BUTTON1 ) {
72
                    doShowMenu();
73
                }
74
            }
75
        });
76
//        this.addActionListener(new ActionListener() {
77
//            @Override
78
//            public void actionPerformed(ActionEvent e) {
79
//                doShowMenu();
80
//            }
81
//        });
82
    }
83
    
84
    public void update(Collection<Action> actions) {
85
        this.actions = actions;
86
        if( actions==null || actions.isEmpty() ) {
87
            this.setVisible(false);
88
        } else {
89
            this.setVisible(true);
90
        }
91
    }
92
    
93
    private void doShowMenu() {
94
        JPopupMenu menu = new JPopupMenu();
95
        for (Action action : actions) {
96
            JMenuItem item = new JMenuItem(new ActionListenerAdapter(action));
97
            menu.add(item);
98
        }
99
        menu.show(this, 0,this.getHeight());
100
    }
101
}