Project

General

Profile

Download (13.4 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(), getParentElement());
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

    
245
		button_selection.setEnabled(enabled);
246
		if (isDeletable){
247
		    button_remove.setEnabled(enabled);
248
		}
249
		if (isEditable) {
250
			updateButtonStates();
251
		}
252

    
253
	}
254

    
255
	/* (non-Javadoc)
256
	 * @see eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement#isEnabled()
257
	 */
258
	@Override
259
	public boolean isEnabled() {
260
	    return button_selection.isEnabled();
261
	}
262

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

    
278
	/**
279
	 * <p>
280
	 * Setter for the field <code>entity</code>.
281
	 * </p>
282
	 *
283
	 * @param selection
284
	 *            a T object.
285
	 */
286
	public void setEntity(T selection) {
287
		this.entity = selection;
288
		updateElement();
289
	}
290

    
291
	/**
292
	 * Updates this elements view
293
	 */
294
	protected void updateElement() {
295
		String title = CdmUtils.Nz(getTitle());
296
		text.setText(title); // title can be null
297
		if (isEditable) {
298
			updateButtonStates();
299
		}
300
	}
301

    
302
    public void updateFromWizard() {
303
		updateElement();
304
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
305
	}
306

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

    
330
		}
331
		return "";
332
	}
333

    
334
	/** {@inheritDoc} */
335
	@Override
336
	public void setSelected(boolean selected) {
337
		setBackground(selected ? SELECTED : getPersistentBackground());
338
	}
339

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

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

    
376
	/**
377
	 * Convenient access to current shell
378
	 *
379
	 * @return a {@link org.eclipse.swt.widgets.Shell} object.
380
	 */
381
    public Shell getShell() {
382
		return getLayoutComposite().getShell();
383
	}
384

    
385
	/** {@inheritDoc} */
386
	@Override
387
	public void setIrrelevant(boolean irrelevant) {
388
		String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT
389
				: Resources.COLOR_TEXT_DISABLED_BACKGROUND;
390

    
391
		Color color = StoreUtil.getColor(colorId);
392
		text.setBackground(color);
393
	}
394

    
395
	private class DeleteListener extends SelectionAdapter {
396

    
397
		private final EntitySelectionElement<T> selectionElement;
398

    
399
		public DeleteListener(EntitySelectionElement<T> selectionElement) {
400
			this.selectionElement = selectionElement;
401
		}
402

    
403
		@Override
404
		public void widgetSelected(SelectionEvent e) {
405
			setEntity(null);
406
			firePropertyChangeEvent(new CdmPropertyChangeEvent(
407
					selectionElement, null));
408
		}
409
	}
410

    
411
	private class EditListener extends SelectionAdapter {
412

    
413
        private final EntitySelectionElement<T> selectionElement;
414

    
415
		public EditListener(EntitySelectionElement<T> selectionElement) {
416
			this.selectionElement = selectionElement;
417
		}
418

    
419
		/** {@inheritDoc} */
420
		@Override
421
		public void widgetSelected(SelectionEvent e) {
422
			WizardDialog dialog = new WizardDialog(selectionElement.getShell(),
423
					new EditFromSelectionWizard(selectionElement));
424
			if (dialog.open() == IStatus.OK) {
425
				selectionElement.updateFromWizard();
426
			}
427
		}
428
	}
429

    
430
	// not used
431
	/** {@inheritDoc} */
432
	@Override
433
	public void widgetDefaultSelected(SelectionEvent e) {
434
	}
435

    
436
	/**
437
	 * <p>
438
	 * getConversationHolder
439
	 * </p>
440
	 *
441
	 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
442
	 *         object.
443
	 */
444
	@Override
445
	public ConversationHolder getConversationHolder() {
446
		return conversation;
447
	}
448

    
449
	/** {@inheritDoc} */
450
	@Override
451
	public void setBackground(Color color) {
452
	    if(!label.isDisposed()){
453
	        label.setBackground(color);
454
	    }
455
	}
456

    
457
	/** {@inheritDoc} */
458
	@Override
459
	public void setLabel(String labelString) {
460
		if (label != null) {
461
			label.setText(labelString);
462
		}
463
	}
464

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

    
480
	/** {@inheritDoc} */
481
	@Override
482
	public void update(CdmDataChangeMap changeEvents) {
483
	}
484

    
485
	/* (non-Javadoc)
486
	 * @see eu.etaxonomy.taxeditor.ui.element.AbstractCdmFormElement#removeElements()
487
	 */
488
	@Override
489
    public void removeElements(){
490
		super.removeElements();
491
		LoginManager loginManager = CdmStore.getLoginManager();
492
		loginManager.addObserver(this);
493
	}
494

    
495
	@Override
496
	public void update(Observable o, Object arg) {
497
		if(o instanceof LoginManager){
498
			updateButtonStates();
499
		}
500
	}
501

    
502
	private void updateButtonStates() {
503
	    if(button_edit != null && !button_selection.isDisposed()){
504
	        button_edit.setEnabled(isEditable && button_selection.isEnabled() && getEntity() != null  && CdmStore.currentAuthentiationHasPermission((CdmBase) getEntity(), UPDATE));
505
	    }
506
	}
507
}
(3-3/6)