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 / ActionsController.java @ 2758

History | View | Annotate | Download (4.29 KB)

1

    
2
package org.gvsig.configurableactions;
3

    
4
import java.awt.event.ActionEvent;
5
import java.awt.event.MouseAdapter;
6
import java.awt.event.MouseEvent;
7
import java.net.URL;
8
import java.util.Collection;
9
import javax.swing.AbstractAction;
10
import javax.swing.AbstractButton;
11
import javax.swing.Action;
12
import javax.swing.ImageIcon;
13
import javax.swing.JComponent;
14
import javax.swing.JLabel;
15
import javax.swing.JMenuItem;
16
import javax.swing.JPopupMenu;
17
import org.gvsig.tools.ToolsLocator;
18
import org.gvsig.tools.i18n.I18nManager;
19
import org.gvsig.tools.swing.api.SupportIsEnable;
20
import org.gvsig.tools.swing.api.SupportIsVisible;
21

    
22

    
23
public class ActionsController {
24

    
25
    private final JComponent component;
26
    private final Object source;
27
    private Collection<Action> actions;
28
    
29

    
30
    private class ActionListenerAdapter extends AbstractAction {
31

    
32
        private final Action action;
33

    
34
        public ActionListenerAdapter(Action action) {
35
            this.action = action;
36
        }
37
        
38
        @Override
39
        public void actionPerformed(ActionEvent e) {
40
            e.setSource(source);
41
            action.actionPerformed(e);
42
        }
43

    
44
        @Override
45
        public Object getValue(String key) {
46
            return action.getValue(key);
47
        }
48

    
49
        @Override
50
        public void putValue(String key, Object newValue) {
51
            action.putValue(key, newValue);
52
        }
53

    
54
        @Override
55
        public boolean isEnabled() {
56
            return action.isEnabled(); 
57
        }
58

    
59
        @Override
60
        public void setEnabled(boolean newValue) {
61
            action.setEnabled(newValue); 
62
        }
63
    }
64
    
65
    @SuppressWarnings("OverridableMethodCallInConstructor")
66
    public ActionsController(Collection<Action> actions, Object source, JComponent component)  {
67
        this.component = component;
68
        this.source = source;
69
        this.update(actions);
70
        this.initComponent();
71
    }
72
    
73
    private void initComponent() {
74
        I18nManager i18n = ToolsLocator.getI18nManager();
75
        this.component.setToolTipText(i18n.getTranslation("_Click_to_see_more_action_in_this_panel"));
76
        URL url = this.getClass().getResource("/org/gvsig/configurableactions/images/configurableactions.png");
77
        if( component instanceof AbstractButton ) {
78
            AbstractButton comp = (AbstractButton) this.component;
79
            if( comp.getText().equals("...") ) {
80
                comp.setText("");
81
            }
82
            comp.setIcon(new ImageIcon(url));
83
        } else if( component instanceof JLabel ) {
84
            JLabel comp = (JLabel) this.component;
85
            if( comp.getText().equals("...") ) {
86
                comp.setText("");
87
            }
88
            comp.setIcon(new ImageIcon(url));
89
        }
90
        this.component.setBorder(null);
91
        this.component.addMouseListener(new MouseAdapter() {
92
            @Override
93
            public void mouseClicked(MouseEvent e) {
94
                if( e.getClickCount()==1 && e.getButton()==MouseEvent.BUTTON1 ) {
95
                    doShowMenu();
96
                }
97
            }
98
        });
99
//        this.addActionListener(new ActionListener() {
100
//            @Override
101
//            public void actionPerformed(ActionEvent e) {
102
//                doShowMenu();
103
//            }
104
//        });
105
    }
106
    
107
    public void update(Collection<Action> actions) {
108
        this.actions = actions;
109
        if( actions==null || actions.isEmpty() ) {
110
            this.component.setVisible(false);
111
        } else {
112
            this.component.setVisible(true);
113
        }
114
    }
115
    
116
    private void doShowMenu() {
117
        JPopupMenu menu = new JPopupMenu();
118
        SupportIsVisible visible = null;
119
        if( this.source instanceof SupportIsVisible ) {
120
            visible = (SupportIsVisible) this.source;
121
        }
122
        SupportIsEnable enabled = null;
123
        if( this.source instanceof SupportIsEnable ) {
124
            enabled = (SupportIsEnable) this.source;
125
        }
126
        for (Action action : actions) {
127
            if( visible==null || visible.isVisible(action) ) {
128
                JMenuItem item = new JMenuItem(new ActionListenerAdapter(action));
129
                if( enabled!=null && !enabled.isEnabled(action)) {
130
                    item.setEnabled(false);
131
                }
132
                menu.add(item);
133
            }
134
        }
135
        menu.show(this.component, 0,this.component.getHeight());
136
    }
137
}