Skip to main content
summaryrefslogtreecommitdiffstats
blob: 349bb76a34e280fe69ecdd6f4f97e376ca44078c (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
/*******************************************************************************
 * Copyright (c) 2004 - 2006 Mylar 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.mylar.internal.tasks.ui.views;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IMenuCreator;
import org.eclipse.mylar.internal.tasks.ui.TasksUiImages;
import org.eclipse.mylar.internal.tasks.ui.views.TaskListTableSorter.SortByIndex;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;

/**
 * @author Mik Kersten
 */
class SortyByDropDownAction extends Action implements IMenuCreator {

	private final TaskListView taskListView;

	private static final String LABEL = "Sort by";

	private Action byPriority;

	private Action bySummary;

	private Action byDateCreated;

	private Menu dropDownMenu = null;

	public SortyByDropDownAction(TaskListView taskListView) {
		super();
		this.taskListView = taskListView;
		setText(LABEL);
		setToolTipText(LABEL);
		setMenuCreator(this);
	}

	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;
	}

	public void addActionsToMenu() {
		byPriority = new Action("", AS_CHECK_BOX) {
			@Override
			public void run() {
				taskListView.setSortBy(SortByIndex.PRIORITY);
				byPriority.setChecked(true);
				bySummary.setChecked(false);
				byDateCreated.setChecked(false);
			}
		};
		byPriority.setEnabled(true);
		byPriority.setText("Priority");
		byPriority.setImageDescriptor(TasksUiImages.PRIORITY_1);
		ActionContributionItem item = new ActionContributionItem(byPriority);
		item.fill(dropDownMenu, -1);

		bySummary = new Action("", AS_CHECK_BOX) {
			@Override
			public void run() {
				taskListView.setSortBy(SortByIndex.SUMMARY);
				byPriority.setChecked(false);
				bySummary.setChecked(true);
				byDateCreated.setChecked(false);
			}
		};
		bySummary.setEnabled(true);
		bySummary.setText("Summary");
		item = new ActionContributionItem(bySummary);
		item.fill(dropDownMenu, -1);

		byDateCreated = new Action("", AS_CHECK_BOX) {
			@Override
			public void run() {
				taskListView.setSortBy(SortByIndex.DATE_CREATED);
				byPriority.setChecked(false);
				bySummary.setChecked(false);
				byDateCreated.setChecked(true);
			}
		};
		byDateCreated.setEnabled(true);
		byDateCreated.setText("Date Created");
		byDateCreated.setImageDescriptor(TasksUiImages.CALENDAR_SMALL);
		item = new ActionContributionItem(byDateCreated);
		item.fill(dropDownMenu, -1);

		switch (taskListView.getSortByIndex()) {
		case PRIORITY:
			byPriority.setChecked(true);
			break;
		case SUMMARY:
			bySummary.setChecked(true);
			break;
		case DATE_CREATED:
			byDateCreated.setChecked(true);
			break;
		}
	}

	@Override
	public void run() {
		this.setChecked(isChecked());
	}
}

Back to the top