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
.dialogs
.MessageDialog
;
12 import org
.eclipse
.jface
.wizard
.WizardDialog
;
13 import org
.eclipse
.swt
.SWT
;
14 import org
.eclipse
.swt
.events
.SelectionAdapter
;
15 import org
.eclipse
.swt
.events
.SelectionEvent
;
16 import org
.eclipse
.swt
.events
.SelectionListener
;
17 import org
.eclipse
.swt
.graphics
.Color
;
18 import org
.eclipse
.swt
.widgets
.Button
;
19 import org
.eclipse
.swt
.widgets
.Composite
;
20 import org
.eclipse
.swt
.widgets
.Label
;
21 import org
.eclipse
.swt
.widgets
.Shell
;
22 import org
.eclipse
.swt
.widgets
.Text
;
23 import org
.springframework
.security
.core
.GrantedAuthority
;
25 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
26 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
27 import eu
.etaxonomy
.cdm
.api
.service
.IService
;
28 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
29 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
30 import eu
.etaxonomy
.cdm
.model
.common
.Group
;
31 import eu
.etaxonomy
.cdm
.model
.common
.IIdentifiableEntity
;
32 import eu
.etaxonomy
.cdm
.model
.common
.User
;
33 import eu
.etaxonomy
.cdm
.model
.molecular
.Amplification
;
34 import eu
.etaxonomy
.cdm
.model
.molecular
.Primer
;
35 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
36 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.CRUD
;
37 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
38 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
39 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
40 import eu
.etaxonomy
.taxeditor
.store
.LoginManager
;
41 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
42 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.SelectionDialogFactory
;
43 import eu
.etaxonomy
.taxeditor
.ui
.element
.AbstractCdmFormElement
;
44 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
45 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmPropertyChangeEvent
;
46 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
47 import eu
.etaxonomy
.taxeditor
.ui
.element
.IEnableableFormElement
;
48 import eu
.etaxonomy
.taxeditor
.ui
.element
.IEntityElement
;
49 import eu
.etaxonomy
.taxeditor
.ui
.element
.ILabeledElement
;
50 import eu
.etaxonomy
.taxeditor
.ui
.element
.ISelectableElement
;
51 import eu
.etaxonomy
.taxeditor
.ui
.element
.LayoutConstants
;
52 import eu
.etaxonomy
.taxeditor
.ui
.element
.SelectionArbitrator
;
53 import eu
.etaxonomy
.taxeditor
.ui
.section
.grantedAuthority
.GrantedAuthorityLabelTextProvider
;
57 * Abstract AbstractSelectionElement class.
61 * @created Nov 17, 2009
65 public class EntitySelectionElement
<T
extends CdmBase
> extends
66 AbstractCdmFormElement
implements SelectionListener
, IEnableableFormElement
, ISelectableElement
, IEntityElement
<T
>, ILabeledElement
, IConversationEnabled
, Observer
{
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
);
73 * Bitmask for configuring functionality of selection element
75 public static final int NOTHING
= 0; // 000
76 public static final int EDITABLE
= 1 << 0; // 001
77 public static final int DELETABLE
= 1 << 1; // 010
78 public static final int SELECTABLE
= 1 << 2; // 100
79 public static final int ALL
= EDITABLE
| DELETABLE
| SELECTABLE
; // 111
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
) {
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
);
154 public EntitySelectionElement(CdmFormFactory formFactory
,
155 ConversationHolder conversation
, ICdmFormElement parentElement
, Class
<T
> clazz
,
156 String labelString
, T entity
, int mode
, int style
) {
157 this(formFactory
, conversation
, parentElement
, labelString
, entity
, mode
, style
);
161 private void createControls(Composite parent
, int style
) {
163 label
= formFactory
.createLabel(getLayoutComposite(), labelString
,
168 selectableComposite
= formFactory
.createComposite(getLayoutComposite());
178 selectableComposite
.setLayout(LayoutConstants
.LAYOUT(columns
, false));
179 selectableComposite
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY());
181 addControl(selectableComposite
);
183 text
= formFactory
.createText(selectableComposite
, null, SWT
.WRAP
);
184 text
.setEditable(false);
187 text
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY());
188 text
.setBackground(StoreUtil
189 .getColor(Resources
.COLOR_TEXT_DISABLED_BACKGROUND
));
191 button_selection
= formFactory
.createButton(selectableComposite
, null,
193 button_selection
.setImage(ImageResources
194 .getImage(ImageResources
.BROWSE_ICON
));
195 button_selection
.setToolTipText("Browse existing");
197 addControl(button_selection
);
198 button_selection
.addSelectionListener(this);
201 button_edit
= formFactory
.createButton(selectableComposite
, null,
203 button_edit
.setImage(ImageResources
204 .getImage(ImageResources
.EDIT_ICON
));
205 button_edit
.setToolTipText("Edit");
206 addControl(button_edit
);
207 button_edit
.addSelectionListener(new EditListener(this));
211 button_remove
= formFactory
.createButton(selectableComposite
, null,
213 button_remove
.setImage(ImageResources
214 .getImage(ImageResources
.TRASH_ICON
));
215 button_remove
.setToolTipText("Remove");
216 addControl(button_remove
);
217 button_remove
.addSelectionListener(new DeleteListener(this));
222 public void widgetSelected(SelectionEvent e
) {
223 T selection
= SelectionDialogFactory
.getSelectionFromDialog(clazz
, getShell(), getConversationHolder(), getEntity(), getParentElement());
224 setSelectionInternal(selection
);
228 * Return the selected object
230 * @return a T object.
232 public T
getSelection() {
240 * eu.etaxonomy.taxeditor.forms.IEnableableFormElement#setEnabled(boolean)
244 public void setEnabled(boolean enabled
) {
246 button_selection
.setEnabled(enabled
);
248 button_remove
.setEnabled(enabled
);
251 updateButtonStates();
257 * @see eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement#isEnabled()
260 public boolean isEnabled() {
261 return button_selection
.isEnabled();
266 * setSelectionInternal
272 protected void setSelectionInternal(T selection
) {
273 if (selection
!= null && !selection
.equals(this.entity
)) {
274 setEntity(selection
);
275 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
281 * Setter for the field <code>entity</code>.
287 public void setEntity(T selection
) {
288 this.entity
= selection
;
293 * Updates this elements view
295 protected void updateElement() {
296 String title
= CdmUtils
.Nz(getTitle());
297 text
.setText(title
); // title can be null
299 updateButtonStates();
303 public void updateFromWizard() {
305 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
313 * @return a {@link java.lang.String} object.
315 protected String
getTitle() {
317 if(entity
instanceof Group
){
318 return ((Group
) entity
).getName();
319 } else if(entity
instanceof GrantedAuthority
){
320 return GrantedAuthorityLabelTextProvider
.getText(((GrantedAuthority
) entity
));
321 } else if(entity
instanceof User
){
322 return ((User
) entity
).getUsername();
323 } else if (entity
instanceof Primer
){
324 return ((Primer
) entity
).getLabel();
325 } else if (entity
instanceof Amplification
){
326 return ((Amplification
) entity
).getLabelCache();
328 else if(entity
instanceof IIdentifiableEntity
) {
329 return ((IIdentifiableEntity
) entity
).getTitleCache();
338 public void setSelected(boolean selected
) {
339 setBackground(selected ? SELECTED
: getPersistentBackground());
345 * @see eu.etaxonomy.taxeditor.forms.IEntityElement#getEntity()
349 * Getter for the field <code>entity</code>.
352 * @return a T object.
355 public T
getEntity() {
362 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#
363 * getSelectionArbitrator()
367 * Getter for the field <code>selectionArbitrator</code>.
370 * @return a {@link eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator}
374 public SelectionArbitrator
getSelectionArbitrator() {
375 return selectionArbitrator
;
379 * Convenient access to current shell
381 * @return a {@link org.eclipse.swt.widgets.Shell} object.
383 public Shell
getShell() {
384 return getLayoutComposite().getShell();
389 public void setIrrelevant(boolean irrelevant
) {
390 String colorId
= irrelevant ? Resources
.COLOR_COMPOSITE_IRRELEVANT
391 : Resources
.COLOR_TEXT_DISABLED_BACKGROUND
;
393 Color color
= StoreUtil
.getColor(colorId
);
394 text
.setBackground(color
);
397 private class DeleteListener
extends SelectionAdapter
{
399 private final EntitySelectionElement
<T
> selectionElement
;
401 public DeleteListener(EntitySelectionElement
<T
> selectionElement
) {
402 this.selectionElement
= selectionElement
;
406 public void widgetSelected(SelectionEvent e
) {
408 firePropertyChangeEvent(new CdmPropertyChangeEvent(
409 selectionElement
, null));
413 private class EditListener
extends SelectionAdapter
{
415 private final EntitySelectionElement
<T
> selectionElement
;
417 public EditListener(EntitySelectionElement
<T
> selectionElement
) {
418 this.selectionElement
= selectionElement
;
423 public void widgetSelected(SelectionEvent e
) {
424 WizardDialog dialog
= new WizardDialog(selectionElement
.getShell(),
425 new EditFromSelectionWizard(selectionElement
));
426 if (dialog
.open() == IStatus
.OK
) {
427 selectionElement
.updateFromWizard();
428 //if the edited entity has already been persisted
429 //but the transient entity is still set in this
430 //EntitySelectionElement, re-load it and set it
431 IService
<T
> service
= CdmStore
.getService(entity
);
432 if(entity
.getId()==0){
433 T loadedEntity
= service
.load(entity
.getUuid());
434 if(loadedEntity
!=null){
435 setEntity(loadedEntity
);
445 public void widgetDefaultSelected(SelectionEvent e
) {
450 * getConversationHolder
453 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
457 public ConversationHolder
getConversationHolder() {
463 public void setBackground(Color color
) {
464 if(!label
.isDisposed()){
465 label
.setBackground(color
);
471 public void setLabel(String labelString
) {
473 label
.setText(labelString
);
479 * Getter for the field <code>label</code>.
482 * @return a {@link java.lang.String} object.
485 public String
getLabel() {
487 return label
.getText() + " : ";
494 public void update(CdmDataChangeMap changeEvents
) {
498 * @see eu.etaxonomy.taxeditor.ui.element.AbstractCdmFormElement#removeElements()
501 public void removeElements(){
502 super.removeElements();
503 LoginManager loginManager
= CdmStore
.getLoginManager();
504 loginManager
.addObserver(this);
508 public void update(Observable o
, Object arg
) {
509 if(o
instanceof LoginManager
){
510 updateButtonStates();
514 private void updateButtonStates() {
515 if(button_edit
!= null && !button_selection
.isDisposed()){
516 button_edit
.setEnabled(isEditable
&& button_selection
.isEnabled() && getEntity() != null && CdmStore
.currentAuthentiationHasPermission(getEntity(), UPDATE
));