1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.charts.ui;
16 import org.eclipse.core.commands.AbstractHandler;
17 import org.eclipse.core.commands.Command;
18 import org.eclipse.core.commands.ExecutionEvent;
19 import org.eclipse.core.commands.ExecutionException;
20 import org.eclipse.core.commands.IHandler;
21 import org.eclipse.core.commands.State;
22 import org.eclipse.ui.IEditorPart;
23 import org.eclipse.ui.IEditorReference;
24 import org.eclipse.ui.IWorkbenchPage;
25 import org.eclipse.ui.IWorkbenchPart;
26 import org.eclipse.ui.IWorkbenchWindow;
27 import org.eclipse.ui.PlatformUI;
28 import org.eclipse.ui.commands.ICommandService;
29 import org.eclipse.ui.commands.IElementUpdater;
30 import org.eclipse.ui.handlers.HandlerUtil;
31 import org.eclipse.ui.menus.UIElement;
32 import org.simantics.charts.editor.TimeSeriesEditor;
34 public class LinkTimeHandler extends AbstractHandler implements IHandler, IElementUpdater {
36 public static final String COMMAND_ID = "org.simantics.charts.linktime";
37 public static final String STATE_ID = "org.simantics.charts.linktime.state";
40 public Object execute(ExecutionEvent event) throws ExecutionException {
41 ICommandService service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
42 Command command = service.getCommand( COMMAND_ID );
43 State state = command.getState( STATE_ID );
45 ChartLinkData oldData = (ChartLinkData) state.getValue();
46 if (oldData == null) {
47 TimeSeriesEditor editor = getEditor( event );
48 ChartLinkData currentData = new ChartLinkData();
50 editor.getFromEnd(currentData);
52 state.setValue( currentData );
58 service.refreshElements(COMMAND_ID, null);
63 * Get active editor or first one
66 TimeSeriesEditor getEditor(ExecutionEvent event) {
67 IWorkbenchPart ap = HandlerUtil.getActivePart(event);
68 if ( ap instanceof TimeSeriesEditor ) return (TimeSeriesEditor) ap;
70 IWorkbenchWindow win = HandlerUtil.getActiveWorkbenchWindow(event);
71 if (win==null) return null;
72 for (IWorkbenchPage page : win.getPages()) {
73 for (IEditorReference ref : page.getEditorReferences()) {
74 if (ref.getId().equals(TimeSeriesEditor.ID)) {
75 IEditorPart part = ref.getEditor(false);
76 return (TimeSeriesEditor) part;
85 public void updateElement(UIElement element, @SuppressWarnings("rawtypes") Map parameters) {
86 ICommandService service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
87 Command command = service.getCommand( COMMAND_ID );
88 State state = command.getState( STATE_ID );
89 element.setChecked( state != null && state.getValue()!=null );