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
.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
.persistence
.hibernate
.CdmDataChangeMap
;
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
;
56 * Abstract AbstractSelectionElement class.
60 * @created Nov 17, 2009
64 public class EntitySelectionElement
<T
extends CdmBase
> extends
65 AbstractCdmFormElement
implements SelectionListener
, IEnableableFormElement
, ISelectableElement
, IEntityElement
<T
>, ILabeledElement
, IConversationEnabled
, Observer
{
67 private static final EnumSet
<CRUD
> UPDATE
= EnumSet
.of(CRUD
.UPDATE
);
68 private static final EnumSet
<CRUD
> DELETE
= EnumSet
.of(CRUD
.DELETE
);
69 private static final EnumSet
<CRUD
> CREATE
= EnumSet
.of(CRUD
.CREATE
);
72 * Bitmask for configuring functionality of selection element
74 public static final int NOTHING
= 0; // 000
75 public static final int EDITABLE
= 1 << 0; // 001
76 public static final int DELETABLE
= 1 << 1; // 010
77 public static final int SELECTABLE
= 1 << 2; // 100
78 public static final int ALL
= EDITABLE
| DELETABLE
| SELECTABLE
; // 111
81 protected T filteredEntity
;
83 protected Label label
;
85 protected Button button_selection
;
87 private SelectionArbitrator selectionArbitrator
;
89 protected Button button_edit
;
91 private final String labelString
;
93 private Composite selectableComposite
;
95 private Button button_remove
;
97 private final boolean isEditable
;
99 private final boolean isDeletable
;
101 private final ConversationHolder conversation
;
102 private Class
<T
> clazz
;
106 * Constructor for AbstractSelectionElement.
110 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
112 * @param conversation
114 * @param parentElement
115 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
118 * a {@link java.lang.String} object.
123 * @param isSelectable
132 public EntitySelectionElement(CdmFormFactory formFactory
,
133 ConversationHolder conversation
, ICdmFormElement parentElement
,
134 String labelString
, T entity
, int mode
, int style
, boolean filterElement
) {
135 super(formFactory
, parentElement
);
137 this.isEditable
= (mode
& EDITABLE
) == EDITABLE
;
138 this.isDeletable
= (mode
& DELETABLE
) == DELETABLE
;
139 boolean isSelectable
= (mode
& SELECTABLE
) == SELECTABLE
;
141 this.labelString
= (labelString
== null || labelString
.equals("")) ?
"" : labelString
;
143 this.conversation
= conversation
;
145 if (isSelectable
&& formFactory
.getSelectionProvider() != null) {
146 selectionArbitrator
= formFactory
.createSelectionArbitrator(this);
149 createControls(getLayoutComposite(), SWT
.NULL
);
151 setFilteredEntity(entity
);
157 public EntitySelectionElement(CdmFormFactory formFactory
,
158 ConversationHolder conversation
, ICdmFormElement parentElement
,
159 String labelString
, T entity
, int mode
, int style
){
160 this(formFactory
, conversation
, parentElement
, labelString
, entity
, mode
, style
, false);
166 private void setFilteredEntity(T filterEntity
) {
167 this.filteredEntity
=filterEntity
;
171 public EntitySelectionElement(CdmFormFactory formFactory
,
172 ConversationHolder conversation
, ICdmFormElement parentElement
, Class
<T
> clazz
,
173 String labelString
, T entity
, int mode
, int style
, boolean filterElement
) {
174 this(formFactory
, conversation
, parentElement
, labelString
, entity
, mode
, style
, filterElement
);
178 public EntitySelectionElement(CdmFormFactory formFactory
,
179 ConversationHolder conversation
, ICdmFormElement parentElement
, Class
<T
> clazz
,
180 String labelString
, T entity
, int mode
, int style
){
181 this(formFactory
, conversation
, parentElement
, labelString
, entity
, mode
, style
, false);
184 private void createControls(Composite parent
, int style
) {
186 label
= formFactory
.createLabel(getLayoutComposite(), labelString
,
191 selectableComposite
= formFactory
.createComposite(getLayoutComposite());
201 selectableComposite
.setLayout(LayoutConstants
.LAYOUT(columns
, false));
202 selectableComposite
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY());
204 addControl(selectableComposite
);
206 text
= formFactory
.createText(selectableComposite
, null, SWT
.WRAP
);
207 text
.setEditable(false);
210 text
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY());
211 text
.setBackground(StoreUtil
212 .getColor(Resources
.COLOR_TEXT_DISABLED_BACKGROUND
));
214 button_selection
= formFactory
.createButton(selectableComposite
, null,
216 button_selection
.setImage(ImageResources
217 .getImage(ImageResources
.BROWSE_ICON
));
218 button_selection
.setToolTipText("Browse existing");
220 addControl(button_selection
);
221 button_selection
.addSelectionListener(this);
224 button_edit
= formFactory
.createButton(selectableComposite
, null,
226 button_edit
.setImage(ImageResources
227 .getImage(ImageResources
.EDIT_ICON
));
228 button_edit
.setToolTipText("Edit");
229 addControl(button_edit
);
230 button_edit
.addSelectionListener(new EditListener(this));
234 button_remove
= formFactory
.createButton(selectableComposite
, null,
236 button_remove
.setImage(ImageResources
237 .getImage(ImageResources
.TRASH_ICON
));
238 button_remove
.setToolTipText("Remove");
239 addControl(button_remove
);
240 button_remove
.addSelectionListener(new DeleteListener(this));
245 public void widgetSelected(SelectionEvent e
) {
246 T selection
= SelectionDialogFactory
.getSelectionFromDialog(clazz
, getShell(), getConversationHolder(), getFilteredEntity(), getParentElement());
247 setSelectionInternal(selection
);
253 public T
getFilteredEntity() {
255 return this.filteredEntity
;
259 * Return the selected object
261 * @return a T object.
263 public T
getSelection() {
271 * eu.etaxonomy.taxeditor.forms.IEnableableFormElement#setEnabled(boolean)
275 public void setEnabled(boolean enabled
) {
277 button_selection
.setEnabled(enabled
);
279 button_remove
.setEnabled(enabled
);
282 updateButtonStates();
288 * @see eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement#isEnabled()
291 public boolean isEnabled() {
292 return button_selection
.isEnabled();
297 * setSelectionInternal
303 protected void setSelectionInternal(T selection
) {
304 if (selection
!= null && !selection
.equals(this.entity
)) {
305 setEntity(selection
);
306 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
312 * Setter for the field <code>entity</code>.
318 public void setEntity(T selection
) {
319 this.entity
= selection
;
324 * Updates this elements view
326 protected void updateElement() {
327 String title
= CdmUtils
.Nz(getTitle());
328 text
.setText(title
); // title can be null
330 updateButtonStates();
334 public void updateFromWizard() {
336 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
344 * @return a {@link java.lang.String} object.
346 protected String
getTitle() {
348 if(entity
instanceof Group
){
349 return ((Group
) entity
).getName();
350 } else if(entity
instanceof GrantedAuthority
){
351 return GrantedAuthorityLabelTextProvider
.getText(((GrantedAuthority
) entity
));
352 } else if(entity
instanceof User
){
353 return ((User
) entity
).getUsername();
354 } else if (entity
instanceof Primer
){
355 return ((Primer
) entity
).getLabel();
356 } else if (entity
instanceof Amplification
){
357 return ((Amplification
) entity
).getLabelCache();
359 else if(entity
instanceof IIdentifiableEntity
) {
360 return ((IIdentifiableEntity
) entity
).getTitleCache();
369 public void setSelected(boolean selected
) {
370 setBackground(selected ? SELECTED
: getPersistentBackground());
376 * @see eu.etaxonomy.taxeditor.forms.IEntityElement#getEntity()
380 * Getter for the field <code>entity</code>.
383 * @return a T object.
386 public T
getEntity() {
393 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#
394 * getSelectionArbitrator()
398 * Getter for the field <code>selectionArbitrator</code>.
401 * @return a {@link eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator}
405 public SelectionArbitrator
getSelectionArbitrator() {
406 return selectionArbitrator
;
410 * Convenient access to current shell
412 * @return a {@link org.eclipse.swt.widgets.Shell} object.
414 public Shell
getShell() {
415 return getLayoutComposite().getShell();
420 public void setIrrelevant(boolean irrelevant
) {
421 String colorId
= irrelevant ? Resources
.COLOR_COMPOSITE_IRRELEVANT
422 : Resources
.COLOR_TEXT_DISABLED_BACKGROUND
;
424 Color color
= StoreUtil
.getColor(colorId
);
425 text
.setBackground(color
);
428 private class DeleteListener
extends SelectionAdapter
{
430 private final EntitySelectionElement
<T
> selectionElement
;
432 public DeleteListener(EntitySelectionElement
<T
> selectionElement
) {
433 this.selectionElement
= selectionElement
;
437 public void widgetSelected(SelectionEvent e
) {
439 firePropertyChangeEvent(new CdmPropertyChangeEvent(
440 selectionElement
, null));
444 private class EditListener
extends SelectionAdapter
{
446 private final EntitySelectionElement
<T
> selectionElement
;
448 public EditListener(EntitySelectionElement
<T
> selectionElement
) {
449 this.selectionElement
= selectionElement
;
454 public void widgetSelected(SelectionEvent e
) {
455 WizardDialog dialog
= new WizardDialog(selectionElement
.getShell(),
456 new EditFromSelectionWizard(selectionElement
));
457 if (dialog
.open() == IStatus
.OK
) {
458 selectionElement
.updateFromWizard();
459 //if the edited entity has already been persisted
460 //but the transient entity is still set in this
461 //EntitySelectionElement, re-load it and set it
462 IService
<T
> service
= CdmStore
.getService(entity
);
463 if(entity
.getId()==0){
464 T loadedEntity
= service
.load(entity
.getUuid());
465 if(loadedEntity
!=null){
466 setEntity(loadedEntity
);
476 public void widgetDefaultSelected(SelectionEvent e
) {
481 * getConversationHolder
484 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
488 public ConversationHolder
getConversationHolder() {
494 public void setBackground(Color color
) {
495 if(!label
.isDisposed()){
496 label
.setBackground(color
);
502 public void setLabel(String labelString
) {
504 label
.setText(labelString
);
510 * Getter for the field <code>label</code>.
513 * @return a {@link java.lang.String} object.
516 public String
getLabel() {
518 return label
.getText() + " : ";
525 public void update(CdmDataChangeMap changeEvents
) {
529 * @see eu.etaxonomy.taxeditor.ui.element.AbstractCdmFormElement#removeElements()
532 public void removeElements(){
533 super.removeElements();
534 LoginManager loginManager
= CdmStore
.getLoginManager();
535 loginManager
.addObserver(this);
539 public void update(Observable o
, Object arg
) {
540 if(o
instanceof LoginManager
){
541 updateButtonStates();
545 private void updateButtonStates() {
546 if(button_edit
!= null && !button_selection
.isDisposed()){
547 button_edit
.setEnabled(isEditable
&& button_selection
.isEnabled() && getEntity() != null && CdmStore
.currentAuthentiationHasPermission(getEntity(), UPDATE
));