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
.ISelectable
;
38 import eu
.etaxonomy
.taxeditor
.ui
.element
.ISelectableElement
;
39 import eu
.etaxonomy
.taxeditor
.ui
.element
.LayoutConstants
;
40 import eu
.etaxonomy
.taxeditor
.ui
.element
.SelectionArbitrator
;
44 * Abstract AbstractSelectionElement class.
48 * @created Nov 17, 2009
52 public class EntitySelectionElement
<T
extends ICdmBase
> extends
53 AbstractCdmFormElement
implements SelectionListener
,
54 IEnableableFormElement
, ISelectableElement
, IEntityElement
<T
>,
55 ILabeledElement
, IConversationEnabled
, ISelectable
{
58 * Bitmask for configuring functionality of selection element
60 public static final int NOTHING
= 0; // 000
61 public static final int EDITABLE
= 1 << 0; // 001
62 public static final int DELETABLE
= 1 << 1; // 010
63 public static final int SELECTABLE
= 1 << 2; // 100
64 public static final int ALL
= EDITABLE
| DELETABLE
| SELECTABLE
; // 111
68 protected Label label
;
70 protected Button button_selection
;
72 private SelectionArbitrator selectionArbitrator
;
74 protected Button button_edit
;
76 private final String labelString
;
78 private Composite selectableComposite
;
80 private Button button_remove
;
82 private final boolean isEditable
;
84 private final boolean isDeletable
;
86 private final ConversationHolder conversation
;
87 private Class
<T
> clazz
;
91 * Constructor for AbstractSelectionElement.
95 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
99 * @param parentElement
100 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
103 * a {@link java.lang.String} object.
108 * @param isSelectable
117 public EntitySelectionElement(CdmFormFactory formFactory
,
118 ConversationHolder conversation
, ICdmFormElement parentElement
,
119 String labelString
, T entity
, int mode
, int style
) {
120 super(formFactory
, parentElement
);
122 this.isEditable
= (mode
& EDITABLE
) == EDITABLE
;
123 this.isDeletable
= (mode
& DELETABLE
) == DELETABLE
;
124 boolean isSelectable
= (mode
& SELECTABLE
) == SELECTABLE
;
126 this.labelString
= labelString
;
128 this.conversation
= conversation
;
130 if (isSelectable
&& formFactory
.getSelectionProvider() != null) {
131 selectionArbitrator
= formFactory
.createSelectionArbitrator(this);
134 createControls(getLayoutComposite(), SWT
.NULL
);
139 public EntitySelectionElement(CdmFormFactory formFactory
,
140 ConversationHolder conversation
, ICdmFormElement parentElement
, Class
<T
> clazz
,
141 String labelString
, T entity
, int mode
, int style
) {
142 this(formFactory
, conversation
, parentElement
, labelString
, entity
, mode
, style
);
146 private void createControls(Composite parent
, int style
) {
148 label
= formFactory
.createLabel(getLayoutComposite(), labelString
+ " : ",
153 selectableComposite
= formFactory
.createComposite(getLayoutComposite());
163 selectableComposite
.setLayout(LayoutConstants
.LAYOUT(columns
, false));
164 selectableComposite
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY());
166 addControl(selectableComposite
);
168 text
= formFactory
.createLabel(selectableComposite
, null, SWT
.WRAP
);
171 text
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY());
172 text
.setBackground(StoreUtil
173 .getColor(Resources
.COLOR_TEXT_DISABLED_BACKGROUND
));
175 button_selection
= formFactory
.createButton(selectableComposite
, null,
177 button_selection
.setImage(ImageResources
178 .getImage(ImageResources
.BROWSE_ICON
));
179 button_selection
.setToolTipText("Browse existing");
181 addControl(button_selection
);
182 button_selection
.addSelectionListener(this);
185 button_edit
= formFactory
.createButton(selectableComposite
, null,
187 button_edit
.setImage(ImageResources
188 .getImage(ImageResources
.EDIT_ICON
));
189 button_edit
.setToolTipText("Edit");
190 addControl(button_edit
);
191 button_edit
.addSelectionListener(new EditListener(this));
195 button_remove
= formFactory
.createButton(selectableComposite
, null,
197 button_remove
.setImage(ImageResources
198 .getImage(ImageResources
.TRASH_ICON
));
199 button_remove
.setToolTipText("Remove");
200 addControl(button_remove
);
201 button_remove
.addSelectionListener(new DeleteListener(this));
205 public void widgetSelected(SelectionEvent e
) {
206 T selection
= SelectionDialogFactory
.getSelectionFromDialog(clazz
, getShell(), getConversationHolder(), getEntity());
207 setSelectionInternal(selection
);
211 * Return the selected object
213 * @return a T object.
215 public T
getSelection() {
223 * eu.etaxonomy.taxeditor.forms.IEnableableFormElement#setEnabled(boolean)
227 public void setEnabled(boolean enabled
) {
228 button_selection
.setEnabled(enabled
);
230 button_edit
.setEnabled(enabled
&& entity
!= null);
236 * setSelectionInternal
242 protected void setSelectionInternal(T selection
) {
243 if (selection
!= null && !selection
.equals(this.entity
)) {
244 setEntity(selection
);
245 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
251 * Setter for the field <code>entity</code>.
257 public void setEntity(T selection
) {
258 this.entity
= selection
;
263 * Updates this elements view
265 protected void updateElement() {
266 String title
= CdmUtils
.Nz(getTitle());
267 // we have to duplicate ampersands otherwise they are treated as
268 // mnenomic (see Label.setText() documentation)
269 title
= title
.replace("&", "&&");
270 text
.setText(title
); // title can be null
272 button_edit
.setEnabled(entity
!= null);
281 protected void updateFromWizard() {
283 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
291 * @return a {@link java.lang.String} object.
293 protected String
getTitle() {
295 if(entity
instanceof IIdentifiableEntity
) {
296 return ((IIdentifiableEntity
) entity
).getTitleCache();
297 } else if(entity
instanceof Group
){
298 return ((Group
) entity
).getName();
299 } else if(entity
instanceof GrantedAuthority
){
300 return ((GrantedAuthority
) entity
).getAuthority();
308 public void setSelected(boolean selected
) {
309 setBackground(selected ? SELECTED
: getPersistentBackground());
315 * @see eu.etaxonomy.taxeditor.forms.IEntityElement#getEntity()
319 * Getter for the field <code>entity</code>.
322 * @return a T object.
325 public T
getEntity() {
332 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#
333 * getSelectionArbitrator()
337 * Getter for the field <code>selectionArbitrator</code>.
340 * @return a {@link eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator}
344 public SelectionArbitrator
getSelectionArbitrator() {
345 return selectionArbitrator
;
349 * Convenient access to current shell
351 * @return a {@link org.eclipse.swt.widgets.Shell} object.
353 protected Shell
getShell() {
354 return getLayoutComposite().getShell();
359 public void setIrrelevant(boolean irrelevant
) {
360 String colorId
= irrelevant ? Resources
.COLOR_COMPOSITE_IRRELEVANT
361 : Resources
.COLOR_TEXT_DISABLED_BACKGROUND
;
363 Color color
= StoreUtil
.getColor(colorId
);
364 text
.setBackground(color
);
367 private class DeleteListener
extends SelectionAdapter
{
369 private final EntitySelectionElement
<T
> selectionElement
;
371 public DeleteListener(EntitySelectionElement
<T
> selectionElement
) {
372 this.selectionElement
= selectionElement
;
376 public void widgetSelected(SelectionEvent e
) {
378 firePropertyChangeEvent(new CdmPropertyChangeEvent(
379 selectionElement
, null));
383 private class EditListener
extends SelectionAdapter
{
385 private final EntitySelectionElement
<T
> selectionElement
;
387 public EditListener(EntitySelectionElement
<T
> selectionElement
) {
388 this.selectionElement
= selectionElement
;
393 public void widgetSelected(SelectionEvent e
) {
394 WizardDialog dialog
= new WizardDialog(selectionElement
.getShell(),
395 new EditFromSelectionWizard(selectionElement
));
396 if (dialog
.open() == IStatus
.OK
) {
397 selectionElement
.updateFromWizard();
405 public void widgetDefaultSelected(SelectionEvent e
) {
410 * getConversationHolder
413 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
417 public ConversationHolder
getConversationHolder() {
423 public void setBackground(Color color
) {
424 label
.setBackground(color
);
429 public void setLabel(String labelString
) {
431 label
.setText(labelString
);
437 * Getter for the field <code>label</code>.
440 * @return a {@link java.lang.String} object.
443 public String
getLabel() {
445 return label
.getText() + " : ";
452 public void update(CdmDataChangeMap changeEvents
) {