X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.charts%2Fsrc%2Forg%2Fsimantics%2Fcharts%2Fui%2FLinkTimeHandler.java;fp=bundles%2Forg.simantics.charts%2Fsrc%2Forg%2Fsimantics%2Fcharts%2Fui%2FLinkTimeHandler.java;h=1f04f7056b6ef2967a27e1d30409a6578cbc51c5;hp=ce08eddcb531c6b8ea85a5646776781a43f81fbd;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 diff --git a/bundles/org.simantics.charts/src/org/simantics/charts/ui/LinkTimeHandler.java b/bundles/org.simantics.charts/src/org/simantics/charts/ui/LinkTimeHandler.java index ce08eddcb..1f04f7056 100644 --- a/bundles/org.simantics.charts/src/org/simantics/charts/ui/LinkTimeHandler.java +++ b/bundles/org.simantics.charts/src/org/simantics/charts/ui/LinkTimeHandler.java @@ -1,92 +1,92 @@ -/******************************************************************************* - * Copyright (c) 2007, 2011 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.charts.ui; - -import java.util.Map; - -import org.eclipse.core.commands.AbstractHandler; -import org.eclipse.core.commands.Command; -import org.eclipse.core.commands.ExecutionEvent; -import org.eclipse.core.commands.ExecutionException; -import org.eclipse.core.commands.IHandler; -import org.eclipse.core.commands.State; -import org.eclipse.ui.IEditorPart; -import org.eclipse.ui.IEditorReference; -import org.eclipse.ui.IWorkbenchPage; -import org.eclipse.ui.IWorkbenchPart; -import org.eclipse.ui.IWorkbenchWindow; -import org.eclipse.ui.PlatformUI; -import org.eclipse.ui.commands.ICommandService; -import org.eclipse.ui.commands.IElementUpdater; -import org.eclipse.ui.handlers.HandlerUtil; -import org.eclipse.ui.menus.UIElement; -import org.simantics.charts.editor.TimeSeriesEditor; - -public class LinkTimeHandler extends AbstractHandler implements IHandler, IElementUpdater { - - public static final String COMMAND_ID = "org.simantics.charts.linktime"; - public static final String STATE_ID = "org.simantics.charts.linktime.state"; - - @Override - public Object execute(ExecutionEvent event) throws ExecutionException { - ICommandService service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class); - Command command = service.getCommand( COMMAND_ID ); - State state = command.getState( STATE_ID ); - if (state != null) { - ChartLinkData oldData = (ChartLinkData) state.getValue(); - if (oldData == null) { - TimeSeriesEditor editor = getEditor( event ); - ChartLinkData currentData = new ChartLinkData(); - if (editor != null) { - editor.getFromEnd(currentData); - } - state.setValue( currentData ); - } else { - state.setValue(null); - } - } - - service.refreshElements(COMMAND_ID, null); - return null; - } - - /** - * Get active editor or first one - * @return - */ - TimeSeriesEditor getEditor(ExecutionEvent event) { - IWorkbenchPart ap = HandlerUtil.getActivePart(event); - if ( ap instanceof TimeSeriesEditor ) return (TimeSeriesEditor) ap; - - IWorkbenchWindow win = HandlerUtil.getActiveWorkbenchWindow(event); - if (win==null) return null; - for (IWorkbenchPage page : win.getPages()) { - for (IEditorReference ref : page.getEditorReferences()) { - if (ref.getId().equals(TimeSeriesEditor.ID)) { - IEditorPart part = ref.getEditor(false); - return (TimeSeriesEditor) part; - } - } - } - - return null; - } - - @Override - public void updateElement(UIElement element, @SuppressWarnings("rawtypes") Map parameters) { - ICommandService service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class); - Command command = service.getCommand( COMMAND_ID ); - State state = command.getState( STATE_ID ); - element.setChecked( state != null && state.getValue()!=null ); - } - -} +/******************************************************************************* + * Copyright (c) 2007, 2011 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.charts.ui; + +import java.util.Map; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.Command; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.commands.IHandler; +import org.eclipse.core.commands.State; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.IEditorReference; +import org.eclipse.ui.IWorkbenchPage; +import org.eclipse.ui.IWorkbenchPart; +import org.eclipse.ui.IWorkbenchWindow; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.commands.ICommandService; +import org.eclipse.ui.commands.IElementUpdater; +import org.eclipse.ui.handlers.HandlerUtil; +import org.eclipse.ui.menus.UIElement; +import org.simantics.charts.editor.TimeSeriesEditor; + +public class LinkTimeHandler extends AbstractHandler implements IHandler, IElementUpdater { + + public static final String COMMAND_ID = "org.simantics.charts.linktime"; + public static final String STATE_ID = "org.simantics.charts.linktime.state"; + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + ICommandService service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class); + Command command = service.getCommand( COMMAND_ID ); + State state = command.getState( STATE_ID ); + if (state != null) { + ChartLinkData oldData = (ChartLinkData) state.getValue(); + if (oldData == null) { + TimeSeriesEditor editor = getEditor( event ); + ChartLinkData currentData = new ChartLinkData(); + if (editor != null) { + editor.getFromEnd(currentData); + } + state.setValue( currentData ); + } else { + state.setValue(null); + } + } + + service.refreshElements(COMMAND_ID, null); + return null; + } + + /** + * Get active editor or first one + * @return + */ + TimeSeriesEditor getEditor(ExecutionEvent event) { + IWorkbenchPart ap = HandlerUtil.getActivePart(event); + if ( ap instanceof TimeSeriesEditor ) return (TimeSeriesEditor) ap; + + IWorkbenchWindow win = HandlerUtil.getActiveWorkbenchWindow(event); + if (win==null) return null; + for (IWorkbenchPage page : win.getPages()) { + for (IEditorReference ref : page.getEditorReferences()) { + if (ref.getId().equals(TimeSeriesEditor.ID)) { + IEditorPart part = ref.getEditor(false); + return (TimeSeriesEditor) part; + } + } + } + + return null; + } + + @Override + public void updateElement(UIElement element, @SuppressWarnings("rawtypes") Map parameters) { + ICommandService service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class); + Command command = service.getCommand( COMMAND_ID ); + State state = command.getState( STATE_ID ); + element.setChecked( state != null && state.getValue()!=null ); + } + +}