Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libCorePlugin / src / org / gvsig / coreplugin / menus / ToolbarMenus.java @ 29630

History | View | Annotate | Download (4.23 KB)

1
/**
2
 * 
3
 */
4
package org.gvsig.coreplugin.menus;
5

    
6
import java.awt.event.ActionEvent;
7
import java.awt.event.ActionListener;
8
import java.net.URL;
9

    
10
import javax.swing.ImageIcon;
11

    
12
import org.gvsig.andami.PluginServices;
13
import org.gvsig.andami.plugins.Extension;
14
import org.gvsig.andami.plugins.config.generate.Menu;
15
import org.gvsig.andami.ui.mdiFrame.SelectableToolBar;
16
import org.gvsig.utils.XMLEntity;
17

    
18

    
19
/**
20
 * @author cesar
21
 *
22
 */
23
public class ToolbarMenus extends Extension implements ActionListener {
24
        private final String ACTIONCOMMANDBASE = "CHANGE_VISIBILITY-";
25
        private final String MENUBASE = "Ver/Toolbars/";
26
        private final String ENABLEDIMAGE = "images/enabled.png";
27

    
28
        /* (non-Javadoc)
29
         * @see com.iver.andami.plugins.Extension#execute(java.lang.String)
30
         */
31
        public void execute(String actionCommand) {
32
                // TODO Auto-generated method stub
33

    
34
        }
35

    
36

    
37
        /* (non-Javadoc)
38
         * @see com.iver.andami.plugins.Extension#isEnabled()
39
         */
40
        public boolean isEnabled() {
41
                return true;
42
        }
43

    
44
        /* (non-Javadoc)
45
         * @see com.iver.andami.plugins.Extension#isVisible()
46
         */
47
        public boolean isVisible() {
48
                // TODO Auto-generated method stub
49
                return false;
50
        }
51
        
52
        /*
53
         *  (non-Javadoc)
54
         * @see com.iver.andami.plugins.Extension#actionPerformed()
55
         */
56
        public void actionPerformed(ActionEvent e) {
57
                String toolbarName = e.getActionCommand().substring(ACTIONCOMMANDBASE.length());
58
                javax.swing.JMenuItem menu = PluginServices.getMainFrame().getMenuEntry((MENUBASE+toolbarName).split("/"));
59
                
60
                if (!toolbarName.equals("")) {
61

    
62
                        boolean oldVisibility = PluginServices.getMainFrame().getToolbarVisibility(toolbarName);
63
                        if (oldVisibility==false) {
64
                                URL icon = PluginServices.getPluginServices(this).getClassLoader().getResource(ENABLEDIMAGE);                                
65
                                menu.setIcon(new ImageIcon(icon));
66
                                persistStatus(toolbarName, !oldVisibility);
67
                        }
68
                        else {
69
                                menu.setIcon(null);
70
                                persistStatus(toolbarName, !oldVisibility);
71
                        }
72
                        PluginServices.getMainFrame().setToolbarVisibility(toolbarName, !oldVisibility);
73
                }
74
        }
75

    
76
        /*
77
         *  (non-Javadoc)
78
         * @see com.iver.andami.plugins.Extension#initialize()
79
         */
80
        public void initialize() {
81
                getPersistedStatus();
82
                SelectableToolBar[] toolBars = PluginServices.getMainFrame().getToolbars();
83
                for (int i=toolBars.length-1; i>0; i--) {
84
                        Menu menu = new Menu();
85
                        menu.setActionCommand(ACTIONCOMMANDBASE+toolBars[i].getName());
86
                        //menu.setTooltip(PluginServices.getText(this, "muestra_oculta_la_toolbar"));
87
                        menu.setText(MENUBASE+toolBars[i].getName());
88
                        if (toolBars[i].getAndamiVisibility())
89
                                menu.setIcon(ENABLEDIMAGE);
90
                        PluginServices.getMainFrame().addMenu(menu, this, PluginServices.getPluginServices(this).getClassLoader());
91
                }
92
                
93
        }
94
        
95
        /**
96
         * Save the status of the provided toolbar.
97
         * 
98
         * @param toolbarName The toolbar name whose status wants to be saved.
99
         * @param visible Whether or not the toolbar is visible.
100
         */
101
        private void persistStatus(String toolbarName, boolean visible) {
102
                PluginServices ps = PluginServices.getPluginServices(this); 
103
                XMLEntity xml = ps.getPersistentXML();
104
                XMLEntity child = null;
105
                for (int i=xml.getChildrenCount()-1; i>=0; i--) {
106
                        if (xml.getChild(i).getName().equals("Toolbars"))
107
                                child = xml.getChild(i).getChild(0);
108
                                
109
                }
110
                if (child==null) {
111
                        XMLEntity toolbars = new XMLEntity();
112
                        toolbars.setName("Toolbars");
113
                        child = new XMLEntity();
114
                        toolbars.addChild(child);
115
                        xml.addChild(toolbars);
116
                }
117
                
118
                if (visible) {
119
                        child.putProperty(toolbarName, "visible");
120
                }
121
                else {
122
                        child.putProperty(toolbarName, "hidden");
123
                }
124
                ps.setPersistentXML(xml);
125
                
126
        }
127

    
128
        /**
129
         * Reads the stored toolbars' status from plugin-persinstence.xml,
130
         * and sets the toolbars accordingly. 
131
         *
132
         */
133
        private void getPersistedStatus() {
134
                PluginServices ps = PluginServices.getPluginServices(this); 
135
                XMLEntity xml = ps.getPersistentXML();
136
                XMLEntity child = null;
137
                for (int i=xml.getChildrenCount()-1; i>=0; i--) {
138
                        if (xml.getChild(i).getName().equals("Toolbars"))
139
                                child = xml.getChild(i).getChild(0);
140
                                
141
                }
142
                if (child!=null) {
143
                        SelectableToolBar[] toolBars = PluginServices.getMainFrame().getToolbars();
144
                        for (int i=toolBars.length-1; i>=0; i--) {
145
                                if (child.contains(toolBars[i].getName()))
146
                                        toolBars[i].setAndamiVisibility(child.getStringProperty(toolBars[i].getName()).equals("visible"));
147
                        }
148
                }
149
        }
150
}