Project

General

Profile

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

    
6
import java.util.EnumSet;
7
import java.util.Observable;
8
import java.util.Observer;
9

    
10
import org.eclipse.core.runtime.IStatus;
11
import org.eclipse.jface.wizard.WizardDialog;
12
import org.eclipse.swt.SWT;
13
import org.eclipse.swt.events.SelectionAdapter;
14
import org.eclipse.swt.events.SelectionEvent;
15
import org.eclipse.swt.events.SelectionListener;
16
import org.eclipse.swt.graphics.Color;
17
import org.eclipse.swt.widgets.Button;
18
import org.eclipse.swt.widgets.Composite;
19
import org.eclipse.swt.widgets.Label;
20
import org.eclipse.swt.widgets.Shell;
21
import org.eclipse.swt.widgets.Text;
22
import org.springframework.security.core.GrantedAuthority;
23

    
24
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
25
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
26
import eu.etaxonomy.cdm.common.CdmUtils;
27
import eu.etaxonomy.cdm.model.common.CdmBase;
28
import eu.etaxonomy.cdm.model.common.Group;
29
import eu.etaxonomy.cdm.model.common.ICdmBase;
30
import eu.etaxonomy.cdm.model.common.IIdentifiableEntity;
31
import eu.etaxonomy.cdm.model.common.User;
32
import eu.etaxonomy.cdm.model.molecular.Amplification;
33
import eu.etaxonomy.cdm.model.molecular.Primer;
34
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
35
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
36
import eu.etaxonomy.taxeditor.model.ImageResources;
37
import eu.etaxonomy.taxeditor.preference.Resources;
38
import eu.etaxonomy.taxeditor.store.CdmStore;
39
import eu.etaxonomy.taxeditor.store.LoginManager;
40
import eu.etaxonomy.taxeditor.store.StoreUtil;
41
import eu.etaxonomy.taxeditor.ui.dialog.selection.SelectionDialogFactory;
42
import eu.etaxonomy.taxeditor.ui.element.AbstractCdmFormElement;
43
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
44
import eu.etaxonomy.taxeditor.ui.element.CdmPropertyChangeEvent;
45
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
46
import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
47
import eu.etaxonomy.taxeditor.ui.element.IEntityElement;
48
import eu.etaxonomy.taxeditor.ui.element.ILabeledElement;
49
import eu.etaxonomy.taxeditor.ui.element.ISelectableElement;
50
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
51
import eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator;
52
import eu.etaxonomy.taxeditor.ui.section.grantedAuthority.GrantedAuthorityLabelTextProvider;
53

    
54
/**
55
 * <p>
56
 * Abstract AbstractSelectionElement class.
57
 * </p>
58
 *
59
 * @author n.hoffmann
60
 * @created Nov 17, 2009
61
 * @version 1.0
62
 * @param <T>
63
 */
