/******************************************************************************* * Copyright (c) 2012, 2013 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.g3d.toolbar; import java.util.HashMap; import java.util.Map; import org.eclipse.core.commands.Command; import org.eclipse.core.commands.State; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.commands.ICommandService; import org.eclipse.ui.handlers.IHandlerService; import org.eclipse.ui.handlers.RadioState; import org.eclipse.ui.handlers.RegistryToggleState; /** * Registry for storing command states separately for each IEditorPart * * TODO : how to change toggle/radios state from editor? * TODO : how to update visible buttons (ToolbarContributor) * * @author Marko Luukkainen * */ public class CommandStateRegistry { private static CommandStateRegistry instance; public static CommandStateRegistry getInstance() { if (instance == null) instance = new CommandStateRegistry(); return instance; } private Map> toggleStates = new HashMap>(); private Map defaultToggleStates = new HashMap(); private Map defaultRadioStates = new HashMap(); private Map> radioStates = new HashMap>(); private ICommandService service; private IHandlerService handlerService; private CommandStateRegistry() { service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class); handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class); } /** * Stores default state for a command. * * Note: uses current state as default. * * @param commandId */ public void storeDefaultState(String commandId) { Command command = service.getCommand(commandId); State toggleState = command.getState(RegistryToggleState.STATE_ID); State radioState = command.getState(RadioState.STATE_ID); if (toggleState != null) { if (!defaultToggleStates.containsKey(commandId)) defaultToggleStates.put(commandId, getToggleState(command)); } else if (radioState != null) { String value = (String) radioState.getValue(); if (!defaultRadioStates.containsKey(commandId)) defaultRadioStates.put(commandId, value); } else { throw new IllegalArgumentException("Command " + commandId + " does not have a state"); } } /** * Stores toggle state of a command. * @param part * @param commandId * @param checked */ public void setEditorState(IWorkbenchPart part, String commandId, boolean checked) { Map editorStates = toggleStates.get(part); if (editorStates == null) { editorStates = new HashMap(); toggleStates.put(part, editorStates); } editorStates.put(commandId, checked); } /** * Stores radio state of a command. * @param part * @param commandId * @param value */ public void setEditorState(IWorkbenchPart part, String commandId, String value) { Map editorStates = radioStates.get(part); if (editorStates == null) { editorStates = new HashMap(); radioStates.put(part, editorStates); } editorStates.put(commandId, value); } public Map getDefaultToggleStates() { return defaultToggleStates; } public Map getDefaultRadioStates() { return defaultRadioStates; } public Map getEditorToggleStates(IWorkbenchPart part) { return toggleStates.get(part); } public Map getEditorRadioStates(IWorkbenchPart part) { return radioStates.get(part); } public Boolean getToggleState(IWorkbenchPart part, String commandId) { if (part == null) return defaultToggleStates.get(commandId); Map editorStates = toggleStates.get(part); if (editorStates == null) { return defaultToggleStates.get(commandId); } return editorStates.get(commandId); } public String getRadioState(IWorkbenchPart part, String commandId) { if (part == null) return defaultRadioStates.get(commandId); Map editorStates = radioStates.get(part); if (editorStates == null) { return defaultRadioStates.get(commandId); } return editorStates.get(commandId); } public void clearStates(IWorkbenchPart part) { toggleStates.remove(part); radioStates.remove(part); } private boolean getToggleState(Command command) { State toggleState = command.getState(RegistryToggleState.STATE_ID); return (Boolean)toggleState.getValue(); } }