Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libCorePlugin / src / com / iver / core / menus / ToolbarMenus.java @ 23854

History | View | Annotate | Download (4.25 KB)

1
/**
2
 * 
3
 */
4
package com.iver.core.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 com.iver.andami.PluginServices;
13
import com.iver.andami.plugins.Extension;
14
import com.iver.andami.plugins.config.generate.Menu;
15
import com.iver.andami.ui.mdiFrame.SelectableToolBar;
16
import com.iver.utiles.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
        public void initialize() {
76
                getPersistedStatus();
77

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

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