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
;
18 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
19 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
20 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
21 import eu
.etaxonomy
.cdm
.model
.common
.ICdmBase
;
22 import eu
.etaxonomy
.cdm
.model
.common
.IIdentifiableEntity
;
23 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
24 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
25 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
26 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
27 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.SelectionDialogFactory
;
28 import eu
.etaxonomy
.taxeditor
.ui
.element
.AbstractCdmFormElement
;
29 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
30 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmPropertyChangeEvent
;
31 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
32 import eu
.etaxonomy
.taxeditor
.ui
.element
.IEnableableFormElement
;
33 import eu
.etaxonomy
.taxeditor
.ui
.element
.IEntityElement
;
34 import eu
.etaxonomy
.taxeditor
.ui
.element
.ILabeledElement
;
35 import eu
.etaxonomy
.taxeditor
.ui
.element
.ISelectable
;
36 import eu
.etaxonomy
.taxeditor
.ui
.element
.ISelectableElement
;
37 import eu
.etaxonomy
.taxeditor
.ui
.element
.LayoutConstants
;
38 import eu
.etaxonomy
.taxeditor
.ui
.element
.SelectionArbitrator
;
42 * Abstract AbstractSelectionElement class.
46 * @created Nov 17, 2009
50 public class EntitySelectionElement
<T
extends ICdmBase
> extends
51 AbstractCdmFormElement
implements SelectionListener
,
52 IEnableableFormElement
, ISelectableElement
, IEntityElement
<T
>,
53 ILabeledElement
, IConversationEnabled
, ISelectable
{
56 * Bitmask for configuring functionality of selection element
58 public static final int NOTHING
= 0; // 000
59 public static final int EDITABLE
= 1 << 0; // 001
60 public static final int DELETABLE
= 1 << 1; // 010
61 public static final int SELECTABLE
= 1 << 2; // 100
62 public static final int ALL
= EDITABLE
| DELETABLE
| SELECTABLE
; // 111
66 protected Label label
;
68 protected Button button_selection
;
70 private SelectionArbitrator selectionArbitrator
;
72 protected Button button_edit
;
74 private final String labelString
;
76 private Composite selectableComposite
;
78 private Button button_remove
;
80 private final boolean isEditable
;
82 private final boolean isDeletable
;
84 private final ConversationHolder conversation
;
85 private Class
<T
> clazz
;
89 * Constructor for AbstractSelectionElement.
93 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
97 * @param parentElement
98 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
101 * a {@link java.lang.String} object.
106 * @param isSelectable
115 public EntitySelectionElement(CdmFormFactory formFactory
,
116 ConversationHolder conversation
, ICdmFormElement parentElement
,
117 String labelString
, T entity
, int mode
, int style
) {
118 super(formFactory
, parentElement
);
120 this.isEditable
= (mode
& EDITABLE
) == EDITABLE
;
121 this.isDeletable
= (mode
& DELETABLE
) == DELETABLE
;
122 boolean isSelectable
= (mode
& SELECTABLE
) == SELECTABLE
;
124 this.labelString
= labelString
;
126 this.conversation
= conversation
;
128 if (isSelectable
&& formFactory
.getSelectionProvider() != null) {
129 selectionArbitrator
= formFactory
.createSelectionArbitrator(this);
132 createControls(getLayoutComposite(), SWT
.NULL
);
137 public EntitySelectionElement(CdmFormFactory formFactory
,
138 ConversationHolder conversation
, ICdmFormElement parentElement
, Class
<T
> clazz
,
139 String labelString
, T entity
, int mode
, int style
) {
140 this(formFactory
, conversation
, parentElement
, labelString
, entity
, mode
, style
);
144 private void createControls(Composite parent
, int style
) {
146 label
= formFactory
.createLabel(getLayoutComposite(), labelString
,
151 selectableComposite
= formFactory
.createComposite(getLayoutComposite());
161 selectableComposite
.setLayout(LayoutConstants
.LAYOUT(columns
, false));
162 selectableComposite
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY());
164 addControl(selectableComposite
);
166 text
= formFactory
.createLabel(selectableComposite
, null, SWT
.WRAP
);
169 text
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY());
170 text
.setBackground(StoreUtil
171 .getColor(Resources
.COLOR_TEXT_DISABLED_BACKGROUND
));
173 button_selection
= formFactory
.createButton(selectableComposite
, null,
175 button_selection
.setImage(ImageResources
176 .getImage(ImageResources
.BROWSE_ICON
));
177 button_selection
.setToolTipText("Browse existing");
179 addControl(button_selection
);
180 button_selection
.addSelectionListener(this);
183 button_edit
= formFactory
.createButton(selectableComposite
, null,
185 button_edit
.setImage(ImageResources
186 .getImage(ImageResources
.EDIT_ICON
));
187 button_edit
.setToolTipText("Edit");
188 addControl(button_edit
);
189 button_edit
.addSelectionListener(new EditListener(this));
193 button_remove
= formFactory
.createButton(selectableComposite
, null,
195 button_remove
.setImage(ImageResources
196 .getImage(ImageResources
.TRASH_ICON
));
197 button_remove
.setToolTipText("Remove");
198 addControl(button_remove
);
199 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);
279 protected void updateFromWizard() {
281 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
289 * @return a {@link java.lang.String} object.
291 protected String
getTitle() {
292 if (entity
!= null && entity
instanceof IIdentifiableEntity
) {
293 return ((IIdentifiableEntity
) entity
).getTitleCache();
300 public void setSelected(boolean selected
) {
301 setBackground(selected ? SELECTED
: getPersistentBackground());
307 * @see eu.etaxonomy.taxeditor.forms.IEntityElement#getEntity()
311 * Getter for the field <code>entity</code>.
314 * @return a T object.
317 public T
getEntity() {
324 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#
325 * getSelectionArbitrator()
329 * Getter for the field <code>selectionArbitrator</code>.
332 * @return a {@link eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator}
336 public SelectionArbitrator
getSelectionArbitrator() {
337 return selectionArbitrator
;
341 * Convenient access to current shell
343 * @return a {@link org.eclipse.swt.widgets.Shell} object.
345 protected Shell
getShell() {
346 return getLayoutComposite().getShell();
351 public void setIrrelevant(boolean irrelevant
) {
352 String colorId
= irrelevant ? Resources
.COLOR_COMPOSITE_IRRELEVANT
353 : Resources
.COLOR_TEXT_DISABLED_BACKGROUND
;
355 Color color
= StoreUtil
.getColor(colorId
);
356 text
.setBackground(color
);
359 private class DeleteListener
extends SelectionAdapter
{
361 private final EntitySelectionElement
<T
> selectionElement
;
363 public DeleteListener(EntitySelectionElement
<T
> selectionElement
) {
364 this.selectionElement
= selectionElement
;
368 public void widgetSelected(SelectionEvent e
) {
370 firePropertyChangeEvent(new CdmPropertyChangeEvent(
371 selectionElement
, null));
375 private class EditListener
extends SelectionAdapter
{
377 private final EntitySelectionElement
<T
> selectionElement
;
379 public EditListener(EntitySelectionElement
<T
> selectionElement
) {
380 this.selectionElement
= selectionElement
;
385 public void widgetSelected(SelectionEvent e
) {
386 WizardDialog dialog
= new WizardDialog(selectionElement
.getShell(),
387 new EditFromSelectionWizard(selectionElement
));
388 if (dialog
.open() == IStatus
.OK
) {
389 selectionElement
.updateFromWizard();
397 public void widgetDefaultSelected(SelectionEvent e
) {
402 * getConversationHolder
405 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
409 public ConversationHolder
getConversationHolder() {
415 public void setBackground(Color color
) {
416 label
.setBackground(color
);
421 public void setLabel(String labelString
) {
423 label
.setText(labelString
);
429 * Getter for the field <code>label</code>.
432 * @return a {@link java.lang.String} object.
435 public String
getLabel() {
437 return label
.getText();
444 public void update(CdmDataChangeMap changeEvents
) {