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
.ISelectableElement
;
34 import eu
.etaxonomy
.taxeditor
.ui
.forms
.SelectionArbitrator
;
38 * Abstract AbstractSelectionElement class.
42 * @created Nov 17, 2009
46 public abstract class AbstractSelectionElement
<T
> extends
47 AbstractCdmFormElement
implements SelectionListener
,
48 IEnableableFormElement
, ISelectableElement
, IEntityElement
<T
>,
49 ILabeledElement
, IConversationEnabled
{
54 public static final int NOTHING
= 0; // 000
55 public static final int EDITABLE
= 1 << 0; // 001
56 public static final int DELETABLE
= 1 << 1; // 010
57 public static final int SELECTABLE
= 1 << 2; // 100
58 public static final int ALL
= (int) Math
.pow(2, 3); // 111
62 protected Label label
;
64 protected Button button_selection
;
66 private SelectionArbitrator selectionArbitrator
;
68 protected Button button_edit
;
70 private final String labelString
;
72 private Composite selectableComposite
;
74 private Button button_remove
;
76 private final boolean isEditable
;
78 private final boolean isDeletable
;
80 private final ConversationHolder conversation
;
84 * Constructor for AbstractSelectionElement.
88 * a {@link eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory}
92 * @param parentElement
93 * a {@link eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement}
96 * a {@link java.lang.String} object.
101 * @param isSelectable
110 public AbstractSelectionElement(CdmFormFactory formFactory
,
111 ConversationHolder conversation
, ICdmFormElement parentElement
,
112 String labelString
, T entity
, int mode
, int style
) {
113 super(formFactory
, parentElement
);
115 this.isEditable
= (mode
& EDITABLE
) == EDITABLE
;
116 this.isDeletable
= (mode
& DELETABLE
) == DELETABLE
;
118 this.labelString
= labelString
;
120 this.conversation
= conversation
;
122 if (((mode
& SELECTABLE
) == SELECTABLE
)
123 && formFactory
.getSelectionProvider() != null) {
124 selectionArbitrator
= formFactory
.createSelectionArbitrator(this);
127 createControls(getLayoutComposite(), SWT
.NULL
);
132 private void createControls(Composite parent
, int style
) {
134 label
= formFactory
.createLabel(getLayoutComposite(), labelString
,
139 selectableComposite
= formFactory
.createComposite(getLayoutComposite());
149 selectableComposite
.setLayout(CdmFormFactory
.LAYOUT(columns
, false));
150 selectableComposite
.setLayoutData(CdmFormFactory
.FILL_HORIZONTALLY());
152 addControl(selectableComposite
);
154 text
= formFactory
.createLabel(selectableComposite
, null, SWT
.WRAP
);
157 text
.setLayoutData(CdmFormFactory
.FILL_HORIZONTALLY());
158 text
.setBackground(StoreUtil
159 .getColor(Resources
.COLOR_TEXT_DISABLED_BACKGROUND
));
161 button_selection
= formFactory
.createButton(selectableComposite
, null,
163 button_selection
.setImage(ImageResources
164 .getImage(ImageResources
.BROWSE_ICON
));
165 button_selection
.setToolTipText("Browse existing");
166 addControl(button_selection
);
167 button_selection
.addSelectionListener(this);
170 button_edit
= formFactory
.createButton(selectableComposite
, null,
172 button_edit
.setImage(ImageResources
173 .getImage(ImageResources
.EDIT_ICON
));
174 button_edit
.setToolTipText("Edit");
175 addControl(button_edit
);
176 button_edit
.addSelectionListener(new EditListener(this));
180 button_remove
= formFactory
.createButton(selectableComposite
, null,
182 button_remove
.setImage(ImageResources
183 .getImage(ImageResources
.TRASH_ICON
));
184 button_remove
.setToolTipText("Remove");
185 addControl(button_remove
);
186 button_remove
.addSelectionListener(new DeleteListener(this));
191 * Return the selected object
193 * @return a T object.
195 public T
getSelection() {
203 * eu.etaxonomy.taxeditor.forms.IEnableableFormElement#setEnabled(boolean)
207 public void setEnabled(boolean enabled
) {
208 button_selection
.setEnabled(enabled
);
210 button_edit
.setEnabled(enabled
&& entity
!= null);
216 * setSelectionInternal
222 protected void setSelectionInternal(T selection
) {
223 if (selection
!= null && !selection
.equals(this.entity
)) {
224 setEntity(selection
);
225 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
231 * Setter for the field <code>entity</code>.
237 public void setEntity(T selection
) {
238 this.entity
= selection
;
243 * Updates this elements view
245 protected void updateElement() {
246 String title
= CdmUtils
.Nz(getTitle());
247 // we have to duplicate ampersands otherwise they are treated as
248 // mnenomic (see Label.setText() documentation)
249 title
= title
.replace("&", "&&");
250 text
.setText(title
); // title can be null
252 button_edit
.setEnabled(entity
!= null);
261 protected void updateFromWizard() {
263 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
271 * @return a {@link java.lang.String} object.
273 protected String
getTitle() {
274 if (entity
!= null && entity
instanceof IIdentifiableEntity
) {
275 return ((IIdentifiableEntity
) entity
).getTitleCache();
282 public void setSelected(boolean selected
) {
283 Color color
= getColor(selected
);
284 getLayoutComposite().setBackground(color
);
285 selectableComposite
.setBackground(color
);
286 label
.setBackground(color
);
292 * @see eu.etaxonomy.taxeditor.forms.IEntityElement#getEntity()
296 * Getter for the field <code>entity</code>.
299 * @return a T object.
302 public T
getEntity() {
309 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#
310 * getSelectionArbitrator()
314 * Getter for the field <code>selectionArbitrator</code>.
317 * @return a {@link eu.etaxonomy.taxeditor.ui.forms.SelectionArbitrator}
321 public SelectionArbitrator
getSelectionArbitrator() {
322 return selectionArbitrator
;
326 * Convenient access to current shell
328 * @return a {@link org.eclipse.swt.widgets.Shell} object.
330 protected Shell
getShell() {
331 return getLayoutComposite().getShell();
336 public void setIrrelevant(boolean irrelevant
) {
337 String colorId
= irrelevant ? Resources
.COLOR_COMPOSITE_IRRELEVANT
338 : Resources
.COLOR_TEXT_DISABLED_BACKGROUND
;
340 Color color
= StoreUtil
.getColor(colorId
);
341 text
.setBackground(color
);
344 private class DeleteListener
extends SelectionAdapter
{
346 private final AbstractSelectionElement
<T
> selectionElement
;
348 public DeleteListener(AbstractSelectionElement
<T
> selectionElement
) {
349 this.selectionElement
= selectionElement
;
353 public void widgetSelected(SelectionEvent e
) {
355 firePropertyChangeEvent(new CdmPropertyChangeEvent(
356 selectionElement
, null));
360 private class EditListener
extends SelectionAdapter
{
362 private final AbstractSelectionElement
<T
> selectionElement
;
364 public EditListener(AbstractSelectionElement
<T
> selectionElement
) {
365 this.selectionElement
= selectionElement
;
370 public void widgetSelected(SelectionEvent e
) {
371 WizardDialog dialog
= new WizardDialog(selectionElement
.getShell(),
372 new EditFromSelectionWizard(selectionElement
));
373 if (dialog
.open() == IStatus
.OK
) {
374 selectionElement
.updateFromWizard();
382 public void widgetDefaultSelected(SelectionEvent e
) {
387 * getConversationHolder
390 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
394 public ConversationHolder
getConversationHolder() {
400 public void setBackground(Color color
) {
401 label
.setBackground(color
);
406 public void setLabel(String labelString
) {
408 label
.setText(labelString
);
414 * Getter for the field <code>label</code>.
417 * @return a {@link java.lang.String} object.
420 public String
getLabel() {
422 return label
.getText();
429 public void update(CdmDataChangeMap changeEvents
) {