Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorchris.poon2012-08-21 22:16:05 +0000
committerSam Davis2015-05-20 17:36:53 +0000
commite3d912eacc50ad636c8865d8fe6831a4564dd767 (patch)
tree5c262104669185ff681a3f038b8f5b9c7e0f3abe
parent65b8a30a5f8c1e576f74187bff37b1d253abcd45 (diff)
downloadorg.eclipse.mylyn.tasks-e3d912eacc50ad636c8865d8fe6831a4564dd767.tar.gz
org.eclipse.mylyn.tasks-e3d912eacc50ad636c8865d8fe6831a4564dd767.tar.xz
org.eclipse.mylyn.tasks-e3d912eacc50ad636c8865d8fe6831a4564dd767.zip
334937: provide API for canceling a task switch
Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=334937 Change-Id: I0962f312b692de78be778d8ecb5dd11f8efdc815 Signed-off-by: chris.poon <chris.poon@tasktop.com>
-rw-r--r--org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/TaskActivityManager.java39
-rw-r--r--org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/tasks/core/TaskActivationAdapter.java7
-rw-r--r--org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/TaskActivityManagerTest.java102
3 files changed, 135 insertions, 13 deletions
diff --git a/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/TaskActivityManager.java b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/TaskActivityManager.java
index b9659b8ab..e5e215843 100644
--- a/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/TaskActivityManager.java
+++ b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/TaskActivityManager.java
@@ -41,11 +41,12 @@ import org.eclipse.mylyn.tasks.core.ITask;
import org.eclipse.mylyn.tasks.core.ITaskActivationListener;
import org.eclipse.mylyn.tasks.core.ITaskActivityListener;
import org.eclipse.mylyn.tasks.core.ITaskActivityManager2;
+import org.eclipse.mylyn.tasks.core.TaskActivationAdapter;
import org.eclipse.osgi.util.NLS;
/**
* Manages task elapsed time, scheduling, due dates, and the date ranges
- *
+ *
* @since 2.1
* @author Rob Elves
*/
@@ -68,7 +69,7 @@ public class TaskActivityManager implements ITaskActivityManager2 {
// Map of Calendar (hour) to Tasks active during that hour
private final SortedMap<Calendar, Set<AbstractTask>> activeTasks = Collections.synchronizedSortedMap(new TreeMap<Calendar, Set<AbstractTask>>());
- // For a given task maps Calendar Hour to duration of time spent (milliseconds) with task active
+ // For a given task maps Calendar Hour to duration of time spent (milliseconds) with task active
private final Map<AbstractTask, SortedMap<Calendar, Long>> taskElapsedTimeMap = new ConcurrentHashMap<AbstractTask, SortedMap<Calendar, Long>>();
private final Map<String, SortedMap<Calendar, Long>> workingSetElapsedTimeMap = new ConcurrentHashMap<String, SortedMap<Calendar, Long>>();
@@ -149,7 +150,7 @@ public class TaskActivityManager implements ITaskActivityManager2 {
/**
* Get the user specified first day of the week (Calendar.SUNDAY | Calendar.MONDAY)
- *
+ *
* @see http://en.wikipedia.org/wiki/Days_of_the_week#First_day_of_the_week
*/
public int getWeekStartDay() {
@@ -158,7 +159,7 @@ public class TaskActivityManager implements ITaskActivityManager2 {
/**
* Set the first day of the week (Calendar.SUNDAY | Calendar.MONDAY)
- *
+ *
* @see http://en.wikipedia.org/wiki/Days_of_the_week#First_day_of_the_week
* @param startDay
* (Calendar.SUNDAY | Calendar.MONDAY)
@@ -457,7 +458,12 @@ public class TaskActivityManager implements ITaskActivityManager2 {
}
public void activateTask(ITask task) {
- deactivateActiveTask();
+ if (activeTask != null) {
+ if (!shouldDeactivateTask(activeTask)) {
+ return;
+ }
+ deactivateTaskInternal(activeTask);
+ }
taskList.addTaskIfAbsent(task);
@@ -493,10 +499,13 @@ public class TaskActivityManager implements ITaskActivityManager2 {
}
public void deactivateTask(ITask task) {
- if (task == null) {
+ if (task == null || (task.isActive() && !shouldDeactivateTask(task))) {
return;
}
+ deactivateTaskInternal(task);
+ }
+ protected void deactivateTaskInternal(ITask task) {
if (task.isActive() && task == activeTask) {
// notify that a task is about to be deactivated
initTaskListeners();
@@ -526,6 +535,22 @@ public class TaskActivityManager implements ITaskActivityManager2 {
}
}
+ protected boolean shouldDeactivateTask(ITask task) {
+ for (ITaskActivationListener listener : new ArrayList<ITaskActivationListener>(activationListeners)) {
+ try {
+ if (listener instanceof TaskActivationAdapter) {
+ if (!((TaskActivationAdapter) listener).canDeactivateTask(task)) {
+ return false;
+ }
+ }
+ } catch (Throwable t) {
+ StatusHandler.log(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN,
+ "Task activity listener failed: " + listener, t)); //$NON-NLS-1$
+ }
+ }
+ return true;
+ }
+
/**
* returns active tasks from start to end (exclusive) where both are snapped to the beginning of the hour
*/
@@ -934,4 +959,4 @@ public class TaskActivityManager implements ITaskActivityManager2 {
Assert.isNotNull(task);
return task.equals(getActiveTask());
}
-} \ No newline at end of file
+}
diff --git a/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/tasks/core/TaskActivationAdapter.java b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/tasks/core/TaskActivationAdapter.java
index 127a66786..5f1de9f44 100644
--- a/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/tasks/core/TaskActivationAdapter.java
+++ b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/tasks/core/TaskActivationAdapter.java
@@ -17,6 +17,13 @@ package org.eclipse.mylyn.tasks.core;
*/
public class TaskActivationAdapter implements ITaskActivationListener {
+ /**
+ * @since 3.16
+ */
+ public boolean canDeactivateTask(ITask task) {
+ return true;
+ }
+
public void preTaskActivated(ITask task) {
}
diff --git a/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/TaskActivityManagerTest.java b/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/TaskActivityManagerTest.java
index 9dc0a2d61..f286c01cf 100644
--- a/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/TaskActivityManagerTest.java
+++ b/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/TaskActivityManagerTest.java
@@ -100,6 +100,18 @@ public class TaskActivityManagerTest extends TestCase {
private boolean hasPreDeactivated = false;
+ private int timesCalledCanDeactivate;
+
+ private final boolean canDeactivate;
+
+ public MockTaskActivationListener() {
+ this(true);
+ }
+
+ public MockTaskActivationListener(boolean canDeactivate) {
+ this.canDeactivate = canDeactivate;
+ }
+
public void reset() {
hasActivated = false;
hasPreActivated = false;
@@ -107,6 +119,7 @@ public class TaskActivityManagerTest extends TestCase {
hasDeactivated = false;
hasPreDeactivated = false;
+ timesCalledCanDeactivate = 0;
}
@Override
@@ -133,6 +146,16 @@ public class TaskActivityManagerTest extends TestCase {
hasDeactivated = true;
}
+ @Override
+ public boolean canDeactivateTask(ITask task) {
+ timesCalledCanDeactivate++;
+ return canDeactivate;
+ }
+
+ public void verifyCalledCanDeactivate(int times) {
+ assertEquals(times, timesCalledCanDeactivate);
+ }
+
}
private TaskActivityManager taskActivityManager;
@@ -141,6 +164,10 @@ public class TaskActivityManagerTest extends TestCase {
private TaskRepository repository;
+ private AbstractTask task1;
+
+ private AbstractTask task2;
+
@Override
protected void setUp() throws Exception {
taskActivityManager = TasksUiPlugin.getTaskActivityManager();
@@ -298,12 +325,7 @@ public class TaskActivityManagerTest extends TestCase {
}
public void testAllTasksDeactivation() {
- AbstractTask task1 = new LocalTask("task1", "description1");
- AbstractTask task2 = new LocalTask("task2", "description2");
- taskList.addTask(task1);
- taskList.addTask(task2);
- assertNull(taskActivityManager.getActiveTask());
-
+ initializeTasks();
taskActivityManager.activateTask(task2);
assertEquals(task2, taskActivityManager.getActiveTask());
@@ -311,4 +333,72 @@ public class TaskActivityManagerTest extends TestCase {
assertNull(taskActivityManager.getActiveTask());
}
+ public void testActivateNonActiveTaskCanDeactivate() {
+ initializeTasks();
+ assertActivateNonActiveTaskCanDeactivate(true, task2);
+ }
+
+ public void testActivateNonActiveTaskCannotDeactivate() {
+ initializeTasks();
+ assertActivateNonActiveTaskCanDeactivate(false, task1);
+ }
+
+ private void assertActivateNonActiveTaskCanDeactivate(final boolean canDeactivate, ITask expectedActiveTask) {
+ MockTaskActivationListener listener = new MockTaskActivationListener(canDeactivate);
+
+ try {
+ taskActivityManager.addActivationListener(listener);
+
+ taskActivityManager.activateTask(task1);
+ assertEquals(task1, taskActivityManager.getActiveTask());
+
+ taskActivityManager.activateTask(task2);
+ assertEquals(expectedActiveTask, taskActivityManager.getActiveTask());
+ listener.verifyCalledCanDeactivate(1);
+ } finally {
+ taskActivityManager.removeActivationListener(listener);
+ }
+ }
+
+ public void testDeactivateTaskCanDeactivate() {
+ initializeTasks();
+ assertDeactivateTaskCanDeactivate(true, null);
+ }
+
+ public void testDeactivateTaskCannotDeactivate() {
+ initializeTasks();
+ assertDeactivateTaskCanDeactivate(false, task1);
+ }
+
+ private void assertDeactivateTaskCanDeactivate(final boolean canDeactivate, ITask expectedActiveTask) {
+ MockTaskActivationListener listener = new MockTaskActivationListener(canDeactivate);
+
+ try {
+ taskActivityManager.addActivationListener(listener);
+
+ taskActivityManager.activateTask(task1);
+ assertEquals(task1, taskActivityManager.getActiveTask());
+
+ taskActivityManager.deactivateTask(task1);
+ assertEquals(expectedActiveTask, taskActivityManager.getActiveTask());
+ listener.verifyCalledCanDeactivate(1);
+ } finally {
+ taskActivityManager.removeActivationListener(listener);
+ }
+ }
+
+ public void testDeactivateTaskOnInactiveTask() {
+ initializeTasks();
+ taskActivityManager.deactivateTask(task1);
+ assertNull(taskActivityManager.getActiveTask());
+ }
+
+ private void initializeTasks() {
+ task1 = new LocalTask("task1", "description1");
+ task2 = new LocalTask("task2", "description2");
+ taskList.addTask(task1);
+ taskList.addTask(task2);
+ assertNull(taskActivityManager.getActiveTask());
+ }
+
} \ No newline at end of file

Back to the top