Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0e8f511cfe53460dbd07232ae85c1543f730bcff (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
/*******************************************************************************
 * Copyright (c) 2004, 2008 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.commands;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.mylyn.internal.tasks.ui.util.TasksUiInternal;
import org.eclipse.mylyn.internal.tasks.ui.util.TreeWalker;
import org.eclipse.mylyn.internal.tasks.ui.util.TreeWalker.Direction;
import org.eclipse.mylyn.internal.tasks.ui.util.TreeWalker.TreeVisitor;
import org.eclipse.mylyn.internal.tasks.ui.views.TaskListView;
import org.eclipse.mylyn.tasks.core.IRepositoryElement;
import org.eclipse.mylyn.tasks.core.ITask;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

/**
 * @author Steffen Pingel
 */
public abstract class GoToUnreadTaskHandler extends AbstractTaskListViewHandler {

	public static final String ID_NEXT = "org.eclipse.mylyn.tasklist.actions.goToNextUnread"; //$NON-NLS-1$

	public static final String ID_PREVIOUS = "org.eclipse.mylyn.tasklist.actions.goToPreviousUnread"; //$NON-NLS-1$

	private Direction direction = Direction.DOWN;

	public Direction getDirection() {
		return direction;
	}

	private TreePath getUnreadItem(TreeViewer treeViewer, Tree tree) {
		TreeItem[] selection = tree.getSelection();
		TreeItem selectedItem = (selection.length > 0) ? selection[0] : null;

		TreeVisitor visitor = new TreeVisitor() {
			@Override
			public boolean visit(Object object) {
				if (object instanceof ITask) {
					ITask task = (ITask) object;
					if (TasksUiInternal.shouldShowIncoming(task)) { // task.getSynchronizationState().isIncoming()
						return true;
					}
				}
				return false;
			}
		};

		TreeWalker treeWalker = new TreeWalker(treeViewer);
		treeWalker.setDirection(direction);
		treeWalker.setExpandNodes(true);
		return treeWalker.walk(visitor, selectedItem);
	}

	@Override
	protected void execute(ExecutionEvent event, TaskListView taskListView, IRepositoryElement item) {
		TreeViewer treeViewer = taskListView.getViewer();
		Tree tree = treeViewer.getTree();

		// need to expand nodes to traverse the tree, disable redraw to avoid flickering
		TreePath treePath = null;
		try {
			tree.setRedraw(false);
			treePath = getUnreadItem(treeViewer, tree);
		} finally {
			tree.setRedraw(true);
		}

		if (treePath != null) {
			treeViewer.expandToLevel(treePath, 0);
			treeViewer.setSelection(new TreeSelection(treePath), true);
		}
	}

	public void setDirection(Direction direction) {
		this.direction = direction;
	}

	public static void execute(ExecutionEvent event, Direction direction) throws ExecutionException {
		GoToUnreadTaskHandler handler = new GoToUnreadTaskHandler() {
		};
		handler.setDirection(direction);
		handler.execute(event);
	}

	public static class GoToNextUnreadTaskHandler extends GoToUnreadTaskHandler {

		public GoToNextUnreadTaskHandler() {
			setDirection(Direction.DOWN);
		}

	}

	public static class GoToPreviousUnreadTaskHandler extends GoToUnreadTaskHandler {

		public GoToPreviousUnreadTaskHandler() {
			setDirection(Direction.UP);
		}

	}

}

Back to the top