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
|
if(!getLayoutComposite().isDisposed()){
|
224
|
StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
|
225
|
}
|
226
|
}else{
|
227
|
Reference selection = SelectionDialogFactory.getSelectionFromExtDialog(Reference.class, getShell(),//null,
|
228
|
getParentElement());
|
229
|
setSelectionInternal((T)selection);
|
230
|
}
|
231
|
|
232
|
}
|
233
|
|
234
|
/**
|
235
|
* @return
|
236
|
*/
|
237
|
public T getFilteredEntity() {
|
238
|
|
239
|
return this.filteredEntity;
|
240
|
}
|
241
|
|
242
|
/**
|
243
|
* Return the selected object
|
244
|
*
|
245
|
* @return a T object.
|
246
|
*/
|
247
|
public T getSelection() {
|
248
|
return entity;
|
249
|
}
|
250
|
|
251
|
/*
|
252
|
* (non-Javadoc)
|
253
|
*
|
254
|
* @see
|
255
|
* eu.etaxonomy.taxeditor.forms.IEnableableFormElement#setEnabled(boolean)
|
256
|
*/
|
257
|
/** {@inheritDoc} */
|
258
|
@Override
|
259
|
public void setEnabled(boolean enabled) {
|
260
|
|
261
|
button_selection.setEnabled(enabled);
|
262
|
if (isDeletable){
|
263
|
button_remove.setEnabled(enabled);
|
264
|
}
|
265
|
if (isEditable) {
|
266
|
updateButtonStates();
|
267
|
}
|
268
|
|
269
|
}
|
270
|
|
271
|
/* (non-Javadoc)
|
272
|
* @see eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement#isEnabled()
|
273
|
*/
|
274
|
@Override
|
275
|
public boolean isEnabled() {
|
276
|
return button_selection.isEnabled();
|
277
|
}
|
278
|
|
279
|
/**
|
280
|
* <p>
|
281
|
* setSelectionInternal
|
282
|
* </p>
|
283
|
*
|
284
|
* @param selection
|
285
|
* a T object.
|
286
|
*/
|
287
|
protected void setSelectionInternal(T selection) {
|
288
|
if (selection != null && !selection.equals(this.entity)) {
|
289
|
setEntity(selection);
|
290
|
firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
|
291
|
}
|
292
|
}
|
293
|
|
294
|
/**
|
295
|
* <p>
|
296
|
* Setter for the field <code>entity</code>.
|
297
|
* </p>
|
298
|
*
|
299
|
* @param selection
|
300
|
* a T object.
|
301
|
*/
|
302
|
public void setEntity(T selection) {
|
303
|
this.entity = selection;
|
304
|
updateElement();
|
305
|
}
|
306
|
|
307
|
/**
|
308
|
* Updates this elements view
|
309
|
*/
|
310
|
protected void updateElement() {
|
311
|
String title = CdmUtils.Nz(getTitle());
|
312
|
text.setText(title); // title can be null
|
313
|
if (isEditable) {
|
314
|
updateButtonStates();
|
315
|
}
|
316
|
}
|
317
|
|
318
|
public void updateFromWizard() {
|
319
|
updateElement();
|
320
|
firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
|
321
|
}
|
322
|
|
323
|
/**
|
324
|
* <p>
|
325
|
* getTitle
|
326
|
* </p>
|
327
|
*
|
328
|
* @return a {@link java.lang.String} object.
|
329
|
*/
|
330
|
protected String getTitle() {
|
331
|
if (entity != null){
|
332
|
if(entity instanceof Group){
|
333
|
return ((Group) entity).getName();
|
334
|
} else if(entity instanceof GrantedAuthority){
|
335
|
return GrantedAuthorityLabelTextProvider.getText(((GrantedAuthority) entity));
|
336
|
} else if(entity instanceof User){
|
337
|
return ((User) entity).getUsername();
|
338
|
} else if (entity instanceof Primer){
|
339
|
return ((Primer) entity).getLabel();
|
340
|
} else if (entity instanceof Amplification){
|
341
|
return ((Amplification) entity).getLabelCache();
|
342
|
}
|
343
|
else if(entity instanceof IIdentifiableEntity) {
|
344
|
return ((IIdentifiableEntity) entity).getTitleCache();
|
345
|
}
|
346
|
|
347
|
}
|
348
|
return "";
|
349
|
}
|
350
|
|
351
|
/** {@inheritDoc} */
|
352
|
@Override
|
353
|
public void setSelected(boolean selected) {
|
354
|
setBackground(selected ? SELECTED : getPersistentBackground());
|
355
|
}
|
356
|
|
357
|
/*
|
358
|
* (non-Javadoc)
|
359
|
*
|
360
|
* @see eu.etaxonomy.taxeditor.forms.IEntityElement#getEntity()
|
361
|
*/
|
362
|
/**
|
363
|
* <p>
|
364
|
* Getter for the field <code>entity</code>.
|
365
|
* </p>
|
366
|
*
|
367
|
* @return a T object.
|
368
|
*/
|
369
|
@Override
|
370
|
public T getEntity() {
|
371
|
return entity;
|
372
|
}
|
373
|
|
374
|
/*
|
375
|
* (non-Javadoc)
|
376
|
*
|
377
|
* @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#
|
378
|
* getSelectionArbitrator()
|
379
|
*/
|
380
|
/**
|
381
|
* <p>
|
382
|
* Getter for the field <code>selectionArbitrator</code>.
|
383
|
* </p>
|
384
|
*
|
385
|
* @return a {@link eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator}
|
386
|
* object.
|
387
|
*/
|
388
|
@Override
|
389
|
public SelectionArbitrator getSelectionArbitrator() {
|
390
|
return selectionArbitrator;
|
391
|
}
|
392
|
|
393
|
/**
|
394
|
* Convenient access to current shell
|
395
|
*
|
396
|
* @return a {@link org.eclipse.swt.widgets.Shell} object.
|
397
|
*/
|
398
|
public Shell getShell() {
|
399
|
return getLayoutComposite().getShell();
|
400
|
}
|
401
|
|
402
|
/** {@inheritDoc} */
|
403
|
@Override
|
404
|
public void setIrrelevant(boolean irrelevant) {
|
405
|
String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT
|
406
|
: Resources.COLOR_TEXT_DISABLED_BACKGROUND;
|
407
|
|
408
|
Color color = StoreUtil.getColor(colorId);
|
409
|
text.setBackground(color);
|
410
|
}
|
411
|
|
412
|
private class DeleteListener extends SelectionAdapter {
|
413
|
|
414
|
private final EntitySelectionElement<T> selectionElement;
|
415
|
|
416
|
public DeleteListener(EntitySelectionElement<T> selectionElement) {
|
417
|
this.selectionElement = selectionElement;
|
418
|
}
|
419
|
|
420
|
@Override
|
421
|
public void widgetSelected(SelectionEvent e) {
|
422
|
setEntity(null);
|
423
|
firePropertyChangeEvent(new CdmPropertyChangeEvent(
|
424
|
selectionElement, null));
|
425
|
}
|
426
|
}
|
427
|
|
428
|
private class EditListener extends SelectionAdapter {
|
429
|
|
430
|
private final EntitySelectionElement<T> selectionElement;
|
431
|
|
432
|
public EditListener(EntitySelectionElement<T> selectionElement) {
|
433
|
this.selectionElement = selectionElement;
|
434
|
}
|
435
|
|
436
|
/** {@inheritDoc} */
|
437
|
@Override
|
438
|
public void widgetSelected(SelectionEvent e) {
|
439
|
WizardDialog dialog = new WizardDialog(selectionElement.getShell(),
|
440
|
new EditFromSelectionWizard(selectionElement));
|
441
|
if (dialog.open() == IStatus.OK) {
|
442
|
selectionElement.updateFromWizard();
|
443
|
//if the edited entity has already been persisted
|
444
|
//but the transient entity is still set in this
|
445
|
//EntitySelectionElement, re-load it and set it
|
446
|
IService<T> service = CdmStore.getService(entity);
|
447
|
if(entity.getId()==0){
|
448
|
T loadedEntity = service.load(entity.getUuid());
|
449
|
if(loadedEntity!=null){
|
450
|
setEntity(loadedEntity);
|
451
|
}
|
452
|
}
|
453
|
}
|
454
|
}
|
455
|
}
|
456
|
|
457
|
// not used
|
458
|
/** {@inheritDoc} */
|
459
|
@Override
|
460
|
public void widgetDefaultSelected(SelectionEvent e) {
|
461
|
}
|
462
|
|
463
|
/**
|
464
|
* <p>
|
465
|
* getConversationHolder
|
466
|
* </p>
|
467
|
*
|
468
|
* @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
|
469
|
* object.
|
470
|
*/
|
471
|
// @Override
|
472
|
public ConversationHolder getConversationHolder() {
|
473
|
if(getParentElement() instanceof IConversationEnabled) {
|
474
|
return ((IConversationEnabled)getParentElement()).getConversationHolder();
|
475
|
}
|
476
|
return null;
|
477
|
}
|
478
|
|
479
|
/** {@inheritDoc} */
|
480
|
@Override
|
481
|
public void setBackground(Color color) {
|
482
|
|
483
|
if(label != null && !label.isDisposed()){
|
484
|
label.setBackground(color);
|
485
|
}
|
486
|
}
|
487
|
|
488
|
/** {@inheritDoc} */
|
489
|
@Override
|
490
|
public void setLabel(String labelString) {
|
491
|
if (label != null) {
|
492
|
label.setText(labelString);
|
493
|
}
|
494
|
}
|
495
|
|
496
|
/**
|
497
|
* <p>
|
498
|
* Getter for the field <code>label</code>.
|
499
|
* </p>
|
500
|
*
|
501
|
* @return a {@link java.lang.String} object.
|
502
|
*/
|
503
|
@Override
|
504
|
public String getLabel() {
|
505
|
if (label != null) {
|
506
|
return label.getText() + " : ";
|
507
|
}
|
508
|
return null;
|
509
|
}
|
510
|
|
511
|
/** {@inheritDoc} */
|
512
|
// @Override
|
513
|
// public void update(CdmDataChangeMap changeEvents) {
|
514
|
// }
|
515
|
|
516
|
/* (non-Javadoc)
|
517
|
* @see eu.etaxonomy.taxeditor.ui.element.AbstractCdmFormElement#removeElements()
|
518
|
*/
|
519
|
@Override
|
520
|
public void removeElements(){
|
521
|
super.removeElements();
|
522
|
LoginManager loginManager = CdmStore.getLoginManager();
|
523
|
loginManager.addObserver(this);
|
524
|
}
|
525
|
|
526
|
@Override
|
527
|
public void update(Observable o, Object arg) {
|
528
|
if(o instanceof LoginManager){
|
529
|
updateButtonStates();
|
530
|
}
|
531
|
}
|
532
|
|
533
|
private void updateButtonStates() {
|
534
|
if(button_edit != null && !button_selection.isDisposed()){
|
535
|
button_edit.setEnabled(isEditable && button_selection.isEnabled() && getEntity() != null && CdmStore.currentAuthentiationHasPermission(getEntity(), UPDATE));
|
536
|
}
|
537
|
}
|
538
|
}
|