Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extDockingSkin / src / main / java / org / gvsig / menus / ToolbarMenus.java @ 29965

History | View | Annotate | Download (4.37 KB)

1
/**
2
 * 
3
 */
4
package org.gvsig.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
 * @author cesar
20
 *
21
 */
22
public class ToolbarMenus extends Extension implements ActionListener {
23
        private final String ACTIONCOMMANDBASE = "CHANGE_VISIBILITY-";
24
        private final String MENUBASE = "Ver/Toolbars/";
25
        private final String ENABLEDIMAGE = "images/enabled.png";
26

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

    
33
        }
34

    
35

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

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

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

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

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