]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
2d32ce846db95f3ec33f1b0eeb16d6769fc3918c
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.ui.handlers;\r
13 \r
14 import java.util.Map;\r
15 \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
29 \r
30 /**\r
31  * This handler changes the orientation of a {@link ChartPanel}\r
32  * \r
33  * @author Teemu Lempinen\r
34  *\r
35  */\r
36 public class ChartPanelOrientationHandler extends AbstractHandler implements IElementUpdater {\r
37 \r
38     private static String COMMAND = "org.simantics.sysdyn.ui.chartPanelOrientation";\r
39     \r
40     /**\r
41      * Read chart panel settings from IDialogSettings and change the orientation accordingly.\r
42      * Finally order the element to update its appearance.\r
43      */\r
44     @Override\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
51             }\r
52             \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
56             \r
57             if(ChartPanel.CHART_PANEL_VERTICAL.equals(orientation))\r
58                 orientation = ChartPanel.CHART_PANEL_HORIZONTAL;\r
59             else\r
60                 orientation = ChartPanel.CHART_PANEL_VERTICAL;\r
61             \r
62             settings.put(ChartPanel.CHART_PANEL_ORIENTATION, orientation);\r
63             ((ChartPanel)part).setOrientation(orientation);\r
64             \r
65             ICommandService commandService =\r
66                     (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);\r
67             commandService.refreshElements(COMMAND, null);\r
68         }\r
69 \r
70         return null;\r
71     }\r
72 \r
73     /**\r
74      * Update the icon of the element. The new icon and text are always opposite to the current situation.\r
75      */\r
76     @SuppressWarnings("rawtypes")\r
77     @Override\r
78     public void updateElement(UIElement element, Map parameters) {\r
79         if(parameters == null)\r
80             return;\r
81         \r
82         IDialogSettings settings = Activator.getDefault().getDialogSettings().getSection(ChartPanel.CHART_PANEL_SETTINGS);\r
83         if(settings == null)\r
84             return;\r
85         \r
86         String orientation = settings.get(ChartPanel.CHART_PANEL_ORIENTATION);\r
87         if(orientation == null)\r
88             return;\r
89         \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
97         }\r
98     }\r
99 \r
100 }\r