--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.charts.ui;\r
+\r
+import java.util.Map;\r
+\r
+import org.eclipse.core.commands.AbstractHandler;\r
+import org.eclipse.core.commands.Command;\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.core.commands.IHandler;\r
+import org.eclipse.core.commands.State;\r
+import org.eclipse.ui.IEditorPart;\r
+import org.eclipse.ui.IEditorReference;\r
+import org.eclipse.ui.IWorkbenchPage;\r
+import org.eclipse.ui.IWorkbenchPart;\r
+import org.eclipse.ui.IWorkbenchWindow;\r
+import org.eclipse.ui.PlatformUI;\r
+import org.eclipse.ui.commands.ICommandService;\r
+import org.eclipse.ui.commands.IElementUpdater;\r
+import org.eclipse.ui.handlers.HandlerUtil;\r
+import org.eclipse.ui.menus.UIElement;\r
+import org.simantics.charts.editor.TimeSeriesEditor;\r
+\r
+public class LinkTimeHandler extends AbstractHandler implements IHandler, IElementUpdater {\r
+\r
+ public static final String COMMAND_ID = "org.simantics.charts.linktime";\r
+ public static final String STATE_ID = "org.simantics.charts.linktime.state";\r
+\r
+ @Override\r
+ public Object execute(ExecutionEvent event) throws ExecutionException {\r
+ ICommandService service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);\r
+ Command command = service.getCommand( COMMAND_ID );\r
+ State state = command.getState( STATE_ID );\r
+ if (state != null) {\r
+ ChartLinkData oldData = (ChartLinkData) state.getValue();\r
+ if (oldData == null) {\r
+ TimeSeriesEditor editor = getEditor( event );\r
+ ChartLinkData currentData = new ChartLinkData();\r
+ if (editor != null) {\r
+ editor.getFromEnd(currentData);\r
+ }\r
+ state.setValue( currentData );\r
+ } else {\r
+ state.setValue(null);\r
+ }\r
+ }\r
+\r
+ service.refreshElements(COMMAND_ID, null);\r
+ return null;\r
+ }\r
+\r
+ /**\r
+ * Get active editor or first one\r
+ * @return\r
+ */\r
+ TimeSeriesEditor getEditor(ExecutionEvent event) {\r
+ IWorkbenchPart ap = HandlerUtil.getActivePart(event);\r
+ if ( ap instanceof TimeSeriesEditor ) return (TimeSeriesEditor) ap;\r
+\r
+ IWorkbenchWindow win = HandlerUtil.getActiveWorkbenchWindow(event);\r
+ if (win==null) return null;\r
+ for (IWorkbenchPage page : win.getPages()) {\r
+ for (IEditorReference ref : page.getEditorReferences()) {\r
+ if (ref.getId().equals(TimeSeriesEditor.ID)) {\r
+ IEditorPart part = ref.getEditor(false);\r
+ return (TimeSeriesEditor) part;\r
+ }\r
+ }\r
+ }\r
+\r
+ return null;\r
+ }\r
+\r
+ @Override\r
+ public void updateElement(UIElement element, @SuppressWarnings("rawtypes") Map parameters) {\r
+ ICommandService service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);\r
+ Command command = service.getCommand( COMMAND_ID );\r
+ State state = command.getState( STATE_ID );\r
+ element.setChecked( state != null && state.getValue()!=null );\r
+ }\r
+\r
+}\r