Project

General

Profile

Download (10.5 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * 
3
 */
4
package eu.etaxonomy.taxeditor.ui.selection;
5

    
6
import org.eclipse.core.runtime.IStatus;
7
import org.eclipse.jface.wizard.WizardDialog;
8
import org.eclipse.swt.SWT;
9
import org.eclipse.swt.events.SelectionAdapter;
10
import org.eclipse.swt.events.SelectionEvent;
11
import org.eclipse.swt.events.SelectionListener;
12
import org.eclipse.swt.graphics.Color;
13
import org.eclipse.swt.widgets.Button;
14
import org.eclipse.swt.widgets.Composite;
15
import org.eclipse.swt.widgets.Label;
16
import org.eclipse.swt.widgets.Shell;
17

    
18
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
19
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
20
import eu.etaxonomy.cdm.common.CdmUtils;
21
import eu.etaxonomy.cdm.model.common.IIdentifiableEntity;
22
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
23
import eu.etaxonomy.taxeditor.model.ImageResources;
24
import eu.etaxonomy.taxeditor.preference.Resources;
25
import eu.etaxonomy.taxeditor.store.StoreUtil;
26
import eu.etaxonomy.taxeditor.ui.forms.AbstractCdmFormElement;
27
import eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory;
28
import eu.etaxonomy.taxeditor.ui.forms.CdmPropertyChangeEvent;
29
import eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement;
30
import eu.etaxonomy.taxeditor.ui.forms.IEnableableFormElement;
31
import eu.etaxonomy.taxeditor.ui.forms.IEntityElement;
32
import eu.etaxonomy.taxeditor.ui.forms.ILabeledElement;
33
import eu.etaxonomy.taxeditor.ui.forms.ISelectableElement;
34
import eu.etaxonomy.taxeditor.ui.forms.SelectionArbitrator;
35

    
36
/**
37
 * <p>
38
 * Abstract AbstractSelectionElement class.
39
 * </p>
40
 * 
41
 * @author n.hoffmann
42
 * @created Nov 17, 2009
43
 * @version 1.0
44
 * @param <T>
45
 */
46
public abstract class AbstractSelectionElement<T> extends
47
		AbstractCdmFormElement implements SelectionListener,
48
		IEnableableFormElement, ISelectableElement, IEntityElement<T>,
49
		ILabeledElement, IConversationEnabled {
50

    
51
	/**
52
	 * bitmask to set
53
	 */
54
	public static final int NOTHING = 0; // 000
55
	public static final int EDITABLE = 1 << 0; // 001
56
	public static final int DELETABLE = 1 << 1; // 010
57
	public static final int SELECTABLE = 1 << 2; // 100
58
	public static final int ALL = (int) Math.pow(2, 3); // 111
59

    
60
	protected T entity;
61

    
62
	protected Label label;
63
	protected Label text;
64
	protected Button button_selection;
65

    
66
	private SelectionArbitrator selectionArbitrator;
67

    
68
	protected Button button_edit;
69

    
70
	private final String labelString;
71

    
72
	private Composite selectableComposite;
73

    
74
	private Button button_remove;
75

    
76
	private final boolean isEditable;
77

    
78
	private final boolean isDeletable;
79

    
80
	private final ConversationHolder conversation;
81

    
82
	/**
83
	 * <p>
84
	 * Constructor for AbstractSelectionElement.
85
	 * </p>
86
	 * 
87
	 * @param formFactory
88
	 *            a {@link eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory}
89
	 *            object.
90
	 * @param conversation
91
	 *            TODO
92
	 * @param parentElement
93
	 *            a {@link eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement}
94
	 *            object.
95
	 * @param labelString
96
	 *            a {@link java.lang.String} object.
97
	 * @param entity
98
	 *            a T object.
99
	 * @param isEditable
100
	 *            a boolean.
101
	 * @param isSelectable
102
	 *            a boolean.
103
	 * @param isDeletable
104
	 *            a boolean.
105
	 * @param style
106
	 *            a int.
107
	 * @param <T>
108
	 *            a T object.
109
	 */
110
	public AbstractSelectionElement(CdmFormFactory formFactory,
111
			ConversationHolder conversation, ICdmFormElement parentElement,
112
			String labelString, T entity, int mode, int style) {
113
		super(formFactory, parentElement);
114

    
115
		this.isEditable = (mode & EDITABLE) == EDITABLE;
116
		this.isDeletable = (mode & DELETABLE) == DELETABLE;
117

    
118
		this.labelString = labelString;
119

    
120
		this.conversation = conversation;
121

    
122
		if (((mode & SELECTABLE) == SELECTABLE)
123
				&& formFactory.getSelectionProvider() != null) {
124
			selectionArbitrator = formFactory.createSelectionArbitrator(this);
125
		}
126

    
127
		createControls(getLayoutComposite(), SWT.NULL);
128

    
129
		setEntity(entity);
130
	}
131

    
132
	private void createControls(Composite parent, int style) {
133

    
134
		label = formFactory.createLabel(getLayoutComposite(), labelString,
135
				SWT.NULL);
136

    
137
		addControl(label);
138

    
139
		selectableComposite = formFactory.createComposite(getLayoutComposite());
140

    
141
		int columns = 2;
142
		if (isEditable) {
143
			columns += 1;
144
		}
145
		if (isDeletable) {
146
			columns += 1;
147
		}
148

    
149
		selectableComposite.setLayout(CdmFormFactory.LAYOUT(columns, false));
150
		selectableComposite.setLayoutData(CdmFormFactory.FILL_HORIZONTALLY());
151

    
152
		addControl(selectableComposite);
153

    
154
		text = formFactory.createLabel(selectableComposite, null, SWT.WRAP);
155
		addControl(text);
156

    
157
		text.setLayoutData(CdmFormFactory.FILL_HORIZONTALLY());
158
		text.setBackground(StoreUtil
159
				.getColor(Resources.COLOR_TEXT_DISABLED_BACKGROUND));
160

    
161
		button_selection = formFactory.createButton(selectableComposite, null,
162
				SWT.PUSH);
163
		button_selection.setImage(ImageResources
164
				.getImage(ImageResources.BROWSE_ICON));
165
		button_selection.setToolTipText("Browse existing");
166
		addControl(button_selection);
167
		button_selection.addSelectionListener(this);
168

    
169
		if (isEditable) {
170
			button_edit = formFactory.createButton(selectableComposite, null,
171
					SWT.PUSH);
172
			button_edit.setImage(ImageResources
173
					.getImage(ImageResources.EDIT_ICON));
174
			button_edit.setToolTipText("Edit");
175
			addControl(button_edit);
176
			button_edit.addSelectionListener(new EditListener(this));
177
		}
178

    
179
		if (isDeletable) {
180
			button_remove = formFactory.createButton(selectableComposite, null,
181
					SWT.PUSH);
182
			button_remove.setImage(ImageResources
183
					.getImage(ImageResources.TRASH_ICON));
184
			button_remove.setToolTipText("Remove");
185
			addControl(button_remove);
186
			button_remove.addSelectionListener(new DeleteListener(this));
187
		}
188
	}
189

    
190
	/**
191
	 * Return the selected object
192
	 * 
193
	 * @return a T object.
194
	 */
195
	public T getSelection() {
196
		return entity;
197
	}
198

    
199
	/*
200
	 * (non-Javadoc)
201
	 * 
202
	 * @see
203
	 * eu.etaxonomy.taxeditor.forms.IEnableableFormElement#setEnabled(boolean)
204
	 */
205
	/** {@inheritDoc} */
206
	@Override
207
	public void setEnabled(boolean enabled) {
208
		button_selection.setEnabled(enabled);
209
		if (isEditable) {
210
			button_edit.setEnabled(enabled && entity != null);
211
		}
212
	}
213

    
214
	/**
215
	 * <p>
216
	 * setSelectionInternal
217
	 * </p>
218
	 * 
219
	 * @param selection
220
	 *            a T object.
221
	 */
222
	protected void setSelectionInternal(T selection) {
223
		if (selection != null && !selection.equals(this.entity)) {
224
			setEntity(selection);
225
			getParentElement().firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
226
		}
227
	}
228

    
229
	/**
230
	 * <p>
231
	 * Setter for the field <code>entity</code>.
232
	 * </p>
233
	 * 
234
	 * @param selection
235
	 *            a T object.
236
	 */
237
	public void setEntity(T selection) {
238
		this.entity = selection;
239
		updateElement();
240
	}
241

    
242
	/**
243
	 * Updates this elements view
244
	 */
245
	protected void updateElement() {
246
		String title = CdmUtils.Nz(getTitle());
247
		// we have to duplicate ampersands otherwise they are treated as
248
		// mnenomic (see Label.setText() documentation)
249
		title = title.replace("&", "&&");
250
		text.setText(title); // title can be null
251
		if (isEditable) {
252
			button_edit.setEnabled(entity != null);
253
		}
254
	}
255

    
256
	/**
257
	 * <p>
258
	 * updateFromWizard
259
	 * </p>
260
	 */
261
	protected void updateFromWizard() {
262
		updateElement();
263
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
264
	}
265

    
266
	/**
267
	 * <p>
268
	 * getTitle
269
	 * </p>
270
	 * 
271
	 * @return a {@link java.lang.String} object.
272
	 */
273
	protected String getTitle() {
274
		if (entity != null && entity instanceof IIdentifiableEntity) {
275
			return ((IIdentifiableEntity) entity).getTitleCache();
276
		}
277
		return "";
278
	}
279

    
280
	/** {@inheritDoc} */
281
	@Override
282
	public void setSelected(boolean selected) {
283
		Color color = getColor(selected);
284
		getLayoutComposite().setBackground(color);
285
		selectableComposite.setBackground(color);
286
		label.setBackground(color);
287
	}
288

    
289
	/*
290
	 * (non-Javadoc)
291
	 * 
292
	 * @see eu.etaxonomy.taxeditor.forms.IEntityElement#getEntity()
293
	 */
294
	/**
295
	 * <p>
296
	 * Getter for the field <code>entity</code>.
297
	 * </p>
298
	 * 
299
	 * @return a T object.
300
	 */
301
	@Override
302
	public T getEntity() {
303
		return entity;
304
	}
305

    
306
	/*
307
	 * (non-Javadoc)
308
	 * 
309
	 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#
310
	 * getSelectionArbitrator()
311
	 */
312
	/**
313
	 * <p>
314
	 * Getter for the field <code>selectionArbitrator</code>.
315
	 * </p>
316
	 * 
317
	 * @return a {@link eu.etaxonomy.taxeditor.ui.forms.SelectionArbitrator}
318
	 *         object.
319
	 */
320
	@Override
321
	public SelectionArbitrator getSelectionArbitrator() {
322
		return selectionArbitrator;
323
	}
324

    
325
	/**
326
	 * Convenient access to current shell
327
	 * 
328
	 * @return a {@link org.eclipse.swt.widgets.Shell} object.
329
	 */
330
	protected Shell getShell() {
331
		return getLayoutComposite().getShell();
332
	}
333

    
334
	/** {@inheritDoc} */
335
	@Override
336
	public void setIrrelevant(boolean irrelevant) {
337
		String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT
338
				: Resources.COLOR_TEXT_DISABLED_BACKGROUND;
339

    
340
		Color color = StoreUtil.getColor(colorId);
341
		text.setBackground(color);
342
	}
343

    
344
	private class DeleteListener extends SelectionAdapter {
345

    
346
		private final AbstractSelectionElement<T> selectionElement;
347

    
348
		public DeleteListener(AbstractSelectionElement<T> selectionElement) {
349
			this.selectionElement = selectionElement;
350
		}
351

    
352
		@Override
353
		public void widgetSelected(SelectionEvent e) {
354
			setEntity(null);
355
			firePropertyChangeEvent(new CdmPropertyChangeEvent(
356
					selectionElement, null));
357
		}
358
	}
359

    
360
	private class EditListener extends SelectionAdapter {
361

    
362
		private final AbstractSelectionElement<T> selectionElement;
363

    
364
		public EditListener(AbstractSelectionElement<T> selectionElement) {
365
			this.selectionElement = selectionElement;
366
		}
367

    
368
		/** {@inheritDoc} */
369
		@Override
370
		public void widgetSelected(SelectionEvent e) {
371
			WizardDialog dialog = new WizardDialog(selectionElement.getShell(),
372
					new EditFromSelectionWizard(selectionElement));
373
			if (dialog.open() == IStatus.OK) {
374
				selectionElement.updateFromWizard();
375
			}
376
		}
377
	}
378

    
379
	// not used
380
	/** {@inheritDoc} */
381
	@Override
382
	public void widgetDefaultSelected(SelectionEvent e) {
383
	}
384

    
385
	/**
386
	 * <p>
387
	 * getConversationHolder
388
	 * </p>
389
	 * 
390
	 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
391
	 *         object.
392
	 */
393
	@Override
394
	public ConversationHolder getConversationHolder() {
395
		return conversation;
396
	}
397

    
398
	/** {@inheritDoc} */
399
	@Override
400
	public void setBackground(Color color) {
401
		label.setBackground(color);
402
	}
403

    
404
	/** {@inheritDoc} */
405
	@Override
406
	public void setLabel(String labelString) {
407
		if (label != null) {
408
			label.setText(labelString);
409
		}
410
	}
411

    
412
	/**
413
	 * <p>
414
	 * Getter for the field <code>label</code>.
415
	 * </p>
416
	 * 
417
	 * @return a {@link java.lang.String} object.
418
	 */
419
	@Override
420
	public String getLabel() {
421
		if (label != null) {
422
			return label.getText();
423
		}
424
		return null;
425
	}
426

    
427
	/** {@inheritDoc} */
428
	@Override
429
	public void update(CdmDataChangeMap changeEvents) {
430
	}
431
}
(1-1/24)