blob: 8fb65bf4e74fe3dc57eb321428611d25e07a9f05 (
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
|
/*******************************************************************************
* Copyright (c) 2004, 2007 Mylyn project committers 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
*******************************************************************************/
package org.eclipse.mylyn.internal.tasks.ui;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.mylyn.internal.tasks.core.AbstractTask;
import org.eclipse.mylyn.internal.tasks.core.AbstractTaskCategory;
import org.eclipse.mylyn.internal.tasks.core.UnmatchedTaskContainer;
import org.eclipse.mylyn.internal.tasks.ui.actions.NewCategoryAction;
import org.eclipse.mylyn.internal.tasks.ui.util.TasksUiInternal;
import org.eclipse.mylyn.tasks.core.ITask;
import org.eclipse.mylyn.tasks.core.ITaskElement;
import org.eclipse.mylyn.tasks.ui.TasksUiImages;
/**
* @author Mik Kersten
* @author Raphael Ackermann (bug 160315)
*/
public class MoveToCategoryMenuContributor implements IDynamicSubMenuContributor {
private static final String LABEL = "Move to";
public MenuManager getSubMenuManager(final List<ITaskElement> selectedElements) {
final MenuManager subMenuManager = new MenuManager(LABEL);
// Compute selected tasks
List<AbstractTask> selectedTasks = new ArrayList<AbstractTask>(selectedElements.size());
for (ITaskElement element : selectedElements) {
if (element instanceof ITask) {
selectedTasks.add((AbstractTask) element);
}
}
subMenuManager.setVisible(!selectedTasks.isEmpty());
List<AbstractTaskCategory> categories = new ArrayList<AbstractTaskCategory>(TasksUiInternal.getTaskList()
.getCategories());
Collections.sort(categories);
for (final AbstractTaskCategory category : categories) {
if (!(category instanceof UnmatchedTaskContainer)) {
String text = handleAcceleratorKeys(category.getSummary());
Action action = new Action(text, IAction.AS_RADIO_BUTTON) {
@Override
public void run() {
moveToCategory(selectedElements, category);
}
};
action.setImageDescriptor(TasksUiImages.CATEGORY);
if (selectedTasks.size() == 1) {
if (category.contains(selectedTasks.get(0).getHandleIdentifier())) {
action.setChecked(true);
}
}
subMenuManager.add(action);
}
}
// add New Category action at the end of the Move to Category Submenu
// and move selected actions to this newly created category
Action action = new NewCategoryAction() {
@Override
public void run() {
super.run();
if (super.cat != null) {
moveToCategory(selectedElements, super.cat);
}
}
};
subMenuManager.add(new Separator());
subMenuManager.add(action);
return subMenuManager;
}
/**
* public for testing
*
* Deals with text where user has entered a '@' or tab character but which are not meant to be accelerators. from:
* Action#setText: Note that if you want to insert a '@' character into the text (but no accelerator, you can simply
* insert a '@' or a tab at the end of the text. see Action#setText
*/
public String handleAcceleratorKeys(String text) {
if (text == null) {
return null;
}
int index = text.lastIndexOf('\t');
if (index == -1) {
index = text.lastIndexOf('@');
}
if (index >= 0) {
return text.concat("@");
}
return text;
}
/**
* @param selectedElements
* @param category
*/
private void moveToCategory(final List<ITaskElement> selectedElements, AbstractTaskCategory category) {
for (ITaskElement element : selectedElements) {
if (element instanceof ITask) {
TasksUiInternal.getTaskList().addTask((AbstractTask) element, category);
}
}
}
}
|