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
.graphics
.Color
;
12 import org
.eclipse
.swt
.widgets
.Button
;
13 import org
.eclipse
.swt
.widgets
.Composite
;
14 import org
.eclipse
.swt
.widgets
.Label
;
15 import org
.eclipse
.swt
.widgets
.Shell
;
16 import org
.springframework
.security
.core
.GrantedAuthority
;
18 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
19 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
20 import eu
.etaxonomy
.cdm
.model
.common
.Group
;
21 import eu
.etaxonomy
.cdm
.model
.common
.ICdmBase
;
22 import eu
.etaxonomy
.cdm
.model
.common
.IIdentifiableEntity
;
23 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
24 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
25 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
26 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
27 import eu
.etaxonomy
.taxeditor
.ui
.campanula
.compatibility
.ICdmFormElement
;
28 import eu
.etaxonomy
.taxeditor
.ui
.campanula
.compatibility
.IEntitySelectionElement
;
29 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.SelectionDialogFactory
;
30 import eu
.etaxonomy
.taxeditor
.ui
.element
.AbstractCdmFormElement
;
31 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
32 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmPropertyChangeEvent
;
33 import eu
.etaxonomy
.taxeditor
.ui
.element
.LayoutConstants
;
34 import eu
.etaxonomy
.taxeditor
.ui
.element
.SelectionArbitrator
;
38 * Abstract AbstractSelectionElement class.
42 * @created Nov 17, 2009
46 public class EntitySelectionElement
<T
extends ICdmBase
> extends
47 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 protected Label label
;
62 protected Button button_selection
;
64 private SelectionArbitrator selectionArbitrator
;
66 protected Button button_edit
;
68 private final String labelString
;
70 private Composite selectableComposite
;
72 private Button button_remove
;
74 private final boolean isEditable
;
76 private final boolean isDeletable
;
78 private final ConversationHolder conversation
;
79 private Class
<T
> clazz
;
83 * Constructor for AbstractSelectionElement.
87 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
91 * @param parentElement
92 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
95 * a {@link java.lang.String} object.
100 * @param isSelectable
109 public EntitySelectionElement(CdmFormFactory formFactory
,
110 ConversationHolder conversation
, ICdmFormElement parentElement
,
111 String labelString
, T entity
, int mode
, int style
) {
112 super(formFactory
, parentElement
);
114 this.isEditable
= (mode
& EDITABLE
) == EDITABLE
;
115 this.isDeletable
= (mode
& DELETABLE
) == DELETABLE
;
116 boolean isSelectable
= (mode
& SELECTABLE
) == SELECTABLE
;
118 this.labelString
= labelString
;
120 this.conversation
= conversation
;
122 if (isSelectable
&& formFactory
.getSelectionProvider() != null) {
123 selectionArbitrator
= formFactory
.createSelectionArbitrator(this);
126 createControls(getLayoutComposite(), SWT
.NULL
);
131 public EntitySelectionElement(CdmFormFactory formFactory
,
132 ConversationHolder conversation
, ICdmFormElement parentElement
, Class
<T
> clazz
,
133 String labelString
, T entity
, int mode
, int style
) {
134 this(formFactory
, conversation
, parentElement
, labelString
, entity
, mode
, style
);
138 private void createControls(Composite parent
, int style
) {
140 label
= formFactory
.createLabel(getLayoutComposite(), labelString
+ " : ",
145 selectableComposite
= formFactory
.createComposite(getLayoutComposite());
155 selectableComposite
.setLayout(LayoutConstants
.LAYOUT(columns
, false));
156 selectableComposite
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY());
158 addControl(selectableComposite
);
160 text
= formFactory
.createLabel(selectableComposite
, null, SWT
.WRAP
);
163 text
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY());
164 text
.setBackground(StoreUtil
165 .getColor(Resources
.COLOR_TEXT_DISABLED_BACKGROUND
));
167 button_selection
= formFactory
.createButton(selectableComposite
, null,
169 button_selection
.setImage(ImageResources
170 .getImage(ImageResources
.BROWSE_ICON
));
171 button_selection
.setToolTipText("Browse existing");
173 addControl(button_selection
);
174 button_selection
.addSelectionListener(this);
177 button_edit
= formFactory
.createButton(selectableComposite
, null,
179 button_edit
.setImage(ImageResources
180 .getImage(ImageResources
.EDIT_ICON
));
181 button_edit
.setToolTipText("Edit");
182 addControl(button_edit
);
183 button_edit
.addSelectionListener(new EditListener(this));
187 button_remove
= formFactory
.createButton(selectableComposite
, null,
189 button_remove
.setImage(ImageResources
190 .getImage(ImageResources
.TRASH_ICON
));
191 button_remove
.setToolTipText("Remove");
192 addControl(button_remove
);
193 button_remove
.addSelectionListener(new DeleteListener(this));
198 public void widgetSelected(SelectionEvent e
) {
199 T selection
= SelectionDialogFactory
.getSelectionFromDialog(clazz
, getShell(), getConversationHolder(), getEntity());
200 setSelectionInternal(selection
);
204 * Return the selected object
206 * @return a T object.
208 public T
getSelection() {
216 * eu.etaxonomy.taxeditor.forms.IEnableableFormElement#setEnabled(boolean)
220 public void setEnabled(boolean enabled
) {
221 button_selection
.setEnabled(enabled
);
223 button_edit
.setEnabled(enabled
&& entity
!= null);
229 * setSelectionInternal
235 protected void setSelectionInternal(T selection
) {
236 if (selection
!= null && !selection
.equals(this.entity
)) {
237 setEntity(selection
);
238 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
244 * Setter for the field <code>entity</code>.
250 public void setEntity(T selection
) {
251 this.entity
= selection
;
256 * Updates this elements view
258 protected void updateElement() {
259 String title
= CdmUtils
.Nz(getTitle());
260 // we have to duplicate ampersands otherwise they are treated as
261 // mnenomic (see Label.setText() documentation)
262 title
= title
.replace("&", "&&");
263 text
.setText(title
); // title can be null
265 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() {
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
;
338 * Convenient access to current shell
340 * @return a {@link org.eclipse.swt.widgets.Shell} object.
343 public Shell
getShell() {
344 return getLayoutComposite().getShell();
349 public void setIrrelevant(boolean irrelevant
) {
350 String colorId
= irrelevant ? Resources
.COLOR_COMPOSITE_IRRELEVANT
351 : Resources
.COLOR_TEXT_DISABLED_BACKGROUND
;
353 Color color
= StoreUtil
.getColor(colorId
);
354 text
.setBackground(color
);
357 private class DeleteListener
extends SelectionAdapter
{
359 private final EntitySelectionElement
<T
> selectionElement
;
361 public DeleteListener(EntitySelectionElement
<T
> selectionElement
) {
362 this.selectionElement
= selectionElement
;
366 public void widgetSelected(SelectionEvent e
) {
368 firePropertyChangeEvent(new CdmPropertyChangeEvent(
369 selectionElement
, null));
373 private class EditListener
extends SelectionAdapter
{
375 private final EntitySelectionElement
<T
> selectionElement
;
377 public EditListener(EntitySelectionElement
<T
> selectionElement
) {
378 this.selectionElement
= selectionElement
;
383 public void widgetSelected(SelectionEvent e
) {
384 WizardDialog dialog
= new WizardDialog(selectionElement
.getShell(),
385 new EditFromSelectionWizard(selectionElement
));
386 if (dialog
.open() == IStatus
.OK
) {
387 selectionElement
.updateFromWizard();
395 public void widgetDefaultSelected(SelectionEvent e
) {
400 * getConversationHolder
403 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
407 public ConversationHolder
getConversationHolder() {
413 public void setBackground(Color color
) {
414 label
.setBackground(color
);
419 public void setLabel(String labelString
) {
421 label
.setText(labelString
);
427 * Getter for the field <code>label</code>.
430 * @return a {@link java.lang.String} object.
433 public String
getLabel() {
435 return label
.getText() + " : ";
442 public void update(CdmDataChangeMap changeEvents
) {