Project

General

Profile

Download (25.5 KB) Statistics
| Branch: | Tag: | Revision:
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
package eu.etaxonomy.taxeditor.editor.name.e4;
10

    
11
import java.util.ArrayList;
12
import java.util.HashSet;
13
import java.util.List;
14
import java.util.Set;
15
import java.util.UUID;
16

    
17
import javax.annotation.PostConstruct;
18
import javax.annotation.PreDestroy;
19
import javax.inject.Inject;
20

    
21
import org.apache.commons.lang3.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.di.annotations.Optional;
28
import org.eclipse.e4.core.services.events.IEventBroker;
29
import org.eclipse.e4.ui.di.Focus;
30
import org.eclipse.e4.ui.di.Persist;
31
import org.eclipse.e4.ui.di.UIEventTopic;
32
import org.eclipse.e4.ui.di.UISynchronize;
33
import org.eclipse.e4.ui.model.application.MApplication;
34
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
35
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
36
import org.eclipse.e4.ui.services.EMenuService;
37
import org.eclipse.e4.ui.workbench.modeling.EModelService;
38
import org.eclipse.e4.ui.workbench.modeling.EPartService;
39
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
40
import org.eclipse.jface.dialogs.MessageDialog;
41
import org.eclipse.jface.viewers.ISelection;
42
import org.eclipse.jface.viewers.StructuredSelection;
43
import org.eclipse.swt.dnd.DND;
44
import org.eclipse.swt.dnd.DropTarget;
45
import org.eclipse.swt.dnd.Transfer;
46
import org.eclipse.swt.graphics.Color;
47
import org.eclipse.swt.widgets.Composite;
48
import org.eclipse.ui.ISelectionListener;
49
import org.eclipse.ui.IWorkbenchPart;
50
import org.eclipse.ui.IWorkbenchPartReference;
51
import org.eclipse.ui.forms.ManagedForm;
52
import org.eclipse.ui.forms.widgets.FormToolkit;
53
import org.eclipse.ui.forms.widgets.ScrolledForm;
54
import org.eclipse.ui.forms.widgets.TableWrapLayout;
55

    
56
import eu.etaxonomy.cdm.api.service.DeleteResult;
57
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
58
import eu.etaxonomy.cdm.model.common.CdmBase;
59
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
60
import eu.etaxonomy.cdm.model.name.TaxonName;
61
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
62
import eu.etaxonomy.cdm.model.taxon.Taxon;
63
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
64
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
65
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
66
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
67
import eu.etaxonomy.taxeditor.editor.CdmDataTransfer;
68
import eu.etaxonomy.taxeditor.editor.EditorUtil;
69
import eu.etaxonomy.taxeditor.editor.ISecuredEditor;
70
import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
71
import eu.etaxonomy.taxeditor.editor.e4.TaxonEditorInput;
72
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
73
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
74
import eu.etaxonomy.taxeditor.editor.name.e4.container.AbstractGroup;
75
import eu.etaxonomy.taxeditor.editor.name.e4.container.AbstractGroupedContainerE4;
76
import eu.etaxonomy.taxeditor.editor.name.e4.container.AcceptedGroup;
77
import eu.etaxonomy.taxeditor.editor.name.e4.container.AcceptedNameContainerE4;
78
import eu.etaxonomy.taxeditor.editor.name.e4.container.ConceptContainer;
79
import eu.etaxonomy.taxeditor.editor.name.e4.container.ContainerFactory;
80
import eu.etaxonomy.taxeditor.editor.name.e4.container.HomotypicalSynonymGroup;
81
import eu.etaxonomy.taxeditor.editor.name.e4.container.MisappliedGroup;
82
import eu.etaxonomy.taxeditor.editor.name.e4.dnd.NameEditorDropTargetListener;
83
import eu.etaxonomy.taxeditor.event.EventUtility;
84
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
85
import eu.etaxonomy.taxeditor.model.AbstractUtility;
86
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
87
import eu.etaxonomy.taxeditor.model.IPartChangeListener;
88
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
89
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
90
import eu.etaxonomy.taxeditor.model.IPartContentHasMedia;
91
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
92
import eu.etaxonomy.taxeditor.model.MessagingUtils;
93
import eu.etaxonomy.taxeditor.model.TaxeditorPartService;
94
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
95
import eu.etaxonomy.taxeditor.preference.Resources;
96
import eu.etaxonomy.taxeditor.security.RequiredPermissions;
97
import eu.etaxonomy.taxeditor.store.CdmStore;
98
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
99

    
100
/**
101
 * @author pplitzner
102
 * @date Aug 24, 2017
103
 */
