Project

General

Profile

Download (14.7 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
	                getFilteredEntity(), getParentElement());
222
            setSelectionInternal(selection);
223
	    }else{
224
	        Reference selection = SelectionDialogFactory.getSelectionFromExtDialog(Reference.class, getShell(),//null,
225
	                getParentElement());
226
            setSelectionInternal((T)selection);
227
	    }
228

    
229
	}
230

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

    
236
        return this.filteredEntity;
237
    }
238

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

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

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

    
266
	}
267

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

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

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

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

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

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

    
344
		}
345
		return "";
346
	}
347

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

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

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

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

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

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

    
409
	private class DeleteListener extends SelectionAdapter {
410

    
411
		private final EntitySelectionElement<T> selectionElement;
412

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

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

    
425
	private class EditListener extends SelectionAdapter {
426

    
427
        private final EntitySelectionElement<T> selectionElement;
428

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

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

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

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

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

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

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

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

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

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

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