Skip to main content
summaryrefslogtreecommitdiffstats
blob: e90ff058715746f8c16ed81ed74a0b4951aff08e (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*******************************************************************************
 * Copyright (c) 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
 *     Maarten Meijer - fix for bug 284559
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.ui.editors;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.mylyn.commons.ui.CommonImages;
import org.eclipse.mylyn.tasks.core.ITask.PriorityLevel;
import org.eclipse.mylyn.tasks.ui.TasksUiImages;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.ui.forms.widgets.FormToolkit;

/**
 * @author David Shepherd
 * @author Steffen Pingel
 */
public class PriorityEditor {

	private Control control;

	private boolean ignoreNotification;

	private Label label;

	private Map<String, String> labelByValue;

	private Menu menu;

	private boolean readOnly;

	private ToolItem selectionButton;

	private ToolBar toolBar;

	private String value;

	public PriorityEditor() {
	}

	public void createControl(final Composite parent, FormToolkit toolkit) {
		if (isReadOnly()) {
			label = toolkit.createLabel(parent, ""); //$NON-NLS-1$
			setControl(label);
		} else {
			toolBar = new ToolBar(parent, SWT.FLAT);
			selectionButton = new ToolItem(toolBar, SWT.DROP_DOWN);
			selectionButton.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					if (menu == null) {
						createMenu(toolBar);
					}
					Point location = parent.toDisplay(toolBar.getLocation());
					location.y = location.y + selectionButton.getBounds().height;
					if (value != null) {
						MenuItem[] items = menu.getItems();
						for (MenuItem item : items) {
							item.setSelection(value.equals(item.getData()));
						}
					}
					menu.setLocation(location);
					menu.setVisible(true);
				}
			});
			selectionButton.addDisposeListener(new DisposeListener() {
				public void widgetDisposed(DisposeEvent e) {
					if (menu != null) {
						menu.dispose();
					}
				}
			});
			toolkit.adapt(toolBar);
			setControl(toolBar);
		}
	}

	private void createMenu(final ToolBar bar) {
		menu = new Menu(bar);
		for (String key : labelByValue.keySet()) {
			final MenuItem item = new MenuItem(menu, SWT.CHECK);
			item.setText(labelByValue.get(key));
			item.setData(key);
			item.setImage(getSmallImage(key));
			item.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					if (!ignoreNotification) {
						value = (String) item.getData();
						valueChanged(value);
					}
				}
			});
		}
	}

	public Control getControl() {
		return control;
	}

	public Map<String, String> getLabelByValue() {
		return Collections.unmodifiableMap(labelByValue);
	}

	private ImageDescriptor getLargeImageDescriptor(PriorityLevel priorityLevel) {
		if (priorityLevel != null) {
			switch (priorityLevel) {
			case P1:
				return CommonImages.PRIORITY_1_LARGE;
			case P2:
				return CommonImages.PRIORITY_2_LARGE;
			case P3:
				return CommonImages.PRIORITY_3_LARGE;
			case P4:
				return CommonImages.PRIORITY_4_LARGE;
			case P5:
				return CommonImages.PRIORITY_5_LARGE;
			}
		}
		return CommonImages.PRIORITY_3_LARGE;
	}

	private Image getSmallImage(String value) {
		ImageDescriptor descriptor = getSmallImageDescriptor(value);
		if (descriptor != null) {
			return CommonImages.getImage(descriptor);
		}
		return null;
	}

	private ImageDescriptor getSmallImageDescriptor(String value) {
		PriorityLevel priorityLevel = PriorityLevel.fromString(value);
		if (priorityLevel != null) {
			return TasksUiImages.getImageDescriptorForPriority(priorityLevel);
		}
		return null;
	}

	public String getToolTipText() {
		if (label != null) {
			return label.getToolTipText();
		}
		if (selectionButton != null) {
			return selectionButton.getToolTipText();
		}
		return null;
	}

	public boolean isReadOnly() {
		return readOnly;
	}

	public void select(String value, PriorityLevel level) {
		try {
			ignoreNotification = true;
			this.value = value;
			if (label != null) {
				label.setImage(CommonImages.getImage(getLargeImageDescriptor(level)));
			}
			if (selectionButton != null && toolBar != null) {
				selectionButton.setImage(CommonImages.getImage(getLargeImageDescriptor(level)));
			}
		} finally {
			ignoreNotification = false;
		}
	}

	private void setControl(Control control) {
		this.control = control;
	}

	public void setLabelByValue(Map<String, String> labelByValue) {
		this.labelByValue = new LinkedHashMap<String, String>(labelByValue);
		// the menu will be re-created with updated options when it is requested again
		if (menu != null) {
			menu.dispose();
		}
		menu = null;
	}

	public void setReadOnly(boolean readOnly) {
		this.readOnly = readOnly;
	}

	public void setToolTipText(String text) {
		if (label != null) {
			label.setToolTipText(text);
		}
		if (selectionButton != null) {
			selectionButton.setToolTipText(text);
		}
	}

	protected void valueChanged(String key) {
	}

	public String getValue() {
		return value;
	}

}

Back to the top