Skip to main content
summaryrefslogtreecommitdiffstats
blob: 088add854344e694128f6a9803a98daf7f5158fc (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
227
228
229
230
231
232
233
234
235
236
/*******************************************************************************
 * 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
 *     Pawel Niewiadomski - fix for bug 287832
 *******************************************************************************/
package org.eclipse.mylyn.internal.tasks.ui.editors;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.window.Window;
import org.eclipse.mylyn.internal.provisional.commons.ui.WorkbenchUtil;
import org.eclipse.mylyn.internal.provisional.commons.ui.dialogs.AbstractInPlaceDialog;
import org.eclipse.mylyn.internal.provisional.commons.ui.dialogs.IInPlaceDialogListener;
import org.eclipse.mylyn.internal.provisional.commons.ui.dialogs.InPlaceCheckBoxTreeDialog;
import org.eclipse.mylyn.internal.provisional.commons.ui.dialogs.InPlaceDialogEvent;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.core.data.TaskDataModel;
import org.eclipse.mylyn.tasks.ui.editors.AbstractAttributeEditor;
import org.eclipse.mylyn.tasks.ui.editors.LayoutHint;
import org.eclipse.mylyn.tasks.ui.editors.LayoutHint.ColumnSpan;
import org.eclipse.mylyn.tasks.ui.editors.LayoutHint.RowSpan;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.widgets.SharedScrolledComposite;

/**
 * @author Shawn Minto
 */
public class CheckboxMultiSelectAttributeEditor extends AbstractAttributeEditor {

	private Text valueText;

	private Composite parent;

	private Button button;

	private boolean cflowSelect;

	public CheckboxMultiSelectAttributeEditor(TaskDataModel manager, TaskAttribute taskAttribute) {
		super(manager, taskAttribute);
		setLayoutHint(new LayoutHint(RowSpan.SINGLE, ColumnSpan.MULTIPLE));
	}

	@Override
	public void createControl(Composite parent, FormToolkit toolkit) {
		if (isReadOnly()) {
			valueText = new Text(parent, SWT.FLAT | SWT.READ_ONLY | SWT.WRAP);
			valueText.setFont(EditorUtil.TEXT_FONT);
			toolkit.adapt(valueText, false, false);
			valueText.setData(FormToolkit.KEY_DRAW_BORDER, Boolean.FALSE);
			valueText.setToolTipText(getDescription());
			updateText();
			setControl(valueText);
		} else {
			this.parent = parent;

			Composite composite = toolkit.createComposite(parent);
			composite.setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TREE_BORDER);
			GridLayout layout = new GridLayout(2, false);
			layout.marginWidth = 0;
			layout.marginBottom = 0;
			layout.marginLeft = 0;
			layout.marginRight = 0;
			layout.marginTop = 0;
			layout.marginHeight = 0;
			composite.setLayout(layout);

			valueText = toolkit.createText(composite, "", SWT.FLAT | SWT.WRAP); //$NON-NLS-1$
			GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(valueText);
			valueText.setFont(EditorUtil.TEXT_FONT);
			valueText.setEditable(false);
			valueText.setToolTipText(getDescription());

			button = toolkit.createButton(composite, "", SWT.ARROW | SWT.DOWN); //$NON-NLS-1$
			GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.TOP).applyTo(button);
			button.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					final List<String> values = getValues();
					Map<String, String> validValues = getAttributeMapper().getOptions(getTaskAttribute());

					final InPlaceCheckBoxTreeDialog selectionDialog = new InPlaceCheckBoxTreeDialog(
							WorkbenchUtil.getShell(), button, values, validValues, NLS.bind(
									Messages.CheckboxMultiSelectAttributeEditor_Select_X, getLabel()));

					selectionDialog.addEventListener(new IInPlaceDialogListener() {

						public void buttonPressed(InPlaceDialogEvent event) {
							cflowSelect = true;
							try {
								if (event.getReturnCode() == Window.OK) {
									Set<String> newValues = selectionDialog.getSelectedValues();
									if (!new HashSet<String>(values).equals(newValues)) {
										setValues(new ArrayList<String>(newValues));
										attributeChanged();
										updateText();
									}
								} else if (event.getReturnCode() == AbstractInPlaceDialog.ID_CLEAR) {
									Set<String> newValues = new HashSet<String>();
									if (!new HashSet<String>(values).equals(newValues)) {
										setValues(new ArrayList<String>(newValues));
										attributeChanged();
										updateText();
									}
								}
							} finally {
								cflowSelect = false;
							}
						}
					});
					selectionDialog.open();
				}
			});
			toolkit.adapt(valueText, false, false);
			updateText();
			setControl(composite);
		}
	}

	private void updateText() {
		if (valueText != null && !valueText.isDisposed()) {
			StringBuilder valueString = new StringBuilder();
			List<String> values = getValuesLabels();
			Collections.sort(values);
			for (int i = 0; i < values.size(); i++) {
				valueString.append(values.get(i));
				if (i != values.size() - 1) {
					valueString.append(", "); //$NON-NLS-1$
				}
			}
			valueText.setText(valueString.toString());
			if (valueText != null && parent != null && parent.getParent() != null
					&& parent.getParent().getParent() != null) {
				Point size = valueText.getSize();
				// subtract 1 from size for border
				Point newSize = valueText.computeSize(size.x - 1, SWT.DEFAULT);
				if (newSize.y != size.y) {
					reflow();
				}
			}
		}
	}

	/**
	 * Update scroll bars of the enclosing form.
	 * 
	 * @see Section#reflow()
	 */
	private void reflow() {
		Composite c = parent;
		while (c != null) {
			c.setRedraw(false);
			c = c.getParent();
			if (c instanceof SharedScrolledComposite || c instanceof Shell) {
				break;
			}
		}
		c = parent;
		while (c != null) {
			c.layout(true);
			c = c.getParent();
			if (c instanceof SharedScrolledComposite) {
				((SharedScrolledComposite) c).reflow(true);
				break;
			}
		}
		c = parent;
		while (c != null) {
			c.setRedraw(true);
			c = c.getParent();
			if (c instanceof SharedScrolledComposite || c instanceof Shell) {
				break;
			}
		}
	}

	public List<String> getValues() {
		return getAttributeMapper().getValues(getTaskAttribute());
	}

	public List<String> getValuesLabels() {
		return getAttributeMapper().getValueLabels(getTaskAttribute());
	}

	public void setValues(List<String> newValues) {
		getAttributeMapper().setValues(getTaskAttribute(), newValues);
		attributeChanged();
	}

	@Override
	protected void decorateIncoming(Color color) {
		super.decorateIncoming(color);
		if (valueText != null && !valueText.isDisposed()) {
			valueText.setBackground(color);
		}
		if (button != null && !button.isDisposed()) {
			button.setBackground(color);
		}
	}

	@Override
	public void refresh() {
		if (!cflowSelect) {
			updateText();
		}
	}

	@Override
	public boolean shouldAutoRefresh() {
		return true;
	}
}

Back to the top