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
.element
.AbstractCdmFormElement
;
27 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
28 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmPropertyChangeEvent
;
29 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
30 import eu
.etaxonomy
.taxeditor
.ui
.element
.IEnableableFormElement
;
31 import eu
.etaxonomy
.taxeditor
.ui
.element
.IEntityElement
;
32 import eu
.etaxonomy
.taxeditor
.ui
.element
.ILabeledElement
;
33 import eu
.etaxonomy
.taxeditor
.ui
.element
.ISelectable
;
34 import eu
.etaxonomy
.taxeditor
.ui
.element
.ISelectableElement
;
35 import eu
.etaxonomy
.taxeditor
.ui
.element
.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.element.CdmFormFactory}
93 * @param parentElement
94 * a {@link eu.etaxonomy.taxeditor.ui.element.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");
168 addControl(button_selection
);
169 button_selection
.addSelectionListener(this);
172 button_edit
= formFactory
.createButton(selectableComposite
, null,
174 button_edit
.setImage(ImageResources
175 .getImage(ImageResources
.EDIT_ICON
));
176 button_edit
.setToolTipText("Edit");
177 addControl(button_edit
);
178 button_edit
.addSelectionListener(new EditListener(this));
182 button_remove
= formFactory
.createButton(selectableComposite
, null,
184 button_remove
.setImage(ImageResources
185 .getImage(ImageResources
.TRASH_ICON
));
186 button_remove
.setToolTipText("Remove");
187 addControl(button_remove
);
188 button_remove
.addSelectionListener(new DeleteListener(this));
193 * Return the selected object
195 * @return a T object.
197 public T
getSelection() {
205 * eu.etaxonomy.taxeditor.forms.IEnableableFormElement#setEnabled(boolean)
209 public void setEnabled(boolean enabled
) {
210 button_selection
.setEnabled(enabled
);
212 button_edit
.setEnabled(enabled
&& entity
!= null);
218 * setSelectionInternal
224 protected void setSelectionInternal(T selection
) {
225 if (selection
!= null && !selection
.equals(this.entity
)) {
226 setEntity(selection
);
227 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
233 * Setter for the field <code>entity</code>.
239 public void setEntity(T selection
) {
240 this.entity
= selection
;
245 * Updates this elements view
247 protected void updateElement() {
248 String title
= CdmUtils
.Nz(getTitle());
249 // we have to duplicate ampersands otherwise they are treated as
250 // mnenomic (see Label.setText() documentation)
251 title
= title
.replace("&", "&&");
252 text
.setText(title
); // title can be null
254 button_edit
.setEnabled(entity
!= null);
263 protected void updateFromWizard() {
265 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
273 * @return a {@link java.lang.String} object.
275 protected String
getTitle() {
276 if (entity
!= null && entity
instanceof IIdentifiableEntity
) {
277 return ((IIdentifiableEntity
) entity
).getTitleCache();
284 public void setSelected(boolean selected
) {
285 setBackground(selected ? SELECTED
: getPersistentBackground());
291 * @see eu.etaxonomy.taxeditor.forms.IEntityElement#getEntity()
295 * Getter for the field <code>entity</code>.
298 * @return a T object.
301 public T
getEntity() {
308 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#
309 * getSelectionArbitrator()
313 * Getter for the field <code>selectionArbitrator</code>.
316 * @return a {@link eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator}
320 public SelectionArbitrator
getSelectionArbitrator() {
321 return selectionArbitrator
;
325 * Convenient access to current shell
327 * @return a {@link org.eclipse.swt.widgets.Shell} object.
329 protected Shell
getShell() {
330 return getLayoutComposite().getShell();
335 public void setIrrelevant(boolean irrelevant
) {
336 String colorId
= irrelevant ? Resources
.COLOR_COMPOSITE_IRRELEVANT
337 : Resources
.COLOR_TEXT_DISABLED_BACKGROUND
;
339 Color color
= StoreUtil
.getColor(colorId
);
340 text
.setBackground(color
);
343 private class DeleteListener
extends SelectionAdapter
{
345 private final AbstractSelectionElement
<T
> selectionElement
;
347 public DeleteListener(AbstractSelectionElement
<T
> selectionElement
) {
348 this.selectionElement
= selectionElement
;
352 public void widgetSelected(SelectionEvent e
) {
354 firePropertyChangeEvent(new CdmPropertyChangeEvent(
355 selectionElement
, null));
359 private class EditListener
extends SelectionAdapter
{
361 private final AbstractSelectionElement
<T
> selectionElement
;
363 public EditListener(AbstractSelectionElement
<T
> selectionElement
) {
364 this.selectionElement
= selectionElement
;
369 public void widgetSelected(SelectionEvent e
) {
370 WizardDialog dialog
= new WizardDialog(selectionElement
.getShell(),
371 new EditFromSelectionWizard(selectionElement
));
372 if (dialog
.open() == IStatus
.OK
) {
373 selectionElement
.updateFromWizard();
381 public void widgetDefaultSelected(SelectionEvent e
) {
386 * getConversationHolder
389 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
393 public ConversationHolder
getConversationHolder() {
399 public void setBackground(Color color
) {
400 label
.setBackground(color
);
405 public void setLabel(String labelString
) {
407 label
.setText(labelString
);
413 * Getter for the field <code>label</code>.
416 * @return a {@link java.lang.String} object.
419 public String
getLabel() {
421 return label
.getText();
428 public void update(CdmDataChangeMap changeEvents
) {