ref #6595, #6597 Optimize selection event propagation
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / name / e4 / TaxonNameEditorE4.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.editor.name.e4;
11
12 import java.util.ArrayList;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Set;
16
17 import javax.annotation.PostConstruct;
18 import javax.annotation.PreDestroy;
19 import javax.inject.Inject;
20
21 import org.apache.commons.lang.StringUtils;
22 import org.eclipse.core.commands.operations.IUndoContext;
23 import org.eclipse.core.commands.operations.UndoContext;
24 import org.eclipse.core.runtime.IProgressMonitor;
25 import org.eclipse.core.runtime.OperationCanceledException;
26 import org.eclipse.e4.core.contexts.IEclipseContext;
27 import org.eclipse.e4.core.services.events.IEventBroker;
28 import org.eclipse.e4.ui.di.Focus;
29 import org.eclipse.e4.ui.di.Persist;
30 import org.eclipse.e4.ui.model.application.ui.MDirtyable;
31 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
32 import org.eclipse.e4.ui.services.EMenuService;
33 import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
34 import org.eclipse.jface.dialogs.MessageDialog;
35 import org.eclipse.jface.viewers.ISelection;
36 import org.eclipse.jface.viewers.ISelectionProvider;
37 import org.eclipse.jface.viewers.StructuredSelection;
38 import org.eclipse.swt.dnd.DND;
39 import org.eclipse.swt.dnd.DropTarget;
40 import org.eclipse.swt.dnd.Transfer;
41 import org.eclipse.swt.graphics.Color;
42 import org.eclipse.swt.widgets.Composite;
43 import org.eclipse.ui.ISelectionListener;
44 import org.eclipse.ui.IWorkbenchPart;
45 import org.eclipse.ui.IWorkbenchPartReference;
46 import org.eclipse.ui.forms.ManagedForm;
47 import org.eclipse.ui.forms.widgets.FormToolkit;
48 import org.eclipse.ui.forms.widgets.ScrolledForm;
49 import org.eclipse.ui.forms.widgets.TableWrapLayout;
50
51 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
52 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
53 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
54 import eu.etaxonomy.cdm.model.common.CdmBase;
55 import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
56 import eu.etaxonomy.cdm.model.taxon.Taxon;
57 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
58 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
59 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
60 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
61 import eu.etaxonomy.taxeditor.editor.CdmDataTransfer;
62 import eu.etaxonomy.taxeditor.editor.ISecuredEditor;
63 import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
64 import eu.etaxonomy.taxeditor.editor.e4.TaxonEditorInputE4;
65 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
66 import eu.etaxonomy.taxeditor.editor.name.e4.container.AbstractGroupE4;
67 import eu.etaxonomy.taxeditor.editor.name.e4.container.AbstractGroupedContainerE4;
68 import eu.etaxonomy.taxeditor.editor.name.e4.container.AcceptedGroupE4;
69 import eu.etaxonomy.taxeditor.editor.name.e4.container.AcceptedNameContainerE4;
70 import eu.etaxonomy.taxeditor.editor.name.e4.container.ContainerFactoryE4;
71 import eu.etaxonomy.taxeditor.editor.name.e4.container.HomotypicalSynonymGroupE4;
72 import eu.etaxonomy.taxeditor.editor.name.e4.container.MisappliedGroupE4;
73 import eu.etaxonomy.taxeditor.editor.name.e4.dnd.NameEditorDropTargetListenerE4;
74 import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
75 import eu.etaxonomy.taxeditor.model.AbstractUtility;
76 import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
77 import eu.etaxonomy.taxeditor.model.IPartChangeListener;
78 import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
79 import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
80 import eu.etaxonomy.taxeditor.model.IPartContentHasMedia;
81 import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
82 import eu.etaxonomy.taxeditor.model.MessagingUtils;
83 import eu.etaxonomy.taxeditor.model.TaxeditorPartService;
84 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
85 import eu.etaxonomy.taxeditor.preference.Resources;
86 import eu.etaxonomy.taxeditor.security.RequiredPermissions;
87 import eu.etaxonomy.taxeditor.store.CdmStore;
88 import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
89
90 /**
91 *
92 * @author pplitzner
93 * @date Aug 24, 2017
94 *
95 */
96 public class TaxonNameEditorE4 implements IConversationEnabled, IDirtyMarkable, IPartContentHasDetails,
97 IPartContentHasSupplementalData, IPartContentHasMedia, IPartContentHasFactualData, IPartChangeListener,
98 ISelectionListener, ISecuredEditor, IPostOperationEnabled, IE4SavablePart, ITaxonEditor, IDropTargetableE4 {
99
100 private Taxon taxon;
101
102 private ManagedForm managedForm;
103 private ScrolledForm scrolledForm;
104 private Composite parent;
105 private ISelectionProvider simpleSelectionProvider;
106
107 private TaxonBase selection;
108
109 private ConversationHolder conversation;
110
111 private AcceptedGroupE4 acceptedGroup;
112 private List<HomotypicalSynonymGroupE4> heterotypicSynonymGroups = new ArrayList<>();
113 private MisappliedGroupE4 misappliedGroup;
114
115 private DropTarget target;
116
117 private TaxonBase objectAffectedByLastOperation;
118
119 @Inject
120 private EMenuService menuService;
121
122 @Inject
123 private ESelectionService selService;
124
125 @Inject
126 private IEclipseContext context;
127
128 @Inject
129 private MDirtyable dirty;
130
131 private MPart thisPart;
132
133 private TaxonEditorInputE4 input;
134
135 private UndoContext undoContext;
136
137 @Inject
138 private IEventBroker eventBroker;
139
140 @Inject
141 public TaxonNameEditorE4() {
142 undoContext = new UndoContext();
143 }
144
145
146 @PostConstruct
147 public void createPartControl(Composite parent, MPart thisPart) {
148 this.thisPart = thisPart;
149 if (CdmStore.isActive()){
150 if(conversation == null){
151 conversation = CdmStore.createConversation();
152 }
153 }
154 else{
155 return;
156 }
157
158 createManagedForm(parent);
159
160 TaxeditorPartService.getInstance().addListener(
161 TaxeditorPartService.PART_ACTIVATED, this);
162
163 }
164
165 protected void createManagedForm(Composite composite) {
166
167 managedForm = new ManagedForm(composite) {
168
169 @Override
170 public void dirtyStateChanged() {
171 dirty.setDirty(true);
172 }
173
174 @Override
175 public boolean setInput(Object input) {
176 if (input instanceof AbstractGroupedContainerE4) {
177 TaxonBase newSelection = ((AbstractGroupedContainerE4) input).getData();
178 if(selection!=newSelection){
179 selection = newSelection;
180 selService.setSelection(new StructuredSelection(selection));
181 }
182 }else if(input == null){
183 selection = null;
184 selService.setSelection(new StructuredSelection());
185 }
186
187
188 return super.setInput(input);
189 }
190 };
191
192 scrolledForm = managedForm.getForm();
193 parent = scrolledForm.getBody();
194
195 parent.setData(taxon);
196
197 TableWrapLayout layout = new TableWrapLayout();
198 layout.leftMargin = 0;
199 layout.rightMargin = 0;
200 layout.topMargin = 0;
201 layout.bottomMargin = 0;
202
203 layout.verticalSpacing = 0;
204 layout.horizontalSpacing = 0;
205
206 parent.setLayout(layout);
207 parent.setBackground(AbstractUtility
208 .getColor(Resources.COLOR_COMPOSITE_BACKGROUND));
209 }
210
211 public void createOrUpdateNameComposites() {
212 ContainerFactoryE4.createOrUpdateAcceptedTaxonsHomotypicGroup(this);
213 ContainerFactoryE4.createOrUpdateHeterotypicSynonymyGroups(this);
214 ContainerFactoryE4.createOrUpdateMisapplicationsGroup(this);
215
216
217 // Redraw composite
218 managedForm.reflow(true);
219 managedForm.refresh();
220 }
221
222 @Override
223 public Taxon getTaxon() {
224 return HibernateProxyHelper.deproxy(taxon);
225 }
226
227 public void setDirty() {
228 managedForm.dirtyStateChanged();
229 }
230
231 @Focus
232 public void setFocus() {
233 //make sure to bind again if maybe in another view the conversation was unbound
234 if(conversation!=null && !conversation.isBound()){
235 conversation.bind();
236 }
237 if(input!=null){
238 if (getSelectedContainer() == null) {
239 throw new IllegalStateException(
240 Messages.TaxonNameEditor_THERE_SHOULD_ALWAYS_BE);
241 }
242 getSelectedContainer().setSelected();
243
244 // check permissions
245 boolean doEnable = permissionsSatisfied();
246 managedForm.getForm().setEnabled(doEnable);
247 }
248 eventBroker.post(WorkbenchEventConstants.CURRENT_ACTIVE_EDITOR, this);
249 }
250
251 @Override
252 public boolean permissionsSatisfied() {
253 TaxonNode taxonNode = input.getTaxonNode();
254 boolean doEnable = CdmStore.currentAuthentiationHasPermission(taxonNode, RequiredPermissions.TAXONNODE_EDIT);
255 return doEnable;
256 }
257
258 @Override
259 public ConversationHolder getConversationHolder() {
260 return conversation;
261 }
262
263 /** {@inheritDoc} */
264 @Override
265 public void update(CdmDataChangeMap events) {
266 // redraw();
267 }
268
269 /**
270 * Redraws this editor return true on success
271 *
272 * @return a boolean.
273 */
274 public boolean redraw() {
275 return redraw(true);
276 }
277
278 /**
279 * {@inheritDoc}
280 *
281 * Redraws the editor controls
282 */
283 public boolean redraw(boolean focus) {
284
285 createOrUpdateNameComposites();
286
287 if (focus) {
288 setFocus();
289 }
290
291 return true;
292 }
293
294 @Override
295 public boolean postOperation(CdmBase objectAffectedByOperation) {
296
297 changed(objectAffectedByOperation);
298
299 redraw(false);
300
301 if (objectAffectedByOperation instanceof TaxonBase) {
302 objectAffectedByLastOperation = (TaxonBase) objectAffectedByOperation;
303 }
304
305 return true;
306 }
307
308 public ManagedForm getManagedForm() {
309 return managedForm;
310 }
311
312
313 /**
314 * <p>
315 * checkForEmptyNames
316 * </p>
317 *
318 * @return true if there are empty names
319 */
320 public boolean checkForEmptyNames() {
321 for (AbstractGroupedContainerE4 container : getGroupedContainers()) {
322 if (container.getName() == null
323 || StringUtils.isEmpty(container.getName().getTitleCache())) {
324 return true;
325 }
326 }
327 return false;
328 }
329
330 public Set<AbstractGroupedContainerE4> getEmptyContainers() {
331 Set<AbstractGroupedContainerE4> containersWithEmptyNames = new HashSet<>();
332
333 for (AbstractGroupedContainerE4 container : getGroupedContainers()) {
334 if (container.getName() == null
335 || StringUtils.isEmpty(container.getName().getTitleCache())) {
336 containersWithEmptyNames.add(container);
337 }
338 }
339
340 return containersWithEmptyNames;
341 }
342
343 /** {@inheritDoc} */
344 @Override
345 @Persist
346 public void save(IProgressMonitor monitor) {
347
348 monitor.beginTask(Messages.TaxonNameEditor_SAVING_NAMES, getGroupedContainers().size());
349 if (!conversation.isBound()) {
350 conversation.bind();
351 }
352 monitor.worked(1);
353
354 // check for empty names
355 if (checkForEmptyNames()) {
356 MessageDialog.openWarning(AbstractUtility.getShell(), Messages.MultiPageTaxonEditor_NO_NAME_SPECIFIED,
357 Messages.MultiPageTaxonEditor_NO_NAME_SPECIFIED_MESSAGE);
358 return;
359 }
360 for (AbstractGroupedContainerE4 container : getGroupedContainers()) {
361
362 monitor.subTask(Messages.TaxonNameEditor_SAVING_COMPOSITES
363 + container.getTaxonBase().getTitleCache());
364 container.persistName();
365
366 // In case the progress monitor was canceled throw an exception.
367 if (monitor.isCanceled()) {
368 throw new OperationCanceledException();
369 }
370 // commit the conversation and start a new transaction immediately
371 input.merge();
372
373 conversation.commit(true);
374
375 // Otherwise declare this step as done.
376 monitor.worked(1);
377
378 dirty.setDirty(false);
379 }
380
381 // Stop the progress monitor.
382 monitor.done();
383 }
384
385 public void init(TaxonEditorInputE4 input) {
386
387 if (!(input != null)) {
388 MessagingUtils.error(this.getClass(), new Exception(Messages.TaxonNameEditor_INVALID_INPUT));
389 return;
390 }
391
392 if (input.getAdapter(Taxon.class) != null) {
393 taxon = CdmBase.deproxy(input.getAdapter(Taxon.class), Taxon.class);
394 } else {
395 MessagingUtils.error(this.getClass(), new Exception(Messages.TaxonNameEditor_INVALID_INPUT_TAXON_NULL));
396 return;
397 }
398
399 this.input = input;
400
401 createOrUpdateNameComposites();
402
403 createDragSupport();
404
405 setPartName();
406
407 //set initial selection
408 TaxonBase initiallySelectedTaxonBase = input.getInitiallySelectedTaxonBase();
409 if(initiallySelectedTaxonBase!=null){
410 selService.setSelection(new StructuredSelection(initiallySelectedTaxonBase));
411 getContainer(initiallySelectedTaxonBase).setSelected();
412 }
413 }
414
415 private void createDragSupport() {
416 // Listen for names being dragged outside of existing homotypic groups -
417 // user wants to create a new group
418 Transfer[] types = new Transfer[] { CdmDataTransfer.getInstance() };
419 int operations = DND.DROP_MOVE;
420 if (target == null) {
421 target = new DropTarget(parent, operations);
422 target.setTransfer(types);
423 target.addDropListener(new NameEditorDropTargetListenerE4(this));
424 }
425 }
426
427 public AcceptedNameContainerE4 getAcceptedNameContainer() {
428 return getAcceptedGroup().getAcceptedNameContainer();
429 }
430
431 public HomotypicalSynonymGroupE4 getHomotypicalGroupContainer(
432 HomotypicalGroup homotypicalGroup) {
433 for (HomotypicalSynonymGroupE4 group : getHeterotypicSynonymGroups()) {
434 if (group.getGroup().equals(homotypicalGroup)) {
435 return group;
436 }
437 }
438
439 return null;
440 }
441
442 /**
443 * <p>
444 * getDirtyNames
445 * </p>
446 *
447 * @return a Set containing all composites that have been edited
448 */
449 public Set<AbstractGroupedContainerE4> getDirtyNames() {
450 Set<AbstractGroupedContainerE4> dirtyNames = new HashSet<>();
451
452 for (AbstractGroupedContainerE4 composite : getGroupedContainers()) {
453 if (composite.isDirty()) {
454 dirtyNames.add(composite);
455 }
456 }
457
458 return dirtyNames;
459 }
460
461 public List<AbstractGroupedContainerE4> getGroupedContainers() {
462 List<AbstractGroupedContainerE4> groupedComposites = new ArrayList<>();
463
464 for (AbstractGroupE4 group : getAllGroups()) {
465 if (group!= null){
466 groupedComposites.addAll(group.getGroupedContainers());
467 }
468 }
469
470 return groupedComposites;
471 }
472
473 public List<AbstractGroupE4> getAllGroups() {
474 List<AbstractGroupE4> allGroups = new ArrayList<>();
475
476 allGroups.add(getAcceptedGroup());
477
478 heterotypicSynonymGroups = getHeterotypicSynonymGroups();
479
480 if (heterotypicSynonymGroups != null) {
481 allGroups.addAll(heterotypicSynonymGroups);
482 }
483
484 if (misappliedGroup != null) {
485 allGroups.add(misappliedGroup);
486 }
487
488 return allGroups;
489 }
490
491 @Override
492 public IEclipseContext getContext() {
493 return context;
494 }
495
496 public boolean isDirty() {
497 return dirty.isDirty();
498 }
499
500 @PreDestroy
501 public void dispose() {
502 if(conversation!=null){
503 conversation.unregisterForDataStoreChanges(this);
504 conversation.close();
505 }
506 eventBroker.post(WorkbenchEventConstants.CURRENT_ACTIVE_EDITOR, null);
507 }
508
509 /** {@inheritDoc} */
510 @Override
511 public void selectionChanged(IWorkbenchPart part, ISelection selection) {
512
513 }
514
515 public AbstractGroupedContainerE4 getSelectedContainer() {
516 return (selection != null) ? getContainer(selection)
517 : getAcceptedNameContainer();
518 }
519
520 @Override
521 public void dragEntered() {
522 // TODO change this
523 getControl().setBackground(
524 AbstractUtility.getColor(Resources.COLOR_DRAG_ENTER));
525 }
526
527 @Override
528 public void dragLeft() {
529 getControl().setBackground(
530 AbstractUtility.getColor(Resources.COLOR_COMPOSITE_BACKGROUND));
531 }
532
533
534 public void setMisapplicationsGroup(MisappliedGroupE4 misappliedGroup) {
535 this.misappliedGroup = misappliedGroup;
536 }
537
538 public FormToolkit getToolkit() {
539 return managedForm.getToolkit();
540 }
541
542 public List<HomotypicalSynonymGroupE4> getHeterotypicSynonymGroups() {
543 return heterotypicSynonymGroups;
544 }
545
546 public void addHeterotypicSynonymGroup(HomotypicalSynonymGroupE4 group) {
547 heterotypicSynonymGroups.add(group);
548 }
549
550 public AcceptedGroupE4 getAcceptedGroup() {
551 return acceptedGroup;
552 }
553
554 public void setAcceptedGroup(AcceptedGroupE4 acceptedGroup) {
555 this.acceptedGroup = acceptedGroup;
556 }
557
558 public MisappliedGroupE4 getMisappliedGroup() {
559 return misappliedGroup;
560 }
561
562 public boolean isActive() {
563 return this.equals(AbstractUtility.getActivePart());
564 }
565
566 @Override
567 public boolean onComplete() {
568 getContainer(objectAffectedByLastOperation).setSelected();
569 return true;
570 }
571
572 /** {@inheritDoc} */
573 @Override
574 public void partChanged(Integer eventType, IWorkbenchPartReference partRef) {
575 if (!partRef.getPart(false).equals(this)) {
576 // getSelectedObject().colorSelected(AbstractGroupedContainer.SELECTED_NO_FOCUS);
577 }
578 }
579
580 public void removeGroup(AbstractGroupE4 group) {
581 if (group != null) {
582 group.dispose();
583
584 //if (heterotypicSynonymGroups != null) {
585 heterotypicSynonymGroups.remove(group);
586 //}
587 }
588 }
589
590 public AbstractGroupedContainerE4 getContainer(TaxonBase taxonBase) {
591 List<AbstractGroupedContainerE4> groupedContainers = getGroupedContainers();
592 for (AbstractGroupedContainerE4 container : groupedContainers) {
593 if (container.getData().equals(taxonBase)
594 && container.getNameViewer().getTextWidget() != null) {
595 return container;
596 }
597 }
598 return getAcceptedNameContainer();
599 }
600
601 public void setOnError() {
602 Color disabledColor = AbstractUtility.getColor(Resources.COLOR_EDITOR_ERROR);
603 setEnabled(false, disabledColor);
604 }
605
606 public void setDisabled(){
607 Color disabledColor = AbstractUtility.getColor(Resources.COLOR_TEXT_DISABLED_BACKGROUND);
608 setEnabled(false, disabledColor);
609 }
610
611 protected void setEnabled(boolean enabled, Color background) {
612
613 for(AbstractGroupedContainerE4 groupedContainer : getGroupedContainers()){
614 groupedContainer.setEnabled(enabled);
615 }
616
617 // send an empty selection to the current provider - TODO only on error ???
618 if (!enabled) {
619 getManagedForm().setInput(null);
620
621 for (AbstractGroupedContainerE4 groupedContainer : getGroupedContainers()) {
622 groupedContainer.setBackground(background);
623 }
624 }
625 getControl().setBackground(background);
626 }
627
628 @Override
629 public void changed(Object element) {
630 // setDirty(true);
631 // if the attribute is null then do not set the dirty flag -> hotfix for the problem that for tasks done in service methods the changes are saved automatically
632 if (element != null){
633 dirty.setDirty(true);
634 //refresh part title
635 //TODO: refresh taxon node in taxon navigator
636 setPartName();
637 }
638
639 if (element instanceof TaxonBase) {
640 AbstractGroupedContainerE4 container = getContainer((TaxonBase) element);
641 if (container != null) {
642 container.refresh();
643 }
644 }
645 if (element instanceof TaxonRelationship) {
646 AbstractGroupedContainerE4 container = getContainer(((TaxonRelationship) element).getFromTaxon());
647 if (container != null) {
648 container.refresh();
649 }
650 }
651 }
652
653 public void setPartName(){
654 thisPart.setLabel(taxon.toString());
655 }
656
657 @Override
658 public void forceDirty() {
659 setDirty();
660 }
661
662
663 public IUndoContext getUndoContext() {
664 return undoContext;
665 }
666
667 @Override
668 public Composite getControl(){
669 return managedForm.getForm().getBody();
670 }
671
672 public EMenuService getMenuService() {
673 return menuService;
674 }
675
676 public ESelectionService getSelectionService() {
677 return selService;
678 }
679
680
681 /**
682 * {@inheritDoc}
683 */
684 @Override
685 public boolean canAttachMedia() {
686 return true;
687 }
688
689 public TaxonEditorInputE4 getEditorInput() {
690 return input;
691 }
692
693 /**
694 * {@inheritDoc}
695 */
696 @Override
697 public TaxonNameEditorE4 getEditor() {
698 return this;
699 }
700
701 }