4 package eu
.etaxonomy
.taxeditor
.ui
.selection
;
6 import java
.util
.EnumSet
;
7 import java
.util
.Observable
;
8 import java
.util
.Observer
;
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
;
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
.IIdentifiableEntity
;
30 import eu
.etaxonomy
.cdm
.model
.molecular
.Amplification
;
31 import eu
.etaxonomy
.cdm
.model
.molecular
.Primer
;
32 import eu
.etaxonomy
.cdm
.model
.permission
.CRUD
;
33 import eu
.etaxonomy
.cdm
.model
.permission
.Group
;
34 import eu
.etaxonomy
.cdm
.model
.permission
.User
;
35 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
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
;
56 * @created Nov 17, 2009
60 public class EntitySelectionElement
<T
extends CdmBase
> extends
61 AbstractCdmFormElement
implements SelectionListener
, IEnableableFormElement
, ISelectableElement
, IEntityElement
<T
>, ILabeledElement
, //IConversationEnabled,
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
);
69 * Bitmask for configuring functionality of selection element
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
79 protected T filteredEntity
;
81 protected Label label
;
83 protected Button button_selection
;
85 protected Button button_selectionExt
;
87 private SelectionArbitrator selectionArbitrator
;
89 protected Button button_edit
;
91 private final String labelString
;
93 private Composite selectableComposite
;
95 private Button button_remove
;
97 protected final boolean isEditable
;
99 private final boolean isDeletable
;
100 private final boolean isExternal
;
102 // private final ConversationHolder conversation;
103 private Class
<T
> clazz
;
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 this(formFactory
, parentElement
, clazz
, labelString
, entity
, mode
, style
, filterElement
, null);
113 public EntitySelectionElement(CdmFormFactory formFactory
,
114 // ConversationHolder conversation,
115 ICdmFormElement parentElement
, Class
<T
> clazz
,
116 String labelString
, T entity
, int mode
, int style
, boolean filterElement
, Integer limit
) {
117 super(formFactory
, parentElement
);
120 this.isEditable
= (mode
& EDITABLE
) == EDITABLE
;
121 this.isDeletable
= (mode
& DELETABLE
) == DELETABLE
;
122 this.isExternal
= (mode
& EXTERNAL
) == EXTERNAL
;
123 boolean isSelectable
= (mode
& SELECTABLE
) == SELECTABLE
;
125 this.labelString
= (labelString
== null || labelString
.equals("")) ?
"" : labelString
;
127 // this.conversation = conversation;
129 if (isSelectable
&& formFactory
.getSelectionProvider() != null) {
130 selectionArbitrator
= formFactory
.createSelectionArbitrator(this);
133 createControls(getLayoutComposite(), SWT
.NULL
, limit
);
135 setFilteredEntity(entity
);
141 private void setFilteredEntity(T filterEntity
) {
142 this.filteredEntity
=filterEntity
;
146 public EntitySelectionElement(CdmFormFactory formFactory
,//ConversationHolder conversation,
147 ICdmFormElement parentElement
, Class
<T
> clazz
,
148 String labelString
, T entity
, int mode
, int style
){
149 this(formFactory
, //conversation,
150 parentElement
, clazz
, labelString
, entity
, mode
, style
, false);
153 public EntitySelectionElement(CdmFormFactory formFactory
,//ConversationHolder conversation,
154 ICdmFormElement parentElement
, Class
<T
> clazz
,
155 String labelString
, T entity
, int mode
, int style
, Integer limit
){
156 this(formFactory
, //conversation,
157 parentElement
, clazz
, labelString
, entity
, mode
, style
, false, limit
);
160 private void createControls(Composite parent
, int style
, Integer limit
) {
162 label
= formFactory
.createLabel(getLayoutComposite(), labelString
,
167 selectableComposite
= formFactory
.createComposite(getLayoutComposite());
180 selectableComposite
.setLayout(LayoutConstants
.LAYOUT(columns
, false));
181 selectableComposite
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY());
183 addControl(selectableComposite
);
185 text
= formFactory
.createText(selectableComposite
, null, SWT
.WRAP
);
186 text
.setEditable(false);
190 text
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY());
191 text
.setBackground(StoreUtil
192 .getColor(Resources
.COLOR_TEXT_DISABLED_BACKGROUND
));
194 text
.setTextLimit(limit
);
196 button_selection
= formFactory
.createButton(selectableComposite
, null,
198 button_selection
.setImage(ImageResources
199 .getImage(ImageResources
.BROWSE_ICON
));
200 button_selection
.setToolTipText("Browse existing");
202 addControl(button_selection
);
203 button_selection
.addSelectionListener(this);
205 button_selectionExt
= formFactory
.createButton(selectableComposite
, null,
207 button_selectionExt
.setImage(ImageResources
208 .getImage(ImageResources
.BROWSE_ICON
));
209 button_selectionExt
.setToolTipText("Browse existing from external cdm store");
210 button_selectionExt
.setText("Ext");
211 addControl(button_selectionExt
);
212 button_selectionExt
.addSelectionListener(this);
215 button_edit
= formFactory
.createButton(selectableComposite
, null,
217 button_edit
.setImage(ImageResources
218 .getImage(ImageResources
.EDIT_ICON
));
219 button_edit
.setToolTipText("Edit");
220 addControl(button_edit
);
221 button_edit
.addSelectionListener(new EditListener(this));
225 button_remove
= formFactory
.createButton(selectableComposite
, null,
227 button_remove
.setImage(ImageResources
228 .getImage(ImageResources
.TRASH_ICON
));
229 button_remove
.setToolTipText("Remove");
230 addControl(button_remove
);
231 button_remove
.addSelectionListener(new DeleteListener(this));
236 public void widgetSelected(SelectionEvent e
) {
237 if (e
.getSource().equals(button_selection
) ){
238 T selection
= SelectionDialogFactory
.getSelectionFromDialog(clazz
, getShell(), //getConversationHolder(),
239 getEntity(), getParentElement());
240 setSelectionInternal(selection
);
241 if(!getLayoutComposite().isDisposed()){
242 StoreUtil
.reflowParentScrolledForm(getLayoutComposite(), true);
245 Reference selection
= SelectionDialogFactory
.getSelectionFromExtDialog(Reference
.class, getShell(),//null,
247 setSelectionInternal((T
)selection
);
255 public T
getFilteredEntity() {
257 return this.filteredEntity
;
261 * Return the selected object
263 * @return a T object.
265 public T
getSelection() {
273 * eu.etaxonomy.taxeditor.forms.IEnableableFormElement#setEnabled(boolean)
277 public void setEnabled(boolean enabled
) {
279 button_selection
.setEnabled(enabled
);
281 button_remove
.setEnabled(enabled
);
284 updateButtonStates();
290 * @see eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement#isEnabled()
293 public boolean isEnabled() {
294 return button_selection
.isEnabled();
299 * setSelectionInternal
305 protected void setSelectionInternal(T selection
) {
306 if (selection
!= null && !selection
.equals(this.entity
)) {
307 setEntity(selection
);
308 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
314 * Setter for the field <code>entity</code>.
320 public void setEntity(T selection
) {
321 this.entity
= selection
;
326 * Updates this elements view
328 protected void updateElement() {
329 String title
= CdmUtils
.Nz(getTitle());
331 text
.setText(title
); // title can be null
333 updateButtonStates();
337 public void updateFromWizard() {
339 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
347 * @return a {@link java.lang.String} object.
349 protected String
getTitle() {
351 if(entity
instanceof Group
){
352 return ((Group
) entity
).getName();
353 } else if(entity
instanceof GrantedAuthority
){
354 return GrantedAuthorityLabelTextProvider
.getText(((GrantedAuthority
) entity
));
355 } else if(entity
instanceof User
){
356 return ((User
) entity
).getUsername();
357 } else if (entity
instanceof Primer
){
358 return ((Primer
) entity
).getLabel();
359 } else if (entity
instanceof Amplification
){
360 return ((Amplification
) entity
).getLabelCache();
362 else if(entity
instanceof IIdentifiableEntity
) {
363 return ((IIdentifiableEntity
) entity
).getTitleCache();
372 public void setSelected(boolean selected
) {
373 setBackground(selected ? SELECTED
: getPersistentBackground());
379 * @see eu.etaxonomy.taxeditor.forms.IEntityElement#getEntity()
383 * Getter for the field <code>entity</code>.
386 * @return a T object.
389 public T
getEntity() {
396 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#
397 * getSelectionArbitrator()
401 * Getter for the field <code>selectionArbitrator</code>.
404 * @return a {@link eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator}
408 public SelectionArbitrator
getSelectionArbitrator() {
409 return selectionArbitrator
;
413 * Convenient access to current shell
415 * @return a {@link org.eclipse.swt.widgets.Shell} object.
417 public Shell
getShell() {
418 return getLayoutComposite().getShell();
423 public void setIrrelevant(boolean irrelevant
) {
424 String colorId
= irrelevant ? Resources
.COLOR_COMPOSITE_IRRELEVANT
425 : Resources
.COLOR_TEXT_DISABLED_BACKGROUND
;
427 Color color
= StoreUtil
.getColor(colorId
);
428 text
.setBackground(color
);
431 private class DeleteListener
extends SelectionAdapter
{
433 private final EntitySelectionElement
<T
> selectionElement
;
435 public DeleteListener(EntitySelectionElement
<T
> selectionElement
) {
436 this.selectionElement
= selectionElement
;
440 public void widgetSelected(SelectionEvent e
) {
442 firePropertyChangeEvent(new CdmPropertyChangeEvent(
443 selectionElement
, null));
447 private class EditListener
extends SelectionAdapter
{
449 private final EntitySelectionElement
<T
> selectionElement
;
451 public EditListener(EntitySelectionElement
<T
> selectionElement
) {
452 this.selectionElement
= selectionElement
;
456 public void widgetSelected(SelectionEvent e
) {
457 WizardDialog dialog
= new WizardDialog(selectionElement
.getShell(),
458 new EditFromSelectionWizard(selectionElement
));
459 if (dialog
.open() == IStatus
.OK
) {
460 selectionElement
.updateFromWizard();
461 //if the edited entity has already been persisted
462 //but the transient entity is still set in this
463 //EntitySelectionElement, re-load it and set it
464 if(!entity
.isPersited()){
465 IService
<T
> service
= CdmStore
.getService(entity
);
466 T loadedEntity
= service
.load(entity
.getUuid());
467 if(loadedEntity
!= null){
468 setEntity(loadedEntity
);
477 public void widgetDefaultSelected(SelectionEvent e
) {
482 * getConversationHolder
485 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
489 public ConversationHolder
getConversationHolder() {
490 if(getParentElement() instanceof IConversationEnabled
) {
491 return ((IConversationEnabled
)getParentElement()).getConversationHolder();
498 public void setBackground(Color color
) {
500 if(label
!= null && !label
.isDisposed()){
501 label
.setBackground(color
);
507 public void setLabel(String labelString
) {
509 label
.setText(labelString
);
515 * Getter for the field <code>label</code>.
518 * @return a {@link java.lang.String} object.
521 public String
getLabel() {
523 return label
.getText() + " : ";
530 // public void update(CdmDataChangeMap changeEvents) {
534 * @see eu.etaxonomy.taxeditor.ui.element.AbstractCdmFormElement#removeElements()
537 public void removeElements(){
538 super.removeElements();
539 LoginManager loginManager
= CdmStore
.getLoginManager();
540 loginManager
.addObserver(this);
544 public void update(Observable o
, Object arg
) {
545 if(o
instanceof LoginManager
){
546 updateButtonStates();
550 protected void updateButtonStates() {
551 if(button_edit
!= null && !button_selection
.isDisposed()){
552 button_edit
.setEnabled(isEditable
&& button_selection
.isEnabled() && getEntity() != null && CdmStore
.currentAuthentiationHasPermission(getEntity(), UPDATE
));