reintegrated model changes from branch 3.3-MC-SNAPSHOT
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / selection / EntitySelectionElement.java
1 /**
2 *
3 */
4 package eu.etaxonomy.taxeditor.ui.selection;
5
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;
17 import org.springframework.security.core.GrantedAuthority;
18
19 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
20 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
21 import eu.etaxonomy.cdm.common.CdmUtils;
22 import eu.etaxonomy.cdm.model.common.Group;
23 import eu.etaxonomy.cdm.model.common.ICdmBase;
24 import eu.etaxonomy.cdm.model.common.IIdentifiableEntity;
25 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
26 import eu.etaxonomy.taxeditor.model.ImageResources;
27 import eu.etaxonomy.taxeditor.preference.Resources;
28 import eu.etaxonomy.taxeditor.store.StoreUtil;
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.ICdmFormElement;
34 import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
35 import eu.etaxonomy.taxeditor.ui.element.IEntityElement;
36 import eu.etaxonomy.taxeditor.ui.element.ILabeledElement;
37 import eu.etaxonomy.taxeditor.ui.element.ISelectable;
38 import eu.etaxonomy.taxeditor.ui.element.ISelectableElement;
39 import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
40 import eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator;
41
42 /**
43 * <p>
44 * Abstract AbstractSelectionElement class.
45 * </p>
46 *
47 * @author n.hoffmann
48 * @created Nov 17, 2009
49 * @version 1.0
50 * @param <T>
51 */
52 public class EntitySelectionElement<T extends ICdmBase> extends
53 AbstractCdmFormElement implements SelectionListener,
54 IEnableableFormElement, ISelectableElement, IEntityElement<T>,
55 ILabeledElement, IConversationEnabled, ISelectable {
56
57 /**
58 * Bitmask for configuring functionality of selection element
59 */
60 public static final int NOTHING = 0; // 000
61 public static final int EDITABLE = 1 << 0; // 001
62 public static final int DELETABLE = 1 << 1; // 010
63 public static final int SELECTABLE = 1 << 2; // 100
64 public static final int ALL = EDITABLE | DELETABLE | SELECTABLE; // 111
65
66 protected T entity;
67
68 protected Label label;
69 protected Label text;
70 protected Button button_selection;
71
72 private SelectionArbitrator selectionArbitrator;
73
74 protected Button button_edit;
75
76 private final String labelString;
77
78 private Composite selectableComposite;
79
80 private Button button_remove;
81
82 private final boolean isEditable;
83
84 private final boolean isDeletable;
85
86 private final ConversationHolder conversation;
87 private Class<T> clazz;
88
89 /**
90 * <p>
91 * Constructor for AbstractSelectionElement.
92 * </p>
93 *
94 * @param formFactory
95 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
96 * object.
97 * @param conversation
98 * TODO
99 * @param parentElement
100 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
101 * object.
102 * @param labelString
103 * a {@link java.lang.String} object.
104 * @param entity
105 * a T object.
106 * @param isEditable
107 * a boolean.
108 * @param isSelectable
109 * a boolean.
110 * @param isDeletable
111 * a boolean.
112 * @param style
113 * a int.
114 * @param <T>
115 * a T object.
116 */
117 public EntitySelectionElement(CdmFormFactory formFactory,
118 ConversationHolder conversation, ICdmFormElement parentElement,
119 String labelString, T entity, int mode, int style) {
120 super(formFactory, parentElement);
121
122 this.isEditable = (mode & EDITABLE) == EDITABLE;
123 this.isDeletable = (mode & DELETABLE) == DELETABLE;
124 boolean isSelectable = (mode & SELECTABLE) == SELECTABLE;
125
126 this.labelString = labelString;
127
128 this.conversation = conversation;
129
130 if (isSelectable && formFactory.getSelectionProvider() != null) {
131 selectionArbitrator = formFactory.createSelectionArbitrator(this);
132 }
133
134 createControls(getLayoutComposite(), SWT.NULL);
135
136 setEntity(entity);
137 }
138
139 public EntitySelectionElement(CdmFormFactory formFactory,
140 ConversationHolder conversation, ICdmFormElement parentElement, Class<T> clazz,
141 String labelString, T entity, int mode, int style) {
142 this(formFactory, conversation, parentElement, labelString, entity, mode, style);
143 this.clazz = clazz;
144 }
145
146 private void createControls(Composite parent, int style) {
147
148 label = formFactory.createLabel(getLayoutComposite(), labelString + " : ",
149 SWT.NULL);
150
151 addControl(label);
152
153 selectableComposite = formFactory.createComposite(getLayoutComposite());
154
155 int columns = 2;
156 if (isEditable) {
157 columns += 1;
158 }
159 if (isDeletable) {
160 columns += 1;
161 }
162
163 selectableComposite.setLayout(LayoutConstants.LAYOUT(columns, false));
164 selectableComposite.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
165
166 addControl(selectableComposite);
167
168 text = formFactory.createLabel(selectableComposite, null, SWT.WRAP);
169 addControl(text);
170
171 text.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
172 text.setBackground(StoreUtil
173 .getColor(Resources.COLOR_TEXT_DISABLED_BACKGROUND));
174
175 button_selection = formFactory.createButton(selectableComposite, null,
176 SWT.PUSH);
177 button_selection.setImage(ImageResources
178 .getImage(ImageResources.BROWSE_ICON));
179 button_selection.setToolTipText("Browse existing");
180
181 addControl(button_selection);
182 button_selection.addSelectionListener(this);
183
184 if (isEditable) {
185 button_edit = formFactory.createButton(selectableComposite, null,
186 SWT.PUSH);
187 button_edit.setImage(ImageResources
188 .getImage(ImageResources.EDIT_ICON));
189 button_edit.setToolTipText("Edit");
190 addControl(button_edit);
191 button_edit.addSelectionListener(new EditListener(this));
192 }
193
194 if (isDeletable) {
195 button_remove = formFactory.createButton(selectableComposite, null,
196 SWT.PUSH);
197 button_remove.setImage(ImageResources
198 .getImage(ImageResources.TRASH_ICON));
199 button_remove.setToolTipText("Remove");
200 addControl(button_remove);
201 button_remove.addSelectionListener(new DeleteListener(this));
202 }
203 }
204
205 public void widgetSelected(SelectionEvent e) {
206 T selection = SelectionDialogFactory.getSelectionFromDialog(clazz, getShell(), getConversationHolder(), getEntity());
207 setSelectionInternal(selection);
208 }
209
210 /**
211 * Return the selected object
212 *
213 * @return a T object.
214 */
215 public T getSelection() {
216 return entity;
217 }
218
219 /*
220 * (non-Javadoc)
221 *
222 * @see
223 * eu.etaxonomy.taxeditor.forms.IEnableableFormElement#setEnabled(boolean)
224 */
225 /** {@inheritDoc} */
226 @Override
227 public void setEnabled(boolean enabled) {
228 button_selection.setEnabled(enabled);
229 if (isEditable) {
230 button_edit.setEnabled(enabled && entity != null);
231 }
232 }
233
234 /**
235 * <p>
236 * setSelectionInternal
237 * </p>
238 *
239 * @param selection
240 * a T object.
241 */
242 protected void setSelectionInternal(T selection) {
243 if (selection != null && !selection.equals(this.entity)) {
244 setEntity(selection);
245 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
246 }
247 }
248
249 /**
250 * <p>
251 * Setter for the field <code>entity</code>.
252 * </p>
253 *
254 * @param selection
255 * a T object.
256 */
257 public void setEntity(T selection) {
258 this.entity = selection;
259 updateElement();
260 }
261
262 /**
263 * Updates this elements view
264 */
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
271 if (isEditable) {
272 button_edit.setEnabled(entity != null);
273 }
274 }
275
276 /**
277 * <p>
278 * updateFromWizard
279 * </p>
280 */
281 protected void updateFromWizard() {
282 updateElement();
283 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
284 }
285
286 /**
287 * <p>
288 * getTitle
289 * </p>
290 *
291 * @return a {@link java.lang.String} object.
292 */
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();
301 }
302 }
303 return "";
304 }
305
306 /** {@inheritDoc} */
307 @Override
308 public void setSelected(boolean selected) {
309 setBackground(selected ? SELECTED : getPersistentBackground());
310 }
311
312 /*
313 * (non-Javadoc)
314 *
315 * @see eu.etaxonomy.taxeditor.forms.IEntityElement#getEntity()
316 */
317 /**
318 * <p>
319 * Getter for the field <code>entity</code>.
320 * </p>
321 *
322 * @return a T object.
323 */
324 @Override
325 public T getEntity() {
326 return entity;
327 }
328
329 /*
330 * (non-Javadoc)
331 *
332 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#
333 * getSelectionArbitrator()
334 */
335 /**
336 * <p>
337 * Getter for the field <code>selectionArbitrator</code>.
338 * </p>
339 *
340 * @return a {@link eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator}
341 * object.
342 */
343 @Override
344 public SelectionArbitrator getSelectionArbitrator() {
345 return selectionArbitrator;
346 }
347
348 /**
349 * Convenient access to current shell
350 *
351 * @return a {@link org.eclipse.swt.widgets.Shell} object.
352 */
353 protected Shell getShell() {
354 return getLayoutComposite().getShell();
355 }
356
357 /** {@inheritDoc} */
358 @Override
359 public void setIrrelevant(boolean irrelevant) {
360 String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT
361 : Resources.COLOR_TEXT_DISABLED_BACKGROUND;
362
363 Color color = StoreUtil.getColor(colorId);
364 text.setBackground(color);
365 }
366
367 private class DeleteListener extends SelectionAdapter {
368
369 private final EntitySelectionElement<T> selectionElement;
370
371 public DeleteListener(EntitySelectionElement<T> selectionElement) {
372 this.selectionElement = selectionElement;
373 }
374
375 @Override
376 public void widgetSelected(SelectionEvent e) {
377 setEntity(null);
378 firePropertyChangeEvent(new CdmPropertyChangeEvent(
379 selectionElement, null));
380 }
381 }
382
383 private class EditListener extends SelectionAdapter {
384
385 private final EntitySelectionElement<T> selectionElement;
386
387 public EditListener(EntitySelectionElement<T> selectionElement) {
388 this.selectionElement = selectionElement;
389 }
390
391 /** {@inheritDoc} */
392 @Override
393 public void widgetSelected(SelectionEvent e) {
394 WizardDialog dialog = new WizardDialog(selectionElement.getShell(),
395 new EditFromSelectionWizard(selectionElement));
396 if (dialog.open() == IStatus.OK) {
397 selectionElement.updateFromWizard();
398 }
399 }
400 }
401
402 // not used
403 /** {@inheritDoc} */
404 @Override
405 public void widgetDefaultSelected(SelectionEvent e) {
406 }
407
408 /**
409 * <p>
410 * getConversationHolder
411 * </p>
412 *
413 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
414 * object.
415 */
416 @Override
417 public ConversationHolder getConversationHolder() {
418 return conversation;
419 }
420
421 /** {@inheritDoc} */
422 @Override
423 public void setBackground(Color color) {
424 label.setBackground(color);
425 }
426
427 /** {@inheritDoc} */
428 @Override
429 public void setLabel(String labelString) {
430 if (label != null) {
431 label.setText(labelString);
432 }
433 }
434
435 /**
436 * <p>
437 * Getter for the field <code>label</code>.
438 * </p>
439 *
440 * @return a {@link java.lang.String} object.
441 */
442 @Override
443 public String getLabel() {
444 if (label != null) {
445 return label.getText() + " : ";
446 }
447 return null;
448 }
449
450 /** {@inheritDoc} */
451 @Override
452 public void update(CdmDataChangeMap changeEvents) {
453 }
454 }