Project

General

Profile

Download (16.8 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2007 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7
 * See LICENSE.TXT at the top of this package for the full license terms.
8
 */
9
package eu.etaxonomy.taxeditor.ui.selection;
10

    
11
import java.util.EnumSet;
12
import java.util.Observable;
13
import java.util.Observer;
14

    
15
import org.eclipse.core.runtime.IStatus;
16
import org.eclipse.jface.wizard.WizardDialog;
17
import org.eclipse.swt.SWT;
18
import org.eclipse.swt.events.SelectionAdapter;
19
import org.eclipse.swt.events.SelectionEvent;
20
import org.eclipse.swt.events.SelectionListener;
21
import org.eclipse.swt.graphics.Color;
22
import org.eclipse.swt.widgets.Button;
23
import org.eclipse.swt.widgets.Composite;
24
import org.eclipse.swt.widgets.Label;
25
import org.eclipse.swt.widgets.Shell;
26
import org.eclipse.swt.widgets.Text;
27
import org.eclipse.ui.forms.widgets.TableWrapData;
28
import org.springframework.security.core.GrantedAuthority;
29

    
30
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
31
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
32
import eu.etaxonomy.cdm.api.service.IService;
33
import eu.etaxonomy.cdm.common.CdmUtils;
34
import eu.etaxonomy.cdm.model.common.CdmBase;
35
import eu.etaxonomy.cdm.model.common.IIdentifiableEntity;
36
import eu.etaxonomy.cdm.model.molecular.Amplification;
37
import eu.etaxonomy.cdm.model.molecular.Primer;
38
import eu.etaxonomy.cdm.model.permission.CRUD;
39
import eu.etaxonomy.cdm.model.permission.Group;
40
import eu.etaxonomy.cdm.model.permission.User;
41
import eu.etaxonomy.cdm.model.reference.Reference;
42
import eu.etaxonomy.taxeditor.model.ImageResources;
43
import eu.etaxonomy.taxeditor.preference.Resources;
44
import eu.etaxonomy.taxeditor.store.CdmStore;
45
import eu.etaxonomy.taxeditor.store.LoginManager;
46
import eu.etaxonomy.taxeditor.store.StoreUtil;
47
import eu.etaxonomy.taxeditor.ui.dialog.selection.SelectionDialogFactory;
48
import eu.etaxonomy.taxeditor.ui.element.AbstractCdmFormElement;
49
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
50
import eu.etaxonomy.taxeditor.ui.element.CdmPropertyChangeEvent;
51
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
52
import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
53
import eu.etaxonomy.taxeditor.ui.element.IEntityElement;
54
import eu.etaxonomy.taxeditor.ui.element.ILabeledElement;
55
import eu.etaxonomy.taxeditor.ui.element.ISelectableElement;
56
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
57
import eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator;
58
import eu.etaxonomy.taxeditor.ui.section.grantedAuthority.GrantedAuthorityLabelTextProvider;
59

    
60
/**
61
 * @author n.hoffmann
62
 * @created Nov 17, 2009
63
 * @param <T>
64
 */
65
public class EntitySelectionElement<T extends CdmBase> extends
66
		AbstractCdmFormElement implements  SelectionListener, IEnableableFormElement, ISelectableElement, IEntityElement<T>, ILabeledElement, //IConversationEnabled,
67
		Observer {
68

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

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

    
86
	protected Label label;
87
	protected Text text;
88
	protected Button button_selection;
89

    
90
	protected Button button_selectionExt;
91

    
92
	private SelectionArbitrator selectionArbitrator;
93

    
94
	protected Button button_edit;
95

    
96
	private final String labelString;
97

    
98
	private Composite selectableComposite;
99

    
100
	private Button button_remove;
101

    
102
	protected final boolean isEditable;
103

    
104
	private final boolean isDeletable;
105
	private final boolean isExternal;
106

    
107
//	private final ConversationHolder conversation;
108
	private Class<T> clazz;
109

    
110
	public EntitySelectionElement(CdmFormFactory formFactory,
111
//          ConversationHolder conversation,
112
            ICdmFormElement parentElement, Class<T> clazz,
113
            String labelString, T entity, int mode, int style, boolean filterElement){
114
	    this(formFactory, parentElement, clazz, labelString, entity, mode, style, filterElement, null);
115
	}
116

    
117

    
118
	public EntitySelectionElement(CdmFormFactory formFactory,
119
//			ConversationHolder conversation,
120
	        ICdmFormElement parentElement, Class<T> clazz,
121
			String labelString, T entity, int mode, int style, boolean filterElement, Integer limit) {
122
		super(formFactory, parentElement);
123

    
124
		this.clazz = clazz;
125
		this.isEditable = (mode & EDITABLE) == EDITABLE;
126
		this.isDeletable = (mode & DELETABLE) == DELETABLE;
127
		this.isExternal= (mode & EXTERNAL) == EXTERNAL;
128
		boolean isSelectable = (mode & SELECTABLE) == SELECTABLE;
129

    
130
		this.labelString = (labelString == null || labelString.equals("")) ? "" : labelString;
131

    
132
//		this.conversation = conversation;
133

    
134
		if (isSelectable && formFactory.getSelectionProvider() != null) {
135
			selectionArbitrator = formFactory.createSelectionArbitrator(this);
136
		}
137

    
138
		createControls(getLayoutComposite(), SWT.NULL, limit);
139
		if (filterElement){
140
		    setFilteredEntity(entity);
141
		}else{
142
		    setEntity(entity);
143
		}
144
	}
145

    
146
    private void setFilteredEntity(T filterEntity) {
147
        this.filteredEntity =filterEntity;
148

    
149
    }
150

    
151
    public EntitySelectionElement(CdmFormFactory formFactory,//ConversationHolder conversation,
152
             ICdmFormElement parentElement, Class<T> clazz,
153
            String labelString, T entity, int mode, int style){
154
        this(formFactory, //conversation,
155
                parentElement, clazz, labelString, entity, mode, style, false);
156
    }
157

    
158
    public EntitySelectionElement(CdmFormFactory formFactory,//ConversationHolder conversation,
159
            ICdmFormElement parentElement, Class<T> clazz,
160
           String labelString, T entity, int mode, int style, Integer limit){
161
       this(formFactory, //conversation,
162
               parentElement, clazz, labelString, entity, mode, style, false, limit);
163
   }
164

    
165
	private void createControls(Composite parent, int style, Integer limit) {
166

    
167
		label = formFactory.createLabel(getLayoutComposite(), labelString,
168
				SWT.NULL);
169

    
170
		addControl(label);
171

    
172
		selectableComposite = formFactory.createComposite(getLayoutComposite());
173

    
174
		int columns = 2;
175
		if (isEditable) {
176
			columns += 1;
177
		}
178
		if (isDeletable) {
179
			columns += 1;
180
		}
181
		if (isExternal) {
182
            columns += 1;
183
        }
184

    
185
		selectableComposite.setLayout(LayoutConstants.LAYOUT(columns, false));
186
		selectableComposite.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
187

    
188
		addControl(selectableComposite);
189

    
190
		text = formFactory.createText(selectableComposite, null, SWT.WRAP);
191
		text.setEditable(false);
192

    
193
		addControl(text);
194

    
195
		text.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
196
		text.setBackground(StoreUtil
197
				.getColor(Resources.COLOR_TEXT_DISABLED_BACKGROUND));
198
		if (limit != null){
199
		    text.setTextLimit(limit);
200
		}
201
		button_selection = formFactory.createButton(selectableComposite, null,
202
				SWT.PUSH);
203
		button_selection.setImage(ImageResources
204
				.getImage(ImageResources.BROWSE_ICON));
205
		button_selection.setToolTipText("Browse existing");
206

    
207
		addControl(button_selection);
208
		button_selection.addSelectionListener(this);
209
		if (isExternal){
210
    		button_selectionExt = formFactory.createButton(selectableComposite, null,
211
                    SWT.PUSH);
212
    		button_selectionExt.setImage(ImageResources
213
                    .getImage(ImageResources.BROWSE_ICON));
214
    		button_selectionExt.setToolTipText("Browse existing from external cdm store");
215
    		button_selectionExt.setText("Ext");
216
            addControl(button_selectionExt);
217
            button_selectionExt.addSelectionListener(this);
218
		}
219
		if (isEditable) {
220
			button_edit = formFactory.createButton(selectableComposite, null,
221
					SWT.PUSH);
222
			button_edit.setImage(ImageResources
223
					.getImage(ImageResources.EDIT_ICON));
224
			button_edit.setToolTipText("Edit");
225
			addControl(button_edit);
226
			button_edit.addSelectionListener(new EditListener(this));
227
		}
228

    
229
		if (isDeletable) {
230
			button_remove = formFactory.createButton(selectableComposite, null,
231
					SWT.PUSH);
232
			button_remove.setImage(ImageResources
233
					.getImage(ImageResources.TRASH_ICON));
234
			button_remove.setToolTipText("Remove");
235
			addControl(button_remove);
236
			button_remove.addSelectionListener(new DeleteListener(this));
237
		}
238
	}
239

    
240
	@Override
241
    public void widgetSelected(SelectionEvent e) {
242
	    if (e.getSource().equals(button_selection) ){
243
	        T selection = SelectionDialogFactory.getSelectionFromDialog(clazz, getShell(), //getConversationHolder(),
244
	                getEntity(), getParentElement());
245
            setSelectionInternal(selection);
246
            if(!getLayoutComposite().isDisposed()){
247
                StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
248
            }
249
	    }else{
250
	        Reference selection = SelectionDialogFactory.getSelectionFromExtDialog(Reference.class, getShell(),//null,
251
	                getParentElement());
252
            setSelectionInternal((T)selection);
253
	    }
254

    
255
	}
256

    
257
	/**
258
     * @return
259
     */
260
    public T getFilteredEntity() {
261

    
262
        return this.filteredEntity;
263
    }
264

    
265
    /**
266
	 * Return the selected object
267
	 *
268
	 * @return a T object.
269
	 */
270
	public T getSelection() {
271
		return entity;
272
	}
273

    
274
	/*
275
	 * (non-Javadoc)
276
	 *
277
	 * @see
278
	 * eu.etaxonomy.taxeditor.forms.IEnableableFormElement#setEnabled(boolean)
279
	 */
280
	/** {@inheritDoc} */
281
	@Override
282
	public void setEnabled(boolean enabled) {
283

    
284
		button_selection.setEnabled(enabled);
285
		if (isDeletable){
286
		    button_remove.setEnabled(enabled);
287
		}
288
		if (isEditable) {
289
			updateButtonStates();
290
		}
291

    
292
	}
293

    
294
	/* (non-Javadoc)
295
	 * @see eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement#isEnabled()
296
	 */
297
	@Override
298
	public boolean isEnabled() {
299
	    return button_selection.isEnabled();
300
	}
301

    
302
	/**
303
	 * <p>
304
	 * setSelectionInternal
305
	 * </p>
306
	 *
307
	 * @param selection
308
	 *            a T object.
309
	 */
310
	protected void setSelectionInternal(T selection) {
311
		if (selection != null && !selection.equals(this.entity)) {
312
			setEntity(selection);
313
			firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
314
		}
315
	}
316

    
317
	/**
318
	 * <p>
319
	 * Setter for the field <code>entity</code>.
320
	 * </p>
321
	 *
322
	 * @param selection
323
	 *            a T object.
324
	 */
325
	public void setEntity(T selection) {
326
		this.entity = selection;
327
		updateElement();
328
	}
329

    
330
	/**
331
	 * Updates this elements view
332
	 */
333
	protected void updateElement() {
334
		String title = CdmUtils.Nz(getTitle());
335

    
336
		text.setText(title); // title can be null
337
		if (isEditable) {
338
			updateButtonStates();
339
		}
340
	}
341

    
342
    public void updateFromWizard() {
343
		updateElement();
344
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
345
	}
346

    
347
	/**
348
	 * <p>
349
	 * getTitle
350
	 * </p>
351
	 *
352
	 * @return a {@link java.lang.String} object.
353
	 */
354
	protected String getTitle() {
355
		if (entity != null){
356
			if(entity instanceof Group){
357
				return ((Group) entity).getName();
358
			} else if(entity instanceof GrantedAuthority){
359
				return GrantedAuthorityLabelTextProvider.getText(((GrantedAuthority) entity));
360
			} else if(entity instanceof User){
361
                return ((User) entity).getUsername();
362
            } else if (entity instanceof Primer){
363
                return ((Primer) entity).getLabel();
364
            } else if (entity instanceof Amplification){
365
                return ((Amplification) entity).getLabelCache();
366
            }
367
            else if(entity instanceof IIdentifiableEntity) {
368
                return ((IIdentifiableEntity) entity).getTitleCache();
369
            }
370

    
371
		}
372
		return "";
373
	}
374

    
375
	/** {@inheritDoc} */
376
	@Override
377
	public void setSelected(boolean selected) {
378
		setBackground(selected ? SELECTED : getPersistentBackground());
379
	}
380

    
381
	/*
382
	 * (non-Javadoc)
383
	 *
384
	 * @see eu.etaxonomy.taxeditor.forms.IEntityElement#getEntity()
385
	 */
386
	/**
387
	 * <p>
388
	 * Getter for the field <code>entity</code>.
389
	 * </p>
390
	 *
391
	 * @return a T object.
392
	 */
393
	@Override
394
	public T getEntity() {
395
		return entity;
396
	}
397

    
398
	/*
399
	 * (non-Javadoc)
400
	 *
401
	 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#
402
	 * getSelectionArbitrator()
403
	 */
404
	/**
405
	 * <p>
406
	 * Getter for the field <code>selectionArbitrator</code>.
407
	 * </p>
408
	 *
409
	 * @return a {@link eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator}
410
	 *         object.
411
	 */
412
	@Override
413
	public SelectionArbitrator getSelectionArbitrator() {
414
		return selectionArbitrator;
415
	}
416

    
417
	/**
418
	 * Convenient access to current shell
419
	 *
420
	 * @return a {@link org.eclipse.swt.widgets.Shell} object.
421
	 */
422
    public Shell getShell() {
423
		return getLayoutComposite().getShell();
424
	}
425

    
426
	/** {@inheritDoc} */
427
	@Override
428
	public void setIrrelevant(boolean irrelevant) {
429
		String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT
430
				: Resources.COLOR_TEXT_DISABLED_BACKGROUND;
431

    
432
		Color color = StoreUtil.getColor(colorId);
433
		text.setBackground(color);
434
	}
435

    
436
	private class DeleteListener extends SelectionAdapter {
437

    
438
		private final EntitySelectionElement<T> selectionElement;
439

    
440
		public DeleteListener(EntitySelectionElement<T> selectionElement) {
441
			this.selectionElement = selectionElement;
442
		}
443

    
444
		@Override
445
		public void widgetSelected(SelectionEvent e) {
446
			setEntity(null);
447
			firePropertyChangeEvent(new CdmPropertyChangeEvent(
448
					selectionElement, null));
449
		}
450
	}
451

    
452
	private class EditListener extends SelectionAdapter {
453

    
454
        private final EntitySelectionElement<T> selectionElement;
455

    
456
		public EditListener(EntitySelectionElement<T> selectionElement) {
457
			this.selectionElement = selectionElement;
458
		}
459

    
460
		@Override
461
		public void widgetSelected(SelectionEvent e) {
462
			WizardDialog dialog = new WizardDialog(selectionElement.getShell(),
463
					new EditFromSelectionWizard(selectionElement));
464
			if (dialog.open() == IStatus.OK) {
465
				selectionElement.updateFromWizard();
466
				//if the edited entity has already been persisted
467
				//but the transient entity is still set in this
468
				//EntitySelectionElement, re-load it and set it
469
				if(!entity.isPersited()){
470
				    IService<T> service = CdmStore.getService(entity);
471
					T loadedEntity = service.load(entity.getUuid());
472
					if(loadedEntity != null){
473
						setEntity(loadedEntity);
474
					}
475
				}
476
			}
477
		}
478
	}
479

    
480
	// not used
481
	@Override
482
	public void widgetDefaultSelected(SelectionEvent e) {
483
	}
484

    
485
	/**
486
	 * <p>
487
	 * getConversationHolder
488
	 * </p>
489
	 *
490
	 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
491
	 *         object.
492
	 */
493
//	@Override
494
	public ConversationHolder getConversationHolder() {
495
	    if(getParentElement() instanceof IConversationEnabled) {
496
            return ((IConversationEnabled)getParentElement()).getConversationHolder();
497
        }
498
	    return null;
499
	}
500

    
501
	/** {@inheritDoc} */
502
	@Override
503
	public void setBackground(Color color) {
504

    
505
	    if(label != null && !label.isDisposed()){
506
	        label.setBackground(color);
507
	    }
508
	}
509

    
510
	/** {@inheritDoc} */
511
	@Override
512
	public void setLabel(String labelString) {
513
		if (label != null) {
514
			label.setText(labelString);
515
		}
516
	}
517

    
518
	/**
519
	 * <p>
520
	 * Getter for the field <code>label</code>.
521
	 * </p>
522
	 *
523
	 * @return a {@link java.lang.String} object.
524
	 */
525
	@Override
526
	public String getLabel() {
527
		if (label != null) {
528
			return label.getText() + " : ";
529
		}
530
		return null;
531
	}
532

    
533
	/** {@inheritDoc} */
534
//	@Override
535
//	public void update(CdmDataChangeMap changeEvents) {
536
//	}
537

    
538
	/* (non-Javadoc)
539
	 * @see eu.etaxonomy.taxeditor.ui.element.AbstractCdmFormElement#removeElements()
540
	 */
541
	@Override
542
    public void removeElements(){
543
		super.removeElements();
544
		LoginManager loginManager = CdmStore.getLoginManager();
545
		loginManager.addObserver(this);
546
	}
547

    
548
	@Override
549
	public void update(Observable o, Object arg) {
550
		if(o instanceof LoginManager){
551
			updateButtonStates();
552
		}
553
	}
554

    
555
	protected void updateButtonStates() {
556
	    if(button_edit != null && !button_selection.isDisposed()){
557
	        button_edit.setEnabled(isEditable && button_selection.isEnabled() && getEntity() != null  && CdmStore.currentAuthentiationHasPermission(getEntity(), UPDATE));
558
	    }
559
	}
560

    
561
	public void setIndent(int indent){
562
        TableWrapData tableWrapData = (TableWrapData)label.getLayoutData();
563

    
564
        if (tableWrapData == null){
565
            tableWrapData = new TableWrapData();
566

    
567
        }
568
        tableWrapData.indent = indent;
569
        label.setLayoutData(tableWrapData);
570

    
571
        getLayoutComposite().layout();
572

    
573
    }
574
}
(4-4/8)