Skip to main content
summaryrefslogtreecommitdiffstats
blob: 37c4e365dceb36829ae7ffdc36629865770aed54 (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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*******************************************************************************
 * Copyright (c) 2004, 2010 Mylyn project 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.mylyn.internal.bugzilla.ui.editor;

import java.util.Map;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
import org.eclipse.jface.fieldassist.IContentProposalProvider;
import org.eclipse.jface.fieldassist.TextContentAdapter;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.mylyn.internal.provisional.commons.ui.CommonImages;
import org.eclipse.mylyn.internal.tasks.ui.PersonProposalLabelProvider;
import org.eclipse.mylyn.internal.tasks.ui.PersonProposalProvider;
import org.eclipse.mylyn.internal.tasks.ui.editors.EditorUtil;
import org.eclipse.mylyn.internal.tasks.ui.editors.Messages;
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.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.fieldassist.ContentAssistCommandAdapter;
import org.eclipse.ui.forms.events.HyperlinkAdapter;
import org.eclipse.ui.forms.events.HyperlinkEvent;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ImageHyperlink;
import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;

/**
 * @author Frank Becker
 */
public class FlagAttributeEditor extends AbstractAttributeEditor {

	private String[] values;

	private CCombo combo;

	private Text requesteeText;

	private ImageHyperlink selfLink;

	private Text flagText;

	private Text requesteeROText;

	private boolean suppressRefresh;

	private Composite flagComposite;

	public FlagAttributeEditor(TaskDataModel manager, TaskAttribute taskAttribute) {
		super(manager, taskAttribute);
		setLayoutHint(new LayoutHint(RowSpan.SINGLE, ColumnSpan.SINGLE));
		if (taskAttribute.getAttribute("state") != null) { //$NON-NLS-1$
			setReadOnly(taskAttribute.getAttribute("state").getMetaData().isReadOnly()); //$NON-NLS-1$
		}
	}

	@Override
	public void createControl(Composite parent, FormToolkit toolkit) {
		flagComposite = toolkit.createComposite(parent);

		GridLayout layout = new GridLayout(2, false);
		layout.marginHeight = 1;
		layout.marginWidth = 1;
		layout.horizontalSpacing = 10;
		flagComposite.setLayout(layout);
		if (isReadOnly()) {
			flagText = new Text(flagComposite, SWT.FLAT | SWT.READ_ONLY);
			toolkit.adapt(flagText, false, false);
			flagText.setData(FormToolkit.KEY_DRAW_BORDER, Boolean.FALSE);
			flagText.setText(getValueLabel());
			flagText.setBackground(parent.getBackground());
			flagText.setEditable(false);
			String tooltip = getTaskAttribute().getMetaData().getLabel();
			if (tooltip == null) {
				tooltip = getDescription();
			}
			if (tooltip != null) {
				flagText.setToolTipText(tooltip);
			}
			TaskAttribute requestee = getTaskAttribute().getAttribute("requestee"); //$NON-NLS-1$
			requesteeROText = new Text(flagComposite, SWT.FLAT | SWT.READ_ONLY);
			toolkit.adapt(requesteeROText, false, false);
			requesteeROText.setData(FormToolkit.KEY_DRAW_BORDER, Boolean.FALSE);
			requesteeROText.setText(requestee.getValue());
			requesteeROText.setBackground(parent.getBackground());
			requesteeROText.setEditable(false);
			requesteeROText.setToolTipText(requestee.getMetaData().getValue(TaskAttribute.META_DESCRIPTION));
		} else {
			combo = new CCombo(flagComposite, SWT.FLAT | SWT.READ_ONLY);
			toolkit.adapt(combo, false, false);
			combo.setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TEXT_BORDER);
			String tooltip = getTaskAttribute().getMetaData().getLabel();
			if (tooltip == null) {
				tooltip = getDescription();
			}
			if (tooltip != null) {
				combo.setToolTipText(tooltip);
			}
			EditorUtil.addScrollListener(combo);

			updateComboWithOptions();

			select(getValue(), getValueLabel());

			if (values != null) {
				combo.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent event) {
						int index = combo.getSelectionIndex();
						if (index > -1) {
							Assert.isNotNull(values);
							Assert.isLegal(index >= 0 && index <= values.length - 1);
							try {
								suppressRefresh = true;
								setValue(values[index]);
								selectionChanged(index);
							} finally {
								suppressRefresh = false;
							}
						}
					}
				});
			}
			TaskAttribute requestee = getTaskAttribute().getAttribute("requestee"); //$NON-NLS-1$
			if (requestee != null && !requestee.getMetaData().isReadOnly()) {
				final Composite requesteeComposite = new Composite(flagComposite, SWT.NONE);
				GridLayout requesteeLayout = new GridLayout(2, false);
				requesteeLayout.marginHeight = 0;
				requesteeLayout.marginWidth = 0;
				requesteeLayout.horizontalSpacing = 0;
				requesteeComposite.setLayout(requesteeLayout);
				requesteeComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
				requesteeComposite.setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TREE_BORDER);

				requesteeText = toolkit.createText(requesteeComposite, requestee.getValue());
				boolean enabled = "?".equals(getValueLabel()); //$NON-NLS-1$
				requesteeText.setEnabled(enabled);
				requesteeText.setToolTipText(requestee.getMetaData().getValue(TaskAttribute.META_DESCRIPTION));
				IContentProposalProvider contentProposalProvider = new PersonProposalProvider(null,
						requestee.getTaskData());
				ILabelProvider labelPropsalProvider = new PersonProposalLabelProvider();
				if (contentProposalProvider != null && labelPropsalProvider != null) {
					ContentAssistCommandAdapter adapter = new ContentAssistCommandAdapter(requesteeText,
							new TextContentAdapter(), contentProposalProvider,
							ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS, new char[0], true);
					adapter.setLabelProvider(labelPropsalProvider);
					adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
				}

				selfLink = new ImageHyperlink(requesteeComposite, SWT.NO_FOCUS);
				selfLink.setToolTipText(Messages.PersonAttributeEditor_Insert_My_User_Id_Tooltip);
				selfLink.setActiveImage(CommonImages.getImage(CommonImages.PERSON_ME_SMALL));
				selfLink.setHoverImage(CommonImages.getImage(CommonImages.PERSON_ME_SMALL));
				selfLink.setEnabled(enabled);
				selfLink.setBackground(requesteeText.getBackground());
				selfLink.addHyperlinkListener(new HyperlinkAdapter() {
					@Override
					public void linkActivated(HyperlinkEvent e) {
						String userName = getModel().getTaskRepository().getUserName();
						if (userName != null && userName.length() > 0) {
							requesteeText.setText(userName);
							setValue(userName);
						}
					}
				});
				GridDataFactory.fillDefaults().align(SWT.BEGINNING, SWT.CENTER).exclude(true).applyTo(selfLink);
				MouseTrackListener mouseListener = new MouseTrackAdapter() {
					int version = 0;

					@Override
					public void mouseEnter(MouseEvent e) {
						((GridData) selfLink.getLayoutData()).exclude = false;
						requesteeComposite.layout();
						selfLink.setImage(CommonImages.getImage(CommonImages.PERSON_ME_SMALL));
						selfLink.redraw();
						version++;
					}

					@Override
					public void mouseExit(MouseEvent e) {
						final int lastVersion = version;
						Display.getDefault().asyncExec(new Runnable() {
							public void run() {
								if (version != lastVersion || selfLink.isDisposed()) {
									return;
								}
								selfLink.setImage(null);
								selfLink.redraw();
								((GridData) selfLink.getLayoutData()).exclude = true;
								requesteeComposite.layout();
							}
						});
					}
				};
				requesteeText.addMouseTrackListener(mouseListener);
				selfLink.addMouseTrackListener(mouseListener);
				GridData requesteeData = new GridData(SWT.FILL, SWT.CENTER, true, false);
				requesteeText.setLayoutData(requesteeData);
				requesteeText.setFont(EditorUtil.TEXT_FONT);
				requesteeText.setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TREE_BORDER);

				requesteeText.addKeyListener(new KeyListener() {

					public void keyReleased(KeyEvent e) {
						try {
							suppressRefresh = true;
							setRequestee(requesteeText.getText());
						} finally {
							suppressRefresh = false;
						}
					}

					public void keyPressed(KeyEvent e) {
					}
				});
				requesteeText.addModifyListener(new ModifyListener() {

					public void modifyText(ModifyEvent e) {
						try {
							suppressRefresh = true;
							setRequestee(requesteeText.getText());
						} finally {
							suppressRefresh = false;
						}
					}
				});
				toolkit.adapt(requesteeText, false, false);

			}
		}
		toolkit.paintBordersFor(flagComposite);
		setControl(flagComposite);
	}

	private void updateComboWithOptions() {
		Map<String, String> labelByValue = getAttributeMapper().getAssoctiatedAttribute(getTaskAttribute())
				.getOptions();
		if (labelByValue != null) {
			combo.removeAll();
			values = labelByValue.keySet().toArray(new String[0]);
			for (String value : values) {
				combo.add(labelByValue.get(value));
			}
		}
	}

	public String getValue() {
		return getAttributeMapper().getValue(getAttributeMapper().getAssoctiatedAttribute(getTaskAttribute()));
	}

	public String getValueLabel() {
		return getAttributeMapper().getValueLabel(getAttributeMapper().getAssoctiatedAttribute(getTaskAttribute()));
	}

	private void select(String value, String label) {
		if (values != null) {
			for (int i = 0; i < values.length; i++) {
				if (values[i].equals(value)) {
					combo.select(i);
					break;
				}
			}
		} else {
			combo.setText(label);
		}
	}

	public void setRequestee(String value) {
		TaskAttribute requestee = getTaskAttribute().getAttribute("requestee"); //$NON-NLS-1$
		if (requestee != null) {
			if (!requestee.getValue().equals(value)) {
				getAttributeMapper().setValue(requestee, value);
				attributeChanged();
			}
		}
	}

	public void setValue(String value) {
		getAttributeMapper().setValue(getAttributeMapper().getAssoctiatedAttribute(getTaskAttribute()), value);
		attributeChanged();
	}

	@Override
	public String getLabel() {
		String label = getAttributeMapper().getLabel(getAttributeMapper().getAssoctiatedAttribute(getTaskAttribute()));
		if (label != null) {
			label.replace("&", "&&"); //$NON-NLS-1$//$NON-NLS-2$
		} else {
			label = ""; //$NON-NLS-1$
		}

		TaskAttribute setter = getTaskAttribute().getAttribute("setter"); //$NON-NLS-1$
		if (setter != null) {
			String setterValue = setter.getValue();
			if (setterValue != null && !setterValue.equals("")) { //$NON-NLS-1$
				if (setterValue.indexOf("@") > -1) { //$NON-NLS-1$
					setterValue = setterValue.substring(0, setterValue.indexOf("@")); //$NON-NLS-1$
				}
				label = setterValue + ": " + label; //$NON-NLS-1$
			}
		}
		return label;
	}

	@Override
	public void refresh() {
		if (flagText != null && !flagText.isDisposed()) {
			flagText.setText(getValueLabel());
		}
		if (combo != null && !combo.isDisposed()) {
			updateComboWithOptions();
			select(getValue(), getValueLabel());
			if (combo.getSelectionIndex() >= 0) {
				selectionChanged(combo.getSelectionIndex());
			}
		}
		TaskAttribute requestee = getTaskAttribute().getAttribute("requestee"); //$NON-NLS-1$
		if (requestee != null) {
			if (requesteeROText != null && !requesteeROText.isDisposed()) {
				requesteeROText.setText(requestee.getValue());
			}
			if (requesteeText != null && !requesteeText.isDisposed()) {
				requesteeText.setText(requestee.getValue());
			}
		}
		updateLabel();
		if (flagComposite != null && !flagComposite.isDisposed()) {
			flagComposite.layout();
		}
	}

	@Override
	public boolean shouldAutoRefresh() {
		return !suppressRefresh;
	}

	private void selectionChanged(int index) {
		if (requesteeText != null) {
			requesteeText.setEnabled(values[index].equals("?")); //$NON-NLS-1$
			selfLink.setEnabled(values[index].equals("?")); //$NON-NLS-1$
		}
	}
}

Back to the top