Project

General

Profile

Download (11.6 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
import org.springframework.security.core.GrantedAuthority;
18

    
19
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
20
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
21
import eu.etaxonomy.cdm.common.CdmUtils;
22
import eu.etaxonomy.cdm.model.common.Group;
23
import eu.etaxonomy.cdm.model.common.ICdmBase;
24
import eu.etaxonomy.cdm.model.common.IIdentifiableEntity;
25
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
26
import eu.etaxonomy.taxeditor.model.ImageResources;
27
import eu.etaxonomy.taxeditor.preference.Resources;
28
import eu.etaxonomy.taxeditor.store.StoreUtil;
29
import eu.etaxonomy.taxeditor.ui.dialog.selection.SelectionDialogFactory;
30
import eu.etaxonomy.taxeditor.ui.element.AbstractCdmFormElement;
31
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
32
import eu.etaxonomy.taxeditor.ui.element.CdmPropertyChangeEvent;
33
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
34
import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
35
import eu.etaxonomy.taxeditor.ui.element.IEntityElement;
36
import eu.etaxonomy.taxeditor.ui.element.ILabeledElement;
37
import eu.etaxonomy.taxeditor.ui.element.ISelectableElement;
38
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
39
import eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator;
40
import eu.etaxonomy.taxeditor.ui.section.grantedAuthority.GrantedAuthorityLabelTextProvider;
41

    
42
/**
43
 * <p>
44
 * Abstract AbstractSelectionElement class.
45
 * </p>
46
 *
47
 * @author n.hoffmann
48
 * @created Nov 17, 2009
49
 * @version 1.0
50
 * @param <T>
51
 */
52
public class EntitySelectionElement<T extends ICdmBase> extends
53
		AbstractCdmFormElement implements  SelectionListener, IEnableableFormElement, ISelectableElement, IEntityElement<T>, ILabeledElement, IConversationEnabled {
54

    
55
	/**
56
	 * Bitmask for configuring functionality of selection element
57
	 */
58
	public static final int NOTHING = 0; // 000
59
	public static final int EDITABLE = 1 << 0; // 001
60
	public static final int DELETABLE = 1 << 1; // 010
61
	public static final int SELECTABLE = 1 << 2; // 100
62
	public static final int ALL = EDITABLE | DELETABLE | SELECTABLE; // 111
63

    
64
	protected T entity;
65

    
66
	protected Label label;
67
	protected Label text;
68
	protected Button button_selection;
69

    
70
	private SelectionArbitrator selectionArbitrator;
71

    
72
	protected Button button_edit;
73

    
74
	private final String labelString;
75

    
76
	private Composite selectableComposite;
77

    
78
	private Button button_remove;
79

    
80
	private final boolean isEditable;
81

    
82
	private final boolean isDeletable;
83

    
84
	private final ConversationHolder conversation;
85
	private Class<T> clazz;
86

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

    
120
		this.isEditable = (mode & EDITABLE) == EDITABLE;
121
		this.isDeletable = (mode & DELETABLE) == DELETABLE;
122
		boolean isSelectable = (mode & SELECTABLE) == SELECTABLE;
123

    
124
		this.labelString = labelString;
125

    
126
		this.conversation = conversation;
127

    
128
		if (isSelectable && formFactory.getSelectionProvider() != null) {
129
			selectionArbitrator = formFactory.createSelectionArbitrator(this);
130
		}
131

    
132
		createControls(getLayoutComposite(), SWT.NULL);
133

    
134
		setEntity(entity);
135
	}
136

    
137
	public EntitySelectionElement(CdmFormFactory formFactory,
138
			ConversationHolder conversation, ICdmFormElement parentElement, Class<T> clazz,
139
			String labelString, T entity, int mode, int style) {
140
		this(formFactory, conversation, parentElement, labelString, entity, mode, style);
141
		this.clazz = clazz;
142
	}
143

    
144
	private void createControls(Composite parent, int style) {
145

    
146
		label = formFactory.createLabel(getLayoutComposite(), labelString + " : ",
147
				SWT.NULL);
148

    
149
		addControl(label);
150

    
151
		selectableComposite = formFactory.createComposite(getLayoutComposite());
152

    
153
		int columns = 2;
154
		if (isEditable) {
155
			columns += 1;
156
		}
157
		if (isDeletable) {
158
			columns += 1;
159
		}
160

    
161
		selectableComposite.setLayout(LayoutConstants.LAYOUT(columns, false));
162
		selectableComposite.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
163

    
164
		addControl(selectableComposite);
165

    
166
		text = formFactory.createLabel(selectableComposite, null, SWT.WRAP);
167
		addControl(text);
168

    
169
		text.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
170
		text.setBackground(StoreUtil
171
				.getColor(Resources.COLOR_TEXT_DISABLED_BACKGROUND));
172

    
173
		button_selection = formFactory.createButton(selectableComposite, null,
174
				SWT.PUSH);
175
		button_selection.setImage(ImageResources
176
				.getImage(ImageResources.BROWSE_ICON));
177
		button_selection.setToolTipText("Browse existing");
178

    
179
		addControl(button_selection);
180
		button_selection.addSelectionListener(this);
181

    
182
		if (isEditable) {
183
			button_edit = formFactory.createButton(selectableComposite, null,
184
					SWT.PUSH);
185
			button_edit.setImage(ImageResources
186
					.getImage(ImageResources.EDIT_ICON));
187
			button_edit.setToolTipText("Edit");
188
			addControl(button_edit);
189
			button_edit.addSelectionListener(new EditListener(this));
190
		}
191

    
192
		if (isDeletable) {
193
			button_remove = formFactory.createButton(selectableComposite, null,
194
					SWT.PUSH);
195
			button_remove.setImage(ImageResources
196
					.getImage(ImageResources.TRASH_ICON));
197
			button_remove.setToolTipText("Remove");
198
			addControl(button_remove);
199
			button_remove.addSelectionListener(new DeleteListener(this));
200
		}
201
	}
202

    
203
	@Override
204
    public void widgetSelected(SelectionEvent e) {
205
		T selection = SelectionDialogFactory.getSelectionFromDialog(clazz, getShell(), getConversationHolder(), getEntity());
206
		setSelectionInternal(selection);
207
	}
208

    
209
	/**
210
	 * Return the selected object
211
	 *
212
	 * @return a T object.
213
	 */
214
	public T getSelection() {
215
		return entity;
216
	}
217

    
218
	/*
219
	 * (non-Javadoc)
220
	 *
221
	 * @see
222
	 * eu.etaxonomy.taxeditor.forms.IEnableableFormElement#setEnabled(boolean)
223
	 */
224
	/** {@inheritDoc} */
225
	@Override
226
	public void setEnabled(boolean enabled) {
227
		button_selection.setEnabled(enabled);
228
		if (isEditable) {
229
			button_edit.setEnabled(enabled && entity != null);
230
		}
231
	}
232

    
233
	/**
234
	 * <p>
235
	 * setSelectionInternal
236
	 * </p>
237
	 *
238
	 * @param selection
239
	 *            a T object.
240
	 */
241
	protected void setSelectionInternal(T selection) {
242
		if (selection != null && !selection.equals(this.entity)) {
243
			setEntity(selection);
244
			firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
245
		}
246
	}
247

    
248
	/**
249
	 * <p>
250
	 * Setter for the field <code>entity</code>.
251
	 * </p>
252
	 *
253
	 * @param selection
254
	 *            a T object.
255
	 */
256
	public void setEntity(T selection) {
257
		this.entity = selection;
258
		updateElement();
259
	}
260

    
261
	/**
262
	 * Updates this elements view
263
	 */
264
	protected void updateElement() {
265
		String title = CdmUtils.Nz(getTitle());
266
		// we have to duplicate ampersands otherwise they are treated as
267
		// mnenomic (see Label.setText() documentation)
268
		title = title.replace("&", "&&");
269
		text.setText(title); // title can be null
270
		if (isEditable) {
271
			button_edit.setEnabled(entity != null);
272
		}
273
	}
274

    
275
    public void updateFromWizard() {
276
		updateElement();
277
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
278
	}
279

    
280
	/**
281
	 * <p>
282
	 * getTitle
283
	 * </p>
284
	 *
285
	 * @return a {@link java.lang.String} object.
286
	 */
287
	protected String getTitle() {
288
		if (entity != null){
289
			if(entity instanceof IIdentifiableEntity) {
290
				return ((IIdentifiableEntity) entity).getTitleCache();
291
			} else if(entity instanceof Group){
292
				return ((Group) entity).getName();
293
			} else if(entity instanceof GrantedAuthority){
294
				return GrantedAuthorityLabelTextProvider.getText(((GrantedAuthority) entity));
295
			}
296
		}
297
		return "";
298
	}
299

    
300
	/** {@inheritDoc} */
301
	@Override
302
	public void setSelected(boolean selected) {
303
		setBackground(selected ? SELECTED : getPersistentBackground());
304
	}
305

    
306
	/*
307
	 * (non-Javadoc)
308
	 *
309
	 * @see eu.etaxonomy.taxeditor.forms.IEntityElement#getEntity()
310
	 */
311
	/**
312
	 * <p>
313
	 * Getter for the field <code>entity</code>.
314
	 * </p>
315
	 *
316
	 * @return a T object.
317
	 */
318
	@Override
319
	public T getEntity() {
320
		return entity;
321
	}
322

    
323
	/*
324
	 * (non-Javadoc)
325
	 *
326
	 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#
327
	 * getSelectionArbitrator()
328
	 */
329
	/**
330
	 * <p>
331
	 * Getter for the field <code>selectionArbitrator</code>.
332
	 * </p>
333
	 *
334
	 * @return a {@link eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator}
335
	 *         object.
336
	 */
337
	@Override
338
	public SelectionArbitrator getSelectionArbitrator() {
339
		return selectionArbitrator;
340
	}
341

    
342
	/**
343
	 * Convenient access to current shell
344
	 *
345
	 * @return a {@link org.eclipse.swt.widgets.Shell} object.
346
	 */
347
    public Shell getShell() {
348
		return getLayoutComposite().getShell();
349
	}
350

    
351
	/** {@inheritDoc} */
352
	@Override
353
	public void setIrrelevant(boolean irrelevant) {
354
		String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT
355
				: Resources.COLOR_TEXT_DISABLED_BACKGROUND;
356

    
357
		Color color = StoreUtil.getColor(colorId);
358
		text.setBackground(color);
359
	}
360

    
361
	private class DeleteListener extends SelectionAdapter {
362

    
363
		private final EntitySelectionElement<T> selectionElement;
364

    
365
		public DeleteListener(EntitySelectionElement<T> selectionElement) {
366
			this.selectionElement = selectionElement;
367
		}
368

    
369
		@Override
370
		public void widgetSelected(SelectionEvent e) {
371
			setEntity(null);
372
			firePropertyChangeEvent(new CdmPropertyChangeEvent(
373
					selectionElement, null));
374
		}
375
	}
376

    
377
	private class EditListener extends SelectionAdapter {
378

    
379
		private final EntitySelectionElement<T> selectionElement;
380

    
381
		public EditListener(EntitySelectionElement<T> selectionElement) {
382
			this.selectionElement = selectionElement;
383
		}
384

    
385
		/** {@inheritDoc} */
386
		@Override
387
		public void widgetSelected(SelectionEvent e) {
388
			WizardDialog dialog = new WizardDialog(selectionElement.getShell(),
389
					new EditFromSelectionWizard(selectionElement));
390
			if (dialog.open() == IStatus.OK) {
391
				selectionElement.updateFromWizard();
392
			}
393
		}
394
	}
395

    
396
	// not used
397
	/** {@inheritDoc} */
398
	@Override
399
	public void widgetDefaultSelected(SelectionEvent e) {
400
	}
401

    
402
	/**
403
	 * <p>
404
	 * getConversationHolder
405
	 * </p>
406
	 *
407
	 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
408
	 *         object.
409
	 */
410
	@Override
411
	public ConversationHolder getConversationHolder() {
412
		return conversation;
413
	}
414

    
415
	/** {@inheritDoc} */
416
	@Override
417
	public void setBackground(Color color) {
418
		label.setBackground(color);
419
	}
420

    
421
	/** {@inheritDoc} */
422
	@Override
423
	public void setLabel(String labelString) {
424
		if (label != null) {
425
			label.setText(labelString);
426
		}
427
	}
428

    
429
	/**
430
	 * <p>
431
	 * Getter for the field <code>label</code>.
432
	 * </p>
433
	 *
434
	 * @return a {@link java.lang.String} object.
435
	 */
436
	@Override
437
	public String getLabel() {
438
		if (label != null) {
439
			return label.getText() + " : ";
440
		}
441
		return null;
442
	}
443

    
444
	/** {@inheritDoc} */
445
	@Override
446
	public void update(CdmDataChangeMap changeEvents) {
447
	}
448
}
(2-2/4)