1 package org.simantics.ui.toolbar;
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.HashSet;
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;
21 * Registry for storing command states separately for each IEditorPart
23 * TODO : cleaning editor states and listeners is now implemented through ToolBarContributor. Is this OK?
25 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
28 public class CommandStateRegistry {
31 private static CommandStateRegistry instance;
34 public static CommandStateRegistry getInstance() {
36 instance = new CommandStateRegistry();
42 private Map<IWorkbenchPart,Map<String,Boolean>> toggleStates = new HashMap<IWorkbenchPart, Map<String,Boolean>>();
43 private Map<String,Boolean> defaultToggleStates = new HashMap<String, Boolean>();
45 private Map<String,String> defaultRadioStates = new HashMap<String, String>();
46 private Map<IWorkbenchPart,Map<String,String>> radioStates = new HashMap<IWorkbenchPart, Map<String,String>>();
48 private ICommandService service;
49 private IHandlerService handlerService;
51 private CommandStateRegistry() {
52 service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
53 handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
57 * Stores default state for a command.
59 * Note: uses current state as default.
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);
75 throw new IllegalArgumentException("Command " + commandId + " does not have a state");
79 * Stores toggle state of a command.
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);
90 editorStates.put(commandId, checked);
91 fireStateChange(part, commandId, Boolean.toString(checked));
95 * Stores radio state of a command.
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);
106 editorStates.put(commandId, value);
107 fireStateChange(part, commandId, value);
112 public Map<String, Boolean> getDefaultToggleStates() {
113 return defaultToggleStates;
116 public Map<String, String> getDefaultRadioStates() {
117 return defaultRadioStates;
120 public Map<String, Boolean> getEditorToggleStates(IWorkbenchPart part) {
121 return toggleStates.get(part);
124 public Map<String, String> getEditorRadioStates(IWorkbenchPart part) {
125 return radioStates.get(part);
128 public Boolean getToggleState(IWorkbenchPart part, String commandId) {
130 return defaultToggleStates.get(commandId);
131 Map<String,Boolean> editorStates = toggleStates.get(part);
132 if (editorStates == null) {
133 return defaultToggleStates.get(commandId);
135 return editorStates.get(commandId);
138 public String getRadioState(IWorkbenchPart part, String commandId) {
140 return defaultRadioStates.get(commandId);
141 Map<String,String> editorStates = radioStates.get(part);
142 if (editorStates == null) {
143 return defaultRadioStates.get(commandId);
145 return editorStates.get(commandId);
148 public void clearStates(IWorkbenchPart part) {
149 toggleStates.remove(part);
150 radioStates.remove(part);
151 listenerWithPart.remove(part);
154 private boolean getToggleState(Command command) {
155 State toggleState = command.getState(RegistryToggleState.STATE_ID);
156 return (Boolean)toggleState.getValue();
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>();
165 * Attaches a listener that receives all state changes of all commands and all workbench parts.
168 public void addListener(CommandStateListener listener) {
169 if (!listeners.contains(listener))
170 listeners.add(listener);
174 * Attaches a listener to receive state changes of specific workbench part.
178 public void addListener(IWorkbenchPart part, CommandStateListener listener) {
179 if (!listenerWithPart.contains(part,listener))
180 listenerWithPart.add(part,listener);
184 * Attaches a listener to receive state changes of specific command.
188 public void addListener(String commandId, CommandStateListener listener) {
189 if (!listenerWithCommandId.contains(commandId,listener))
190 listenerWithCommandId.add(commandId, listener);
194 * Removes the listener from the registry.
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);
205 Set<IWorkbenchPart> parts = new HashSet<IWorkbenchPart>();
206 parts.addAll(listenerWithPart.getKeys());
207 for (IWorkbenchPart part : parts) {
208 listenerWithPart.remove(part, listener);
213 private List<CommandStateListener> fireList = new ArrayList<CommandStateListener>();
215 private void fireStateChange(IWorkbenchPart part, String commandId, String state) {
217 fireList.addAll(listeners);
218 List<CommandStateListener> list = listenerWithCommandId.getValues(commandId);
220 fireList.addAll(list);
223 list = listenerWithPart.getValues(part);
225 fireList.addAll(list);
228 for (CommandStateListener l : fireList) {
229 l.stateChanged(part, commandId, state);