Skip to main content
summaryrefslogtreecommitdiffstats
blob: 19cc0038594171b9b8dd2faf1f96abf425ee1060 (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
/*******************************************************************************
 * Copyright (c) 2004, 2010 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.List;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IMenuCreator;
import org.eclipse.jface.action.Separator;
import org.eclipse.mylyn.internal.provisional.commons.ui.CommonImages;
import org.eclipse.mylyn.internal.provisional.commons.ui.WorkbenchUtil;
import org.eclipse.mylyn.internal.tasks.ui.views.AbstractTaskListPresentation;
import org.eclipse.mylyn.internal.tasks.ui.views.TaskListView;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;

/**
 * @author Rob Elves
 * @author Steffen Pingel
 */
public class PresentationDropDownSelectionAction extends Action implements IMenuCreator {

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

	private final TaskListView view;

	private Menu dropDownMenu;

	public PresentationDropDownSelectionAction(TaskListView view) {
		this.view = view;
		setMenuCreator(this);
		setText(Messages.PresentationDropDownSelectionAction_Task_Presentation);
		setToolTipText(Messages.PresentationDropDownSelectionAction_Task_Presentation);
		setId(ID);
		setEnabled(true);
		setImageDescriptor(CommonImages.PRESENTATION);
	}

	private void addActionsToMenu() {
		for (AbstractTaskListPresentation presentation : TaskListView.getPresentations()) {
			if (presentation.isPrimary()) {
				PresentationSelectionAction action = new PresentationSelectionAction(view, presentation);
				ActionContributionItem item = new ActionContributionItem(action);
				item.fill(dropDownMenu, -1);
			}
		}

		boolean separatorAdded = false;

		for (AbstractTaskListPresentation presentation : TaskListView.getPresentations()) {
			if (WorkbenchUtil.allowUseOf(presentation)) {
				if (!presentation.isPrimary()) {
					if (!separatorAdded) {
						new Separator().fill(dropDownMenu, -1);
						separatorAdded = true;
					}

					PresentationSelectionAction action = new PresentationSelectionAction(view, presentation);
					ActionContributionItem item = new ActionContributionItem(action);
					item.fill(dropDownMenu, -1);
				}
			}
		}
	}

	@Override
	public void run() {
		AbstractTaskListPresentation current = view.getCurrentPresentation();
		List<AbstractTaskListPresentation> all = TaskListView.getPresentations();
		int size = all.size();
		if (size == 0) {
			return;
		}

		// cycle between primary presentations
		int index = all.indexOf(current) + 1;
		for (int i = 0; i < size; i++) {
			AbstractTaskListPresentation presentation = all.get(index % size);
			if (presentation.isPrimary()) {
				view.applyPresentation(presentation);
				return;
			}
			index++;
		}

		// fall back to next presentation in list
		index = all.indexOf(current) + 1;
		if (index < size) {
			view.applyPresentation(all.get(index));
		} else {
			view.applyPresentation(all.get(0));
		}
	}

	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 static class PresentationSelectionAction extends Action {

		private final AbstractTaskListPresentation presentation;

		private final TaskListView view;

		public PresentationSelectionAction(TaskListView view, AbstractTaskListPresentation presentation) {
			this.view = view;
			this.presentation = presentation;
			setText(presentation.getName());
			setImageDescriptor(presentation.getImageDescriptor());
			update();
		}

		public void update() {
			setChecked(view.getCurrentPresentation().getId().equals(presentation.getId()));
		}

		@Override
		public void run() {
			view.applyPresentation(presentation);
		}

	}

}

Back to the top