64
public class EntitySelectionElement<T extends ICdmBase> extends
65
		AbstractCdmFormElement implements  SelectionListener, IEnableableFormElement, ISelectableElement, IEntityElement<T>, ILabeledElement, IConversationEnabled, Observer {
66

    
67
	private static final EnumSet<CRUD> UPDATE = EnumSet.of(CRUD.UPDATE);
68
	private static final EnumSet<CRUD> DELETE = EnumSet.of(CRUD.DELETE);
69
	private static final EnumSet<CRUD> CREATE = EnumSet.of(CRUD.CREATE);
70

    
71
	/**
72
	 * Bitmask for configuring functionality of selection element
73
	 */
74
	public static final int NOTHING = 0; // 000
75
	public static final int EDITABLE = 1 << 0; // 001
76
	public static final int DELETABLE = 1 << 1; // 010
77
	public static final int SELECTABLE = 1 << 2; // 100
78
	public static final int ALL = EDITABLE | DELETABLE | SELECTABLE; // 111
79

    
80
	protected T entity;
81

    
82
	protected Label label;
83
	protected Text text;
84
	protected Button button_selection;
85

    
86
	private SelectionArbitrator selectionArbitrator;
87

    
88
	protected Button button_edit;
89

    
90
	private final String labelString;
91

    
92
	private Composite selectableComposite;
93

    
94
	private Button button_remove;
95

    
96
	private final boolean isEditable;
97

    
98
	private final boolean isDeletable;
99

    
100
	private final ConversationHolder conversation;
101
	private Class<T> clazz;
102

    
103
	/**
104
	 * <p>
105
	 * Constructor for AbstractSelectionElement.
106
	 * </p>
107
	 *
108
	 * @param formFactory
109
	 *            a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
110
	 *            object.
111
	 * @param conversation
112
	 *            TODO
113
	 * @param parentElement
114
	 *            a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
115
	 *            object.
116
	 * @param labelString
117
	 *            a {@link java.lang.String} object.
118
	 * @param entity
119
	 *            a T object.
120
	 * @param isEditable
121
	 *            a boolean.
122
	 * @param isSelectable
123
	 *            a boolean.
124
	 * @param isDeletable
125
	 *            a boolean.
126
	 * @param style
127
	 *            a int.
128
	 * @param <T>
129
	 *            a T object.
130
	 */
131
	public EntitySelectionElement(CdmFormFactory formFactory,
132
			ConversationHolder conversation, ICdmFormElement parentElement,
133
			String labelString, T entity, int mode, int style) {
134
		super(formFactory, parentElement);
135

    
136
		this.isEditable = (mode & EDITABLE) == EDITABLE;
137
		this.isDeletable = (mode & DELETABLE) == DELETABLE;
138
		boolean isSelectable = (mode & SELECTABLE) == SELECTABLE;
139

    
140
		this.labelString = (labelString == null || labelString.equals("")) ? "" : labelString + " : ";
141

    
142
		this.conversation = conversation;
143

    
144
		if (isSelectable && formFactory.getSelectionProvider() != null) {
145
			selectionArbitrator = formFactory.createSelectionArbitrator(this);
146
		}
147

    
148
		createControls(getLayoutComposite(), SWT.NULL);
149

    
150
		setEntity(entity);
151
	}
152

    
153
	public EntitySelectionElement(CdmFormFactory formFactory,
154
			ConversationHolder conversation, ICdmFormElement parentElement, Class<T> clazz,
155
			String labelString, T entity, int mode, int style) {
156
		this(formFactory, conversation, parentElement, labelString, entity, mode, style);
157
		this.clazz = clazz;
158
	}
159

    
160
	private void createControls(Composite parent, int style) {
161
		
162
		label = formFactory.createLabel(getLayoutComposite(), labelString,
163
				SWT.NULL);
164

    
165
		addControl(label);
166

    
167
		selectableComposite = formFactory.createComposite(getLayoutComposite());
168

    
169
		int columns = 2;
170
		if (isEditable) {
171
			columns += 1;
172
		}
173
		if (isDeletable) {
174
			columns += 1;
175
		}
176

    
177
		selectableComposite.setLayout(LayoutConstants.LAYOUT(columns, false));
178
		selectableComposite.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
179

    
180
		addControl(selectableComposite);
181

    
182
		text = formFactory.createText(selectableComposite, null, SWT.WRAP);
183
		text.setEditable(false);
184
		addControl(text);
185

    
186
		text.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
187
		text.setBackground(StoreUtil
188
				.getColor(Resources.COLOR_TEXT_DISABLED_BACKGROUND));
189

    
190
		button_selection = formFactory.createButton(selectableComposite, null,
191
				SWT.PUSH);
192
		button_selection.setImage(ImageResources
193
				.getImage(ImageResources.BROWSE_ICON));
194
		button_selection.setToolTipText("Browse existing");
195

    
196
		addControl(button_selection);
197
		button_selection.addSelectionListener(this);
198

    
199
		if (isEditable) {
200
			button_edit = formFactory.createButton(selectableComposite, null,
201
					SWT.PUSH);
202
			button_edit.setImage(ImageResources
203
					.getImage(ImageResources.EDIT_ICON));
204
			button_edit.setToolTipText("Edit");
205
			addControl(button_edit);
206
			button_edit.addSelectionListener(new EditListener(this));
207
		}
208

    
209
		if (isDeletable) {
210
			button_remove = formFactory.createButton(selectableComposite, null,
211
					SWT.PUSH);
212
			button_remove.setImage(ImageResources
213
					.getImage(ImageResources.TRASH_ICON));
214
			button_remove.setToolTipText("Remove");
215
			addControl(button_remove);
216
			button_remove.addSelectionListener(new DeleteListener(this));
217
		}
218
	}
219

    
220
	@Override
221
    public void widgetSelected(SelectionEvent e) {
222
		T selection = SelectionDialogFactory.getSelectionFromDialog(clazz, getShell(), getConversationHolder(), getEntity());
223
		setSelectionInternal(selection);
224
	}
225

    
226
	/**
227
	 * Return the selected object
228
	 *
229
	 * @return a T object.
230
	 */
231
	public T getSelection() {
232
		return entity;
233
	}
234

    
235
	/*
236
	 * (non-Javadoc)
237
	 *
238
	 * @see
239
	 * eu.etaxonomy.taxeditor.forms.IEnableableFormElement#setEnabled(boolean)
240
	 */
241
	/** {@inheritDoc} */
242
	@Override
243
	public void setEnabled(boolean enabled) {
244
		button_selection.setEnabled(enabled);
245
		if (isEditable) {
246
			updateButtonStates();
247
		}
248
	}
249

    
250
	/* (non-Javadoc)
251
	 * @see eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement#isEnabled()
252
	 */
253
	@Override
254
	public boolean isEnabled() {
255
	    return button_selection.isEnabled();
256
	}
257

    
258
	/**
259
	 * <p>
260
	 * setSelectionInternal
261
	 * </p>
262
	 *
263
	 * @param selection
264
	 *            a T object.
265
	 */
266
	protected void setSelectionInternal(T selection) {
267
		if (selection != null && !selection.equals(this.entity)) {
268
			setEntity(selection);
269
			firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
270
		}
271
	}
272

    
273
	/**
274
	 * <p>
275
	 * Setter for the field <code>entity</code>.
276
	 * </p>
277
	 *
278
	 * @param selection
279
	 *            a T object.
280
	 */
281
	public void setEntity(T selection) {
282
		this.entity = selection;
283
		updateElement();
284
	}
285

    
286
	/**
287
	 * Updates this elements view
288
	 */
289
	protected void updateElement() {
290
		String title = CdmUtils.Nz(getTitle());
291
		text.setText(title); // title can be null
292
		if (isEditable) {
293
			updateButtonStates();
294
		}
295
	}
296

    
297
    public void updateFromWizard() {
298
		updateElement();
299
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
300
	}
301

    
302
	/**
303
	 * <p>
304
	 * getTitle
305
	 * </p>
306
	 *
307
	 * @return a {@link java.lang.String} object.
308
	 */
309
	protected String getTitle() {
310
		if (entity != null){
311
			if(entity instanceof IIdentifiableEntity) {
312
				return ((IIdentifiableEntity) entity).getTitleCache();
313
			} else if(entity instanceof Group){
314
				return ((Group) entity).getName();
315
			} else if(entity instanceof GrantedAuthority){
316
				return GrantedAuthorityLabelTextProvider.getText(((GrantedAuthority) entity));
317
			} else if(entity instanceof User){
318
                return ((User) entity).getUsername();
319
            } else if (entity instanceof Primer){
320
                return ((Primer) entity).getLabel();
321
            } else if (entity instanceof Amplification){
322
                return ((Amplification) entity).getDescription();
323
            }
324

    
325
		}
326
		return "";
327
	}
328

    
329
	/** {@inheritDoc} */
330
	@Override
331
	public void setSelected(boolean selected) {
332
		setBackground(selected ? SELECTED : getPersistentBackground());
333
	}
334

    
335
	/*
336
	 * (non-Javadoc)
337
	 *
338
	 * @see eu.etaxonomy.taxeditor.forms.IEntityElement#getEntity()
339
	 */
340
	/**
341
	 * <p>
342
	 * Getter for the field <code>entity</code>.
343
	 * </p>
344
	 *
345
	 * @return a T object.
346
	 */
347
	@Override
348
	public T getEntity() {
349
		return entity;
350
	}
351

    
352
	/*
353
	 * (non-Javadoc)
354
	 *
355
	 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#
356
	 * getSelectionArbitrator()
357
	 */
358
	/**
359
	 * <p>
360
	 * Getter for the field <code>selectionArbitrator</code>.
361
	 * </p>
362
	 *
363
	 * @return a {@link eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator}
364
	 *         object.
365
	 */
366
	@Override
367
	public SelectionArbitrator getSelectionArbitrator() {
368
		return selectionArbitrator;
369
	}
370

    
371
	/**
372
	 * Convenient access to current shell
373
	 *
374
	 * @return a {@link org.eclipse.swt.widgets.Shell} object.
375
	 */
376
    public Shell getShell() {
377
		return getLayoutComposite().getShell();
378
	}
379

    
380
	/** {@inheritDoc} */
381
	@Override
382
	public void setIrrelevant(boolean irrelevant) {
383
		String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT
384
				: Resources.COLOR_TEXT_DISABLED_BACKGROUND;
385

    
386
		Color color = StoreUtil.getColor(colorId);
387
		text.setBackground(color);
388
	}
389

    
390
	private class DeleteListener extends SelectionAdapter {
391

    
392
		private final EntitySelectionElement<T> selectionElement;
393

    
394
		public DeleteListener(EntitySelectionElement<T> selectionElement) {
395
			this.selectionElement = selectionElement;
396
		}
397

    
398
		@Override
399
		public void widgetSelected(SelectionEvent e) {
400
			setEntity(null);
401
			firePropertyChangeEvent(new CdmPropertyChangeEvent(
402
					selectionElement, null));
403
		}
404
	}
405

    
406
	private class EditListener extends SelectionAdapter {
407

    
408
		private final EntitySelectionElement<T> selectionElement;
409

    
410
		public EditListener(EntitySelectionElement<T> selectionElement) {
411
			this.selectionElement = selectionElement;
412
		}
413

    
414
		/** {@inheritDoc} */
415
		@Override
416
		public void widgetSelected(SelectionEvent e) {
417
			WizardDialog dialog = new WizardDialog(selectionElement.getShell(),
418
					new EditFromSelectionWizard(selectionElement));
419
			if (dialog.open() == IStatus.OK) {
420
				selectionElement.updateFromWizard();
421
			}
422
		}
423
	}
424

    
425
	// not used
426
	/** {@inheritDoc} */
427
	@Override
428
	public void widgetDefaultSelected(SelectionEvent e) {
429
	}
430

    
431
	/**
432
	 * <p>
433
	 * getConversationHolder
434
	 * </p>
435
	 *
436
	 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
437
	 *         object.
438
	 */
439
	@Override
440
	public ConversationHolder getConversationHolder() {
441
		return conversation;
442
	}
443

    
444
	/** {@inheritDoc} */
445
	@Override
446
	public void setBackground(Color color) {
447
	    if(!label.isDisposed()){
448
	        label.setBackground(color);
449
	    }
450
	}
451

    
452
	/** {@inheritDoc} */
453
	@Override
454
	public void setLabel(String labelString) {
455
		if (label != null) {
456
			label.setText(labelString);
457
		}
458
	}
459

    
460
	/**
461
	 * <p>
462
	 * Getter for the field <code>label</code>.
463
	 * </p>
464
	 *
465
	 * @return a {@link java.lang.String} object.
466
	 */
467
	@Override
468
	public String getLabel() {
469
		if (label != null) {
470
			return label.getText() + " : ";
471
		}
472
		return null;
473
	}
474

    
475
	/** {@inheritDoc} */
476
	@Override
477
	public void update(CdmDataChangeMap changeEvents) {
478
	}
479

    
480
	/* (non-Javadoc)
481
	 * @see eu.etaxonomy.taxeditor.ui.element.AbstractCdmFormElement#removeElements()
482
	 */
483
	@Override
484
    public void removeElements(){
485
		super.removeElements();
486
		LoginManager loginManager = CdmStore.getLoginManager();
487
		loginManager.addObserver(this);
488
	}
489

    
490
	@Override
491
	public void update(Observable o, Object arg) {
492
		if(o instanceof LoginManager){
493
			updateButtonStates();
494
		}
495
	}
496

    
497
	private void updateButtonStates() {
498
	    if(button_edit != null && !button_selection.isDisposed()){
499
	        button_edit.setEnabled(isEditable && button_selection.isEnabled() && getEntity() != null  && CdmStore.currentAuthentiationHasPermission((CdmBase) getEntity(), UPDATE));
500
	    }
501
	}
502
}
(2-2/4)