Skip to main content
summaryrefslogtreecommitdiffstats
blob: c81f8b4ff3de592efeb1ede4bc76ec32cf0c86b7 (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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*******************************************************************************
 * Copyright (c) 2009, 2011 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.actions;

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

import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.GroupMarker;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.util.SafeRunnable;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.internal.tasks.core.AbstractTask;
import org.eclipse.mylyn.internal.tasks.core.AbstractTaskCategory;
import org.eclipse.mylyn.internal.tasks.core.LocalRepositoryConnector;
import org.eclipse.mylyn.internal.tasks.core.TaskCategory;
import org.eclipse.mylyn.internal.tasks.core.UncategorizedTaskContainer;
import org.eclipse.mylyn.internal.tasks.ui.IDynamicSubMenuContributor;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
import org.eclipse.mylyn.internal.tasks.ui.actions.CopyTaskDetailsAction.Mode;
import org.eclipse.mylyn.internal.tasks.ui.views.Messages;
import org.eclipse.mylyn.internal.tasks.ui.views.UpdateRepositoryConfigurationAction;
import org.eclipse.mylyn.tasks.core.IRepositoryElement;
import org.eclipse.mylyn.tasks.core.IRepositoryQuery;
import org.eclipse.mylyn.tasks.core.ITask;
import org.eclipse.mylyn.tasks.core.ITaskContainer;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds;

/**
 * @author Steffen Pingel
 */
public class RepositoryElementActionGroup {

	protected static final String ID_SEPARATOR_NEW = "new"; //$NON-NLS-1$

	private static final String ID_SEPARATOR_OPERATIONS = "operations"; //$NON-NLS-1$

	private static final String ID_SEPARATOR_TASKS = "tasks"; //$NON-NLS-1$

	protected static final String ID_SEPARATOR_REPOSITORY = "repository"; //$NON-NLS-1$

	private static final String ID_SEPARATOR_PROPERTIES = "properties"; //$NON-NLS-1$

	protected static final String ID_SEPARATOR_NAVIGATE = "navigate"; //$NON-NLS-1$

	private static final String ID_SEPARATOR_OPEN = "open"; //$NON-NLS-1$

	private static final String ID_SEPARATOR_SHOW_IN = "showIn"; //$NON-NLS-1$

	protected static final String ID_SEPARATOR_EDIT = "edit"; //$NON-NLS-1$

	private final CopyTaskDetailsAction copyUrlAction;

	private final CopyTaskDetailsAction copyKeyAction;

	private final CopyTaskDetailsAction copyDetailsAction;

	private final OpenTaskListElementAction openAction;

	private final OpenWithBrowserAction openWithBrowserAction;

	private final DeleteAction deleteAction;

	private final DeleteTaskEditorAction deleteTaskEditorAction;

	private final RemoveFromCategoryAction removeFromCategoryAction;

	private final ShowInSearchViewAction showInSearchViewAction;

	private final ShowInTaskListAction showInTaskListAction;

	private final TaskActivateAction activateAction;

	private final TaskDeactivateAction deactivateAction;

	private ISelectionProvider selectionProvider;

	private final List<ISelectionChangedListener> actions;

	private final AutoUpdateQueryAction autoUpdateAction;

	private final NewSubTaskAction newSubTaskAction;

	private final CloneTaskAction cloneTaskAction;

	public RepositoryElementActionGroup() {
		actions = new ArrayList<ISelectionChangedListener>();

		newSubTaskAction = add(new NewSubTaskAction());

		cloneTaskAction = add(new CloneTaskAction());

		activateAction = add(new TaskActivateAction());
		deactivateAction = new TaskDeactivateAction();

		copyKeyAction = add(new CopyTaskDetailsAction(Mode.KEY));
		copyUrlAction = add(new CopyTaskDetailsAction(Mode.URL));
		copyDetailsAction = add(new CopyTaskDetailsAction(Mode.SUMMARY_URL));
		if (!isInEditor()) {
			copyDetailsAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.COPY);
		}

		removeFromCategoryAction = add(new RemoveFromCategoryAction());

		deleteAction = add(new DeleteAction());
		deleteTaskEditorAction = add(new DeleteTaskEditorAction());
		openAction = add(new OpenTaskListElementAction());
		openWithBrowserAction = add(new OpenWithBrowserAction());
		showInSearchViewAction = add(new ShowInSearchViewAction());
		showInTaskListAction = add(new ShowInTaskListAction());

		autoUpdateAction = add(new AutoUpdateQueryAction());
	}

	protected <T extends ISelectionChangedListener> T add(T action) {
		actions.add(action);
		return action;
	}

	public void setSelectionProvider(ISelectionProvider selectionProvider) {
		if (this.selectionProvider != null) {
			for (ISelectionChangedListener action : actions) {
				this.selectionProvider.removeSelectionChangedListener(action);
			}
		}
		this.selectionProvider = selectionProvider;
		if (selectionProvider != null) {
			for (ISelectionChangedListener action : actions) {
				this.selectionProvider.addSelectionChangedListener(action);
				ISelection selection = selectionProvider.getSelection();
				if (selection == null) {
					selection = StructuredSelection.EMPTY;
				}
				action.selectionChanged(new SelectionChangedEvent(selectionProvider, selection));
			}
		}
	}

	public void fillContextMenu(final IMenuManager manager) {
		manager.add(new Separator(ID_SEPARATOR_NEW)); // new, schedule
		manager.add(new GroupMarker(ID_SEPARATOR_NAVIGATE)); // mark, go into, go up
		manager.add(new Separator(ID_SEPARATOR_OPEN)); // open, activate
		manager.add(new GroupMarker(ID_SEPARATOR_SHOW_IN)); // open, activate
		manager.add(new Separator(ID_SEPARATOR_EDIT)); // cut, copy paste, delete, rename
		manager.add(new Separator(ID_SEPARATOR_TASKS)); // move to
		manager.add(new GroupMarker(ID_SEPARATOR_OPERATIONS)); // repository properties, import/export, context
		manager.add(new Separator(ID_SEPARATOR_REPOSITORY)); // synchronize
		manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
		manager.add(new Separator(ID_SEPARATOR_PROPERTIES)); // properties

		final ITaskContainer element;
		IStructuredSelection selection = getSelection();
		final Object firstSelectedObject = selection.getFirstElement();
		if (firstSelectedObject instanceof ITaskContainer) {
			element = (ITaskContainer) firstSelectedObject;
		} else {
			element = null;
		}
		final List<IRepositoryElement> selectedElements = getSelectedTaskContainers(selection);
		AbstractTask task = null;
		if (element instanceof ITask) {
			task = (AbstractTask) element;
		}

		if (!isInTaskList()) {
			MenuManager newSubMenu = new MenuManager(Messages.RepositoryElementActionGroup_New);
			if (newSubTaskAction.isEnabled()) {
				newSubMenu.add(newSubTaskAction);
			}
			if (cloneTaskAction.isEnabled()) {
				newSubMenu.add(new Separator());
				newSubMenu.add(cloneTaskAction);
			}
			manager.appendToGroup(ID_SEPARATOR_NEW, newSubMenu);
		}

		if (element instanceof ITask && !isInEditor()) {
			addAction(ID_SEPARATOR_OPEN, openAction, manager, element);
		}
		if (openWithBrowserAction.isEnabled()) {
			manager.appendToGroup(ID_SEPARATOR_OPEN, openWithBrowserAction);
		}
		showInSearchViewAction.selectionChanged(selection);
		if (showInSearchViewAction.isEnabled()) {
			manager.appendToGroup(ID_SEPARATOR_OPEN, showInSearchViewAction);
		}
		showInTaskListAction.selectionChanged(selection);
		if (showInTaskListAction.isEnabled() && !isInTaskList()) {
			manager.appendToGroup(ID_SEPARATOR_OPEN, showInTaskListAction);
		}
		if (task != null) {
			if (task.isActive()) {
				manager.appendToGroup(ID_SEPARATOR_SHOW_IN, deactivateAction);
			} else {
				manager.appendToGroup(ID_SEPARATOR_SHOW_IN, activateAction);
			}
		}

		if (!selection.isEmpty()) {
			MenuManager copyDetailsSubMenu = new MenuManager(
					Messages.RepositoryElementActionGroup_Copy_Detail_Menu_Label, CopyTaskDetailsAction.ID);
			copyDetailsSubMenu.add(copyKeyAction);
			copyDetailsSubMenu.add(copyUrlAction);
			copyDetailsSubMenu.add(copyDetailsAction);
			manager.appendToGroup(ID_SEPARATOR_EDIT, copyDetailsSubMenu);
		}
		if (isInTaskList() && !selection.isEmpty()) {
			manager.appendToGroup(ID_SEPARATOR_EDIT, deleteAction);
		}

		if (isInEditor()) {
			manager.appendToGroup(ID_SEPARATOR_TASKS, deleteTaskEditorAction);
		}

		removeFromCategoryAction.selectionChanged(selection);
		removeFromCategoryAction.setEnabled(isRemoveFromCategoryEnabled(selectedElements));
		if (removeFromCategoryAction.isEnabled()) {
			manager.appendToGroup(ID_SEPARATOR_EDIT, removeFromCategoryAction);
		}

		if (autoUpdateAction.isEnabled()) {
			manager.appendToGroup(ID_SEPARATOR_REPOSITORY, autoUpdateAction);
		}

		if (element instanceof IRepositoryQuery) {
			EditRepositoryPropertiesAction repositoryPropertiesAction = new EditRepositoryPropertiesAction();
			repositoryPropertiesAction.selectionChanged(new StructuredSelection(element));
			if (repositoryPropertiesAction.isEnabled()) {
				MenuManager subMenu = new MenuManager(Messages.TaskListView_Repository);
				manager.appendToGroup(ID_SEPARATOR_OPERATIONS, subMenu);

				UpdateRepositoryConfigurationAction resetRepositoryConfigurationAction = new UpdateRepositoryConfigurationAction();
				resetRepositoryConfigurationAction.selectionChanged(new StructuredSelection(element));
				subMenu.add(resetRepositoryConfigurationAction);
				subMenu.add(new Separator());
				subMenu.add(repositoryPropertiesAction);
			}
		}

		Map<String, List<IDynamicSubMenuContributor>> dynamicMenuMap = TasksUiPlugin.getDefault().getDynamicMenuMap();
		for (final String menuPath : dynamicMenuMap.keySet()) {
			for (final IDynamicSubMenuContributor contributor : dynamicMenuMap.get(menuPath)) {
				SafeRunnable.run(new ISafeRunnable() {
					public void handleException(Throwable e) {
						StatusHandler.log(new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, "Menu contributor failed")); //$NON-NLS-1$
					}

					public void run() throws Exception {
						MenuManager subMenuManager = contributor.getSubMenuManager(selectedElements);
						if (subMenuManager != null) {
							addMenuManager(menuPath, subMenuManager, manager, element);
						}
					}
				});
			}
		}
	}

	private boolean isInTaskList() {
		return (this instanceof TaskListViewActionGroup);
	}

	private IStructuredSelection getSelection() {
		ISelection selection = (selectionProvider != null) ? selectionProvider.getSelection() : null;
		if (selection instanceof IStructuredSelection) {
			return (IStructuredSelection) selection;
		}
		return StructuredSelection.EMPTY;
	}

	private boolean isInEditor() {
		return (this instanceof TaskEditorActionGroup);
	}

	private boolean isRemoveFromCategoryEnabled(final List<IRepositoryElement> selectedElements) {
		if (selectedElements.isEmpty()) {
			return false;
		}
		for (IRepositoryElement element : selectedElements) {
			if (element instanceof AbstractTask) {
				boolean hasCategory = false;
				for (ITaskContainer container : ((AbstractTask) element).getParentContainers()) {
					if (container instanceof TaskCategory) {
						hasCategory = true;
					}
					if (container instanceof UncategorizedTaskContainer
							&& !LocalRepositoryConnector.CONNECTOR_KIND.equals(((AbstractTask) element).getConnectorKind())) {
						hasCategory = true;
					}
				}
				if (!hasCategory) {
					return false;
				}
			} else {
				return false;
			}
		}
		return true;
	}

	private void addMenuManager(String path, IMenuManager menuToAdd, IMenuManager manager, ITaskContainer element) {
		if (element instanceof ITask || element instanceof IRepositoryQuery) {
			manager.appendToGroup(path, menuToAdd);
		}
	}

	private void addAction(String path, Action action, IMenuManager manager, ITaskContainer element) {
		action.setEnabled(false);
		if (element != null) {
			updateActionEnablement(action, element);
		}
		manager.appendToGroup(path, action);
	}

	// TODO move the enablement to the action classes
	private void updateActionEnablement(Action action, ITaskContainer element) {
		if (element instanceof ITask) {
			if (action instanceof OpenTaskListElementAction) {
				action.setEnabled(true);
			} else if (action instanceof CopyTaskDetailsAction) {
				action.setEnabled(true);
			} else if (action instanceof RenameAction) {
				action.setEnabled(true);
			}
		} else if (element != null) {
			if (action instanceof GoIntoAction) {
				TaskCategory cat = (TaskCategory) element;
				if (cat.getChildren().size() > 0) {
					action.setEnabled(true);
				} else {
					action.setEnabled(false);
				}
			} else if (action instanceof OpenTaskListElementAction) {
				action.setEnabled(true);
			} else if (action instanceof CopyTaskDetailsAction) {
				action.setEnabled(true);
			} else if (action instanceof RenameAction) {
				if (element instanceof AbstractTaskCategory) {
					AbstractTaskCategory container = (AbstractTaskCategory) element;
					action.setEnabled(container.isUserManaged());
				} else if (element instanceof IRepositoryQuery) {
					action.setEnabled(true);
				}
			}
		} else {
			action.setEnabled(true);
		}
	}

	public List<IRepositoryElement> getSelectedTaskContainers(IStructuredSelection selection) {
		List<IRepositoryElement> selectedElements = new ArrayList<IRepositoryElement>();
		for (Iterator<?> i = selection.iterator(); i.hasNext();) {
			Object object = i.next();
			if (object instanceof ITaskContainer) {
				selectedElements.add((IRepositoryElement) object);
			}
		}
		return selectedElements;
	}

	public OpenTaskListElementAction getOpenAction() {
		return openAction;
	}

	public TaskActivateAction getActivateAction() {
		return activateAction;
	}

	public DeleteAction getDeleteAction() {
		return deleteAction;
	}

	public CopyTaskDetailsAction getCopyDetailsAction() {
		return copyDetailsAction;
	}

}

Back to the top