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
.IIdentifiableEntity
;
22 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
23 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
24 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
25 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
26 import eu
.etaxonomy
.taxeditor
.ui
.forms
.AbstractCdmFormElement
;
27 import eu
.etaxonomy
.taxeditor
.ui
.forms
.CdmFormFactory
;
28 import eu
.etaxonomy
.taxeditor
.ui
.forms
.CdmPropertyChangeEvent
;
29 import eu
.etaxonomy
.taxeditor
.ui
.forms
.ICdmFormElement
;
30 import eu
.etaxonomy
.taxeditor
.ui
.forms
.IEnableableFormElement
;
31 import eu
.etaxonomy
.taxeditor
.ui
.forms
.IEntityElement
;
32 import eu
.etaxonomy
.taxeditor
.ui
.forms
.ILabeledElement
;
33 import eu
.etaxonomy
.taxeditor
.ui
.forms
.ISelectable
;
34 import eu
.etaxonomy
.taxeditor
.ui
.forms
.ISelectableElement
;
35 import eu
.etaxonomy
.taxeditor
.ui
.forms
.SelectionArbitrator
;
39 * Abstract AbstractSelectionElement class.
43 * @created Nov 17, 2009
47 public abstract class AbstractSelectionElement
<T
> extends
48 AbstractCdmFormElement
implements SelectionListener
,
49 IEnableableFormElement
, ISelectableElement
, IEntityElement
<T
>,
50 ILabeledElement
, IConversationEnabled
, ISelectable
{
55 public static final int NOTHING
= 0; // 000
56 public static final int EDITABLE
= 1 << 0; // 001
57 public static final int DELETABLE
= 1 << 1; // 010
58 public static final int SELECTABLE
= 1 << 2; // 100
59 public static final int ALL
= (int) Math
.pow(2, 3); // 111
63 protected Label label
;
65 protected Button button_selection
;
67 private SelectionArbitrator selectionArbitrator
;
69 protected Button button_edit
;
71 private final String labelString
;
73 private Composite selectableComposite
;
75 private Button button_remove
;
77 private final boolean isEditable
;
79 private final boolean isDeletable
;
81 private final ConversationHolder conversation
;
85 * Constructor for AbstractSelectionElement.
89 * a {@link eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory}
93 * @param parentElement
94 * a {@link eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement}
97 * a {@link java.lang.String} object.
102 * @param isSelectable
111 public AbstractSelectionElement(CdmFormFactory formFactory
,
112 ConversationHolder conversation
, ICdmFormElement parentElement
,
113 String labelString
, T entity
, int mode
, int style
) {
114 super(formFactory
, parentElement
);
116 this.isEditable
= (mode
& EDITABLE
) == EDITABLE
;
117 this.isDeletable
= (mode
& DELETABLE
) == DELETABLE
;
119 this.labelString
= labelString
;
121 this.conversation
= conversation
;
123 if (((mode
& SELECTABLE
) == SELECTABLE
)
124 && formFactory
.getSelectionProvider() != null) {
125 selectionArbitrator
= formFactory
.createSelectionArbitrator(this);
128 createControls(getLayoutComposite(), SWT
.NULL
);
133 private void createControls(Composite parent
, int style
) {
135 label
= formFactory
.createLabel(getLayoutComposite(), labelString
,
140 selectableComposite
= formFactory
.createComposite(getLayoutComposite());
150 selectableComposite
.setLayout(CdmFormFactory
.LAYOUT(columns
, false));
151 selectableComposite
.setLayoutData(CdmFormFactory
.FILL_HORIZONTALLY());
153 addControl(selectableComposite
);
155 text
= formFactory
.createLabel(selectableComposite
, null, SWT
.WRAP
);
158 text
.setLayoutData(CdmFormFactory
.FILL_HORIZONTALLY());
159 text
.setBackground(StoreUtil
160 .getColor(Resources
.COLOR_TEXT_DISABLED_BACKGROUND
));
162 button_selection
= formFactory
.createButton(selectableComposite
, null,
164 button_selection
.setImage(ImageResources
165 .getImage(ImageResources
.BROWSE_ICON
));
166 button_selection
.setToolTipText("Browse existing");
167 addControl(button_selection
);
168 button_selection
.addSelectionListener(this);
171 button_edit
= formFactory
.createButton(selectableComposite
, null,
173 button_edit
.setImage(ImageResources
174 .getImage(ImageResources
.EDIT_ICON
));
175 button_edit
.setToolTipText("Edit");
176 addControl(button_edit
);
177 button_edit
.addSelectionListener(new EditListener(this));
181 button_remove
= formFactory
.createButton(selectableComposite
, null,
183 button_remove
.setImage(ImageResources
184 .getImage(ImageResources
.TRASH_ICON
));
185 button_remove
.setToolTipText("Remove");
186 addControl(button_remove
);
187 button_remove
.addSelectionListener(new DeleteListener(this));
192 * Return the selected object
194 * @return a T object.
196 public T
getSelection() {
204 * eu.etaxonomy.taxeditor.forms.IEnableableFormElement#setEnabled(boolean)
208 public void setEnabled(boolean enabled
) {
209 button_selection
.setEnabled(enabled
);
211 button_edit
.setEnabled(enabled
&& entity
!= null);
217 * setSelectionInternal
223 protected void setSelectionInternal(T selection
) {
224 if (selection
!= null && !selection
.equals(this.entity
)) {
225 setEntity(selection
);
226 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
232 * Setter for the field <code>entity</code>.
238 public void setEntity(T selection
) {
239 this.entity
= selection
;
244 * Updates this elements view
246 protected void updateElement() {
247 String title
= CdmUtils
.Nz(getTitle());
248 // we have to duplicate ampersands otherwise they are treated as
249 // mnenomic (see Label.setText() documentation)
250 title
= title
.replace("&", "&&");
251 text
.setText(title
); // title can be null
253 button_edit
.setEnabled(entity
!= null);
262 protected void updateFromWizard() {
264 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
272 * @return a {@link java.lang.String} object.
274 protected String
getTitle() {
275 if (entity
!= null && entity
instanceof IIdentifiableEntity
) {
276 return ((IIdentifiableEntity
) entity
).getTitleCache();
283 public void setSelected(boolean selected
) {
284 setBackground(selected ? SELECTED
: getPersistentBackground());
290 * @see eu.etaxonomy.taxeditor.forms.IEntityElement#getEntity()
294 * Getter for the field <code>entity</code>.
297 * @return a T object.
300 public T
getEntity() {
307 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#
308 * getSelectionArbitrator()
312 * Getter for the field <code>selectionArbitrator</code>.
315 * @return a {@link eu.etaxonomy.taxeditor.ui.forms.SelectionArbitrator}
319 public SelectionArbitrator
getSelectionArbitrator() {
320 return selectionArbitrator
;
324 * Convenient access to current shell
326 * @return a {@link org.eclipse.swt.widgets.Shell} object.
328 protected Shell
getShell() {
329 return getLayoutComposite().getShell();
334 public void setIrrelevant(boolean irrelevant
) {
335 String colorId
= irrelevant ? Resources
.COLOR_COMPOSITE_IRRELEVANT
336 : Resources
.COLOR_TEXT_DISABLED_BACKGROUND
;
338 Color color
= StoreUtil
.getColor(colorId
);
339 text
.setBackground(color
);
342 private class DeleteListener
extends SelectionAdapter
{
344 private final AbstractSelectionElement
<T
> selectionElement
;
346 public DeleteListener(AbstractSelectionElement
<T
> selectionElement
) {
347 this.selectionElement
= selectionElement
;
351 public void widgetSelected(SelectionEvent e
) {
353 firePropertyChangeEvent(new CdmPropertyChangeEvent(
354 selectionElement
, null));
358 private class EditListener
extends SelectionAdapter
{
360 private final AbstractSelectionElement
<T
> selectionElement
;
362 public EditListener(AbstractSelectionElement
<T
> selectionElement
) {
363 this.selectionElement
= selectionElement
;
368 public void widgetSelected(SelectionEvent e
) {
369 WizardDialog dialog
= new WizardDialog(selectionElement
.getShell(),
370 new EditFromSelectionWizard(selectionElement
));
371 if (dialog
.open() == IStatus
.OK
) {
372 selectionElement
.updateFromWizard();
380 public void widgetDefaultSelected(SelectionEvent e
) {
385 * getConversationHolder
388 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
392 public ConversationHolder
getConversationHolder() {
398 public void setBackground(Color color
) {
399 label
.setBackground(color
);
404 public void setLabel(String labelString
) {
406 label
.setText(labelString
);
412 * Getter for the field <code>label</code>.
415 * @return a {@link java.lang.String} object.
418 public String
getLabel() {
420 return label
.getText();
427 public void update(CdmDataChangeMap changeEvents
) {