Skip to main content
summaryrefslogtreecommitdiffstats
blob: e4010c35e9e8c0cb6ae0e54683dbd706fb72f16f (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*******************************************************************************
 * Copyright (c) 2004, 2014 Tasktop Technologies 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:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.ui;

import java.lang.reflect.InvocationTargetException;
import java.util.Collections;
import java.util.Set;

import org.apache.commons.lang.reflect.MethodUtils;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.mylyn.commons.ui.SelectionProviderAdapter;
import org.eclipse.mylyn.internal.tasks.core.ITaskListChangeListener;
import org.eclipse.mylyn.internal.tasks.core.TaskContainerDelta;
import org.eclipse.mylyn.internal.tasks.ui.actions.RepositoryElementActionGroup;
import org.eclipse.mylyn.internal.tasks.ui.util.TasksUiInternal;
import org.eclipse.mylyn.internal.tasks.ui.views.TaskListView;
import org.eclipse.mylyn.tasks.core.ITask;
import org.eclipse.mylyn.tasks.core.ITaskActivationListener;
import org.eclipse.mylyn.tasks.core.TaskActivationAdapter;
import org.eclipse.mylyn.tasks.ui.TasksUi;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MenuDetectEvent;
import org.eclipse.swt.events.MenuDetectListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.forms.events.HyperlinkAdapter;
import org.eclipse.ui.forms.events.HyperlinkEvent;
import org.eclipse.ui.internal.ObjectActionContributorManager;
import org.eclipse.ui.menus.WorkbenchWindowControlContribution;

/**
 * @author Mik Kersten
 * @author Leo Dos Santos
 */
public class TaskTrimWidget extends WorkbenchWindowControlContribution {

	public static String ID_CONTAINER = "org.eclipse.mylyn.tasks.ui.trim.container"; //$NON-NLS-1$

	public static String ID_CONTROL = "org.eclipse.mylyn.tasks.ui.trim.control"; //$NON-NLS-1$

	private Composite composite;

	private ITask activeTask;

	private MenuManager menuManager;

	private Menu menu;

	private TaskScalingHyperlink activeTaskLabel;

	private final ITaskActivationListener taskActivationListener = new TaskActivationAdapter() {

		@Override
		public void taskActivated(ITask task) {
			activeTask = task;
			indicateActiveTask();
		}

		@Override
		public void taskDeactivated(ITask task) {
			activeTask = null;
			indicateNoActiveTask();
		}

	};

	private final ITaskListChangeListener taskListListener = new ITaskListChangeListener() {
		public void containersChanged(Set<TaskContainerDelta> containers) {
			// update label in case task changes
			if (activeTask != null) {
				for (TaskContainerDelta taskContainerDelta : containers) {
					if (activeTask.equals(taskContainerDelta.getElement())) {
						if (taskContainerDelta.getKind().equals(TaskContainerDelta.Kind.CONTENT)) {
							Display.getDefault().asyncExec(new Runnable() {
								public void run() {
									if (activeTask != null && activeTask.isActive()) {
										indicateActiveTask();
									}
								}
							});
							return;
						}
					}
				}
			}
		}
	};

	private SelectionProviderAdapter activeTaskSelectionProvider;

	private RepositoryElementActionGroup actionGroup;

	public TaskTrimWidget() {
		TasksUi.getTaskActivityManager().addActivationListener(taskActivationListener);
		TasksUiPlugin.getTaskList().addChangeListener(taskListListener);
		hookContextMenu();
	}

	@Override
	public void dispose() {
		if (composite != null && !composite.isDisposed()) {
			composite.dispose();
		}
		composite = null;

		if (menuManager != null) {
			menuManager.removeAll();
			menuManager.dispose();
		}
		menuManager = null;

		if (menu != null && !menu.isDisposed()) {
			menu.dispose();
		}
		menu = null;

		actionGroup.setSelectionProvider(null);

		TasksUi.getTaskActivityManager().removeActivationListener(taskActivationListener);
		TasksUiPlugin.getTaskList().removeChangeListener(taskListListener);
	}

	@Override
	protected Control createControl(Composite parent) {
		composite = new Composite(parent, SWT.NONE);

		GridLayout layout = new GridLayout();
		layout.numColumns = 1;
		layout.horizontalSpacing = 0;
		layout.marginHeight = 0;
		layout.marginLeft = 0;
		layout.marginRight = 0;
		composite.setLayout(layout);

		//composite.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, true));

		createStatusComposite(composite);

		return composite;
	}

	private Composite createStatusComposite(final Composite container) {
		GC gc = new GC(container);
		Point p = gc.textExtent("WWWWWWWWWWWWWWW"); //$NON-NLS-1$
		gc.dispose();

		activeTaskLabel = new TaskScalingHyperlink(container, SWT.RIGHT);
		// activeTaskLabel.setLayoutData(new GridData(p.x, SWT.DEFAULT));
		GridData gridData = new GridData(SWT.LEFT, SWT.CENTER, false, true);
		gridData.widthHint = p.x;
		gridData.minimumWidth = p.x;
		gridData.horizontalIndent = 0;
		activeTaskLabel.setLayoutData(gridData);
		activeTaskLabel.setText(Messages.TaskTrimWidget__no_task_active_);

		activeTask = TasksUi.getTaskActivityManager().getActiveTask();
		if (activeTask != null) {
			indicateActiveTask();
		}

		activeTaskLabel.addMenuDetectListener(new MenuDetectListener() {
			public void menuDetected(MenuDetectEvent e) {
				if (menu != null) {
					menu.dispose();
				}
				menu = menuManager.createContextMenu(container);
				menu.setVisible(true);
			}
		});

		activeTaskLabel.addHyperlinkListener(new HyperlinkAdapter() {
			@Override
			public void linkActivated(HyperlinkEvent e) {
				TaskListView taskListView = TaskListView.getFromActivePerspective();
				if (taskListView != null && taskListView.getDrilledIntoCategory() != null) {
					taskListView.goUpToRoot();
				}
				TasksUiInternal.refreshAndOpenTaskListElement((TasksUi.getTaskActivityManager().getActiveTask()));
			}
		});

		activeTaskLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				// only handle left clicks, context menu is handled by platform
				if (e.button == 1) {
					if (activeTask == null) {
						return;
					}

					TaskListView taskListView = TaskListView.getFromActivePerspective();
					if (taskListView != null && taskListView.getDrilledIntoCategory() != null) {
						taskListView.goUpToRoot();
					}

					TasksUiInternal.refreshAndOpenTaskListElement(activeTask);
				}
			}
		});

		return activeTaskLabel;
	}

	private void hookContextMenu() {
		activeTaskSelectionProvider = new SelectionProviderAdapter();

		actionGroup = new RepositoryElementActionGroup();
		actionGroup.setSelectionProvider(activeTaskSelectionProvider);

		menuManager = new MenuManager("#PopupMenu"); //$NON-NLS-1$
		menuManager.setRemoveAllWhenShown(true);
		menuManager.addMenuListener(new IMenuListener() {
			public void menuAboutToShow(IMenuManager manager) {
				actionGroup.fillContextMenu(manager);
				// trims do not have a workbench part so there is no simple way of registering the
				// context menu
				if (!contributeObjectActionsOld(manager)) {
					contributeObjectActionsNew(manager);
				}
			}
		});
	}

	public void indicateActiveTask() {
		if (activeTaskLabel == null || activeTaskLabel.isDisposed()) {
			return;
		}

		//activeTaskLabel.setText(shortenText(activeTask.getSummary()));
		activeTaskLabel.setText(activeTask.getSummary());
		activeTaskLabel.setUnderlined(true);
		activeTaskLabel.setToolTipText(activeTask.getSummary());
		activeTaskSelectionProvider.setSelection(new StructuredSelection(activeTask));
	}

	public void indicateNoActiveTask() {
		if (activeTaskLabel == null || activeTaskLabel.isDisposed()) {
			return;
		}

		activeTaskLabel.setText(Messages.TaskTrimWidget__no_active_task_);
		activeTaskLabel.setUnderlined(false);
		activeTaskLabel.setToolTipText(""); //$NON-NLS-1$
		activeTaskSelectionProvider.setSelection(StructuredSelection.EMPTY);
	}

	private boolean contributeObjectActionsOld(IMenuManager manager) {
		try {
			MethodUtils.invokeExactMethod(ObjectActionContributorManager.getManager(), "contributeObjectActions", //$NON-NLS-1$
					new Object[] { null, manager, activeTaskSelectionProvider }, new Class[] { IWorkbenchPart.class,
				IMenuManager.class, ISelectionProvider.class });
		} catch (NoSuchMethodException e) {
			return false;
		} catch (IllegalAccessException e) {
			return false;
		} catch (InvocationTargetException e) {
			return false;
		}
		return true;
	}

	private boolean contributeObjectActionsNew(IMenuManager manager) {
		try {
			MethodUtils.invokeExactMethod(ObjectActionContributorManager.getManager(), "contributeObjectActions", //$NON-NLS-1$
					new Object[] { null, manager, activeTaskSelectionProvider, Collections.EMPTY_SET }, new Class[] {
				IWorkbenchPart.class, IMenuManager.class, ISelectionProvider.class, Set.class });
		} catch (NoSuchMethodException e) {
			return false;
		} catch (IllegalAccessException e) {
			return false;
		} catch (InvocationTargetException e) {
			return false;
		}
		return true;
	}

//	// From PerspectiveBarContributionItem
//	private String shortenText(String taskLabel) {
//		if (taskLabel == null || composite == null || composite.isDisposed()) {
//			return null;
//		}
//
//		String returnText = taskLabel;
//		GC gc = new GC(composite);
//		int maxWidth = p.x;
//
//		if (gc.textExtent(taskLabel).x > maxWidth) {
//			for (int i = taskLabel.length(); i > 0; i--) {
//				String test = taskLabel.substring(0, i);
//				test = test + "...";
//				if (gc.textExtent(test).x < maxWidth) {
//					returnText = test;
//					break;
//				}
//			}
//		}
//
//		gc.dispose();
//		return returnText;
//	}
}

Back to the top