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

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.mylyn.commons.workbench.AdaptiveRefreshPolicy.IFilteredTreeListener;
import org.eclipse.mylyn.internal.tasks.ui.AbstractTaskListFilter;
import org.eclipse.mylyn.internal.tasks.ui.ITasksUiPreferenceConstants;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
import org.eclipse.mylyn.internal.tasks.ui.util.TasksUiInternal;
import org.eclipse.mylyn.internal.tasks.ui.views.TaskListInterestFilter;
import org.eclipse.mylyn.internal.tasks.ui.views.TaskListInterestSorter;
import org.eclipse.mylyn.internal.tasks.ui.views.TaskListView;
import org.eclipse.mylyn.internal.tasks.ui.views.TaskScheduleContentProvider;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.IActionDelegate2;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;

/**
 * @author Mik Kersten
 * @author Steffen Pingel
 */
public class FocusTaskListAction implements IFilteredTreeListener, IViewActionDelegate, IActionDelegate2 {

	private Set<AbstractTaskListFilter> previousFilters = new HashSet<AbstractTaskListFilter>();

	private ViewerSorter previousSorter;

	private final TaskListInterestFilter taskListInterestFilter = new TaskListInterestFilter();

	private final TaskListInterestSorter taskListInterestSorter = new TaskListInterestSorter();

	private TaskListView taskListView;

	private IAction action;

	public FocusTaskListAction() {
	}

	public void filterTextChanged(final String text) {
		if (taskListView.isFocusedMode() && (text == null || "".equals(text.trim()))) { //$NON-NLS-1$
			taskListView.getViewer().expandAll();
		}
	}

	public void init(IAction action) {
		this.action = action;
		initAction();
	}

	public void init(IViewPart view) {
		if (view instanceof TaskListView) {
			taskListView = (TaskListView) view;
			taskListView.getFilteredTree().getRefreshPolicy().addListener(this);
			taskListView.getFilteredTree().addDisposeListener(new DisposeListener() {
				public void widgetDisposed(DisposeEvent e) {
					taskListView.getFilteredTree().getRefreshPolicy().removeListener(FocusTaskListAction.this);
				}
			});
			if (TasksUiPlugin.getDefault()
					.getPreferenceStore()
					.getBoolean(ITasksUiPreferenceConstants.TASK_LIST_FOCUSED)) {
				installInterestFilter();
			}
			initAction();
			showProgressBar(taskListView.isFocusedMode());
		}
	}

	private void initAction() {
		if (action != null && taskListView != null) {
			action.setChecked(taskListView.isFocusedMode());
		}
	}

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

	private void showProgressBar(boolean visible) {
		taskListView.getFilteredTree().setShowProgress(visible);
	}

	protected void installInterestFilter() {
		TasksUiInternal.preservingSelection(taskListView.getViewer(), new Runnable() {
			public void run() {
				try {
					taskListView.getFilteredTree().setRedraw(false);
					taskListView.setFocusedMode(true);
					previousSorter = taskListView.getViewer().getSorter();
					previousFilters = taskListView.clearFilters();
					if (!taskListView.getFilters().contains(taskListInterestFilter)) {
						taskListView.addFilter(taskListInterestFilter);
					}
					// Setting sorter causes root refresh
					taskListView.getViewer().setSorter(taskListInterestSorter);
					taskListView.getViewer().expandAll();
//				taskListView.selectedAndFocusTask(TasksUiPlugin.getTaskList().getActiveTask());

					showProgressBar(true);
				} finally {
					taskListView.getFilteredTree().setRedraw(true);
				}
			}
		});
	}

	protected void uninstallInterestFilter() {
		TasksUiInternal.preservingSelection(taskListView.getViewer(), new Runnable() {
			public void run() {
				try {
					taskListView.getViewer().getControl().setRedraw(false);
					taskListView.setFocusedMode(false);
					for (AbstractTaskListFilter filter : previousFilters) {
						taskListView.addFilter(filter);
					}
					taskListView.removeFilter(taskListInterestFilter);
					Text textControl = taskListView.getFilteredTree().getFilterControl();
					if (textControl != null && textControl.getText().length() > 0) {
						taskListView.getViewer().expandAll();
					} else {
						taskListView.getViewer().collapseAll();
						// expand first element (Today) in scheduled mode
						if (taskListView.getViewer().getContentProvider() instanceof TaskScheduleContentProvider
								&& taskListView.getViewer().getTree().getItemCount() > 0) {
							TreeItem item = taskListView.getViewer().getTree().getItem(0);
							if (item.getData() != null) {
								taskListView.getViewer().expandToLevel(item.getData(), 1);
							}
						}
					}
					taskListView.getViewer().setSorter(previousSorter);
					showProgressBar(false);
				} finally {
					taskListView.getViewer().getControl().setRedraw(true);
				}
			}
		});
	}

	public void run() {
		if (taskListView == null) {
			return;
		}

		if (!taskListView.isFocusedMode()) {
			TasksUiPlugin.getDefault()
					.getPreferenceStore()
					.setValue(ITasksUiPreferenceConstants.TASK_LIST_FOCUSED, true);
			installInterestFilter();
		} else {
			TasksUiPlugin.getDefault()
					.getPreferenceStore()
					.setValue(ITasksUiPreferenceConstants.TASK_LIST_FOCUSED, false);
			uninstallInterestFilter();
		}
	}

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

	public void runWithEvent(IAction action, Event event) {
		run();
	}

	public void dispose() {
		// ignore		
	}

}

Back to the top