Skip to main content
summaryrefslogtreecommitdiffstats
blob: ced32a64b52de96d96aa9f08d883a830740fe673 (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
/*******************************************************************************
 * Copyright (c) 2004, 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.Collections;
import java.util.Set;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuCreator;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.mylyn.commons.ui.CommonImages;
import org.eclipse.mylyn.internal.tasks.core.AbstractTask;
import org.eclipse.mylyn.internal.tasks.core.ITaskListChangeListener;
import org.eclipse.mylyn.internal.tasks.core.TaskActivityUtil;
import org.eclipse.mylyn.internal.tasks.core.TaskContainerDelta;
import org.eclipse.mylyn.internal.tasks.ui.ScheduleTaskMenuContributor;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
import org.eclipse.mylyn.internal.tasks.ui.editors.TaskListChangeAdapter;
import org.eclipse.mylyn.tasks.core.IRepositoryElement;
import org.eclipse.mylyn.tasks.core.ITask;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.PlatformUI;

/**
 * <p>
 * <b>Note:</b> this action must be disposed.
 * 
 * @author Mik Kersten
 * @author Steffen Pingel
 */
public class TaskEditorScheduleAction extends Action implements IMenuCreator {

	private final ITask task;

	private MenuManager menuManager;

	private final ScheduleTaskMenuContributor scheduleMenuContributor = new ScheduleTaskMenuContributor();

	private final ITaskListChangeListener TASK_LIST_LISTENER = new TaskListChangeAdapter() {

		@Override
		public void containersChanged(Set<TaskContainerDelta> containers) {
			for (TaskContainerDelta taskContainerDelta : containers) {
				if (taskContainerDelta.getElement() instanceof ITask) {
					final AbstractTask updateTask = (AbstractTask) taskContainerDelta.getElement();
					if (task.equals(updateTask)) {
						if (PlatformUI.getWorkbench() != null && !PlatformUI.getWorkbench().isClosing()) {
							PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
								public void run() {
									updateImageDescriptor();
								}
							});
						}
					}
				}
			}
		}

	};

	public TaskEditorScheduleAction(ITask task) {
		Assert.isNotNull(task);
		this.task = task;
		updateImageDescriptor();
		setMenuCreator(this);
		setToolTipText(Messages.TaskEditorScheduleAction_Private_Scheduling);
		TasksUiPlugin.getTaskList().addChangeListener(TASK_LIST_LISTENER);
	}

	@Override
	public void run() {
		if (((AbstractTask) task).getScheduledForDate() == null) {
			TasksUiPlugin.getTaskList().addTaskIfAbsent(task);
			TasksUiPlugin.getTaskActivityManager().setScheduledFor((AbstractTask) task,
					TaskActivityUtil.getCurrentWeek().getToday());
		} else {
			TasksUiPlugin.getTaskActivityManager().setScheduledFor((AbstractTask) task, null);
		}
	}

	public void updateImageDescriptor() {
		if (task instanceof AbstractTask && ((AbstractTask) task).getScheduledForDate() != null) {
			setImageDescriptor(CommonImages.SCHEDULE_DAY);
		} else {
			setImageDescriptor(CommonImages.SCHEDULE);
		}
		setEnabled(!task.isCompleted());
	}

	public Menu getMenu(Control parent) {
		if (menuManager != null) {
			menuManager.dispose();
		}
		menuManager = scheduleMenuContributor.getSubMenuManager(Collections.singletonList((IRepositoryElement) task));
		menuManager.createContextMenu(parent);
		return menuManager.getMenu();
	}

	public Menu getMenu(Menu parent) {
		if (menuManager != null) {
			return menuManager.getMenu();
		}
		return null;
	}

	public void dispose() {
		if (menuManager != null) {
			menuManager.dispose();
		}
		TasksUiPlugin.getTaskList().removeChangeListener(TASK_LIST_LISTENER);
	}

}

Back to the top