Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 89d05056944dbdcca78ccf8e2d48606bb283a9a2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*******************************************************************************
 * Copyright (c) 2011, 2014 Wind River Systems, Inc. and others. 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.ui.views.handler;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.tcf.te.runtime.services.ServiceUtils;
import org.eclipse.tcf.te.ui.interfaces.handler.IEditorHandlerDelegate;
import org.eclipse.tcf.te.ui.views.activator.UIPlugin;
import org.eclipse.tcf.te.ui.views.editor.EditorInput;
import org.eclipse.tcf.te.ui.views.interfaces.IUIConstants;
import org.eclipse.tcf.te.ui.views.nls.Messages;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.navigator.CommonNavigator;

/**
 * TCF tree elements open command handler implementation.
 */
public class OpenEditorHandler extends AbstractHandler {

	/* (non-Javadoc)
	 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
	 */
    @Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		// The selection is the Target Explorer tree selection
		ISelection selection = HandlerUtil.getCurrentSelection(event);
		// The active part is the Target Explorer view instance
		IWorkbenchPart part = HandlerUtil.getActivePart(event);
		// Get the currently active workbench window
        // In Eclipse 4.x, the HandlerUtil.getActiveWorkbenchWindow(event) may return null
        IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
        if (window == null) window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		// ALT - Key pressed?
		Object ctrlPressed = HandlerUtil.getVariable(event, "ctrlPressed"); //$NON-NLS-1$

		boolean expand = ctrlPressed instanceof Boolean ? ((Boolean)ctrlPressed).booleanValue() : false;

		if (selection instanceof IStructuredSelection && !selection.isEmpty()) {
			// If the tree node is expandable, expand or collapse it
			TreeViewer viewer = ((CommonNavigator)part).getCommonViewer();
			Object element = ((IStructuredSelection)selection).getFirstElement();
			if (selection instanceof TreeSelection) {
				TreePath[] path = ((TreeSelection)selection).getPaths();
				if (path != null && path.length > 0) {
					element = path[0].getLastSegment();
				}
			}
			if (viewer.isExpandable(element) && expand) {
				viewer.setExpandedState(element, !viewer.getExpandedState(element));
			}
			else {
				openEditorOnSelection(window, selection);
			}
		}

		return null;
	}

	/**
	 * Opens the properties editor in the given workbench window on the given selection.
	 *
	 * @param window The workbench window. Must not be <code>null</code>.
	 * @param selection The selection. Must not be <code>null</code>.
	 */
	public static void openEditorOnSelection(IWorkbenchWindow window, ISelection selection) {
		Assert.isNotNull(window);
		Assert.isNotNull(selection);

		if (selection instanceof IStructuredSelection && !selection.isEmpty()) {
			Object element = ((IStructuredSelection)selection).getFirstElement();
			if (element != null) {
				// Get the active page
				IWorkbenchPage page = window.getActivePage();
				// Create the editor input object
				IEditorHandlerDelegate delegate = ServiceUtils.getUIServiceDelegate(element, element, IEditorHandlerDelegate.class);
				IEditorInput input = delegate != null ? delegate.getEditorInput(element) : new EditorInput(element);
				try {
					// Opens the Target Explorer properties editor
					IEditorPart editor = page.openEditor(input, IUIConstants.ID_EDITOR);
					// Lookup the ui service for post action
					if (delegate != null)
						delegate.postOpenEditor(editor, element);
				} catch (PartInitException e) {
					IStatus status = new Status(IStatus.ERROR, UIPlugin.getUniqueIdentifier(), Messages.OpenCommandHandler_error_openEditor, e);
					UIPlugin.getDefault().getLog().log(status);
				}
			}
		}
	}
}

Back to the top