Statistics
| Revision:

gvsig-projects-pool / org.gvsig.winmgr / trunk / org.gvsig.winmgr.app / org.gvsig.winmgr.app.mainplugin / src / main / java / org / gvsig / coreplugin / menus / ToolbarMenus.java @ 682

History | View | Annotate | Download (5.17 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
/**
25
 * 
26
 */
27
package org.gvsig.coreplugin.menus;
28

    
29
import java.awt.event.ActionEvent;
30
import java.awt.event.ActionListener;
31
import java.net.URL;
32

    
33
import javax.swing.ImageIcon;
34

    
35
import org.gvsig.andami.PluginServices;
36
import org.gvsig.andami.plugins.Extension;
37
import org.gvsig.andami.plugins.config.generate.Menu;
38
import org.gvsig.andami.ui.mdiFrame.SelectableToolBar;
39
import org.gvsig.utils.XMLEntity;
40

    
41

    
42
/**
43
 * @author cesar
44
 *
45
 */
46
public class ToolbarMenus extends Extension implements ActionListener {
47
        private final String ACTIONCOMMANDBASE = "CHANGE_VISIBILITY-";
48
        private final String MENUBASE = "Ver/Toolbars/";
49
        private final String ENABLEDIMAGE = "images/enabled.png";
50

    
51
        /* (non-Javadoc)
52
         * @see com.iver.andami.plugins.Extension#execute(java.lang.String)
53
         */
54
        public void execute(String actionCommand) {
55
                // TODO Auto-generated method stub
56

    
57
        }
58

    
59

    
60
        /* (non-Javadoc)
61
         * @see com.iver.andami.plugins.Extension#isEnabled()
62
         */
63
        public boolean isEnabled() {
64
                return true;
65
        }
66

    
67
        /* (non-Javadoc)
68
         * @see com.iver.andami.plugins.Extension#isVisible()
69
         */
70
        public boolean isVisible() {
71
                // TODO Auto-generated method stub
72
                return false;
73
        }
74
        
75
        /*
76
         *  (non-Javadoc)
77
         * @see com.iver.andami.plugins.Extension#actionPerformed()
78
         */
79
        public void actionPerformed(ActionEvent e) {
80
                String toolbarName = e.getActionCommand().substring(ACTIONCOMMANDBASE.length());
81
                javax.swing.JMenuItem menu = PluginServices.getMainFrame().getMenuEntry((MENUBASE+toolbarName).split("/"));
82
                
83
                if (!toolbarName.equals("")) {
84

    
85
                        boolean oldVisibility = PluginServices.getMainFrame().getToolbarVisibility(toolbarName);
86
                        if (oldVisibility==false) {
87
                                URL icon = PluginServices.getPluginServices(this).getClassLoader().getResource(ENABLEDIMAGE);                                
88
                                menu.setIcon(new ImageIcon(icon));
89
                                persistStatus(toolbarName, !oldVisibility);
90
                        }
91
                        else {
92
                                menu.setIcon(null);
93
                                persistStatus(toolbarName, !oldVisibility);
94
                        }
95
                        PluginServices.getMainFrame().setToolbarVisibility(toolbarName, !oldVisibility);
96
                }
97
        }
98

    
99
        /*
100
         *  (non-Javadoc)
101
         * @see com.iver.andami.plugins.Extension#initialize()
102
         */
103
        public void initialize() {
104
                getPersistedStatus();
105
                SelectableToolBar[] toolBars = PluginServices.getMainFrame().getToolbars();
106
                for (int i=toolBars.length-1; i>0; i--) {
107
                        Menu menu = new Menu();
108
                        menu.setActionCommand(ACTIONCOMMANDBASE+toolBars[i].getName());
109
                        //menu.setTooltip(PluginServices.getText(this, "muestra_oculta_la_toolbar"));
110
                        menu.setText(MENUBASE+toolBars[i].getName());
111
                        if (toolBars[i].getAndamiVisibility())
112
                                menu.setIcon(ENABLEDIMAGE);
113
                        PluginServices.getMainFrame().addMenu(menu, this, PluginServices.getPluginServices(this).getClassLoader());
114
                }
115
                
116
        }
117
        
118
        /**
119
         * Save the status of the provided toolbar.
120
         * 
121
         * @param toolbarName The toolbar name whose status wants to be saved.
122
         * @param visible Whether or not the toolbar is visible.
123
         */
124
        private void persistStatus(String toolbarName, boolean visible) {
125
                PluginServices ps = PluginServices.getPluginServices(this); 
126
                XMLEntity xml = ps.getPersistentXML();
127
                XMLEntity child = null;
128
                for (int i=xml.getChildrenCount()-1; i>=0; i--) {
129
                        if (xml.getChild(i).getName().equals("Toolbars"))
130
                                child = xml.getChild(i).getChild(0);
131
                                
132
                }
133
                if (child==null) {
134
                        XMLEntity toolbars = new XMLEntity();
135
                        toolbars.setName("Toolbars");
136
                        child = new XMLEntity();
137
                        toolbars.addChild(child);
138
                        xml.addChild(toolbars);
139
                }
140
                
141
                if (visible) {
142
                        child.putProperty(toolbarName, "visible");
143
                }
144
                else {
145
                        child.putProperty(toolbarName, "hidden");
146
                }
147
                ps.setPersistentXML(xml);
148
                
149
        }
150

    
151
        /**
152
         * Reads the stored toolbars' status from plugin-persinstence.xml,
153
         * and sets the toolbars accordingly. 
154
         *
155
         */
156
        private void getPersistedStatus() {
157
                PluginServices ps = PluginServices.getPluginServices(this); 
158
                XMLEntity xml = ps.getPersistentXML();
159
                XMLEntity child = null;
160
                for (int i=xml.getChildrenCount()-1; i>=0; i--) {
161
                        if (xml.getChild(i).getName().equals("Toolbars"))
162
                                child = xml.getChild(i).getChild(0);
163
                                
164
                }
165
                if (child!=null) {
166
                        SelectableToolBar[] toolBars = PluginServices.getMainFrame().getToolbars();
167
                        for (int i=toolBars.length-1; i>=0; i--) {
168
                                if (child.contains(toolBars[i].getName()))
169
                                        toolBars[i].setAndamiVisibility(child.getStringProperty(toolBars[i].getName()).equals("visible"));
170
                        }
171
                }
172
        }
173
}