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
.graphics
.Color
;
17 import org
.eclipse
.swt
.widgets
.Button
;
18 import org
.eclipse
.swt
.widgets
.Composite
;
19 import org
.eclipse
.swt
.widgets
.Label
;
20 import org
.eclipse
.swt
.widgets
.Shell
;
21 import org
.springframework
.security
.core
.GrantedAuthority
;
23 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
24 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
25 import eu
.etaxonomy
.cdm
.model
.common
.Group
;
26 import eu
.etaxonomy
.cdm
.model
.common
.ICdmBase
;
27 import eu
.etaxonomy
.cdm
.model
.common
.IIdentifiableEntity
;
28 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
29 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
30 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
31 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
32 import eu
.etaxonomy
.taxeditor
.ui
.campanula
.compatibility
.ICdmFormElement
;
33 import eu
.etaxonomy
.taxeditor
.ui
.campanula
.compatibility
.IEntitySelectionElement
;
34 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.SelectionDialogFactory
;
35 import eu
.etaxonomy
.taxeditor
.ui
.element
.AbstractCdmFormElement
;
36 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
37 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmPropertyChangeEvent
;
38 import eu
.etaxonomy
.taxeditor
.ui
.element
.LayoutConstants
;
39 import eu
.etaxonomy
.taxeditor
.ui
.element
.SelectionArbitrator
;
40 import eu
.etaxonomy
.taxeditor
.ui
.selection
.EditFromSelectionWizard
;
47 public class EntitySelectionFieldController
<T
extends ICdmBase
> extends AbstractCdmFormElement
implements IEntitySelectionElement
<T
>{
50 * Bitmask for configuring functionality of selection element
52 public static final int NOTHING
= 0; // 000
53 public static final int EDITABLE
= 1 << 0; // 001
54 public static final int DELETABLE
= 1 << 1; // 010
55 public static final int SELECTABLE
= 1 << 2; // 100
56 public static final int ALL
= EDITABLE
| DELETABLE
| SELECTABLE
; // 111
60 //TODO also control "label" with this class?
61 // protected Label label;
63 protected Button button_selection
;
65 private SelectionArbitrator selectionArbitrator
;
67 protected Button button_edit
;
69 private final String labelString
;
71 private Composite selectableComposite
;
73 private Button button_remove
;
75 private final boolean isEditable
;
77 private final boolean isDeletable
;
79 private final ConversationHolder conversation
;
80 private Class
<T
> clazz
;
84 * Constructor for AbstractSelectionElement.
88 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
92 * @param parentElement
93 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
96 * a {@link java.lang.String} object.
101 * @param isSelectable
110 public EntitySelectionFieldController(EntitySelectionField entitySelectionField
, CdmFormFactory formFactory
, ConversationHolder conversation
, ICdmFormElement parentElement
, String labelString
, T entity
, int mode
) {
111 super(formFactory
, parentElement
);
113 setPropertyChangeListeners(formFactory
.getPropertyChangeListeners());
114 formFactory
.addPropertyChangeListener(this);
116 this.isEditable
= (mode
& EDITABLE
) == EDITABLE
;
117 this.isDeletable
= (mode
& DELETABLE
) == DELETABLE
;
118 boolean isSelectable
= (mode
& SELECTABLE
) == SELECTABLE
;
120 this.labelString
= labelString
;
122 this.conversation
= conversation
;
124 if (isSelectable
&& formFactory
.getSelectionProvider() != null) {
125 selectionArbitrator
= formFactory
.createSelectionArbitrator(this);
128 createControls(entitySelectionField
);
133 public EntitySelectionFieldController(EntitySelectionField entitySelectionField
, CdmFormFactory formFactory
, ConversationHolder conversation
, ICdmFormElement parentElement
, Class
<T
> clazz
, String labelString
, T entity
, int mode
) {
134 this(entitySelectionField
, formFactory
, conversation
, parentElement
, labelString
, entity
, mode
);
138 private void createControls(EntitySelectionField entitySelectionField
) {
140 // label = formFactory.createLabel(getLayoutComposite(), labelString, SWT.NULL);
142 // addControl(label);
144 selectableComposite
= entitySelectionField
;
154 selectableComposite
.setLayout(LayoutConstants
.LAYOUT(columns
, false));
155 selectableComposite
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY());
157 addControl(selectableComposite
);
159 text
= entitySelectionField
.getText();
162 text
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY());
163 text
.setBackground(AbstractUtility
.getColor(Resources
.COLOR_TEXT_DISABLED_BACKGROUND
));
165 button_selection
= entitySelectionField
.getButton_selection();
166 button_selection
.setImage(ImageResources
.getImage(ImageResources
.BROWSE_ICON
));
167 button_selection
.setToolTipText("Browse existing");
169 addControl(button_selection
);
170 button_selection
.addSelectionListener(this);
172 button_edit
= entitySelectionField
.getButton_edit();
174 addControl(button_edit
);
175 button_edit
.addSelectionListener(new EditListener(this));
178 button_edit
.setVisible(false);
181 button_remove
= entitySelectionField
.getButton_remove();
183 addControl(button_remove
);
184 button_remove
.addSelectionListener(new DeleteListener(this));
187 button_remove
.setVisible(false);
192 public void widgetSelected(SelectionEvent e
) {
193 T selection
= SelectionDialogFactory
.getSelectionFromDialog(clazz
, getShell(), getConversationHolder(), getEntity());
194 setSelectionInternal(selection
);
198 * Return the selected object
200 * @return a T object.
202 public T
getSelection() {
210 * eu.etaxonomy.taxeditor.forms.IEnableableFormElement#setEnabled(boolean)
214 public void setEnabled(boolean enabled
) {
215 button_selection
.setEnabled(enabled
);
217 button_edit
.setEnabled(enabled
&& entity
!= null);
223 * setSelectionInternal
229 protected void setSelectionInternal(T selection
) {
230 if (selection
!= null && !selection
.equals(this.entity
)) {
231 setEntity(selection
);
232 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
238 * Setter for the field <code>entity</code>.
244 public void setEntity(T selection
) {
245 this.entity
= selection
;
250 * Updates this elements view
252 protected void updateElement() {
253 String title
= CdmUtils
.Nz(getTitle());
254 // we have to duplicate ampersands otherwise they are treated as
255 // mnenomic (see Label.setText() documentation)
256 title
= title
.replace("&", "&&");
257 text
.setText(title
); // title can be null
259 button_edit
.setEnabled(entity
!= null);
264 public void updateFromWizard() {
266 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
274 * @return a {@link java.lang.String} object.
276 protected String
getTitle() {
277 if (entity
!= null) {
278 if (entity
instanceof IIdentifiableEntity
) {
279 return ((IIdentifiableEntity
) entity
).getTitleCache();
280 } else if (entity
instanceof Group
) {
281 return ((Group
) entity
).getName();
282 } else if (entity
instanceof GrantedAuthority
) {
283 return ((GrantedAuthority
) entity
).getAuthority();
291 public void setSelected(boolean selected
) {
292 setBackground(selected ? SELECTED
: getPersistentBackground());
298 * @see eu.etaxonomy.taxeditor.forms.IEntityElement#getEntity()
302 * Getter for the field <code>entity</code>.
305 * @return a T object.
308 public T
getEntity() {
315 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#
316 * getSelectionArbitrator()
320 * Getter for the field <code>selectionArbitrator</code>.
323 * @return a {@link eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator}
327 public SelectionArbitrator
getSelectionArbitrator() {
328 return selectionArbitrator
;
332 public Shell
getShell() {
333 return getLayoutComposite().getShell();
338 public void setIrrelevant(boolean irrelevant
) {
339 String colorId
= irrelevant ? Resources
.COLOR_COMPOSITE_IRRELEVANT
: Resources
.COLOR_TEXT_DISABLED_BACKGROUND
;
341 Color color
= AbstractUtility
.getColor(colorId
);
342 text
.setBackground(color
);
345 private class DeleteListener
extends SelectionAdapter
{
347 private final EntitySelectionFieldController
<T
> selectionElement
;
349 public DeleteListener(EntitySelectionFieldController
<T
> selectionElement
) {
350 this.selectionElement
= selectionElement
;
354 public void widgetSelected(SelectionEvent e
) {
356 firePropertyChangeEvent(new CdmPropertyChangeEvent(selectionElement
, null));
360 private class EditListener
extends SelectionAdapter
{
362 private final IEntitySelectionElement
<T
> selectionElement
;
364 public EditListener(IEntitySelectionElement
<T
> selectionElement
) {
365 this.selectionElement
= selectionElement
;
370 public void widgetSelected(SelectionEvent e
) {
371 WizardDialog dialog
= new WizardDialog(selectionElement
.getShell(), 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
) {
406 // if (label != null) {
407 // label.setText(labelString);
413 * Getter for the field <code>label</code>.
416 * @return a {@link java.lang.String} object.
419 public String
getLabel() {
420 // if (label != null) {
421 // return label.getText();
428 public void update(CdmDataChangeMap changeEvents
) {