Skip to main content
summaryrefslogtreecommitdiffstats
blob: a3e36cc4f1ebd141a554b99f798fb3f95513b3ce (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
/*******************************************************************************
 * Copyright (c) 2004, 2010 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.actions;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExecutableExtension;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuCreator;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.mylyn.internal.commons.ui.TaskListImageDescriptor;
import org.eclipse.mylyn.internal.tasks.core.ITaskRepositoryFilter;
import org.eclipse.mylyn.internal.tasks.core.LocalRepositoryConnector;
import org.eclipse.mylyn.internal.tasks.core.RepositoryQuery;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
import org.eclipse.mylyn.internal.tasks.ui.views.TaskRepositoriesSorter;
import org.eclipse.mylyn.tasks.core.AbstractRepositoryConnector;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.ui.TasksUi;
import org.eclipse.mylyn.tasks.ui.TasksUiImages;
import org.eclipse.mylyn.tasks.ui.TasksUiUtil;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkingSet;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.BaseSelectionListenerAction;

/**
 * @author Mik Kersten
 * @author Eugene Kuleshov
 * @author Steffen Pingel
 * @author Rob Elves
 */
@SuppressWarnings("restriction")
public class NewTaskAction extends BaseSelectionListenerAction implements IMenuCreator, IViewActionDelegate,
		IExecutableExtension {

	private static final String LABEL_NEW_TASK = Messages.NewTaskAction_new_task;

	public static final String ID = "org.eclipse.mylyn.tasklist.ui.repositories.actions.create"; //$NON-NLS-1$

	private boolean skipRepositoryPage = false;

	private boolean localTask = false;

	private Menu dropDownMenu;

	public NewTaskAction(String label, boolean alwaysShowWizard) {
		super(label);
		if (!alwaysShowWizard) {
			setMenuCreator(this);
		}
		setText(label);
		setToolTipText(label);
		setId(ID);
		setEnabled(true);
		setImageDescriptor(TasksUiImages.TASK_NEW);
	}

	public NewTaskAction() {
		this(LABEL_NEW_TASK, false);
	}

	@Override
	public void run() {
		Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
		if (shell != null && !shell.isDisposed()) {
			if (localTask) {
				TasksUiUtil.openNewLocalTaskEditor(shell, null);
			} else {
				if (skipRepositoryPage) {
					TasksUiUtil.openNewTaskEditor(shell, null, TasksUiUtil.getSelectedRepository());
				} else {
					TasksUiUtil.openNewTaskEditor(shell, null, null);
				}
			}
		}
	}

	public void run(IAction action) {
		run();
	}

	public void init(IViewPart view) {
	}

	public void selectionChanged(IAction action, ISelection selection) {
	}

	public void setInitializationData(IConfigurationElement config, String propertyName, Object data)
			throws CoreException {
		if ("skipFirstPage".equals(data)) { //$NON-NLS-1$
			this.skipRepositoryPage = true;
		}
		if ("local".equals(data)) { //$NON-NLS-1$
			this.localTask = true;
		}
	}

	public void dispose() {
		if (dropDownMenu != null) {
			dropDownMenu.dispose();
			dropDownMenu = null;
		}
	}

	public Menu getMenu(Control parent) {
		if (dropDownMenu != null) {
			dropDownMenu.dispose();
		}
		dropDownMenu = new Menu(parent);
		addActionsToMenu();
		return dropDownMenu;
	}

	public Menu getMenu(Menu parent) {
		if (dropDownMenu != null) {
			dropDownMenu.dispose();
		}
		dropDownMenu = new Menu(parent);
		addActionsToMenu();
		return dropDownMenu;
	}

	private void addActionsToMenu() {
		NewTaskAction newTaskAction = new NewTaskAction(LABEL_NEW_TASK, true);
		newTaskAction.setText(Messages.NewTaskAction_Show_Wizard_Label);
		new ActionContributionItem(newTaskAction).fill(dropDownMenu, -1);
		new Separator().fill(dropDownMenu, -1);

		Set<TaskRepository> includedRepositories = new HashSet<TaskRepository>();
		TaskRepository localRepository = TasksUi.getRepositoryManager().getRepository(
				LocalRepositoryConnector.CONNECTOR_KIND, LocalRepositoryConnector.REPOSITORY_URL);

		addRepositoryAction(localRepository);

		IWorkingSet workingSet = PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow()
				.getActivePage()
				.getAggregateWorkingSet();
		if (workingSet != null && !workingSet.isEmpty()) {
			// only add repositories in working set 
			for (IAdaptable iterable_element : workingSet.getElements()) {
				if (iterable_element instanceof RepositoryQuery) {
					String repositoryUrl = ((RepositoryQuery) iterable_element).getRepositoryUrl();
					String connectorKind = ((RepositoryQuery) iterable_element).getConnectorKind();
					TaskRepository repository = TasksUi.getRepositoryManager().getRepository(connectorKind,
							repositoryUrl);
					markForInclusion(includedRepositories, repository);

				}
			}
		}

		if (includedRepositories.isEmpty()) {
			// No repositories were added from working sets so show all
			for (TaskRepository repository : TasksUi.getRepositoryManager().getAllRepositories()) {
				markForInclusion(includedRepositories, repository);
			}
		}

		if (!includedRepositories.isEmpty()) {
			//new Separator().fill(dropDownMenu, -1);
			ArrayList<TaskRepository> listOfRepositories = new ArrayList<TaskRepository>(includedRepositories);
			final TaskRepositoriesSorter comparator = new TaskRepositoriesSorter();
			Collections.sort(listOfRepositories, new Comparator<TaskRepository>() {

				public int compare(TaskRepository arg0, TaskRepository arg1) {
					return comparator.compare(null, arg0, arg1);
				}
			});
			for (TaskRepository taskRepository : listOfRepositories) {
				addRepositoryAction(taskRepository);
			}
		}
		new Separator().fill(dropDownMenu, -1);
		new ActionContributionItem(new NewQueryAction()).fill(dropDownMenu, -1);
		new ActionContributionItem(new NewCategoryAction()).fill(dropDownMenu, -1);
		new Separator().fill(dropDownMenu, -1);
		AddRepositoryAction action = new AddRepositoryAction();
		action.setText(Messages.NewTaskAction_Add_Repository);
		action.setImageDescriptor(null);
		new ActionContributionItem(action).fill(dropDownMenu, -1);
		new Separator(IWorkbenchActionConstants.MB_ADDITIONS);
	}

	private void markForInclusion(Set<TaskRepository> includedRepositories, TaskRepository repository) {
		if (repository != null && !repository.getConnectorKind().equals(LocalRepositoryConnector.CONNECTOR_KIND)) {
			AbstractRepositoryConnector connector = TasksUi.getRepositoryConnector(repository.getConnectorKind());
			if (connector != null) {
				if (ITaskRepositoryFilter.CAN_CREATE_NEW_TASK.accept(repository, connector)) {
					includedRepositories.add(repository);
				}
			}
		}
	}

	private RepositorySelectionAction addRepositoryAction(TaskRepository repository) {
		if (repository == null) {
			return null;
		}
		RepositorySelectionAction action = new RepositorySelectionAction(repository);
		ActionContributionItem item = new ActionContributionItem(action);
		action.setText(repository.getRepositoryLabel());
		ImageDescriptor overlay = TasksUiPlugin.getDefault().getBrandManager().getOverlayIcon(repository);
		ImageDescriptor compositeDescriptor = new TaskListImageDescriptor(TasksUiImages.TASK_NEW, overlay, false, false);
		action.setImageDescriptor(compositeDescriptor);
		item.fill(dropDownMenu, -1);
		return action;
	}

	private class RepositorySelectionAction extends Action {

		private final TaskRepository repository;

		public RepositorySelectionAction(TaskRepository repository) {
			this.repository = repository;
			setText(repository.getRepositoryLabel());
		}

		@Override
		public void run() {
			Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
			if (repository.getConnectorKind().equalsIgnoreCase(LocalRepositoryConnector.CONNECTOR_KIND)) {
				TasksUiUtil.openNewLocalTaskEditor(shell, null);
			} else {
				TasksUiUtil.openNewTaskEditor(shell, null, repository);
			}
		}
	}

}

Back to the top