Statistics
| Revision:

root / trunk / libraries / libCorePlugin / src / com / iver / core / menus / ToolbarMenus.java @ 9866

History | View | Annotate | Download (4.18 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

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

    
32
        }
33

    
34

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

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

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

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

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