Project

General

Profile

Download (14.4 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.service.IService;
31
import eu.etaxonomy.cdm.common.CdmUtils;
32
import eu.etaxonomy.cdm.model.common.CdmBase;
33
import eu.etaxonomy.cdm.model.common.IIdentifiableEntity;
34
import eu.etaxonomy.cdm.model.molecular.Amplification;
35
import eu.etaxonomy.cdm.model.molecular.Primer;
36
import eu.etaxonomy.cdm.model.permission.CRUD;
37
import eu.etaxonomy.cdm.model.permission.Group;
38
import eu.etaxonomy.cdm.model.permission.User;
39
import eu.etaxonomy.cdm.model.reference.Reference;
40
import eu.etaxonomy.taxeditor.model.AbstractUtility;
41
import eu.etaxonomy.taxeditor.model.ImageResources;
42
import eu.etaxonomy.taxeditor.preference.Resources;
43
import eu.etaxonomy.taxeditor.store.CdmStore;
44
import eu.etaxonomy.taxeditor.store.LoginManager;
45
import eu.etaxonomy.taxeditor.store.StoreUtil;
46
import eu.etaxonomy.taxeditor.ui.dialog.selection.SelectionDialogFactory;
47
import eu.etaxonomy.taxeditor.ui.element.AbstractRelevanceFormElement;
48
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
49
import eu.etaxonomy.taxeditor.ui.element.CdmPropertyChangeEvent;
50
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
51
import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
52
import eu.etaxonomy.taxeditor.ui.element.IEntityElement;
53
import eu.etaxonomy.taxeditor.ui.element.ILabeledElement;
54
import eu.etaxonomy.taxeditor.ui.element.ISelectableElement;
55
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
56
import eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator;
57
import eu.etaxonomy.taxeditor.ui.section.grantedAuthority.GrantedAuthorityLabelTextProvider;
58

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

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

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

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

    
89
	protected Button button_selectionExt;
90

    
91
	private SelectionArbitrator selectionArbitrator;
92

    
93
	protected Button button_edit;
94

    
95
	private final String labelString;
96

    
97
	private Composite selectableComposite;
98

    
99
	private Button button_remove;
100

    
101
	protected final boolean isEditable;
102

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

    
106
	private Class<T> clazz;
107

    
108
	public EntitySelectionElement(CdmFormFactory formFactory,
109
            ICdmFormElement parentElement, Class<T> clazz,
110
            String labelString, T entity, int mode, int style, boolean filterElement){
111
	    this(formFactory, parentElement, clazz, labelString, entity, mode, style, filterElement, null);
112
	}
113

    
114
	public EntitySelectionElement(CdmFormFactory formFactory,
115
	        ICdmFormElement parentElement, Class<T> clazz,
116
			String labelString, T entity, int mode, int style, boolean filterElement, Integer limit) {
117
		super(formFactory, parentElement);
118

    
119
		this.clazz = clazz;
120
		this.isEditable = (mode & EDITABLE) == EDITABLE;
121
		this.isDeletable = (mode & DELETABLE) == DELETABLE;
122
		this.isExternal= (mode & EXTERNAL) == EXTERNAL;
123
		boolean isSelectable = (mode & SELECTABLE) == SELECTABLE;
124

    
125
		this.labelString = (labelString == null || labelString.equals("")) ? "" : labelString;
126

    
127
		if (isSelectable && formFactory.getSelectionProvider() != null) {
128
			selectionArbitrator = formFactory.createSelectionArbitrator(this);
129
		}
130

    
131
		createControls(getLayoutComposite(), SWT.NULL, limit);
132
		if (filterElement){
133
		    setFilteredEntity(entity);
134
		}else{
135
		    setEntity(entity);
136
		}
137
	}
138

    
139
    private void setFilteredEntity(T filterEntity) {
140
        this.filteredEntity =filterEntity;
141
    }
142

    
143
    public EntitySelectionElement(CdmFormFactory formFactory,
144
             ICdmFormElement parentElement, Class<T> clazz,
145
            String labelString, T entity, int mode, int style){
146
        this(formFactory,
147
                parentElement, clazz, labelString, entity, mode, style, false);
148
    }
149

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

    
157
	private void createControls(Composite parent, int style, Integer limit) {
158

    
159
		label = formFactory.createLabel(getLayoutComposite(), labelString,
160
				SWT.NULL);
161

    
162
		addControl(label);
163

    
164
		selectableComposite = formFactory.createComposite(getLayoutComposite());
165

    
166
		int columns = 2;
167
		if (isEditable) {
168
			columns += 1;
169
		}
170
		if (isDeletable) {
171
			columns += 1;
172
		}
173
		if (isExternal) {
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

    
185
		addControl(text);
186

    
187
		text.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
188
		text.setBackground(AbstractUtility.getColor(Resources.COLOR_TEXT_DISABLED_BACKGROUND));
189
		if (limit != null){
190
		    text.setTextLimit(limit);
191
		}
192
		button_selection = formFactory.createButton(selectableComposite, null,
193
				SWT.PUSH);
194
		button_selection.setImage(ImageResources
195
				.getImage(ImageResources.BROWSE_ICON));
196
		button_selection.setToolTipText("Browse existing");
197

    
198
		addControl(button_selection);
199
		button_selection.addSelectionListener(this);
200
		if (isExternal){
201
    		button_selectionExt = formFactory.createButton(selectableComposite, null,
202
                    SWT.PUSH);
203
    		button_selectionExt.setImage(ImageResources
204
                    .getImage(ImageResources.BROWSE_ICON));
205
    		button_selectionExt.setToolTipText("Browse existing from external cdm store");
206
    		button_selectionExt.setText("Ext");
207
            addControl(button_selectionExt);
208
            button_selectionExt.addSelectionListener(this);
209
		}
210
		if (isEditable) {
211
			button_edit = formFactory.createButton(selectableComposite, null,
212
					SWT.PUSH);
213
			button_edit.setImage(ImageResources
214
					.getImage(ImageResources.EDIT_ICON));
215
			button_edit.setToolTipText("Edit");
216
			addControl(button_edit);
217
			button_edit.addSelectionListener(new EditListener(this));
218
		}
219

    
220
		if (isDeletable) {
221
			button_remove = formFactory.createButton(selectableComposite, null,
222
					SWT.PUSH);
223
			button_remove.setImage(ImageResources
224
					.getImage(ImageResources.TRASH_ICON));
225
			button_remove.setToolTipText("Remove");
226
			addControl(button_remove);
227
			button_remove.addSelectionListener(new DeleteListener(this));
228
		}
229
	}
230

    
231
	@Override
232
    public void widgetSelected(SelectionEvent e) {
233
	    if (e.getSource().equals(button_selection) ){
234
	        T selection = SelectionDialogFactory.getSelectionFromDialog(clazz, getShell(),
235
	                getEntity(), getParentElement());
236
            setSelectionInternal(selection);
237
            if(!getLayoutComposite().isDisposed()){
238
                StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
239
            }
240
	    }else{
241
	        Reference selection = SelectionDialogFactory.getSelectionFromExtDialog(Reference.class, getShell(),
242
	                getParentElement());
243
            setSelectionInternal((T)selection);
244
	    }
245
	}
246

    
247
    public T getFilteredEntity() {
248

    
249
        return this.filteredEntity;
250
    }
251

    
252
    /**
253
	 * Return the selected object
254
	 */
255
	public T getSelection() {
256
		return entity;
257
	}
258

    
259
	@Override
260
	public void setEnabled(boolean enabled) {
261
		this.label.setEnabled(enabled);
262
		button_selection.setEnabled(enabled);
263
		if (isDeletable){
264
		    button_remove.setEnabled(enabled);
265
		}
266
		if (isEditable) {
267
			updateButtonStates();
268
		}
269

    
270
	}
271

    
272
	@Override
273
	public boolean isEnabled() {
274
	    return button_selection.isEnabled();
275
	}
276

    
277
	/**
278
	 * setSelectionInternal
279
	 */
280
	protected void setSelectionInternal(T selection) {
281
		if (selection != null && !selection.equals(this.entity)) {
282
			setEntity(selection);
283
			firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
284
		}
285
	}
286

    
287
	public void setEntity(T selection) {
288
		this.entity = selection;
289
		updateElement();
290
	}
291

    
292
	/**
293
	 * Updates this elements view
294
	 */
295
	protected void updateElement() {
296
		String title = CdmUtils.Nz(getTitle());
297

    
298
		text.setText(title); // title can be null
299
		if (isEditable) {
300
			updateButtonStates();
301
		}
302
	}
303

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

    
309
	protected String getTitle() {
310
		if (entity != null){
311
			if(entity instanceof Group){
312
				return ((Group) entity).getName();
313
			} else if(entity instanceof GrantedAuthority){
314
				return GrantedAuthorityLabelTextProvider.getText(((GrantedAuthority) entity));
315
			} else if(entity instanceof User){
316
                return ((User) entity).getUsername();
317
            } else if (entity instanceof Primer){
318
                return ((Primer) entity).getLabel();
319
            } else if (entity instanceof Amplification){
320
                return ((Amplification) entity).getLabelCache();
321
            }
322
            else if(entity instanceof IIdentifiableEntity) {
323
                return ((IIdentifiableEntity) entity).getTitleCache();
324
            }
325

    
326
		}
327
		return "";
328
	}
329

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

    
335
	@Override
336
	public T getEntity() {
337
		return entity;
338
	}
339

    
340
	@Override
341
	public SelectionArbitrator getSelectionArbitrator() {
342
		return selectionArbitrator;
343
	}
344

    
345
	/**
346
	 * Convenient access to current shell
347
	 *
348
	 * @return a {@link org.eclipse.swt.widgets.Shell} object.
349
	 */
350
    public Shell getShell() {
351
		return getLayoutComposite().getShell();
352
	}
353

    
354
	@Override
355
	public void updateCacheRelevance() {
356
		Color color = cacheRelevance().getColor(Resources.COLOR_TEXT_DISABLED_BACKGROUND);
357
		text.setBackground(color);
358
	}
359

    
360
	private class DeleteListener extends SelectionAdapter {
361

    
362
		private final EntitySelectionElement<T> selectionElement;
363

    
364
		public DeleteListener(EntitySelectionElement<T> selectionElement) {
365
			this.selectionElement = selectionElement;
366
		}
367

    
368
		@Override
369
		public void widgetSelected(SelectionEvent e) {
370
			setEntity(null);
371
			firePropertyChangeEvent(new CdmPropertyChangeEvent(
372
					selectionElement, null));
373
		}
374
	}
375

    
376
	private class EditListener extends SelectionAdapter {
377

    
378
        private final EntitySelectionElement<T> selectionElement;
379

    
380
		public EditListener(EntitySelectionElement<T> selectionElement) {
381
			this.selectionElement = selectionElement;
382
		}
383

    
384
		@Override
385
		public void widgetSelected(SelectionEvent e) {
386
			WizardDialog dialog = new WizardDialog(selectionElement.getShell(),
387
					new EditFromSelectionWizard(selectionElement));
388
			if (dialog.open() == IStatus.OK) {
389
				selectionElement.updateFromWizard();
390
				//if the edited entity has already been persisted
391
				//but the transient entity is still set in this
392
				//EntitySelectionElement, re-load it and set it
393
				if(!entity.isPersited()){
394
				    IService<T> service = CdmStore.getService(entity);
395
					T loadedEntity = service.load(entity.getUuid());
396
					if(loadedEntity != null){
397
						setEntity(loadedEntity);
398
					}
399
				}
400
			}
401
		}
402
	}
403

    
404
	// not used
405
	@Override
406
	public void widgetDefaultSelected(SelectionEvent e) {
407
	}
408

    
409

    
410

    
411
	@Override
412
	public void setBackground(Color color) {
413

    
414
	    if(label != null && !label.isDisposed()){
415
	        label.setBackground(color);
416
	    }
417
	}
418

    
419
	@Override
420
	public void setLabel(String labelString) {
421
		if (label != null) {
422
			label.setText(labelString);
423
		}
424
	}
425

    
426
	@Override
427
	public String getLabel() {
428
		if (label != null) {
429
			return label.getText() + " : ";
430
		}
431
		return null;
432
	}
433

    
434
	@Override
435
    public void removeElements(){
436
		super.removeElements();
437
		LoginManager loginManager = CdmStore.getLoginManager();
438
		loginManager.addObserver(this);
439
	}
440

    
441
	@Override
442
	public void update(Observable o, Object arg) {
443
		if(o instanceof LoginManager){
444
			updateButtonStates();
445
		}
446
	}
447

    
448
	protected void updateButtonStates() {
449
	    if(button_edit != null && !button_selection.isDisposed()){
450
	        button_edit.setEnabled(isEditable && button_selection.isEnabled() && getEntity() != null  && CdmStore.currentAuthentiationHasPermission(getEntity(), UPDATE));
451
	    }
452
	}
453

    
454
	public void setIndent(int indent){
455
        TableWrapData tableWrapData = (TableWrapData)label.getLayoutData();
456

    
457
        if (tableWrapData == null){
458
            tableWrapData = new TableWrapData();
459

    
460
        }
461
        tableWrapData.indent = indent;
462
        label.setLayoutData(tableWrapData);
463

    
464
        getLayoutComposite().layout();
465
    }
466
}
(5-5/8)