Project

General

Profile

Download (25.6 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 TaxonEditor
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
    private 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 TaxonEditor() {
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 || TaxonEditor.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
        this.getDirtyNames().forEach(container -> container.refresh());
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

    
393
        EventUtility.postEvent(WorkbenchEventConstants.REFRESH_NAVIGATOR, new TaxonNodeDto(input.getTaxonNode().getParent()));
394
        EventUtility.postEvent(WorkbenchEventConstants.SAVE_TAXON, true);
395
        // Stop the progress monitor.
396
        monitor.done();
397
    }
398

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

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

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

    
422
        createDragSupport();
423

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

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

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

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

    
457
        return null;
458
    }
459

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

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

    
476
        return dirtyNames;
477
    }
478

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

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

    
488
        return groupedComposites;
489
    }
490

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

    
494
        allGroups.add(getAcceptedGroup());
495

    
496
        heterotypicSynonymGroups = getHeterotypicSynonymGroups();
497

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

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

    
506
        return allGroups;
507
    }
508

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

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

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

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

    
531
    }
532

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

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

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

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

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

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

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

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

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

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

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

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

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

    
593

    
594
        }
595
    }
596

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

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

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

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

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

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

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

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

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

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

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

    
667
        if (element instanceof TaxonRelationship) {
668
            AbstractGroupedContainerE4 container = getContainer(((TaxonRelationship) element).getFromTaxon());
669
            if (container != null) {
670
                container.refresh();
671
                container.setDirty(true);
672
            }
673
        }
674
    }
675

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

    
682
    @Override
683
    public void forceDirty() {
684
        setDirty();
685
    }
686

    
687
    public IUndoContext getUndoContext() {
688
        return undoContext;
689
    }
690

    
691
    @Override
692
    public Composite getControl() {
693
        return managedForm.getForm().getBody();
694
    }
695

    
696
    public EMenuService getMenuService() {
697
        return menuService;
698
    }
699

    
700
    public ESelectionService getSelectionService() {
701
        return selService;
702
    }
703

    
704
    @Override
705
    public boolean canAttachMedia() {
706
        return true;
707
    }
708

    
709
    public TaxonEditorInput getEditorInput() {
710
        return input;
711
    }
712

    
713
    @Override
714
    public TaxonEditor getEditor() {
715
        return this;
716
    }
717

    
718
    @Inject
719
    @Optional
720
    private void updateView(@UIEventTopic(WorkbenchEventConstants.REFRESH_NAME_EDITOR) CdmBase cdmbase) {
721

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

    
732
    @Inject
733
    @Optional
734
    private void updateView(@UIEventTopic(WorkbenchEventConstants.REFRESH_NAME_EDITOR) List<CdmBase> cdmBases) {
735

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

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

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

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

    
786
    @Override
787
    public TaxonNode getTaxonNode() {
788
        return input.getTaxonNode();
789
    }
790

    
791

    
792
}
(2-2/2)