1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.handlers;
\r
14 import java.util.Map;
\r
16 import org.eclipse.core.commands.AbstractHandler;
\r
17 import org.eclipse.core.commands.ExecutionEvent;
\r
18 import org.eclipse.core.commands.ExecutionException;
\r
19 import org.eclipse.jface.dialogs.IDialogSettings;
\r
20 import org.eclipse.jface.resource.ImageDescriptor;
\r
21 import org.eclipse.ui.IWorkbenchPart;
\r
22 import org.eclipse.ui.PlatformUI;
\r
23 import org.eclipse.ui.commands.ICommandService;
\r
24 import org.eclipse.ui.commands.IElementUpdater;
\r
25 import org.eclipse.ui.handlers.HandlerUtil;
\r
26 import org.eclipse.ui.menus.UIElement;
\r
27 import org.simantics.jfreechart.ChartPanel;
\r
28 import org.simantics.sysdyn.ui.Activator;
\r
31 * This handler changes the orientation of a {@link ChartPanel}
\r
33 * @author Teemu Lempinen
\r
36 public class ChartPanelOrientationHandler extends AbstractHandler implements IElementUpdater {
\r
38 private static String COMMAND = "org.simantics.sysdyn.ui.chartPanelOrientation";
\r
41 * Read chart panel settings from IDialogSettings and change the orientation accordingly.
\r
42 * Finally order the element to update its appearance.
\r
45 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
46 IWorkbenchPart part = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getActivePart();
\r
47 if(part instanceof ChartPanel) {
\r
48 IDialogSettings settings = Activator.getDefault().getDialogSettings().getSection(ChartPanel.CHART_PANEL_SETTINGS);
\r
49 if (settings == null) {
\r
50 settings = Activator.getDefault().getDialogSettings().addNewSection(ChartPanel.CHART_PANEL_SETTINGS);
\r
53 String orientation = settings.get(ChartPanel.CHART_PANEL_ORIENTATION);
\r
54 if(orientation == null)
\r
55 settings.put(ChartPanel.CHART_PANEL_ORIENTATION, ChartPanel.CHART_PANEL_HORIZONTAL);
\r
57 if(ChartPanel.CHART_PANEL_VERTICAL.equals(orientation))
\r
58 orientation = ChartPanel.CHART_PANEL_HORIZONTAL;
\r
60 orientation = ChartPanel.CHART_PANEL_VERTICAL;
\r
62 settings.put(ChartPanel.CHART_PANEL_ORIENTATION, orientation);
\r
63 ((ChartPanel)part).setOrientation(orientation);
\r
65 ICommandService commandService =
\r
66 (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
\r
67 commandService.refreshElements(COMMAND, null);
\r
74 * Update the icon of the element. The new icon and text are always opposite to the current situation.
\r
76 @SuppressWarnings("rawtypes")
\r
78 public void updateElement(UIElement element, Map parameters) {
\r
79 if(parameters == null)
\r
82 IDialogSettings settings = Activator.getDefault().getDialogSettings().getSection(ChartPanel.CHART_PANEL_SETTINGS);
\r
83 if(settings == null)
\r
86 String orientation = settings.get(ChartPanel.CHART_PANEL_ORIENTATION);
\r
87 if(orientation == null)
\r
90 // Show the opposite icon and text to indicate change when the button is pressed
\r
91 if(ChartPanel.CHART_PANEL_HORIZONTAL.equals(orientation)) {
\r
92 element.setIcon(ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/page_white_text.png")));
\r
93 element.setTooltip("Vertical Orientation");
\r
94 } else if (ChartPanel.CHART_PANEL_VERTICAL.equals(orientation)) {
\r
95 element.setIcon(ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/page_white_text_width.png")));
\r
96 element.setTooltip("Horizontal Orientation");
\r