]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.charts/src/org/simantics/charts/ui/LinkTimeHandler.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / ui / LinkTimeHandler.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.charts.ui;
13
14 import java.util.Map;
15
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;
33
34 public class LinkTimeHandler extends AbstractHandler implements IHandler, IElementUpdater {
35
36     public static final String COMMAND_ID = "org.simantics.charts.linktime";
37     public static final String STATE_ID = "org.simantics.charts.linktime.state";
38
39     @Override
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 );
44         if (state != null) {
45             ChartLinkData oldData = (ChartLinkData) state.getValue();
46             if (oldData == null) {
47                 TimeSeriesEditor editor = getEditor( event );
48                 ChartLinkData currentData = new ChartLinkData();
49                 if (editor != null) {
50                     editor.getFromEnd(currentData);
51                 }
52                 state.setValue( currentData );
53             } else {
54                 state.setValue(null);
55             }
56         }
57
58         service.refreshElements(COMMAND_ID, null);
59         return null;
60     }
61
62     /**
63      * Get active editor or first one
64      * @return
65      */
66     TimeSeriesEditor getEditor(ExecutionEvent event) {
67         IWorkbenchPart ap = HandlerUtil.getActivePart(event);
68         if ( ap instanceof TimeSeriesEditor ) return (TimeSeriesEditor) ap;
69
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;
77                 }
78             }
79         }
80
81         return null;
82     }
83
84     @Override
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 );
90     }
91
92 }