Project

General

Profile

Download (14.8 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.api.service.IService;
27
import eu.etaxonomy.cdm.common.CdmUtils;
28
import eu.etaxonomy.cdm.model.common.CdmBase;
29
import eu.etaxonomy.cdm.model.common.Group;
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.model.reference.Reference;
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
 * @author n.hoffmann
56
 * @created Nov 17, 2009
57
 * @version 1.0
58
 * @param <T>
59
 */
60
public class EntitySelectionElement<T extends CdmBase> extends
61
		AbstractCdmFormElement implements  SelectionListener, IEnableableFormElement, ISelectableElement, IEntityElement<T>, ILabeledElement, //IConversationEnabled,
62
		Observer {
63

    
64
	private static final EnumSet<CRUD> UPDATE = EnumSet.of(CRUD.UPDATE);
65
	private static final EnumSet<CRUD> DELETE = EnumSet.of(CRUD.DELETE);
66
	private static final EnumSet<CRUD> CREATE = EnumSet.of(CRUD.CREATE);
67

    
68
	/**
69
	 * Bitmask for configuring functionality of selection element
70
	 */
71
	public static final int NOTHING = 0; // 0000
72
	public static final int EDITABLE = 1 << 0; // 0001
73
	public static final int DELETABLE = 1 << 1; // 0010
74
	public static final int SELECTABLE = 1 << 2; // 0100
75
	public static final int EXTERNAL = 1 << 3; // 1000
76
	public static final int ALL = EDITABLE | DELETABLE | SELECTABLE ; // 0111
77
	public static final int ALL_WITH_EXT = EDITABLE | DELETABLE | SELECTABLE | EXTERNAL ; // 1111
78
	protected T entity;
79
	protected T filteredEntity;
80

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

    
85
	protected Button button_selectionExt;
86

    
87
	private SelectionArbitrator selectionArbitrator;
88

    
89
	protected Button button_edit;
90

    
91
	private final String labelString;
92

    
93
	private Composite selectableComposite;
94

    
95
	private Button button_remove;
96

    
97
	private final boolean isEditable;
98

    
99
	private final boolean isDeletable;
100
	private final boolean isExternal;
101

    
102
//	private final ConversationHolder conversation;
103
	private Class<T> clazz;
104

    
105
	public EntitySelectionElement(CdmFormFactory formFactory,
106
//			ConversationHolder conversation,
107
	        ICdmFormElement parentElement, Class<T> clazz,
108
			String labelString, T entity, int mode, int style, boolean filterElement) {
109
		super(formFactory, parentElement);
110

    
111
		this.clazz = clazz;
112
		this.isEditable = (mode & EDITABLE) == EDITABLE;
113
		this.isDeletable = (mode & DELETABLE) == DELETABLE;
114
		this.isExternal= (mode & EXTERNAL) == EXTERNAL;
115
		boolean isSelectable = (mode & SELECTABLE) == SELECTABLE;
116

    
117
		this.labelString = (labelString == null || labelString.equals("")) ? "" : labelString;
118

    
119
//		this.conversation = conversation;
120

    
121
		if (isSelectable && formFactory.getSelectionProvider() != null) {
122
			selectionArbitrator = formFactory.createSelectionArbitrator(this);
123
		}
124

    
125
		createControls(getLayoutComposite(), SWT.NULL);
126
		if (filterElement){
127
		    setFilteredEntity(entity);
128
		}else{
129
		    setEntity(entity);
130
		}
131
	}
132

    
133
    private void setFilteredEntity(T filterEntity) {
134
        this.filteredEntity =filterEntity;
135

    
136
    }
137

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

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

    
147
		label = formFactory.createLabel(getLayoutComposite(), labelString,
148
				SWT.NULL);
149

    
150
		addControl(label);
151

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

    
154
		int columns = 2;
155
		if (isEditable) {
156
			columns += 1;
157
		}
158
		if (isDeletable) {
159
			columns += 1;
160
		}
161
		if (isExternal) {
162
            columns += 1;
163
        }
164

    
165
		selectableComposite.setLayout(LayoutConstants.LAYOUT(columns, false));
166
		selectableComposite.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
167

    
168
		addControl(selectableComposite);
169

    
170
		text = formFactory.createText(selectableComposite, null, SWT.WRAP);
171
		text.setEditable(false);
172
		addControl(text);
173

    
174
		text.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
175
		text.setBackground(StoreUtil
176
				.getColor(Resources.COLOR_TEXT_DISABLED_BACKGROUND));
177

    
178
		button_selection = formFactory.createButton(selectableComposite, null,
179
				SWT.PUSH);
180
		button_selection.setImage(ImageResources
181
				.getImage(ImageResources.BROWSE_ICON));
182
		button_selection.setToolTipText("Browse existing");
183

    
184
		addControl(button_selection);
185
		button_selection.addSelectionListener(this);
186
		if (isExternal){
187
    		button_selectionExt = formFactory.createButton(selectableComposite, null,
188
                    SWT.PUSH);
189
    		button_selectionExt.setImage(ImageResources
190
                    .getImage(ImageResources.BROWSE_ICON));
191
    		button_selectionExt.setToolTipText("Browse existing from external cdm store");
192
    		button_selectionExt.setText("Ext");
193
            addControl(button_selectionExt);
194
            button_selectionExt.addSelectionListener(this);
195
		}
196
		if (isEditable) {
197
			button_edit = formFactory.createButton(selectableComposite, null,
198
					SWT.PUSH);
199
			button_edit.setImage(ImageResources
200
					.getImage(ImageResources.EDIT_ICON));
201
			button_edit.setToolTipText("Edit");
202
			addControl(button_edit);
203
			button_edit.addSelectionListener(new EditListener(this));
204
		}
205

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

    
217
	@Override
218
    public void widgetSelected(SelectionEvent e) {
219
	    if (e.getSource().equals(button_selection) ){
220
	        T selection = SelectionDialogFactory.getSelectionFromDialog(clazz, getShell(), //getConversationHolder(),
221
	                getEntity(), getParentElement());
222
            setSelectionInternal(selection);
223
            StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
224
	    }else{
225
	        Reference selection = SelectionDialogFactory.getSelectionFromExtDialog(Reference.class, getShell(),//null,
226
	                getParentElement());
227
            setSelectionInternal((T)selection);
228
	    }
229

    
230
	}
231

    
232
	/**
233
     * @return
234
     */
235
    public T getFilteredEntity() {
236

    
237
        return this.filteredEntity;
238
    }
239

    
240
    /**
241
	 * Return the selected object
242
	 *
243
	 * @return a T object.
244
	 */
245
	public T getSelection() {
246
		return entity;
247
	}
248

    
249
	/*
250
	 * (non-Javadoc)
251
	 *
252
	 * @see
253
	 * eu.etaxonomy.taxeditor.forms.IEnableableFormElement#setEnabled(boolean)
254
	 */
255
	/** {@inheritDoc} */
256
	@Override
257
	public void setEnabled(boolean enabled) {
258

    
259
		button_selection.setEnabled(enabled);
260
		if (isDeletable){
261
		    button_remove.setEnabled(enabled);
262
		}
263
		if (isEditable) {
264
			updateButtonStates();
265
		}
266

    
267
	}
268

    
269
	/* (non-Javadoc)
270
	 * @see eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement#isEnabled()
271
	 */
272
	@Override
273
	public boolean isEnabled() {
274
	    return button_selection.isEnabled();
275
	}
276

    
277
	/**
278
	 * <p>
279
	 * setSelectionInternal
280
	 * </p>
281
	 *
282
	 * @param selection
283
	 *            a T object.
284
	 */
285
	protected void setSelectionInternal(T selection) {
286
		if (selection != null && !selection.equals(this.entity)) {
287
			setEntity(selection);
288
			firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
289
		}
290
	}
291

    
292
	/**
293
	 * <p>
294
	 * Setter for the field <code>entity</code>.
295
	 * </p>
296
	 *
297
	 * @param selection
298
	 *            a T object.
299
	 */
300
	public void setEntity(T selection) {
301
		this.entity = selection;
302
		updateElement();
303
	}
304

    
305
	/**
306
	 * Updates this elements view
307
	 */
308
	protected void updateElement() {
309
		String title = CdmUtils.Nz(getTitle());
310
		text.setText(title); // title can be null
311
		if (isEditable) {
312
			updateButtonStates();
313
		}
314
	}
315

    
316
    public void updateFromWizard() {
317
		updateElement();
318
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
319
	}
320

    
321
	/**
322
	 * <p>
323
	 * getTitle
324
	 * </p>
325
	 *
326
	 * @return a {@link java.lang.String} object.
327
	 */
328
	protected String getTitle() {
329
		if (entity != null){
330
			if(entity instanceof Group){
331
				return ((Group) entity).getName();
332
			} else if(entity instanceof GrantedAuthority){
333
				return GrantedAuthorityLabelTextProvider.getText(((GrantedAuthority) entity));
334
			} else if(entity instanceof User){
335
                return ((User) entity).getUsername();
336
            } else if (entity instanceof Primer){
337
                return ((Primer) entity).getLabel();
338
            } else if (entity instanceof Amplification){
339
                return ((Amplification) entity).getLabelCache();
340
            }
341
            else if(entity instanceof IIdentifiableEntity) {
342
                return ((IIdentifiableEntity) entity).getTitleCache();
343
            }
344

    
345
		}
346
		return "";
347
	}
348

    
349
	/** {@inheritDoc} */
350
	@Override
351
	public void setSelectedForColorChange(boolean selected) {
352
		setBackground(selected ? SELECTED : getPersistentBackground());
353
	}
354

    
355
	/*
356
	 * (non-Javadoc)
357
	 *
358
	 * @see eu.etaxonomy.taxeditor.forms.IEntityElement#getEntity()
359
	 */
360
	/**
361
	 * <p>
362
	 * Getter for the field <code>entity</code>.
363
	 * </p>
364
	 *
365
	 * @return a T object.
366
	 */
367
	@Override
368
	public T getEntity() {
369
		return entity;
370
	}
371

    
372
	/*
373
	 * (non-Javadoc)
374
	 *
375
	 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#
376
	 * getSelectionArbitrator()
377
	 */
378
	/**
379
	 * <p>
380
	 * Getter for the field <code>selectionArbitrator</code>.
381
	 * </p>
382
	 *
383
	 * @return a {@link eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator}
384
	 *         object.
385
	 */
386
	@Override
387
	public SelectionArbitrator getSelectionArbitrator() {
388
		return selectionArbitrator;
389
	}
390

    
391
	/**
392
	 * Convenient access to current shell
393
	 *
394
	 * @return a {@link org.eclipse.swt.widgets.Shell} object.
395
	 */
396
    public Shell getShell() {
397
		return getLayoutComposite().getShell();
398
	}
399

    
400
	/** {@inheritDoc} */
401
	@Override
402
	public void setIrrelevant(boolean irrelevant) {
403
		String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT
404
				: Resources.COLOR_TEXT_DISABLED_BACKGROUND;
405

    
406
		Color color = StoreUtil.getColor(colorId);
407
		text.setBackground(color);
408
	}
409

    
410
	private class DeleteListener extends SelectionAdapter {
411

    
412
		private final EntitySelectionElement<T> selectionElement;
413

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

    
418
		@Override
419
		public void widgetSelected(SelectionEvent e) {
420
			setEntity(null);
421
			firePropertyChangeEvent(new CdmPropertyChangeEvent(
422
					selectionElement, null));
423
		}
424
	}
425

    
426
	private class EditListener extends SelectionAdapter {
427

    
428
        private final EntitySelectionElement<T> selectionElement;
429

    
430
		public EditListener(EntitySelectionElement<T> selectionElement) {
431
			this.selectionElement = selectionElement;
432
		}
433

    
434
		/** {@inheritDoc} */
435
		@Override
436
		public void widgetSelected(SelectionEvent e) {
437
			WizardDialog dialog = new WizardDialog(selectionElement.getShell(),
438
					new EditFromSelectionWizard(selectionElement));
439
			if (dialog.open() == IStatus.OK) {
440
				selectionElement.updateFromWizard();
441
				//if the edited entity has already been persisted
442
				//but the transient entity is still set in this
443
				//EntitySelectionElement, re-load it and set it
444
				IService<T> service = CdmStore.getService(entity);
445
				if(entity.getId()==0){
446
					T loadedEntity = service.load(entity.getUuid());
447
					if(loadedEntity!=null){
448
						setEntity(loadedEntity);
449
					}
450
				}
451
			}
452
		}
453
	}
454

    
455
	// not used
456
	/** {@inheritDoc} */
457
	@Override
458
	public void widgetDefaultSelected(SelectionEvent e) {
459
	}
460

    
461
	/**
462
	 * <p>
463
	 * getConversationHolder
464
	 * </p>
465
	 *
466
	 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
467
	 *         object.
468
	 */
469
//	@Override
470
	public ConversationHolder getConversationHolder() {
471
	    if(getParentElement() instanceof IConversationEnabled) {
472
            return ((IConversationEnabled)getParentElement()).getConversationHolder();
473
        }
474
	    return null;
475
	}
476

    
477
	/** {@inheritDoc} */
478
	@Override
479
	public void setBackground(Color color) {
480
	    if(!label.isDisposed()){
481
	        label.setBackground(color);
482
	    }
483
	}
484

    
485
	/** {@inheritDoc} */
486
	@Override
487
	public void setLabel(String labelString) {
488
		if (label != null) {
489
			label.setText(labelString);
490
		}
491
	}
492

    
493
	/**
494
	 * <p>
495
	 * Getter for the field <code>label</code>.
496
	 * </p>
497
	 *
498
	 * @return a {@link java.lang.String} object.
499
	 */
500
	@Override
501
	public String getLabel() {
502
		if (label != null) {
503
			return label.getText() + " : ";
504
		}
505
		return null;
506
	}
507

    
508
	/** {@inheritDoc} */
509
//	@Override
510
//	public void update(CdmDataChangeMap changeEvents) {
511
//	}
512

    
513
	/* (non-Javadoc)
514
	 * @see eu.etaxonomy.taxeditor.ui.element.AbstractCdmFormElement#removeElements()
515
	 */
516
	@Override
517
    public void removeElements(){
518
		super.removeElements();
519
		LoginManager loginManager = CdmStore.getLoginManager();
520
		loginManager.addObserver(this);
521
	}
522

    
523
	@Override
524
	public void update(Observable o, Object arg) {
525
		if(o instanceof LoginManager){
526
			updateButtonStates();
527
		}
528
	}
529

    
530
	private void updateButtonStates() {
531
	    if(button_edit != null && !button_selection.isDisposed()){
532
	        button_edit.setEnabled(isEditable && button_selection.isEnabled() && getEntity() != null  && CdmStore.currentAuthentiationHasPermission(getEntity(), UPDATE));
533
	    }
534
	}
535
}
(4-4/7)