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