3 * Copyright (C) 2013 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.ui
.campanula
.basicFields
;
12 import org
.eclipse
.core
.runtime
.IStatus
;
13 import org
.eclipse
.jface
.wizard
.WizardDialog
;
14 import org
.eclipse
.swt
.events
.SelectionAdapter
;
15 import org
.eclipse
.swt
.events
.SelectionEvent
;
16 import org
.eclipse
.swt
.events
.SelectionListener
;
17 import org
.eclipse
.swt
.graphics
.Color
;
18 import org
.eclipse
.swt
.widgets
.Button
;
19 import org
.eclipse
.swt
.widgets
.Composite
;
20 import org
.eclipse
.swt
.widgets
.Label
;
21 import org
.eclipse
.swt
.widgets
.Shell
;
22 import org
.springframework
.security
.core
.GrantedAuthority
;
24 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
25 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
26 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
27 import eu
.etaxonomy
.cdm
.model
.common
.Group
;
28 import eu
.etaxonomy
.cdm
.model
.common
.ICdmBase
;
29 import eu
.etaxonomy
.cdm
.model
.common
.IIdentifiableEntity
;
30 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
31 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
32 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
33 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
34 import eu
.etaxonomy
.taxeditor
.ui
.campanula
.compatibility
.ICdmFormElement
;
35 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.SelectionDialogFactory
;
36 import eu
.etaxonomy
.taxeditor
.ui
.element
.AbstractCdmFormElement
;
37 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
38 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmPropertyChangeEvent
;
39 import eu
.etaxonomy
.taxeditor
.ui
.element
.IEnableableFormElement
;
40 import eu
.etaxonomy
.taxeditor
.ui
.element
.IEntityElement
;
41 import eu
.etaxonomy
.taxeditor
.ui
.element
.ILabeledElement
;
42 import eu
.etaxonomy
.taxeditor
.ui
.element
.ISelectableElement
;
43 import eu
.etaxonomy
.taxeditor
.ui
.element
.LayoutConstants
;
44 import eu
.etaxonomy
.taxeditor
.ui
.element
.SelectionArbitrator
;
45 import eu
.etaxonomy
.taxeditor
.ui
.selection
.EditFromSelectionWizard
;
46 import eu
.etaxonomy
.taxeditor
.ui
.selection
.EntitySelectionElement
;
53 public class EntitySelectionFieldController
<T
extends ICdmBase
> extends AbstractCdmFormElement
implements SelectionListener
, IEnableableFormElement
, ISelectableElement
, IEntityElement
<T
>, ILabeledElement
, IConversationEnabled
{
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 //TODO also control "label" with this class?
67 // protected Label label;
69 protected Button button_selection
;
71 private SelectionArbitrator selectionArbitrator
;
73 protected Button button_edit
;
75 // private final String labelString;
77 private Composite selectableComposite
;
79 private Button button_remove
;
81 private final boolean isEditable
;
83 private final boolean isDeletable
;
85 private final ConversationHolder conversation
;
86 private Class
<T
> clazz
;
90 * Constructor for AbstractSelectionElement.
94 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
98 * @param parentElement
99 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
102 * a {@link java.lang.String} object.
107 * @param isSelectable
116 public EntitySelectionFieldController(EntitySelectionField entitySelectionField
, CdmFormFactory formFactory
, ConversationHolder conversation
, ICdmFormElement parentElement
, T entity
, int mode
) {
117 super(formFactory
, parentElement
);
119 setPropertyChangeListeners(formFactory
.getPropertyChangeListeners());
120 formFactory
.addPropertyChangeListener(this);
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(entitySelectionField
);
139 public EntitySelectionFieldController(EntitySelectionField entitySelectionField
, CdmFormFactory formFactory
, ConversationHolder conversation
, ICdmFormElement parentElement
, Class
<T
> clazz
, T entity
, int mode
) {
140 this(entitySelectionField
, formFactory
, conversation
, parentElement
, entity
, mode
);
144 private void createControls(EntitySelectionField entitySelectionField
) {
146 // label = formFactory.createLabel(getLayoutComposite(), labelString, SWT.NULL);
148 // addControl(label);
150 selectableComposite
= entitySelectionField
;
160 selectableComposite
.setLayout(LayoutConstants
.LAYOUT(columns
, false));
161 selectableComposite
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY());
163 addControl(selectableComposite
);
165 text
= entitySelectionField
.getText();
168 text
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY());
169 text
.setBackground(AbstractUtility
.getColor(Resources
.COLOR_TEXT_DISABLED_BACKGROUND
));
171 button_selection
= entitySelectionField
.getButton_selection();
172 button_selection
.setImage(ImageResources
.getImage(ImageResources
.BROWSE_ICON
));
173 button_selection
.setToolTipText("Browse existing");
175 addControl(button_selection
);
176 button_selection
.addSelectionListener(this);
178 button_edit
= entitySelectionField
.getButton_edit();
180 addControl(button_edit
);
181 //TODO: this worked with the extracted (but now reverted) interfaces done for campanula
182 // button_edit.addSelectionListener(new EditListener(this));
185 button_edit
.setVisible(false);
188 button_remove
= entitySelectionField
.getButton_remove();
190 addControl(button_remove
);
191 button_remove
.addSelectionListener(new DeleteListener(this));
194 button_remove
.setVisible(false);
199 public void widgetSelected(SelectionEvent e
) {
200 T selection
= SelectionDialogFactory
.getSelectionFromDialog(clazz
, getShell(), getConversationHolder(), getEntity());
201 setSelectionInternal(selection
);
205 * Return the selected object
207 * @return a T object.
209 public T
getSelection() {
217 * eu.etaxonomy.taxeditor.forms.IEnableableFormElement#setEnabled(boolean)
221 public void setEnabled(boolean enabled
) {
222 button_selection
.setEnabled(enabled
);
224 button_edit
.setEnabled(enabled
&& entity
!= null);
230 * setSelectionInternal
236 protected void setSelectionInternal(T selection
) {
237 if (selection
!= null && !selection
.equals(this.entity
)) {
238 setEntity(selection
);
239 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
245 * Setter for the field <code>entity</code>.
251 public void setEntity(T selection
) {
252 this.entity
= selection
;
257 * Updates this elements view
259 protected void updateElement() {
260 String title
= CdmUtils
.Nz(getTitle());
261 // we have to duplicate ampersands otherwise they are treated as
262 // mnenomic (see Label.setText() documentation)
263 title
= title
.replace("&", "&&");
264 text
.setText(title
); // title can be null
266 button_edit
.setEnabled(entity
!= null);
270 public void updateFromWizard() {
272 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
280 * @return a {@link java.lang.String} object.
282 protected String
getTitle() {
283 if (entity
!= null) {
284 if (entity
instanceof IIdentifiableEntity
) {
285 return ((IIdentifiableEntity
) entity
).getTitleCache();
286 } else if (entity
instanceof Group
) {
287 return ((Group
) entity
).getName();
288 } else if (entity
instanceof GrantedAuthority
) {
289 return ((GrantedAuthority
) entity
).getAuthority();
297 public void setSelected(boolean selected
) {
298 setBackground(selected ? SELECTED
: getPersistentBackground());
304 * @see eu.etaxonomy.taxeditor.forms.IEntityElement#getEntity()
308 * Getter for the field <code>entity</code>.
311 * @return a T object.
314 public T
getEntity() {
321 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#
322 * getSelectionArbitrator()
326 * Getter for the field <code>selectionArbitrator</code>.
329 * @return a {@link eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator}
333 public SelectionArbitrator
getSelectionArbitrator() {
334 return selectionArbitrator
;
337 public Shell
getShell() {
338 return getLayoutComposite().getShell();
343 public void setIrrelevant(boolean irrelevant
) {
344 String colorId
= irrelevant ? Resources
.COLOR_COMPOSITE_IRRELEVANT
: Resources
.COLOR_TEXT_DISABLED_BACKGROUND
;
346 Color color
= AbstractUtility
.getColor(colorId
);
347 text
.setBackground(color
);
350 private class DeleteListener
extends SelectionAdapter
{
352 private final EntitySelectionFieldController
<T
> selectionElement
;
354 public DeleteListener(EntitySelectionFieldController
<T
> selectionElement
) {
355 this.selectionElement
= selectionElement
;
359 public void widgetSelected(SelectionEvent e
) {
361 firePropertyChangeEvent(new CdmPropertyChangeEvent(selectionElement
, null));
365 private class EditListener
extends SelectionAdapter
{
367 private final EntitySelectionElement
<T
> selectionElement
;
369 public EditListener(EntitySelectionElement
<T
> selectionElement
) {
370 this.selectionElement
= selectionElement
;
375 public void widgetSelected(SelectionEvent e
) {
376 WizardDialog dialog
= new WizardDialog(selectionElement
.getShell(), new EditFromSelectionWizard(selectionElement
));
377 if (dialog
.open() == IStatus
.OK
) {
378 selectionElement
.updateFromWizard();
386 public void widgetDefaultSelected(SelectionEvent e
) {
391 * getConversationHolder
394 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
398 public ConversationHolder
getConversationHolder() {
404 public void setBackground(Color color
) {
405 // label.setBackground(color);
410 public void setLabel(String labelString
) {
411 // if (label != null) {
412 // label.setText(labelString);
418 * Getter for the field <code>label</code>.
421 * @return a {@link java.lang.String} object.
424 public String
getLabel() {
425 // if (label != null) {
426 // return label.getText();
433 public void update(CdmDataChangeMap changeEvents
) {