Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 33ed96a3e0e2589a67af2a1179069bc9c2931a42 (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
/******************************************************************************
 *  Copyright (c) 2011 GitHub Inc.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License 2.0
 *  which accompanies this distribution, and is available at
 *  https://www.eclipse.org/legal/epl-2.0/
 *
 *  SPDX-License-Identifier: EPL-2.0
 *
 *  Contributors:
 *    Kevin Sawicki (GitHub Inc.) - initial API and implementation
 *****************************************************************************/
package org.eclipse.mylyn.internal.github.ui;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.expressions.EvaluationContext;
import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.mylyn.tasks.core.ITask;
import org.eclipse.mylyn.tasks.core.data.TaskData;
import org.eclipse.mylyn.tasks.ui.TasksUi;
import org.eclipse.ui.ISources;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchSite;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.handlers.IHandlerService;
import org.eclipse.ui.progress.IWorkbenchSiteProgressService;

/**
 * Base handler for working with a {@link TaskData} selection
 */
public abstract class TaskDataHandler extends AbstractHandler {

	/**
	 * POST_HANDLER_CALLBACK - variable for post handler callback that is a
	 * {@link Runnable}
	 */
	public static final String POST_HANDLER_CALLBACK = "postHandlerCallback";

	/**
	 * Create context with given selection
	 * 
	 * @param selection
	 * @param handlerService
	 * @return context
	 */
	public static IEvaluationContext createContext(
			IStructuredSelection selection, IHandlerService handlerService) {
		IEvaluationContext context = new EvaluationContext(
				handlerService.createContextSnapshot(false), selection.toList());
		context.addVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME, selection);
		context.removeVariable(ISources.ACTIVE_MENU_SELECTION_NAME);
		return context;
	}

	/**
	 * Get task data from event
	 * 
	 * @param event
	 * @return task data
	 */
	protected TaskData getTaskData(ExecutionEvent event) {
		ISelection selection = HandlerUtil.getCurrentSelection(event);
		if (selection == null || selection.isEmpty())
			selection = HandlerUtil.getActiveMenuSelection(event);

		if (selection instanceof IStructuredSelection && !selection.isEmpty()) {
			Object first = ((IStructuredSelection) selection).getFirstElement();
			if (first instanceof TaskData)
				return (TaskData) first;
			else if (first instanceof ITask)
				try {
					return TasksUi.getTaskDataManager().getTaskData(
							(ITask) first);
				} catch (CoreException e) {
					return null;
				}
		}
		return null;
	}

	/**
	 * Should this handler be enabled for the given task data?
	 * 
	 * Always returns true by default, sub-classes should override
	 * 
	 * @param data
	 * @return true is enabled, false otherwise
	 */
	protected boolean isEnabled(TaskData data) {
		return true;
	}

	/**
	 * Schedule job
	 * 
	 * @param job
	 * @param event
	 */
	protected void schedule(Job job, ExecutionEvent event) {
		IWorkbenchSite site = HandlerUtil.getActiveSite(event);
		if (site == null) {
			IWorkbenchPart part = PlatformUI.getWorkbench()
					.getActiveWorkbenchWindow().getActivePage().getActivePart();
			if (part != null)
				site = part.getSite();
		}
		IWorkbenchSiteProgressService progress = site != null ? (IWorkbenchSiteProgressService) site
				.getService(IWorkbenchSiteProgressService.class) : null;
		if (progress != null)
			progress.schedule(job);
		else
			job.schedule();
	}

	/**
	 * Execute callback on trigger if configured
	 * 
	 * @param event
	 */
	protected void executeCallback(ExecutionEvent event) {
		Object callback = HandlerUtil.getVariable(event, POST_HANDLER_CALLBACK);
		if (callback instanceof Runnable)
			((Runnable) callback).run();
	}
}

Back to the top