104
public class TaxonNameEditor
105
        implements IDirtyMarkable, IPartContentHasDetails,
106
            IPartContentHasSupplementalData, IPartContentHasMedia, IPartContentHasFactualData, IPartChangeListener,
107
            ISelectionListener, ISecuredEditor, IE4SavablePart, ITaxonEditor, IDropTargetable{
108

    
109
    private Taxon taxon;
110

    
111
    private ManagedForm managedForm;
112
    private ScrolledForm scrolledForm;
113
    private Composite parent;
114
    @Inject
115
    private EModelService modelService;
116

    
117
    @Inject
118
    private EPartService partService;
119
    private TaxonBase<?> selection;
120

    
121
    private AcceptedGroup acceptedGroup;
122
    private List<HomotypicalSynonymGroup> heterotypicSynonymGroups = new ArrayList<>();
123
    private MisappliedGroup misappliedGroup;
124

    
125
    private DropTarget target;
126

    
127
    @Inject
128
    UISynchronize sync;
129

    
130
    private TaxonBase<?> objectAffectedByLastOperation;
131

    
132
    @Inject
133
    private EMenuService menuService;
134

    
135
    @Inject
136
    private ESelectionService selService;
137

    
138
    @Inject
139
    private IEclipseContext context;
140

    
141
    @Inject
142
    private MDirtyable dirty;
143

    
144
    @Inject
145
    private MApplication application;
146

    
147
    private MPart thisPart;
148

    
149
    private TaxonEditorInput input;
150

    
151
    private UndoContext undoContext;
152

    
153
    @Inject
154
    private IEventBroker eventBroker;
155

    
156
    @Inject
157
    public TaxonNameEditor() {
158
        undoContext = new UndoContext();
159
    }
160

    
161
    @PostConstruct
162
    public void createPartControl(Composite parent, MPart thisPart) {
163
        this.thisPart = thisPart;
164

    
165
        createManagedForm(parent);
166

    
167
        TaxeditorPartService.getInstance().addListener(TaxeditorPartService.PART_ACTIVATED, this);
168
    }
169

    
170
    protected void createManagedForm(Composite composite) {
171

    
172
        managedForm = new ManagedForm(composite) {
173

    
174
            @Override
175
            public void dirtyStateChanged() {
176
                dirty.setDirty(true);
177
            }
178

    
179
            @Override
180
            public boolean setInput(Object input) {
181
                if (input instanceof AbstractGroupedContainerE4) {
182
                    TaxonBase<?> newSelection = ((AbstractGroupedContainerE4<?>) input).getData();
183
                    if (selection != newSelection || TaxonNameEditor.this.isDirty()) {
184
                        selection = newSelection;
185
                        selService.setSelection(new StructuredSelection(selection));
186

    
187
                    }
188
                } else if (input == null) {
189
                    selection = null;
190
                    selService.setSelection(new StructuredSelection());
191
                }
192

    
193
                return super.setInput(input);
194
            }
195
        };
196

    
197
        scrolledForm = managedForm.getForm();
198
        parent = scrolledForm.getBody();
199

    
200
        parent.setData(taxon);
201

    
202
        TableWrapLayout layout = new TableWrapLayout();
203
        layout.leftMargin = 0;
204
        layout.rightMargin = 0;
205
        layout.topMargin = 0;
206
        layout.bottomMargin = 0;
207

    
208
        layout.verticalSpacing = 0;
209
        layout.horizontalSpacing = 0;
210

    
211
        parent.setLayout(layout);
212
        parent.setBackground(AbstractUtility.getColor(Resources.COLOR_COMPOSITE_BACKGROUND));
213
    }
214

    
215
    public void createOrUpdateNameComposites(boolean accepted, boolean heterotypicGroups, boolean misappliedNames) {
216

    
217
        if (accepted) {
218
            ContainerFactory.createOrUpdateAcceptedTaxonsHomotypicGroup(this);
219
        }
220
        if (heterotypicGroups) {
221
            ContainerFactory.createOrUpdateHeterotypicSynonymyGroups(this);
222
        }
223
        if (misappliedNames) {
224
            ContainerFactory.createOrUpdateMisapplicationsGroup(this);
225
        }
226
        ContainerFactory.setMenuToAllContainers(this);
227

    
228
        // Redraw composite
229
        parent.pack();
230
        managedForm.refresh();
231
        managedForm.reflow(true);
232
    }
233

    
234
    @Override
235
    public Taxon getTaxon() {
236
        return HibernateProxyHelper.deproxy(taxon);
237
    }
238

    
239
    @Override
240
    public void addOperation(AbstractPostOperation operation) {
241
        input.addOperation(operation);
242
    }
243

    
244
    public void setDirty() {
245
        managedForm.dirtyStateChanged();
246
    }
247

    
248
    @Focus
249
    public void setFocus() {
250
        // make sure to bind again if maybe in another view the conversation was
251
        // unbound
252

    
253
        eventBroker.post(WorkbenchEventConstants.CURRENT_ACTIVE_EDITOR, this);
254
        
255
        if (EditorUtil.isFactsVisible()){
256
            EditorUtil.showFacts(modelService, partService);
257
        }
258
        if (EditorUtil.isMediaVisible()){
259
            EditorUtil.showMedia(modelService, partService);
260
        }
261
        if (input != null) {
262
            if (getSelectedContainer() == null) {
263
                throw new IllegalStateException(Messages.TaxonNameEditor_THERE_SHOULD_ALWAYS_BE);
264
            }
265

    
266
            if (input.getCdmEntitySession() != null && !input.getCdmEntitySession().isActive()) {
267
                input.bind();
268
            }
269

    
270
        }
271

    
272
        // check permissions
273
        boolean doEnable = permissionsSatisfied();
274
        managedForm.getForm().setEnabled(doEnable);
275
    }
276

    
277
    @Override
278
    public boolean permissionsSatisfied() {
279
        boolean doEnable = false;
280
        if (input != null){
281
            TaxonNode taxonNode = input.getTaxonNode();
282
            doEnable = CdmStore.currentAuthentiationHasPermission(taxonNode,
283
                    RequiredPermissions.TAXON_EDIT);
284
        }
285
        return doEnable;
286
    }
287

    
288
    /**
289
     * Redraws this editor return true on success
290
     *
291
     * @return a boolean.
292
     */
293
    public boolean redraw() {
294
        return redraw(true, true, true, true);
295
    }
296

    
297
    /**
298
     * {@inheritDoc}
299
     *
300
     * Redraws the editor controls
301
     */
302
    public boolean redraw(boolean focus, boolean accepted, boolean heterotypic, boolean misappliedNames) {
303

    
304
        createOrUpdateNameComposites(accepted, heterotypic, misappliedNames);
305

    
306
        if (focus) {
307
            setFocus();
308
        }
309

    
310
        return true;
311
    }
312

    
313
    @Override
314
    public boolean postOperation(Object objectAffectedByOperation) {
315
        if (objectAffectedByOperation instanceof TaxonBase) {
316
            objectAffectedByLastOperation = (TaxonBase<?>) objectAffectedByOperation;
317
        }
318
        redraw(true, true, true, true);
319
        changed(objectAffectedByOperation);
320
        onComplete();
321
        getContainer(objectAffectedByLastOperation).setFocus();
322
        return true;
323
    }
324

    
325
    public ManagedForm getManagedForm() {
326
        return managedForm;
327
    }
328

    
329
    /**
330
     * <p>
331
     * checkForEmptyNames
332
     * </p>
333
     *
334
     * @return true if there are empty names
335
     */
336
    public boolean checkForEmptyNames() {
337
        for (AbstractGroupedContainerE4<?> container : getGroupedContainers()) {
338
            if (container != null
339
                    && (container.getName() == null || StringUtils.isEmpty(container.getName().getTitleCache()))) {
340
                return true;
341
            }
342
        }
343
        return false;
344
    }
345

    
346
    public Set<AbstractGroupedContainerE4> getEmptyContainers() {
347
        Set<AbstractGroupedContainerE4> containersWithEmptyNames = new HashSet<>();
348

    
349
        for (AbstractGroupedContainerE4<?> container : getGroupedContainers()) {
350
            if (container.getName() == null || StringUtils.isEmpty(container.getName().getTitleCache())) {
351
                containersWithEmptyNames.add(container);
352
            }
353
        }
354

    
355
        return containersWithEmptyNames;
356
    }
357

    
358
    @Override
359
    @Persist
360
    public void save(IProgressMonitor monitor) {
361

    
362
        monitor.beginTask(Messages.TaxonNameEditor_SAVING_NAMES, getGroupedContainers().size());
363
        monitor.worked(1);
364

    
365
        // check for empty names
366
        if (checkForEmptyNames()) {
367
            MessageDialog.openWarning(AbstractUtility.getShell(), Messages.MultiPageTaxonEditor_NO_NAME_SPECIFIED,
368
                    Messages.MultiPageTaxonEditor_NO_NAME_SPECIFIED_MESSAGE);
369
            return;
370
        }
371
        for (AbstractGroupedContainerE4<?> container : getGroupedContainers()) {
372

    
373
            monitor.subTask(Messages.TaxonNameEditor_SAVING_COMPOSITES + container.getTaxonBase().getTitleCache());
374
            container.persistName();
375
            // because of missing cascading the concepts need to be saved
376
            // separately
377
            if (container instanceof ConceptContainer) {
378
                input.addToSaveConcept((Taxon) container.getData());
379
            }
380
            // In case the progress monitor was canceled throw an exception.
381
            if (monitor.isCanceled()) {
382
                throw new OperationCanceledException();
383
            }
384

    
385
            // Otherwise declare this step as done.
386
            monitor.worked(1);
387
        }
388
        input.setSync(sync);
389
        input.merge();
390
       
391
        dirty.setDirty(false);
392
        EventUtility.postEvent(WorkbenchEventConstants.REFRESH_NAVIGATOR, new TaxonNodeDto(input.getTaxonNode().getParent()));
393
        EventUtility.postEvent(WorkbenchEventConstants.SAVE_TAXON, true);
394
        // Stop the progress monitor.
395
        monitor.done();
396
    }
397

    
398
    public void init(TaxonEditorInput input) {
399
        if (this.input != null) {
400
            this.input.dispose();
401
            // this.acceptedGroup = null;
402
            // this.heterotypicSynonymGroups.clear();
403
            // this.misappliedGroup = null;
404
        }
405
        eventBroker.post(WorkbenchEventConstants.CURRENT_ACTIVE_EDITOR, this);
406
        if (!(input != null)) {
407
            MessagingUtils.error(this.getClass(), new Exception(Messages.TaxonNameEditor_INVALID_INPUT));
408
            return;
409
        }
410

    
411
        if (input.getAdapter(Taxon.class) != null) {
412
            taxon = CdmBase.deproxy(input.getAdapter(Taxon.class), Taxon.class);
413
        } else {
414
            MessagingUtils.error(this.getClass(), new Exception(Messages.TaxonNameEditor_INVALID_INPUT_TAXON_NULL));
415
            return;
416
        }
417

    
418
        this.input = input;
419
        createOrUpdateNameComposites(true, true, true);
420

    
421
        createDragSupport();
422

    
423
        setPartName();
424
//        this.setFocus();
425
        // set initial selection
426
        TaxonBase<?> initiallySelectedTaxonBase = input.getInitiallySelectedTaxonBase();
427
        if (initiallySelectedTaxonBase != null) {
428
            selService.setSelection(new StructuredSelection(initiallySelectedTaxonBase));
429
            getContainer(initiallySelectedTaxonBase).setSelected();
430
        }
431
    }
432

    
433
    private void createDragSupport() {
434
        // Listen for names being dragged outside of existing homotypic groups -
435
        // user wants to create a new group
436
        Transfer[] types = new Transfer[] { CdmDataTransfer.getInstance() };
437
        int operations = DND.DROP_MOVE;
438
        if (target == null) {
439
            target = new DropTarget(parent, operations);
440
            target.setTransfer(types);
441
            target.addDropListener(new NameEditorDropTargetListener(this));
442
        }
443
    }
444

    
445
    public AcceptedNameContainerE4 getAcceptedNameContainer() {
446
        return getAcceptedGroup().getAcceptedNameContainer();
447
    }
448

    
449
    public HomotypicalSynonymGroup getHomotypicalGroupContainer(HomotypicalGroup homotypicalGroup) {
450
        for (HomotypicalSynonymGroup group : getHeterotypicSynonymGroups()) {
451
            if (group.getGroup().equals(homotypicalGroup)) {
452
                return group;
453
            }
454
        }
455

    
456
        return null;
457
    }
458

    
459
    /**
460
     * <p>
461
     * getDirtyNames
462
     * </p>
463
     *
464
     * @return a Set containing all composites that have been edited
465
     */
466
    public Set<AbstractGroupedContainerE4> getDirtyNames() {
467
        Set<AbstractGroupedContainerE4> dirtyNames = new HashSet<>();
468

    
469
        for (AbstractGroupedContainerE4<?> composite : getGroupedContainers()) {
470
            if (composite.isDirty()) {
471
                dirtyNames.add(composite);
472
            }
473
        }
474

    
475
        return dirtyNames;
476
    }
477

    
478
    public List<AbstractGroupedContainerE4> getGroupedContainers() {
479
        List<AbstractGroupedContainerE4> groupedComposites = new ArrayList<>();
480

    
481
        for (AbstractGroup group : getAllGroups()) {
482
            if (group != null) {
483
                groupedComposites.addAll(group.getGroupedContainers());
484
            }
485
        }
486

    
487
        return groupedComposites;
488
    }
489

    
490
    public List<AbstractGroup> getAllGroups() {
491
        List<AbstractGroup> allGroups = new ArrayList<>();
492

    
493
        allGroups.add(getAcceptedGroup());
494

    
495
        heterotypicSynonymGroups = getHeterotypicSynonymGroups();
496

    
497
        if (heterotypicSynonymGroups != null) {
498
            allGroups.addAll(heterotypicSynonymGroups);
499
        }
500

    
501
        if (misappliedGroup != null) {
502
            allGroups.add(misappliedGroup);
503
        }
504

    
505
        return allGroups;
506
    }
507

    
508
    @Override
509
    public IEclipseContext getContext() {
510
        return context;
511
    }
512

    
513
    @Override
514
    public boolean isDirty() {
515
        return dirty.isDirty();
516
    }
517

    
518
    @PreDestroy
519
    public void dispose() {
520
        if (input != null) {
521
            input.dispose();
522
        }
523
        dirty.setDirty(false);
524
        eventBroker.post(WorkbenchEventConstants.CURRENT_ACTIVE_EDITOR, null);
525
    }
526

    
527
    @Override
528
    public void selectionChanged(IWorkbenchPart part, ISelection selection) {
529

    
530
    }
531

    
532
    public AbstractGroupedContainerE4 getSelectedContainer() {
533
        if (selection == null && input != null){
534
            selection = this.input.getTaxon();
535
        }
536
        return (selection != null) ? getContainer(selection) : getAcceptedNameContainer();
537
    }
538

    
539
    @Override
540
    public void dragEntered() {
541
        // TODO change this
542
        getControl().setBackground(AbstractUtility.getColor(Resources.COLOR_DRAG_ENTER));
543
    }
544

    
545
    @Override
546
    public void dragLeft() {
547
        getControl().setBackground(AbstractUtility.getColor(Resources.COLOR_COMPOSITE_BACKGROUND));
548
    }
549

    
550
    public void setMisapplicationsGroup(MisappliedGroup misappliedGroup) {
551
        this.misappliedGroup = misappliedGroup;
552
    }
553

    
554
    public FormToolkit getToolkit() {
555
        return managedForm.getToolkit();
556
    }
557

    
558
    public List<HomotypicalSynonymGroup> getHeterotypicSynonymGroups() {
559
        return heterotypicSynonymGroups;
560
    }
561

    
562
    public void addHeterotypicSynonymGroup(HomotypicalSynonymGroup group) {
563
        heterotypicSynonymGroups.add(group);
564
    }
565

    
566
    public AcceptedGroup getAcceptedGroup() {
567
        return acceptedGroup;
568
    }
569

    
570
    public void setAcceptedGroup(AcceptedGroup acceptedGroup) {
571
        this.acceptedGroup = acceptedGroup;
572
    }
573

    
574
    public MisappliedGroup getMisappliedGroup() {
575
        return misappliedGroup;
576
    }
577

    
578
    public boolean isActive() {
579
        return this.equals(AbstractUtility.getActivePart());
580
    }
581

    
582
    @Override
583
    public boolean onComplete() {
584
        getContainer(objectAffectedByLastOperation).setSelected();
585
        return true;
586
    }
587

    
588
    @Override
589
    public void partChanged(Integer eventType, IWorkbenchPartReference partRef) {
590
        if (!partRef.getPart(false).equals(this)) {
591

    
592

    
593
        }
594
    }
595

    
596
    public void removeGroup(AbstractGroup group) {
597
        if (group != null) {
598
            group.dispose();
599

    
600
            // if (heterotypicSynonymGroups != null) {
601
            heterotypicSynonymGroups.remove(group);
602
            // }
603
        }
604
    }
605

    
606
    public AbstractGroupedContainerE4 getContainer(TaxonBase taxonBase) {
607
        @SuppressWarnings("rawtypes")
608
        List<AbstractGroupedContainerE4> groupedContainers = getGroupedContainers();
609
        for (AbstractGroupedContainerE4<?> container : groupedContainers) {
610
            if (container.getData().equals(taxonBase) && container.getNameViewer().getTextWidget() != null) {
611
                return container;
612
            }
613
        }
614
        return getAcceptedNameContainer();
615
    }
616

    
617
    public void setOnError() {
618
        Color disabledColor = AbstractUtility.getColor(Resources.COLOR_EDITOR_ERROR);
619
        setEnabled(false, disabledColor);
620
    }
621

    
622
    public void setDisabled() {
623
        Color disabledColor = AbstractUtility.getColor(Resources.COLOR_TEXT_DISABLED_BACKGROUND);
624
        setEnabled(false, disabledColor);
625
    }
626

    
627
    protected void setEnabled(boolean enabled, Color background) {
628

    
629
        for (AbstractGroupedContainerE4<?> groupedContainer : getGroupedContainers()) {
630
            groupedContainer.setEnabled(enabled);
631
        }
632

    
633
        // send an empty selection to the current provider - TODO only on error
634
        // ???
635
        if (!enabled) {
636
            getManagedForm().setInput(null);
637

    
638
            for (AbstractGroupedContainerE4<?> groupedContainer : getGroupedContainers()) {
639
                groupedContainer.setBackground(background);
640
            }
641
        }
642
        getControl().setBackground(background);
643
    }
644

    
645
    @Override
646
    public void changed(Object element) {
647
        // setDirty(true);
648
        // if the attribute is null then do not set the dirty flag -> hotfix for
649
        // the problem that for tasks done in service methods the changes are
650
        // saved automatically
651
        if (element != null) {
652
            dirty.setDirty(true);
653
            // refresh part title
654
            // TODO: refresh taxon node in taxon navigator
655
            setPartName();
656
        }
657

    
658
        if (element instanceof TaxonBase) {
659
            AbstractGroupedContainerE4<?> container = getContainer((TaxonBase<?>) element);
660
            if (container != null) {
661
                container.refresh();
662
            }
663
        }
664

    
665
        if (element instanceof TaxonRelationship) {
666
            AbstractGroupedContainerE4 container = getContainer(((TaxonRelationship) element).getFromTaxon());
667
            if (container != null) {
668
                container.refresh();
669
            }
670
        }
671
    }
672

    
673
    public void setPartName() {
674
        // FIXME: temporary fix for #6437 to avoid outdated title caches
675
        thisPart.setLabel(this.taxon.getName().generateFullTitle());
676
        // thisPart.setLabel(this.taxon.getName().getFullTitleCache());
677
    }
678

    
679
    @Override
680
    public void forceDirty() {
681
        setDirty();
682
    }
683

    
684
    public IUndoContext getUndoContext() {
685
        return undoContext;
686
    }
687

    
688
    @Override
689
    public Composite getControl() {
690
        return managedForm.getForm().getBody();
691
    }
692

    
693
    public EMenuService getMenuService() {
694
        return menuService;
695
    }
696

    
697
    public ESelectionService getSelectionService() {
698
        return selService;
699
    }
700

    
701
    @Override
702
    public boolean canAttachMedia() {
703
        return true;
704
    }
705

    
706
    public TaxonEditorInput getEditorInput() {
707
        return input;
708
    }
709

    
710
    @Override
711
    public TaxonNameEditor getEditor() {
712
        return this;
713
    }
714

    
715
    @Inject
716
    @Optional
717
    private void updateView(@UIEventTopic(WorkbenchEventConstants.REFRESH_NAME_EDITOR) CdmBase cdmbase) {
718

    
719
          if(this.taxon != null && (this.taxon.equals(cdmbase)
720
                        || (this.taxon.getName() != null && this.taxon.getName().equals(cdmbase)))) {
721
            this.redraw(false, true, true, true);
722
            this.setDirty();
723
            if (cdmbase instanceof TaxonBase) {
724
                this.selection = (TaxonBase<?>) cdmbase;
725
            }
726
        }
727
    }
728

    
729
    @Inject
730
    @Optional
731
    private void updateView(@UIEventTopic(WorkbenchEventConstants.REFRESH_NAME_EDITOR) List<CdmBase> cdmBases) {
732

    
733
    	if (cdmBases == null){
734
    		return;
735
    	}
736
        for (CdmBase cdmBase: cdmBases){
737
            if (cdmBase instanceof Taxon || cdmBase instanceof TaxonName){
738
                if (this.taxon != null && (this.taxon.equals(cdmBase)
739
                                || (this.taxon.getName() != null && this.taxon.getName().equals(cdmBase)))) {
740
                    update();
741
                }
742
            }
743
        }
744
    }
745

    
746
    @Inject
747
    @Optional
748
    private void updateView(@UIEventTopic(WorkbenchEventConstants.REFRESH_NAME_EDITOR) UUID cdmbaseUuid) {
749
        if (this.taxon.getUuid().equals(cdmbaseUuid)) {
750
            TaxonEditorInput input = TaxonEditorInput.NewInstanceFromTaxonBase(cdmbaseUuid);
751
            init(input);
752
        }
753
    }
754

    
755
    @Inject
756
    @Optional
757
    private void updatefromDelete(@UIEventTopic(WorkbenchEventConstants.DELETE_DERIVATIVE) DeleteResult result) {
758
        if (taxon.getName() == null) {
759
            return;
760
        }
761
        Set<DerivedUnit> typeDesignationSpecimens = new HashSet<>();
762
        this.taxon.getName().getSpecimenTypeDesignations()
763
                .forEach(designation -> typeDesignationSpecimens.add(designation.getTypeSpecimen()));
764
        // check if any deleted object was a type specimen
765
        if (result.getUpdatedObjects().stream()
766
                // filter only DerivedUnits
767
                .filter(cdmBase -> cdmBase.isInstanceOf(DerivedUnit.class))
768
                // deproxy from CdmBase to DerivedUnit
769
                .map(unit -> HibernateProxyHelper.deproxy(unit, DerivedUnit.class))
770
                // check for match in type designations
771
                .anyMatch(unit -> typeDesignationSpecimens.contains(unit))) {
772
            EditorUtil.updateEditor(this.input.getTaxonNode(), this);
773
        }
774
    }
775

    
776
    @Override
777
    public void update() {
778
        EPartService partService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EPartService.class);
779
        EModelService modelService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EModelService.class);
780
        EditorUtil.openTaxonBaseE4(this.getTaxon().getUuid(), modelService, partService, application);
781
    }
782

    
783
    @Override
784
    public TaxonNode getTaxonNode() {
785
        return input.getTaxonNode();
786
    }
787
}
(2-2/2)