]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/toolbar/CommandStateRegistry.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / toolbar / CommandStateRegistry.java
1 package org.simantics.ui.toolbar;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.HashSet;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Set;
9
10 import org.eclipse.core.commands.Command;
11 import org.eclipse.core.commands.State;
12 import org.eclipse.ui.IWorkbenchPart;
13 import org.eclipse.ui.PlatformUI;
14 import org.eclipse.ui.commands.ICommandService;
15 import org.eclipse.ui.handlers.IHandlerService;
16 import org.eclipse.ui.handlers.RadioState;
17 import org.eclipse.ui.handlers.RegistryToggleState;
18 import org.simantics.utils.datastructures.MapList;
19
20 /**
21  * Registry for storing command states separately for each IEditorPart
22  * 
23  * TODO : cleaning editor states and listeners is now implemented through ToolBarContributor. Is this OK?
24  * 
25  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
26  *
27  */
28 public class CommandStateRegistry {
29
30         
31         private static CommandStateRegistry instance;
32         
33         
34         public static CommandStateRegistry getInstance() {
35                 if (instance == null)
36                         instance = new CommandStateRegistry();
37                 return instance;
38         }
39         
40         
41         
42         private Map<IWorkbenchPart,Map<String,Boolean>> toggleStates = new HashMap<IWorkbenchPart, Map<String,Boolean>>();
43         private Map<String,Boolean> defaultToggleStates = new HashMap<String, Boolean>();
44         
45         private Map<String,String> defaultRadioStates = new HashMap<String, String>();
46         private Map<IWorkbenchPart,Map<String,String>> radioStates = new HashMap<IWorkbenchPart, Map<String,String>>();
47
48         private ICommandService service;
49         private IHandlerService handlerService;
50         
51         private CommandStateRegistry() {
52                 service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
53                 handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
54         }
55         
56         /**
57          * Stores default state for a command.
58          * 
59          * Note: uses current state as default. 
60          * 
61          * @param commandId
62          */
63         public void storeDefaultState(String commandId) {
64                 Command command = service.getCommand(commandId);
65                 State toggleState = command.getState(RegistryToggleState.STATE_ID);
66                 State radioState = command.getState(RadioState.STATE_ID);
67                 if (toggleState != null) {
68                         if (!defaultToggleStates.containsKey(commandId))
69                                 defaultToggleStates.put(commandId, getToggleState(command));
70                 } else if (radioState != null) {
71                         String value = (String) radioState.getValue();
72                         if (!defaultRadioStates.containsKey(commandId))
73                                 defaultRadioStates.put(commandId, value);
74                 } else {
75                         throw new IllegalArgumentException("Command " + commandId + " does not have a state");
76                 }
77         }
78         /**
79          * Stores toggle state of a command.
80          * @param part
81          * @param commandId
82          * @param checked
83          */
84         public void setEditorState(IWorkbenchPart part, String commandId, boolean checked) {
85                 Map<String,Boolean> editorStates = toggleStates.get(part);
86                 if (editorStates == null) {
87                         editorStates = new HashMap<String, Boolean>();
88                         toggleStates.put(part, editorStates);
89                 }
90                 editorStates.put(commandId, checked);
91                 fireStateChange(part, commandId, Boolean.toString(checked));
92         }
93         
94         /**
95          * Stores radio state of a command.
96          * @param part
97          * @param commandId
98          * @param value
99          */
100         public void setEditorState(IWorkbenchPart part, String commandId, String value) {
101                 Map<String,String> editorStates = radioStates.get(part);
102                 if (editorStates == null) {
103                         editorStates = new HashMap<String, String>();
104                         radioStates.put(part, editorStates);
105                 }
106                 editorStates.put(commandId, value);
107                 fireStateChange(part, commandId, value);
108         }
109         
110         
111         
112         public Map<String, Boolean> getDefaultToggleStates() {
113                 return defaultToggleStates;
114         }
115         
116         public Map<String, String> getDefaultRadioStates() {
117                 return defaultRadioStates;
118         }
119         
120         public Map<String, Boolean> getEditorToggleStates(IWorkbenchPart part) {
121                 return toggleStates.get(part);
122         }
123         
124         public Map<String, String> getEditorRadioStates(IWorkbenchPart part) {
125                 return radioStates.get(part);
126         }
127         
128         public Boolean getToggleState(IWorkbenchPart part, String commandId) {
129                 if (part == null)
130                         return defaultToggleStates.get(commandId);
131                 Map<String,Boolean> editorStates = toggleStates.get(part);
132                 if (editorStates == null) {
133                         return defaultToggleStates.get(commandId);
134                 }
135                 return editorStates.get(commandId);
136         }
137         
138         public String getRadioState(IWorkbenchPart part, String commandId) {
139                 if (part == null)
140                         return defaultRadioStates.get(commandId);
141                 Map<String,String> editorStates = radioStates.get(part);
142                 if (editorStates == null) {
143                         return defaultRadioStates.get(commandId);
144                 }
145                 return editorStates.get(commandId);
146         }
147         
148         public void clearStates(IWorkbenchPart part) {
149                 toggleStates.remove(part);
150                 radioStates.remove(part);
151                 listenerWithPart.remove(part);
152         }
153         
154         private boolean getToggleState(Command command) {
155                 State toggleState = command.getState(RegistryToggleState.STATE_ID);
156                 return (Boolean)toggleState.getValue();
157         }
158         
159         
160         private List<CommandStateListener> listeners = new ArrayList<CommandStateListener>();
161         private MapList<String, CommandStateListener> listenerWithCommandId = new MapList<String, CommandStateListener>();
162         private MapList<IWorkbenchPart, CommandStateListener> listenerWithPart = new MapList<IWorkbenchPart, CommandStateListener>();
163         
164         /**
165          * Attaches a listener that receives all state changes of all commands and all workbench parts.
166          * @param listener
167          */
168         public void addListener(CommandStateListener listener) {
169                 if (!listeners.contains(listener))
170                         listeners.add(listener);
171         }
172         
173         /**
174          * Attaches a listener to receive state changes of specific workbench part.
175          * @param part
176          * @param listener
177          */
178         public void addListener(IWorkbenchPart part, CommandStateListener listener) {
179                 if (!listenerWithPart.contains(part,listener))
180                         listenerWithPart.add(part,listener);
181         }
182         
183         /**
184          * Attaches a listener to receive state changes of specific command.
185          * @param part
186          * @param listener
187          */
188         public void addListener(String commandId, CommandStateListener listener) {
189                 if (!listenerWithCommandId.contains(commandId,listener))
190                         listenerWithCommandId.add(commandId, listener);
191         }
192         
193         /**
194          * Removes the listener from the registry.
195          * @param listener
196          */
197         public void removeListener(CommandStateListener listener) {
198                 listeners.remove(listener);
199                 Set<String> commandIds = new HashSet<String>();
200                 commandIds.addAll(listenerWithCommandId.getKeys());
201                 for (String commandId : commandIds) {
202                         listenerWithCommandId.remove(commandId, listener);
203                 }
204                 commandIds.clear();
205                 Set<IWorkbenchPart> parts = new HashSet<IWorkbenchPart>();
206                 parts.addAll(listenerWithPart.getKeys());
207                 for (IWorkbenchPart part : parts) {
208                         listenerWithPart.remove(part, listener);
209                 }
210                 parts.clear();
211         }
212         
213         private List<CommandStateListener> fireList = new ArrayList<CommandStateListener>();
214         
215         private void fireStateChange(IWorkbenchPart part, String commandId, String state) {
216                 fireList.clear();
217                 fireList.addAll(listeners);
218                 List<CommandStateListener> list = listenerWithCommandId.getValues(commandId);
219                 if (list != null) {
220                         fireList.addAll(list);
221                         
222                 }
223                 list = listenerWithPart.getValues(part);
224                 if (list != null) {
225                         fireList.addAll(list);
226                         
227                 }       
228                 for (CommandStateListener l : fireList) {
229                         l.stateChanged(part, commandId, state);
230                 }
231
232         }
233         
234 }