svn-gvsig-desktop / trunk / libraries / libCorePlugin / src / com / iver / core / menus / ToolbarMenus.java @ 9839
History | View | Annotate | Download (4.18 KB)
1 | 9828 | cesar | /**
|
---|---|---|---|
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 | 9839 | cesar | import com.iver.utiles.XMLEntity; |
17 | 9828 | cesar | |
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 | 9839 | cesar | * @see com.iver.andami.plugins.Extension#execute(java.lang.String)
|
28 | 9828 | cesar | */
|
29 | public void execute(String actionCommand) { |
||
30 | // TODO Auto-generated method stub
|
||
31 | |||
32 | } |
||
33 | |||
34 | |||
35 | /* (non-Javadoc)
|
||
36 | 9839 | cesar | * @see com.iver.andami.plugins.Extension#isEnabled()
|
37 | 9828 | cesar | */
|
38 | public boolean isEnabled() { |
||
39 | return true; |
||
40 | } |
||
41 | |||
42 | /* (non-Javadoc)
|
||
43 | 9839 | cesar | * @see com.iver.andami.plugins.Extension#isVisible()
|
44 | 9828 | cesar | */
|
45 | public boolean isVisible() { |
||
46 | // TODO Auto-generated method stub
|
||
47 | return false; |
||
48 | } |
||
49 | 9839 | cesar | |
50 | /*
|
||
51 | * (non-Javadoc)
|
||
52 | * @see com.iver.andami.plugins.Extension#actionPerformed()
|
||
53 | */
|
||
54 | 9828 | cesar | 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 | 9839 | cesar | |
60 | 9828 | cesar | boolean oldVisibility = PluginServices.getMainFrame().getToolbarVisibility(toolbarName);
|
61 | if (oldVisibility==false) { |
||
62 | 9839 | cesar | URL icon = PluginServices.getPluginServices(this).getClassLoader().getResource("images/gtk-apply.png"); |
63 | 9828 | cesar | menu.setIcon(new ImageIcon(icon)); |
64 | 9839 | cesar | persistStatus(toolbarName, !oldVisibility); |
65 | 9828 | cesar | } |
66 | else {
|
||
67 | menu.setIcon(null);
|
||
68 | 9839 | cesar | persistStatus(toolbarName, !oldVisibility); |
69 | 9828 | cesar | } |
70 | PluginServices.getMainFrame().setToolbarVisibility(toolbarName, !oldVisibility); |
||
71 | } |
||
72 | } |
||
73 | |||
74 | 9839 | cesar | /*
|
75 | * (non-Javadoc)
|
||
76 | * @see com.iver.andami.plugins.Extension#initialize()
|
||
77 | */
|
||
78 | 9828 | cesar | public void initialize() { |
79 | 9839 | cesar | getPersistedStatus(); |
80 | 9828 | cesar | 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 | 9829 | cesar | //menu.setTooltip(PluginServices.getText(this, "muestra_oculta_la_toolbar"));
|
85 | 9828 | cesar | menu.setText(MENUBASE+toolBars[i].getName()); |
86 | if (toolBars[i].getAndamiVisibility())
|
||
87 | menu.setIcon("images/gtk-apply.png");
|
||
88 | PluginServices.getMainFrame().addMenu(menu, this, PluginServices.getPluginServices(this).getClassLoader()); |
||
89 | } |
||
90 | |||
91 | } |
||
92 | 9839 | cesar | |
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 | 9828 | cesar | |
126 | 9839 | cesar | /**
|
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 | 9828 | cesar | } |