1 package eu
.etaxonomy
.taxeditor
.ui
.selection
;
3 import org
.eclipse
.swt
.SWT
;
4 import org
.eclipse
.swt
.events
.SelectionAdapter
;
5 import org
.eclipse
.swt
.events
.SelectionEvent
;
6 import org
.eclipse
.swt
.events
.SelectionListener
;
7 import org
.eclipse
.swt
.graphics
.Color
;
8 import org
.eclipse
.swt
.widgets
.Button
;
9 import org
.eclipse
.swt
.widgets
.Composite
;
10 import org
.eclipse
.swt
.widgets
.Label
;
11 import org
.eclipse
.ui
.forms
.widgets
.FormToolkit
;
12 import org
.eclipse
.wb
.swt
.ResourceManager
;
13 import org
.eclipse
.wb
.swt
.SWTResourceManager
;
14 import org
.springframework
.security
.core
.GrantedAuthority
;
16 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
17 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
18 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
19 import eu
.etaxonomy
.cdm
.model
.common
.Group
;
20 import eu
.etaxonomy
.cdm
.model
.common
.ICdmBase
;
21 import eu
.etaxonomy
.cdm
.model
.common
.IIdentifiableEntity
;
22 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
23 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
24 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
25 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.SelectionDialogFactory
;
26 import eu
.etaxonomy
.taxeditor
.ui
.element
.AbstractCdmFormElementComposite
;
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
.ISelectableElement
;
34 import eu
.etaxonomy
.taxeditor
.ui
.element
.LayoutConstants
;
35 import eu
.etaxonomy
.taxeditor
.ui
.element
.SelectionArbitrator
;
38 * package eu.etaxonomy.taxeditor.ui.selection;
40 * import org.eclipse.core.runtime.IStatus;
44 * Abstract AbstractSelectionElement class.
48 * @created Nov 17, 2009
52 public class EntitySelectionElementComposite
<T
extends ICdmBase
> extends AbstractCdmFormElementComposite
implements SelectionListener
, IEnableableFormElement
, ISelectableElement
, IEntityElement
<T
>, ILabeledElement
, IConversationEnabled
{
55 * Bitmask for configuring functionality of selection element
57 public static final int NOTHING
= 0; // 000
58 public static final int EDITABLE
= 1 << 0; // 001
59 public static final int DELETABLE
= 1 << 1; // 010
60 public static final int SELECTABLE
= 1 << 2; // 100
61 public static final int ALL
= EDITABLE
| DELETABLE
| SELECTABLE
; // 111
65 protected Label label
;
67 protected Button button_selection
;
69 private SelectionArbitrator selectionArbitrator
;
71 protected Button button_edit
;
73 private final String labelString
;
75 private final Composite selectableComposite
;
77 private Button button_remove
;
79 private final boolean isEditable
;
81 private final boolean isDeletable
;
83 private final ConversationHolder conversation
;
84 private Class
<T
> clazz
;
88 * Constructor for AbstractSelectionElement.
92 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
96 * @param parentElement
97 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
100 * a {@link java.lang.String} object.
105 * @param isSelectable
114 public EntitySelectionElementComposite(Composite parent
, FormToolkit formFactory
, ConversationHolder conversation
, ICdmFormElement parentElement
, String labelString
, T entity
, int mode
, int style
) {
115 super(parent
, formFactory
, parentElement
, style
);
117 this.isEditable
= (mode
& EDITABLE
) == EDITABLE
;
118 this.isDeletable
= (mode
& DELETABLE
) == DELETABLE
;
119 boolean isSelectable
= (mode
& SELECTABLE
) == SELECTABLE
;
121 this.labelString
= labelString
;
123 this.conversation
= conversation
;
125 if (isSelectable
&& formFactory
instanceof CdmFormFactory
&& ((CdmFormFactory
) formFactory
).getSelectionProvider() != null) {
126 selectionArbitrator
= ((CdmFormFactory
) formFactory
).createSelectionArbitrator(this);
129 //only for WindowBuilder
130 Composite parentComposite
= this;
131 if(parentElement
!=null){
132 parentComposite
= parentElement
.getLayoutComposite();
135 label
= formFactory
.createLabel(parentComposite
, labelString
, SWT
.NULL
);
139 selectableComposite
= formFactory
.createComposite(parentComposite
, style
);
149 selectableComposite
.setLayout(LayoutConstants
.LAYOUT(columns
, false));
150 selectableComposite
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY());
152 addControl(selectableComposite
);
154 text
= formFactory
.createLabel(selectableComposite
, null, SWT
.WRAP
);
157 text
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY());
159 button_selection
= formFactory
.createButton(selectableComposite
, null, SWT
.PUSH
);
160 button_selection
.setImage(ResourceManager
.getPluginImage("eu.etaxonomy.taxeditor.store", "icons/prj_obj.gif"));
161 button_selection
.setToolTipText("Browse existing");
163 addControl(button_selection
);
164 button_selection
.addSelectionListener(this);
167 button_edit
= formFactory
.createButton(selectableComposite
, null, SWT
.PUSH
);
168 button_edit
.setImage(ResourceManager
.getPluginImage("eu.etaxonomy.taxeditor.store", "icons/text.gif"));
169 button_edit
.setToolTipText("Edit");
170 addControl(button_edit
);
171 button_edit
.addSelectionListener(new EditListener(this));
175 button_remove
= formFactory
.createButton(selectableComposite
, null, SWT
.PUSH
);
176 button_remove
.setImage(ResourceManager
.getPluginImage("eu.etaxonomy.taxeditor.store", "icons/trash.gif"));
177 button_remove
.setToolTipText("Remove");
178 addControl(button_remove
);
179 button_remove
.addSelectionListener(new DeleteListener(this));
189 * @param conversation
190 * @param parentElement
196 * @wbp.parser.constructor
198 public EntitySelectionElementComposite(Composite parent
, FormToolkit formFactory
, ConversationHolder conversation
, ICdmFormElement parentElement
, Class
<T
> clazz
, String labelString
, T entity
, int mode
, int style
) {
199 this(parent
, formFactory
, conversation
, parentElement
, labelString
, entity
, mode
, style
);
200 setBackground(SWTResourceManager
.getColor(SWT
.COLOR_WHITE
));
205 public void widgetSelected(SelectionEvent e
) {
206 T selection
= SelectionDialogFactory
.getSelectionFromDialog(clazz
, getShell(), getConversationHolder(), getEntity());
207 setSelectionInternal(selection
);
211 * Return the selected object
213 * @return a T object.
215 public T
getSelection() {
223 * eu.etaxonomy.taxeditor.forms.IEnableableFormElement#setEnabled(boolean)
227 public void setEnabled(boolean enabled
) {
228 button_selection
.setEnabled(enabled
);
230 button_edit
.setEnabled(enabled
&& entity
!= null);
236 * setSelectionInternal
242 protected void setSelectionInternal(T selection
) {
243 if (selection
!= null && !selection
.equals(this.entity
)) {
244 setEntity(selection
);
245 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
251 * Setter for the field <code>entity</code>.
257 public void setEntity(T selection
) {
258 this.entity
= selection
;
263 * Updates this elements view
265 protected void updateElement() {
266 String title
= CdmUtils
.Nz(getTitle());
267 // we have to duplicate ampersands otherwise they are treated as
268 // mnenomic (see Label.setText() documentation)
269 title
= title
.replace("&", "&&");
270 text
.setText(title
); // title can be null
272 button_edit
.setEnabled(entity
!= null);
281 protected void updateFromWizard() {
283 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
291 * @return a {@link java.lang.String} object.
293 protected String
getTitle() {
294 if (entity
!= null) {
295 if (entity
instanceof IIdentifiableEntity
) {
296 return ((IIdentifiableEntity
) entity
).getTitleCache();
297 } else if (entity
instanceof Group
) {
298 return ((Group
) entity
).getName();
299 } else if (entity
instanceof GrantedAuthority
) {
300 return ((GrantedAuthority
) entity
).getAuthority();
308 public void setSelected(boolean selected
) {
309 setBackground(selected ? SELECTED
: getPersistentBackground());
315 * @see eu.etaxonomy.taxeditor.forms.IEntityElement#getEntity()
319 * Getter for the field <code>entity</code>.
322 * @return a T object.
325 public T
getEntity() {
332 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#
333 * getSelectionArbitrator()
337 * Getter for the field <code>selectionArbitrator</code>.
340 * @return a {@link eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator}
344 public SelectionArbitrator
getSelectionArbitrator() {
345 return selectionArbitrator
;
348 // TODO is method necessary
350 // * Convenient access to current shell
352 // * @return a {@link org.eclipse.swt.widgets.Shell} object.
355 // public Shell getShell() {
356 // return getLayoutComposite().getShell();
361 public void setIrrelevant(boolean irrelevant
) {
362 String colorId
= irrelevant ? Resources
.COLOR_COMPOSITE_IRRELEVANT
: Resources
.COLOR_TEXT_DISABLED_BACKGROUND
;
364 Color color
= AbstractUtility
.getColor(colorId
);
365 text
.setBackground(color
);
368 private class DeleteListener
extends SelectionAdapter
{
370 private final EntitySelectionElementComposite
<T
> selectionElement
;
372 public DeleteListener(EntitySelectionElementComposite
<T
> selectionElement
) {
373 this.selectionElement
= selectionElement
;
377 public void widgetSelected(SelectionEvent e
) {
379 firePropertyChangeEvent(new CdmPropertyChangeEvent(selectionElement
, null));
383 private class EditListener
extends SelectionAdapter
{
385 private final EntitySelectionElementComposite
<T
> selectionElement
;
387 public EditListener(EntitySelectionElementComposite
<T
> selectionElement
) {
388 this.selectionElement
= selectionElement
;
393 public void widgetSelected(SelectionEvent e
) {
395 // WizardDialog dialog = new WizardDialog(selectionElement.getShell(), new EditFromSelectionWizard(selectionElement));
396 // if (dialog.open() == IStatus.OK) {
397 // selectionElement.updateFromWizard();
405 public void widgetDefaultSelected(SelectionEvent e
) {
410 * getConversationHolder
413 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
417 public ConversationHolder
getConversationHolder() {
423 public void setBackground(Color color
) {
424 if(label
!=null && color
!=null){
425 label
.setBackground(color
);
431 public void setLabel(String labelString
) {
433 label
.setText(labelString
);
439 * Getter for the field <code>label</code>.
442 * @return a {@link java.lang.String} object.
445 public String
getLabel() {
447 return label
.getText();
454 public void update(CdmDataChangeMap changeEvents
) {