Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3933c2cce2330747aeaee6753e41e7029b8cbed7 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*******************************************************************************
 * Copyright (c) 2013 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.handler;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

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.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExecutableExtension;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer;
import org.eclipse.tcf.te.runtime.services.ServiceManager;
import org.eclipse.tcf.te.runtime.services.interfaces.IService;
import org.eclipse.tcf.te.runtime.stepper.interfaces.IStepContext;
import org.eclipse.tcf.te.runtime.stepper.interfaces.IStepperOperationService;
import org.eclipse.tcf.te.runtime.stepper.job.StepperJob;
import org.eclipse.tcf.te.runtime.utils.StatusHelper;
import org.eclipse.tcf.te.ui.activator.UIPlugin;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.part.EditorPart;

/**
 * Stepper command handler implementation.
 */
public class StepperCommandHandler extends AbstractHandler implements IExecutableExtension {

	protected String operation = null;
	protected String adaptTo = null;

	/**
	 * Part id: Project Explorer view
	 */
	public static final String PART_ID_PROJECT_VIEW = "org.eclipse.ui.navigator.ProjectExplorer"; //$NON-NLS-1$

	/* (non-Javadoc)
	 * @see com.windriver.te.tcf.ui.handler.AbstractAgentCommandHandler#execute(org.eclipse.core.commands.ExecutionEvent)
	 */
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		Assert.isNotNull(operation);

		IStructuredSelection selection = getSelection(event);
		Assert.isNotNull(selection);

		Iterator<?> iterator = selection.iterator();
		while (iterator.hasNext()) {
			Object element = iterator.next();
			Object adapted = element;
			if (adaptTo != null) {
				Object adapter = Platform.getAdapterManager().getAdapter(element, adaptTo);
				if (adapter != null) adapted = adapter;
			}
			IStepperOperationService stepperOperationService = getStepperService(adapted, operation);
			if (stepperOperationService != null) {
				IStepContext stepContext = stepperOperationService.getStepContext(adapted, operation);
				String stepGroupId = stepperOperationService.getStepGroupId(adapted, operation);
				String name = stepperOperationService.getStepGroupName(adapted, operation);
				boolean isCancelable = stepperOperationService.isCancelable(adapted, operation);
				IPropertiesContainer data = stepperOperationService.getStepData(adapted, operation);

				if (stepGroupId != null && stepContext != null) {
					scheduleStepperJob(stepContext, data, stepGroupId, name, isCancelable);
				}
			}
		}

		return null;
	}

	/**
	 * Get the stepper service for the given context and operation.
	 *
	 * @param context The context.
	 * @param operation The operation.
	 * @return The stepper service or <code>null</code>.
	 */
	protected IStepperOperationService getStepperService(Object context, String operation) {
		IService[] services = ServiceManager.getInstance().getServices(context, IStepperOperationService.class, false);
		IStepperOperationService stepperOperationService = null;
		for (IService service : services) {
			if (service instanceof IStepperOperationService && ((IStepperOperationService)service).isHandledOperation(context, operation)) {
				stepperOperationService = (IStepperOperationService)service;
				break;
			}
        }
		return stepperOperationService;
	}

	/**
	 * Get the selection for the handler execution.
	 *
	 * @param event The event.
	 * @return The selection.
	 */
	protected IStructuredSelection getSelection(ExecutionEvent event) {
		// Get the current selection
		ISelection selection = HandlerUtil.getCurrentSelection(event);

		List<Object> elements = new ArrayList<Object>();
		if (selection instanceof IStructuredSelection && !selection.isEmpty()) {
			Iterator<Object> iterator = ((IStructuredSelection)selection).iterator();
			while (iterator.hasNext()) elements.add(iterator.next());
		}

		// Get the active part
		IWorkbenchPart part = HandlerUtil.getActivePart(event);
		// If the handler is invoked from an editor part, construct an artificial selection
		// from the active editor input.
		if (part instanceof EditorPart) {
			IEditorInput input = ((EditorPart)part).getEditorInput();
			Object element = input != null ? input.getAdapter(Object.class) : null;
			if (element != null && !elements.contains(element)) elements.add(element);
		}

		selection = elements.isEmpty() ? new StructuredSelection() : new StructuredSelection(elements);

		return (IStructuredSelection)selection;
	}

	/**
	 * Schedule the stepper job.
	 * @param stepContext The step context.
	 * @param data The execution data.
	 * @param stepGroupId The step group id to execute.
	 * @param name The job name.
	 * @param isCancelable <code>true</code> if the job should be cancelable.
	 */
	protected void scheduleStepperJob(IStepContext stepContext, IPropertiesContainer data, String stepGroupId, String name, boolean isCancelable) {
		try {
			StepperJob job = new StepperJob(name != null ? name : "", //$NON-NLS-1$
											stepContext,
											data,
											stepGroupId,
											operation,
											isCancelable);
			job.schedule();
		} catch (IllegalStateException e) {
			if (Platform.inDebugMode()) {
				UIPlugin.getDefault().getLog().log(StatusHelper.getStatus(e));
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.runtime.IExecutableExtension#setInitializationData(org.eclipse.core.runtime.IConfigurationElement, java.lang.String, java.lang.Object)
	 */
	@Override
	public void setInitializationData(IConfigurationElement config, String propertyName, Object data) throws CoreException {
		if (data instanceof Map) {
			Map<?,?> dataMap = (Map<?,?>)data;
			if (dataMap.get("operation") instanceof String) { //$NON-NLS-1$
				this.operation = dataMap.get("operation").toString(); //$NON-NLS-1$
			}
			if (dataMap.get("adaptTo") instanceof String) { //$NON-NLS-1$
				this.adaptTo = dataMap.get("adaptTo").toString(); //$NON-NLS-1$
			}
		}
	}

	public static IStructuredSelection getPartSelection(String partId) {
		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		if (partId != null && window != null && window.getActivePage() != null) {
			ISelection sel = window.getActivePage().getSelection(partId);

			if (sel instanceof IStructuredSelection) {
				return (IStructuredSelection)sel;
			}
		}
		return null;
	}

	public static IStructuredSelection getEditorInputSelection() {
		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		if (window != null && window.getActivePage() != null && window.getActivePage().getActiveEditor() != null) {
			return new StructuredSelection(window.getActivePage().getActiveEditor().getEditorInput());
		}
		return null;
	}
}

Back to the top