Avoid persisting data when hitting cancel in EditFromSelectionWizard
[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 java.util.EnumSet;
7 import java.util.Observable;
8 import java.util.Observer;
9
10 import org.eclipse.core.runtime.IStatus;
11 import org.eclipse.jface.wizard.WizardDialog;
12 import org.eclipse.swt.SWT;
13 import org.eclipse.swt.events.SelectionAdapter;
14 import org.eclipse.swt.events.SelectionEvent;
15 import org.eclipse.swt.events.SelectionListener;
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.eclipse.swt.widgets.Text;
22 import org.springframework.security.core.GrantedAuthority;
23
24 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
25 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
26 import eu.etaxonomy.cdm.api.service.IAgentService;
27 import eu.etaxonomy.cdm.api.service.ICollectionService;
28 import eu.etaxonomy.cdm.api.service.INameService;
29 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
30 import eu.etaxonomy.cdm.api.service.IReferenceService;
31 import eu.etaxonomy.cdm.api.service.IService;
32 import eu.etaxonomy.cdm.api.service.IUserService;
33 import eu.etaxonomy.cdm.api.service.molecular.IAmplificationService;
34 import eu.etaxonomy.cdm.api.service.molecular.IPrimerService;
35 import eu.etaxonomy.cdm.common.CdmUtils;
36 import eu.etaxonomy.cdm.model.agent.AgentBase;
37 import eu.etaxonomy.cdm.model.common.CdmBase;
38 import eu.etaxonomy.cdm.model.common.Group;
39 import eu.etaxonomy.cdm.model.common.IIdentifiableEntity;
40 import eu.etaxonomy.cdm.model.common.User;
41 import eu.etaxonomy.cdm.model.molecular.Amplification;
42 import eu.etaxonomy.cdm.model.molecular.Primer;
43 import eu.etaxonomy.cdm.model.name.NonViralName;
44 import eu.etaxonomy.cdm.model.occurrence.Collection;
45 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
46 import eu.etaxonomy.cdm.model.reference.Reference;
47 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
48 import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
49 import eu.etaxonomy.taxeditor.model.ImageResources;
50 import eu.etaxonomy.taxeditor.preference.Resources;
51 import eu.etaxonomy.taxeditor.store.CdmStore;
52 import eu.etaxonomy.taxeditor.store.LoginManager;
53 import eu.etaxonomy.taxeditor.store.StoreUtil;
54 import eu.etaxonomy.taxeditor.ui.dialog.selection.SelectionDialogFactory;
55 import eu.etaxonomy.taxeditor.ui.element.AbstractCdmFormElement;
56 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
57 import eu.etaxonomy.taxeditor.ui.element.CdmPropertyChangeEvent;
58 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
59 import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
60 import eu.etaxonomy.taxeditor.ui.element.IEntityElement;
61 import eu.etaxonomy.taxeditor.ui.element.ILabeledElement;
62 import eu.etaxonomy.taxeditor.ui.element.ISelectableElement;
63 import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
64 import eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator;
65 import eu.etaxonomy.taxeditor.ui.section.grantedAuthority.GrantedAuthorityLabelTextProvider;
66
67 /**
68 * <p>
69 * Abstract AbstractSelectionElement class.
70 * </p>
71 *
72 * @author n.hoffmann
73 * @created Nov 17, 2009
74 * @version 1.0
75 * @param <T>
76 */
77 public class EntitySelectionElement<T extends CdmBase> extends
78 AbstractCdmFormElement implements SelectionListener, IEnableableFormElement, ISelectableElement, IEntityElement<T>, ILabeledElement, IConversationEnabled, Observer {
79
80 private static final EnumSet<CRUD> UPDATE = EnumSet.of(CRUD.UPDATE);
81 private static final EnumSet<CRUD> DELETE = EnumSet.of(CRUD.DELETE);
82 private static final EnumSet<CRUD> CREATE = EnumSet.of(CRUD.CREATE);
83
84 /**
85 * Bitmask for configuring functionality of selection element
86 */
87 public static final int NOTHING = 0; // 000
88 public static final int EDITABLE = 1 << 0; // 001
89 public static final int DELETABLE = 1 << 1; // 010
90 public static final int SELECTABLE = 1 << 2; // 100
91 public static final int ALL = EDITABLE | DELETABLE | SELECTABLE; // 111
92
93 protected T entity;
94
95 protected Label label;
96 protected Text text;
97 protected Button button_selection;
98
99 private SelectionArbitrator selectionArbitrator;
100
101 protected Button button_edit;
102
103 private final String labelString;
104
105 private Composite selectableComposite;
106
107 private Button button_remove;
108
109 private final boolean isEditable;
110
111 private final boolean isDeletable;
112
113 private final ConversationHolder conversation;
114 private Class<T> clazz;
115
116 /**
117 * <p>
118 * Constructor for AbstractSelectionElement.
119 * </p>
120 *
121 * @param formFactory
122 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
123 * object.
124 * @param conversation
125 * TODO
126 * @param parentElement
127 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
128 * object.
129 * @param labelString
130 * a {@link java.lang.String} object.
131 * @param entity
132 * a T object.
133 * @param isEditable
134 * a boolean.
135 * @param isSelectable
136 * a boolean.
137 * @param isDeletable
138 * a boolean.
139 * @param style
140 * a int.
141 * @param <T>
142 * a T object.
143 */
144 public EntitySelectionElement(CdmFormFactory formFactory,
145 ConversationHolder conversation, ICdmFormElement parentElement,
146 String labelString, T entity, int mode, int style) {
147 super(formFactory, parentElement);
148
149 this.isEditable = (mode & EDITABLE) == EDITABLE;
150 this.isDeletable = (mode & DELETABLE) == DELETABLE;
151 boolean isSelectable = (mode & SELECTABLE) == SELECTABLE;
152
153 this.labelString = (labelString == null || labelString.equals("")) ? "" : labelString + " : ";
154
155 this.conversation = conversation;
156
157 if (isSelectable && formFactory.getSelectionProvider() != null) {
158 selectionArbitrator = formFactory.createSelectionArbitrator(this);
159 }
160
161 createControls(getLayoutComposite(), SWT.NULL);
162
163 setEntity(entity);
164 }
165
166 public EntitySelectionElement(CdmFormFactory formFactory,
167 ConversationHolder conversation, ICdmFormElement parentElement, Class<T> clazz,
168 String labelString, T entity, int mode, int style) {
169 this(formFactory, conversation, parentElement, labelString, entity, mode, style);
170 this.clazz = clazz;
171 }
172
173 private void createControls(Composite parent, int style) {
174
175 label = formFactory.createLabel(getLayoutComposite(), labelString,
176 SWT.NULL);
177
178 addControl(label);
179
180 selectableComposite = formFactory.createComposite(getLayoutComposite());
181
182 int columns = 2;
183 if (isEditable) {
184 columns += 1;
185 }
186 if (isDeletable) {
187 columns += 1;
188 }
189
190 selectableComposite.setLayout(LayoutConstants.LAYOUT(columns, false));
191 selectableComposite.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
192
193 addControl(selectableComposite);
194
195 text = formFactory.createText(selectableComposite, null, SWT.WRAP);
196 text.setEditable(false);
197 addControl(text);
198
199 text.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
200 text.setBackground(StoreUtil
201 .getColor(Resources.COLOR_TEXT_DISABLED_BACKGROUND));
202
203 button_selection = formFactory.createButton(selectableComposite, null,
204 SWT.PUSH);
205 button_selection.setImage(ImageResources
206 .getImage(ImageResources.BROWSE_ICON));
207 button_selection.setToolTipText("Browse existing");
208
209 addControl(button_selection);
210 button_selection.addSelectionListener(this);
211
212 if (isEditable) {
213 button_edit = formFactory.createButton(selectableComposite, null,
214 SWT.PUSH);
215 button_edit.setImage(ImageResources
216 .getImage(ImageResources.EDIT_ICON));
217 button_edit.setToolTipText("Edit");
218 addControl(button_edit);
219 button_edit.addSelectionListener(new EditListener(this));
220 }
221
222 if (isDeletable) {
223 button_remove = formFactory.createButton(selectableComposite, null,
224 SWT.PUSH);
225 button_remove.setImage(ImageResources
226 .getImage(ImageResources.TRASH_ICON));
227 button_remove.setToolTipText("Remove");
228 addControl(button_remove);
229 button_remove.addSelectionListener(new DeleteListener(this));
230 }
231 }
232
233 @Override
234 public void widgetSelected(SelectionEvent e) {
235 T selection = SelectionDialogFactory.getSelectionFromDialog(clazz, getShell(), getConversationHolder(), getEntity(), getParentElement());
236 setSelectionInternal(selection);
237 }
238
239 public static <T extends CdmBase> IService<T> getService(T cdmBase){
240 IService<T> service = null;
241 if(cdmBase!=null){
242 //get corresponding service
243 if(cdmBase.isInstanceOf(Reference.class)){
244 service = (IService<T>) CdmStore.getService(IReferenceService.class);
245 }
246 else if (cdmBase.isInstanceOf(AgentBase.class)){
247 service = (IService<T>) CdmStore.getService(IAgentService.class);
248 }
249 else if (cdmBase instanceof NonViralName) {
250 service = (IService<T>) CdmStore.getService(INameService.class);
251 }
252 else if (cdmBase instanceof SpecimenOrObservationBase) {
253 service = (IService<T>) CdmStore.getService(IOccurrenceService.class);
254 }
255 else if (cdmBase instanceof Collection) {
256 service = (IService<T>) CdmStore.getService(ICollectionService.class);
257 }
258 else if (cdmBase instanceof User) {
259 service = (IService<T>) CdmStore.getService(IUserService.class);
260 }
261 else if (cdmBase instanceof Primer) {
262 service = (IService<T>) CdmStore.getService(IPrimerService.class);
263 }
264 else if (cdmBase instanceof Amplification) {
265 service = (IService<T>) CdmStore.getService(IAmplificationService.class);
266 }
267 }
268 return service;
269 }
270
271 /**
272 * Return the selected object
273 *
274 * @return a T object.
275 */
276 public T getSelection() {
277 return entity;
278 }
279
280 /*
281 * (non-Javadoc)
282 *
283 * @see
284 * eu.etaxonomy.taxeditor.forms.IEnableableFormElement#setEnabled(boolean)
285 */
286 /** {@inheritDoc} */
287 @Override
288 public void setEnabled(boolean enabled) {
289
290 button_selection.setEnabled(enabled);
291 if (isDeletable){
292 button_remove.setEnabled(enabled);
293 }
294 if (isEditable) {
295 updateButtonStates();
296 }
297
298 }
299
300 /* (non-Javadoc)
301 * @see eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement#isEnabled()
302 */
303 @Override
304 public boolean isEnabled() {
305 return button_selection.isEnabled();
306 }
307
308 /**
309 * <p>
310 * setSelectionInternal
311 * </p>
312 *
313 * @param selection
314 * a T object.
315 */
316 protected void setSelectionInternal(T selection) {
317 if (selection != null && !selection.equals(this.entity)) {
318 setEntity(selection);
319 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
320 }
321 }
322
323 /**
324 * <p>
325 * Setter for the field <code>entity</code>.
326 * </p>
327 *
328 * @param selection
329 * a T object.
330 */
331 public void setEntity(T selection) {
332 this.entity = selection;
333 updateElement();
334 }
335
336 /**
337 * Updates this elements view
338 */
339 protected void updateElement() {
340 String title = CdmUtils.Nz(getTitle());
341 text.setText(title); // title can be null
342 if (isEditable) {
343 updateButtonStates();
344 }
345 }
346
347 public void updateFromWizard() {
348 updateElement();
349 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
350 }
351
352 /**
353 * <p>
354 * getTitle
355 * </p>
356 *
357 * @return a {@link java.lang.String} object.
358 */
359 protected String getTitle() {
360 if (entity != null){
361 if(entity instanceof Group){
362 return ((Group) entity).getName();
363 } else if(entity instanceof GrantedAuthority){
364 return GrantedAuthorityLabelTextProvider.getText(((GrantedAuthority) entity));
365 } else if(entity instanceof User){
366 return ((User) entity).getUsername();
367 } else if (entity instanceof Primer){
368 return ((Primer) entity).getLabel();
369 } else if (entity instanceof Amplification){
370 return ((Amplification) entity).getLabelCache();
371 }
372 else if(entity instanceof IIdentifiableEntity) {
373 return ((IIdentifiableEntity) entity).getTitleCache();
374 }
375
376 }
377 return "";
378 }
379
380 /** {@inheritDoc} */
381 @Override
382 public void setSelected(boolean selected) {
383 setBackground(selected ? SELECTED : getPersistentBackground());
384 }
385
386 /*
387 * (non-Javadoc)
388 *
389 * @see eu.etaxonomy.taxeditor.forms.IEntityElement#getEntity()
390 */
391 /**
392 * <p>
393 * Getter for the field <code>entity</code>.
394 * </p>
395 *
396 * @return a T object.
397 */
398 @Override
399 public T getEntity() {
400 return entity;
401 }
402
403 /*
404 * (non-Javadoc)
405 *
406 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#
407 * getSelectionArbitrator()
408 */
409 /**
410 * <p>
411 * Getter for the field <code>selectionArbitrator</code>.
412 * </p>
413 *
414 * @return a {@link eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator}
415 * object.
416 */
417 @Override
418 public SelectionArbitrator getSelectionArbitrator() {
419 return selectionArbitrator;
420 }
421
422 /**
423 * Convenient access to current shell
424 *
425 * @return a {@link org.eclipse.swt.widgets.Shell} object.
426 */
427 public Shell getShell() {
428 return getLayoutComposite().getShell();
429 }
430
431 /** {@inheritDoc} */
432 @Override
433 public void setIrrelevant(boolean irrelevant) {
434 String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT
435 : Resources.COLOR_TEXT_DISABLED_BACKGROUND;
436
437 Color color = StoreUtil.getColor(colorId);
438 text.setBackground(color);
439 }
440
441 private class DeleteListener extends SelectionAdapter {
442
443 private final EntitySelectionElement<T> selectionElement;
444
445 public DeleteListener(EntitySelectionElement<T> selectionElement) {
446 this.selectionElement = selectionElement;
447 }
448
449 @Override
450 public void widgetSelected(SelectionEvent e) {
451 setEntity(null);
452 firePropertyChangeEvent(new CdmPropertyChangeEvent(
453 selectionElement, null));
454 }
455 }
456
457 private class EditListener extends SelectionAdapter {
458
459 private final EntitySelectionElement<T> selectionElement;
460
461 public EditListener(EntitySelectionElement<T> selectionElement) {
462 this.selectionElement = selectionElement;
463 }
464
465 /** {@inheritDoc} */
466 @Override
467 public void widgetSelected(SelectionEvent e) {
468 WizardDialog dialog = new WizardDialog(selectionElement.getShell(),
469 new EditFromSelectionWizard(selectionElement));
470 if (dialog.open() == IStatus.OK) {
471
472 selectionElement.updateFromWizard();
473 }
474 }
475 }
476
477 // not used
478 /** {@inheritDoc} */
479 @Override
480 public void widgetDefaultSelected(SelectionEvent e) {
481 }
482
483 /**
484 * <p>
485 * getConversationHolder
486 * </p>
487 *
488 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
489 * object.
490 */
491 @Override
492 public ConversationHolder getConversationHolder() {
493 return conversation;
494 }
495
496 /** {@inheritDoc} */
497 @Override
498 public void setBackground(Color color) {
499 if(!label.isDisposed()){
500 label.setBackground(color);
501 }
502 }
503
504 /** {@inheritDoc} */
505 @Override
506 public void setLabel(String labelString) {
507 if (label != null) {
508 label.setText(labelString);
509 }
510 }
511
512 /**
513 * <p>
514 * Getter for the field <code>label</code>.
515 * </p>
516 *
517 * @return a {@link java.lang.String} object.
518 */
519 @Override
520 public String getLabel() {
521 if (label != null) {
522 return label.getText() + " : ";
523 }
524 return null;
525 }
526
527 /** {@inheritDoc} */
528 @Override
529 public void update(CdmDataChangeMap changeEvents) {
530 }
531
532 /* (non-Javadoc)
533 * @see eu.etaxonomy.taxeditor.ui.element.AbstractCdmFormElement#removeElements()
534 */
535 @Override
536 public void removeElements(){
537 super.removeElements();
538 LoginManager loginManager = CdmStore.getLoginManager();
539 loginManager.addObserver(this);
540 }
541
542 @Override
543 public void update(Observable o, Object arg) {
544 if(o instanceof LoginManager){
545 updateButtonStates();
546 }
547 }
548
549 private void updateButtonStates() {
550 if(button_edit != null && !button_selection.isDisposed()){
551 button_edit.setEnabled(isEditable && button_selection.isEnabled() && getEntity() != null && CdmStore.currentAuthentiationHasPermission(getEntity(), UPDATE));
552 }
553 }
554 }