4 package eu
.etaxonomy
.taxeditor
.ui
.selection
;
6 import org
.eclipse
.core
.runtime
.IStatus
;
7 import org
.eclipse
.jface
.wizard
.WizardDialog
;
8 import org
.eclipse
.swt
.SWT
;
9 import org
.eclipse
.swt
.events
.SelectionAdapter
;
10 import org
.eclipse
.swt
.events
.SelectionEvent
;
11 import org
.eclipse
.swt
.events
.SelectionListener
;
12 import org
.eclipse
.swt
.graphics
.Color
;
13 import org
.eclipse
.swt
.widgets
.Button
;
14 import org
.eclipse
.swt
.widgets
.Composite
;
15 import org
.eclipse
.swt
.widgets
.Label
;
16 import org
.eclipse
.swt
.widgets
.Shell
;
17 import org
.springframework
.security
.core
.GrantedAuthority
;
19 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
20 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
21 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
22 import eu
.etaxonomy
.cdm
.model
.common
.Group
;
23 import eu
.etaxonomy
.cdm
.model
.common
.ICdmBase
;
24 import eu
.etaxonomy
.cdm
.model
.common
.IIdentifiableEntity
;
25 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
26 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
27 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
28 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
29 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.SelectionDialogFactory
;
30 import eu
.etaxonomy
.taxeditor
.ui
.element
.AbstractCdmFormElement
;
31 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
32 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmPropertyChangeEvent
;
33 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
34 import eu
.etaxonomy
.taxeditor
.ui
.element
.IEnableableFormElement
;
35 import eu
.etaxonomy
.taxeditor
.ui
.element
.IEntityElement
;
36 import eu
.etaxonomy
.taxeditor
.ui
.element
.ILabeledElement
;
37 import eu
.etaxonomy
.taxeditor
.ui
.element
.ISelectableElement
;
38 import eu
.etaxonomy
.taxeditor
.ui
.element
.LayoutConstants
;
39 import eu
.etaxonomy
.taxeditor
.ui
.element
.SelectionArbitrator
;
43 * Abstract AbstractSelectionElement class.
47 * @created Nov 17, 2009
51 public class EntitySelectionElement
<T
extends ICdmBase
> extends
52 AbstractCdmFormElement
implements SelectionListener
, IEnableableFormElement
, ISelectableElement
, IEntityElement
<T
>, ILabeledElement
, IConversationEnabled
{
55 * Bitmask for configuring functionality of selection element
57 public static final int NOTHING
= 0; // 000
58 public static final int EDITABLE
= 1 << 0; // 001
59 public static final int DELETABLE
= 1 << 1; // 010
60 public static final int SELECTABLE
= 1 << 2; // 100
61 public static final int ALL
= EDITABLE
| DELETABLE
| SELECTABLE
; // 111
65 protected Label label
;
67 protected Button button_selection
;
69 private SelectionArbitrator selectionArbitrator
;
71 protected Button button_edit
;
73 private final String labelString
;
75 private Composite selectableComposite
;
77 private Button button_remove
;
79 private final boolean isEditable
;
81 private final boolean isDeletable
;
83 private final ConversationHolder conversation
;
84 private Class
<T
> clazz
;
88 * Constructor for AbstractSelectionElement.
92 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
96 * @param parentElement
97 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
100 * a {@link java.lang.String} object.
105 * @param isSelectable
114 public EntitySelectionElement(CdmFormFactory formFactory
,
115 ConversationHolder conversation
, ICdmFormElement parentElement
,
116 String labelString
, T entity
, int mode
, int style
) {
117 super(formFactory
, parentElement
);
119 this.isEditable
= (mode
& EDITABLE
) == EDITABLE
;
120 this.isDeletable
= (mode
& DELETABLE
) == DELETABLE
;
121 boolean isSelectable
= (mode
& SELECTABLE
) == SELECTABLE
;
123 this.labelString
= labelString
;
125 this.conversation
= conversation
;
127 if (isSelectable
&& formFactory
.getSelectionProvider() != null) {
128 selectionArbitrator
= formFactory
.createSelectionArbitrator(this);
131 createControls(getLayoutComposite(), SWT
.NULL
);
136 public EntitySelectionElement(CdmFormFactory formFactory
,
137 ConversationHolder conversation
, ICdmFormElement parentElement
, Class
<T
> clazz
,
138 String labelString
, T entity
, int mode
, int style
) {
139 this(formFactory
, conversation
, parentElement
, labelString
, entity
, mode
, style
);
143 private void createControls(Composite parent
, int style
) {
145 label
= formFactory
.createLabel(getLayoutComposite(), labelString
+ " : ",
150 selectableComposite
= formFactory
.createComposite(getLayoutComposite());
160 selectableComposite
.setLayout(LayoutConstants
.LAYOUT(columns
, false));
161 selectableComposite
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY());
163 addControl(selectableComposite
);
165 text
= formFactory
.createLabel(selectableComposite
, null, SWT
.WRAP
);
168 text
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY());
169 text
.setBackground(StoreUtil
170 .getColor(Resources
.COLOR_TEXT_DISABLED_BACKGROUND
));
172 button_selection
= formFactory
.createButton(selectableComposite
, null,
174 button_selection
.setImage(ImageResources
175 .getImage(ImageResources
.BROWSE_ICON
));
176 button_selection
.setToolTipText("Browse existing");
178 addControl(button_selection
);
179 button_selection
.addSelectionListener(this);
182 button_edit
= formFactory
.createButton(selectableComposite
, null,
184 button_edit
.setImage(ImageResources
185 .getImage(ImageResources
.EDIT_ICON
));
186 button_edit
.setToolTipText("Edit");
187 addControl(button_edit
);
188 button_edit
.addSelectionListener(new EditListener(this));
192 button_remove
= formFactory
.createButton(selectableComposite
, null,
194 button_remove
.setImage(ImageResources
195 .getImage(ImageResources
.TRASH_ICON
));
196 button_remove
.setToolTipText("Remove");
197 addControl(button_remove
);
198 button_remove
.addSelectionListener(new DeleteListener(this));
203 public void widgetSelected(SelectionEvent e
) {
204 T selection
= SelectionDialogFactory
.getSelectionFromDialog(clazz
, getShell(), getConversationHolder(), getEntity());
205 setSelectionInternal(selection
);
209 * Return the selected object
211 * @return a T object.
213 public T
getSelection() {
221 * eu.etaxonomy.taxeditor.forms.IEnableableFormElement#setEnabled(boolean)
225 public void setEnabled(boolean enabled
) {
226 button_selection
.setEnabled(enabled
);
228 button_edit
.setEnabled(enabled
&& entity
!= null);
234 * setSelectionInternal
240 protected void setSelectionInternal(T selection
) {
241 if (selection
!= null && !selection
.equals(this.entity
)) {
242 setEntity(selection
);
243 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
249 * Setter for the field <code>entity</code>.
255 public void setEntity(T selection
) {
256 this.entity
= selection
;
261 * Updates this elements view
263 protected void updateElement() {
264 String title
= CdmUtils
.Nz(getTitle());
265 // we have to duplicate ampersands otherwise they are treated as
266 // mnenomic (see Label.setText() documentation)
267 title
= title
.replace("&", "&&");
268 text
.setText(title
); // title can be null
270 button_edit
.setEnabled(entity
!= null);
274 public void updateFromWizard() {
276 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
284 * @return a {@link java.lang.String} object.
286 protected String
getTitle() {
288 if(entity
instanceof IIdentifiableEntity
) {
289 return ((IIdentifiableEntity
) entity
).getTitleCache();
290 } else if(entity
instanceof Group
){
291 return ((Group
) entity
).getName();
292 } else if(entity
instanceof GrantedAuthority
){
293 return ((GrantedAuthority
) entity
).getAuthority();
301 public void setSelected(boolean selected
) {
302 setBackground(selected ? SELECTED
: getPersistentBackground());
308 * @see eu.etaxonomy.taxeditor.forms.IEntityElement#getEntity()
312 * Getter for the field <code>entity</code>.
315 * @return a T object.
318 public T
getEntity() {
325 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#
326 * getSelectionArbitrator()
330 * Getter for the field <code>selectionArbitrator</code>.
333 * @return a {@link eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator}
337 public SelectionArbitrator
getSelectionArbitrator() {
338 return selectionArbitrator
;
342 * Convenient access to current shell
344 * @return a {@link org.eclipse.swt.widgets.Shell} object.
346 public Shell
getShell() {
347 return getLayoutComposite().getShell();
352 public void setIrrelevant(boolean irrelevant
) {
353 String colorId
= irrelevant ? Resources
.COLOR_COMPOSITE_IRRELEVANT
354 : Resources
.COLOR_TEXT_DISABLED_BACKGROUND
;
356 Color color
= StoreUtil
.getColor(colorId
);
357 text
.setBackground(color
);
360 private class DeleteListener
extends SelectionAdapter
{
362 private final EntitySelectionElement
<T
> selectionElement
;
364 public DeleteListener(EntitySelectionElement
<T
> selectionElement
) {
365 this.selectionElement
= selectionElement
;
369 public void widgetSelected(SelectionEvent e
) {
371 firePropertyChangeEvent(new CdmPropertyChangeEvent(
372 selectionElement
, null));
376 private class EditListener
extends SelectionAdapter
{
378 private final EntitySelectionElement
<T
> selectionElement
;
380 public EditListener(EntitySelectionElement
<T
> selectionElement
) {
381 this.selectionElement
= selectionElement
;
386 public void widgetSelected(SelectionEvent e
) {
387 WizardDialog dialog
= new WizardDialog(selectionElement
.getShell(),
388 new EditFromSelectionWizard(selectionElement
));
389 if (dialog
.open() == IStatus
.OK
) {
390 selectionElement
.updateFromWizard();
398 public void widgetDefaultSelected(SelectionEvent e
) {
403 * getConversationHolder
406 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
410 public ConversationHolder
getConversationHolder() {
416 public void setBackground(Color color
) {
417 label
.setBackground(color
);
422 public void setLabel(String labelString
) {
424 label
.setText(labelString
);
430 * Getter for the field <code>label</code>.
433 * @return a {@link java.lang.String} object.
436 public String
getLabel() {
438 return label
.getText() + " : ";
445 public void update(CdmDataChangeMap changeEvents
) {