Project

General

Profile

Download (27.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

    
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
import java.util.UUID;
17

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

    
22
import org.apache.commons.lang.StringUtils;
23
import org.eclipse.core.commands.operations.IUndoContext;
24
import org.eclipse.core.commands.operations.UndoContext;
25
import org.eclipse.core.runtime.IProgressMonitor;
26
import org.eclipse.core.runtime.OperationCanceledException;
27
import org.eclipse.e4.core.contexts.IEclipseContext;
28
import org.eclipse.e4.core.di.annotations.Optional;
29
import org.eclipse.e4.core.services.events.IEventBroker;
30
import org.eclipse.e4.ui.di.Focus;
31
import org.eclipse.e4.ui.di.Persist;
32
import org.eclipse.e4.ui.di.UIEventTopic;
33
import org.eclipse.e4.ui.di.UISynchronize;
34
import org.eclipse.e4.ui.model.application.MApplication;
35
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
36
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
37
import org.eclipse.e4.ui.services.EMenuService;
38
import org.eclipse.e4.ui.workbench.modeling.EModelService;
39
import org.eclipse.e4.ui.workbench.modeling.EPartService;
40
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
41
import org.eclipse.jface.dialogs.MessageDialog;
42
import org.eclipse.jface.viewers.ISelection;
43
import org.eclipse.jface.viewers.ISelectionProvider;
44
import org.eclipse.jface.viewers.StructuredSelection;
45
import org.eclipse.swt.dnd.DND;
46
import org.eclipse.swt.dnd.DropTarget;
47
import org.eclipse.swt.dnd.Transfer;
48
import org.eclipse.swt.graphics.Color;
49
import org.eclipse.swt.widgets.Composite;
50
import org.eclipse.ui.ISelectionListener;
51
import org.eclipse.ui.IWorkbenchPart;
52
import org.eclipse.ui.IWorkbenchPartReference;
53
import org.eclipse.ui.forms.ManagedForm;
54
import org.eclipse.ui.forms.widgets.FormToolkit;
55
import org.eclipse.ui.forms.widgets.ScrolledForm;
56
import org.eclipse.ui.forms.widgets.TableWrapLayout;
57

    
58
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
59
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
60
import eu.etaxonomy.cdm.api.service.DeleteResult;
61
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
62
import eu.etaxonomy.cdm.model.common.CdmBase;
63
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
64
import eu.etaxonomy.cdm.model.name.TaxonName;
65
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
66
import eu.etaxonomy.cdm.model.taxon.Taxon;
67
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
68
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
69
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
70
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
71
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
72
import eu.etaxonomy.taxeditor.editor.AppModelId;
73
import eu.etaxonomy.taxeditor.editor.CdmDataTransfer;
74
import eu.etaxonomy.taxeditor.editor.EditorUtil;
75
import eu.etaxonomy.taxeditor.editor.ISecuredEditor;
76
import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
77
import eu.etaxonomy.taxeditor.editor.e4.TaxonEditorInputE4;
78
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
79
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
80
import eu.etaxonomy.taxeditor.editor.name.e4.container.AbstractGroupE4;
81
import eu.etaxonomy.taxeditor.editor.name.e4.container.AbstractGroupedContainerE4;
82
import eu.etaxonomy.taxeditor.editor.name.e4.container.AcceptedGroupE4;
83
import eu.etaxonomy.taxeditor.editor.name.e4.container.AcceptedNameContainerE4;
84
import eu.etaxonomy.taxeditor.editor.name.e4.container.ConceptContainerE4;
85
import eu.etaxonomy.taxeditor.editor.name.e4.container.ContainerFactoryE4;
86
import eu.etaxonomy.taxeditor.editor.name.e4.container.HomotypicalSynonymGroupE4;
87
import eu.etaxonomy.taxeditor.editor.name.e4.container.MisappliedGroupE4;
88
import eu.etaxonomy.taxeditor.editor.name.e4.dnd.NameEditorDropTargetListenerE4;
89
import eu.etaxonomy.taxeditor.event.EventUtility;
90
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
91
import eu.etaxonomy.taxeditor.model.AbstractUtility;
92
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
93
import eu.etaxonomy.taxeditor.model.IPartChangeListener;
94
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
95
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
96
import eu.etaxonomy.taxeditor.model.IPartContentHasMedia;
97
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
98
import eu.etaxonomy.taxeditor.model.MessagingUtils;
99
import eu.etaxonomy.taxeditor.model.TaxeditorPartService;
100
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
101
import eu.etaxonomy.taxeditor.preference.Resources;
102
import eu.etaxonomy.taxeditor.security.RequiredPermissions;
103
import eu.etaxonomy.taxeditor.store.CdmStore;
104
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
105

    
106
/**
107
 *
108
 * @author pplitzner
109
 * @date Aug 24, 2017
110
 *
111
 */
112
public class TaxonNameEditorE4 implements IConversationEnabled, IDirtyMarkable, IPartContentHasDetails,
113
        IPartContentHasSupplementalData, IPartContentHasMedia, IPartContentHasFactualData, IPartChangeListener,
114
        ISelectionListener, ISecuredEditor, IE4SavablePart, ITaxonEditor, IDropTargetableE4 {
115

    
116
    private Taxon taxon;
117

    
118
    private ManagedForm managedForm;
119
    private ScrolledForm scrolledForm;
120
    private Composite parent;
121
    private ISelectionProvider simpleSelectionProvider;
122
    @Inject
123
    private EModelService modelService;
124

    
125
    @Inject
126
    private EPartService partService;
127
    private TaxonBase selection;
128

    
129
    private ConversationHolder conversation;
130

    
131
    private AcceptedGroupE4 acceptedGroup;
132
    private List<HomotypicalSynonymGroupE4> heterotypicSynonymGroups = new ArrayList<>();
133
    private MisappliedGroupE4 misappliedGroup;
134

    
135
    private DropTarget target;
136

    
137
    @Inject
138
    UISynchronize sync;
139

    
140
    private TaxonBase objectAffectedByLastOperation;
141

    
142
    @Inject
143
    private EMenuService menuService;
144

    
145
    @Inject
146
    private ESelectionService selService;
147

    
148
    @Inject
149
    private IEclipseContext context;
150

    
151
    @Inject
152
    private MDirtyable dirty;
153

    
154
    @Inject
155
    private MApplication application;
156

    
157
    private MPart thisPart;
158

    
159
    private TaxonEditorInputE4 input;
160

    
161
    private UndoContext undoContext;
162

    
163
    @Inject
164
    private IEventBroker eventBroker;
165

    
166
    @Inject
167
    public TaxonNameEditorE4() {
168
        undoContext = new UndoContext();
169
    }
170

    
171
    @PostConstruct
172
    public void createPartControl(Composite parent, MPart thisPart) {
173
        this.thisPart = thisPart;
174

    
175
        createManagedForm(parent);
176

    
177
        TaxeditorPartService.getInstance().addListener(TaxeditorPartService.PART_ACTIVATED, this);
178

    
179
    }
180

    
181
    protected void createManagedForm(Composite composite) {
182

    
183
        managedForm = new ManagedForm(composite) {
184

    
185
            @Override
186
            public void dirtyStateChanged() {
187
                dirty.setDirty(true);
188
            }
189

    
190
            @Override
191
            public boolean setInput(Object input) {
192
                if (input instanceof AbstractGroupedContainerE4) {
193
                    TaxonBase newSelection = ((AbstractGroupedContainerE4) input).getData();
194
                    if (selection != newSelection || TaxonNameEditorE4.this.isDirty()) {
195
                        selection = newSelection;
196
                        selService.setSelection(new StructuredSelection(selection));
197

    
198
                    }
199
                } else if (input == null) {
200
                    selection = null;
201
                    selService.setSelection(new StructuredSelection());
202
                }
203

    
204
                return super.setInput(input);
205
            }
206
        };
207

    
208
        scrolledForm = managedForm.getForm();
209
        parent = scrolledForm.getBody();
210

    
211
        parent.setData(taxon);
212

    
213
        TableWrapLayout layout = new TableWrapLayout();
214
        layout.leftMargin = 0;
215
        layout.rightMargin = 0;
216
        layout.topMargin = 0;
217
        layout.bottomMargin = 0;
218

    
219
        layout.verticalSpacing = 0;
220
        layout.horizontalSpacing = 0;
221

    
222
        parent.setLayout(layout);
223
        parent.setBackground(AbstractUtility.getColor(Resources.COLOR_COMPOSITE_BACKGROUND));
224
    }
225

    
226
    public void createOrUpdateNameComposites(boolean accepted, boolean heterotypicGroups, boolean misappliedNames) {
227

    
228
        if (accepted) {
229
            ContainerFactoryE4.createOrUpdateAcceptedTaxonsHomotypicGroup(this);
230
        }
231
        if (heterotypicGroups) {
232
            ContainerFactoryE4.createOrUpdateHeterotypicSynonymyGroups(this);
233
        }
234
        if (misappliedNames) {
235
            ContainerFactoryE4.createOrUpdateMisapplicationsGroup(this);
236
        }
237
        ContainerFactoryE4.setMenuToAllContainers(this);
238

    
239
        // Redraw composite
240
        parent.pack();
241
        managedForm.refresh();
242
        managedForm.reflow(true);
243

    
244
    }
245

    
246
    @Override
247
    public Taxon getTaxon() {
248
        return HibernateProxyHelper.deproxy(taxon);
249
    }
250

    
251
    @Override
252
    public void addOperation(AbstractPostOperation operation) {
253
        input.addOperation(operation);
254
    }
255

    
256
    public void setDirty() {
257
        managedForm.dirtyStateChanged();
258
    }
259

    
260
    @Focus
261
    public void setFocus() {
262
        // make sure to bind again if maybe in another view the conversation was
263
        // unbound
264
        if (conversation != null && !conversation.isBound()) {
265
            conversation.bind();
266
        }
267

    
268
        if (input != null) {
269
            if (getSelectedContainer() == null) {
270
                throw new IllegalStateException(Messages.TaxonNameEditor_THERE_SHOULD_ALWAYS_BE);
271
            }
272
            getSelectedContainer().setSelected();
273

    
274
            if (!input.getCdmEntitySession().isActive()) {
275
                input.bind();
276
            }
277

    
278

    
279

    
280

    
281
        }
282
        if(selection!=null){
283
            selService.setSelection(new StructuredSelection(selection));
284
        }
285
        // check permissions
286
        boolean doEnable = permissionsSatisfied();
287
        managedForm.getForm().setEnabled(doEnable);
288
        if (EditorUtil.isFactsVisible()){
289
            String partIdFactualData = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_DESCRIPTIVE_E4_FACTUALDATAPARTE4;
290
            EditorUtil.showPart(partIdFactualData, modelService, partService);
291
        }
292
        if (EditorUtil.isMediaVisible()){
293
            String partIdMediaData = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_MEDIA_E4_MEDIAVIEWPARTE4;
294
            EditorUtil.showPart(partIdMediaData, modelService, partService);
295
        }
296
        eventBroker.post(WorkbenchEventConstants.CURRENT_ACTIVE_EDITOR, this);
297
    }
298

    
299
    @Override
300
    public boolean permissionsSatisfied() {
301
        boolean doEnable = false;
302
        if (input != null){
303
            TaxonNode taxonNode = input.getTaxonNode();
304
            doEnable = CdmStore.currentAuthentiationHasPermission(taxonNode,
305
                    RequiredPermissions.TAXON_EDIT);
306

    
307
        }
308
        return doEnable;
309
    }
310

    
311
    @Override
312
    public ConversationHolder getConversationHolder() {
313
        return conversation;
314
    }
315

    
316
    /** {@inheritDoc} */
317
    @Override
318
    public void update(CdmDataChangeMap events) {
319
        // redraw();
320
    }
321

    
322
    /**
323
     * Redraws this editor return true on success
324
     *
325
     * @return a boolean.
326
     */
327
    public boolean redraw() {
328
        return redraw(true, true, true, true);
329
    }
330

    
331
    /**
332
     * {@inheritDoc}
333
     *
334
     * Redraws the editor controls
335
     */
336
    public boolean redraw(boolean focus, boolean accepted, boolean heterotypic, boolean misappliedNames) {
337

    
338
        createOrUpdateNameComposites(accepted, heterotypic, misappliedNames);
339

    
340
        if (focus) {
341
            setFocus();
342
        }
343

    
344
        return true;
345
    }
346

    
347
    @Override
348
    public boolean postOperation(Object objectAffectedByOperation) {
349
        if (objectAffectedByOperation instanceof TaxonBase) {
350
            objectAffectedByLastOperation = (TaxonBase) objectAffectedByOperation;
351
        }
352

    
353
        redraw(true, true, true, true);
354
        changed(objectAffectedByOperation);
355
        onComplete();
356
        return true;
357
    }
358

    
359
    public ManagedForm getManagedForm() {
360
        return managedForm;
361
    }
362

    
363
    /**
364
     * <p>
365
     * checkForEmptyNames
366
     * </p>
367
     *
368
     * @return true if there are empty names
369
     */
370
    public boolean checkForEmptyNames() {
371
        for (AbstractGroupedContainerE4 container : getGroupedContainers()) {
372
            if (container != null
373
                    && (container.getName() == null || StringUtils.isEmpty(container.getName().getTitleCache()))) {
374
                return true;
375
            }
376
        }
377
        return false;
378
    }
379

    
380
    public Set<AbstractGroupedContainerE4> getEmptyContainers() {
381
        Set<AbstractGroupedContainerE4> containersWithEmptyNames = new HashSet<>();
382

    
383
        for (AbstractGroupedContainerE4 container : getGroupedContainers()) {
384
            if (container.getName() == null || StringUtils.isEmpty(container.getName().getTitleCache())) {
385
                containersWithEmptyNames.add(container);
386
            }
387
        }
388

    
389
        return containersWithEmptyNames;
390
    }
391

    
392
    /** {@inheritDoc} */
393
    @Override
394
    @Persist
395
    public void save(IProgressMonitor monitor) {
396

    
397
        monitor.beginTask(Messages.TaxonNameEditor_SAVING_NAMES, getGroupedContainers().size());
398
        if (!conversation.isBound()) {
399
            conversation.bind();
400

    
401
        }
402
        conversation.commit(true);
403
        monitor.worked(1);
404

    
405
        // check for empty names
406
        if (checkForEmptyNames()) {
407
            MessageDialog.openWarning(AbstractUtility.getShell(), Messages.MultiPageTaxonEditor_NO_NAME_SPECIFIED,
408
                    Messages.MultiPageTaxonEditor_NO_NAME_SPECIFIED_MESSAGE);
409
            return;
410
        }
411
        for (AbstractGroupedContainerE4 container : getGroupedContainers()) {
412

    
413
            monitor.subTask(Messages.TaxonNameEditor_SAVING_COMPOSITES + container.getTaxonBase().getTitleCache());
414
            container.persistName();
415
            // because of missing cascading the concepts need to be saved
416
            // separately
417
            if (container instanceof ConceptContainerE4) {
418
                input.addToSaveNewConcept((Taxon) container.getData());
419
            }
420
            // In case the progress monitor was canceled throw an exception.
421
            if (monitor.isCanceled()) {
422
                throw new OperationCanceledException();
423
            }
424

    
425
            // Otherwise declare this step as done.
426
            monitor.worked(1);
427

    
428
        }
429
        input.setSync(sync);
430
        input.merge();
431
        // commit the conversation and start a new transaction immediately
432
        conversation.commit(true);
433

    
434
        dirty.setDirty(false);
435
        EventUtility.postEvent(WorkbenchEventConstants.REFRESH_NAVIGATOR, new TaxonNodeDto(input.getTaxonNode()));
436
        EventUtility.postEvent(WorkbenchEventConstants.SAVE_TAXON, true);
437
        // Stop the progress monitor.
438
        monitor.done();
439
    }
440

    
441
    public void init(TaxonEditorInputE4 input) {
442
        if (this.input != null) {
443
            this.input.dispose();
444
            // this.acceptedGroup = null;
445
            // this.heterotypicSynonymGroups.clear();
446
            // this.misappliedGroup = null;
447
        }
448
        if (!(input != null)) {
449
            MessagingUtils.error(this.getClass(), new Exception(Messages.TaxonNameEditor_INVALID_INPUT));
450
            return;
451
        }
452

    
453
        if (input.getAdapter(Taxon.class) != null) {
454
            taxon = CdmBase.deproxy(input.getAdapter(Taxon.class), Taxon.class);
455
        } else {
456
            MessagingUtils.error(this.getClass(), new Exception(Messages.TaxonNameEditor_INVALID_INPUT_TAXON_NULL));
457
            return;
458
        }
459

    
460
        this.input = input;
461
        conversation = input.getConversationHolder();
462

    
463
        createOrUpdateNameComposites(true, true, true);
464

    
465
        createDragSupport();
466

    
467
        setPartName();
468

    
469
        // set initial selection
470
        TaxonBase initiallySelectedTaxonBase = input.getInitiallySelectedTaxonBase();
471
        if (initiallySelectedTaxonBase != null) {
472
            selService.setSelection(new StructuredSelection(initiallySelectedTaxonBase));
473
            getContainer(initiallySelectedTaxonBase).setSelected();
474
        }
475
    }
476

    
477
    private void createDragSupport() {
478
        // Listen for names being dragged outside of existing homotypic groups -
479
        // user wants to create a new group
480
        Transfer[] types = new Transfer[] { CdmDataTransfer.getInstance() };
481
        int operations = DND.DROP_MOVE;
482
        if (target == null) {
483
            target = new DropTarget(parent, operations);
484
            target.setTransfer(types);
485
            target.addDropListener(new NameEditorDropTargetListenerE4(this));
486
        }
487
    }
488

    
489
    public AcceptedNameContainerE4 getAcceptedNameContainer() {
490
        return getAcceptedGroup().getAcceptedNameContainer();
491
    }
492

    
493
    public HomotypicalSynonymGroupE4 getHomotypicalGroupContainer(HomotypicalGroup homotypicalGroup) {
494
        for (HomotypicalSynonymGroupE4 group : getHeterotypicSynonymGroups()) {
495
            if (group.getGroup().equals(homotypicalGroup)) {
496
                return group;
497
            }
498
        }
499

    
500
        return null;
501
    }
502

    
503
    /**
504
     * <p>
505
     * getDirtyNames
506
     * </p>
507
     *
508
     * @return a Set containing all composites that have been edited
509
     */
510
    public Set<AbstractGroupedContainerE4> getDirtyNames() {
511
        Set<AbstractGroupedContainerE4> dirtyNames = new HashSet<>();
512

    
513
        for (AbstractGroupedContainerE4 composite : getGroupedContainers()) {
514
            if (composite.isDirty()) {
515
                dirtyNames.add(composite);
516
            }
517
        }
518

    
519
        return dirtyNames;
520
    }
521

    
522
    public List<AbstractGroupedContainerE4> getGroupedContainers() {
523
        List<AbstractGroupedContainerE4> groupedComposites = new ArrayList<>();
524

    
525
        for (AbstractGroupE4 group : getAllGroups()) {
526
            if (group != null) {
527
                groupedComposites.addAll(group.getGroupedContainers());
528
            }
529
        }
530

    
531
        return groupedComposites;
532
    }
533

    
534
    public List<AbstractGroupE4> getAllGroups() {
535
        List<AbstractGroupE4> allGroups = new ArrayList<>();
536

    
537
        allGroups.add(getAcceptedGroup());
538

    
539
        heterotypicSynonymGroups = getHeterotypicSynonymGroups();
540

    
541
        if (heterotypicSynonymGroups != null) {
542
            allGroups.addAll(heterotypicSynonymGroups);
543
        }
544

    
545
        if (misappliedGroup != null) {
546
            allGroups.add(misappliedGroup);
547
        }
548

    
549
        return allGroups;
550
    }
551

    
552
    @Override
553
    public IEclipseContext getContext() {
554
        return context;
555
    }
556

    
557
    @Override
558
    public boolean isDirty() {
559
        return dirty.isDirty();
560
    }
561

    
562
    @PreDestroy
563
    public void dispose() {
564
        if (conversation != null) {
565
            conversation.unregisterForDataStoreChanges(this);
566
            conversation.close();
567
        }
568
        if (input != null) {
569
            input.dispose();
570
        }
571
        dirty.setDirty(false);
572
        eventBroker.post(WorkbenchEventConstants.CURRENT_ACTIVE_EDITOR, null);
573
    }
574

    
575
    /** {@inheritDoc} */
576
    @Override
577
    public void selectionChanged(IWorkbenchPart part, ISelection selection) {
578
        System.err.println("Bla");
579
    }
580

    
581
    public AbstractGroupedContainerE4 getSelectedContainer() {
582
        return (selection != null) ? getContainer(selection) : getAcceptedNameContainer();
583
    }
584

    
585
    @Override
586
    public void dragEntered() {
587
        // TODO change this
588
        getControl().setBackground(AbstractUtility.getColor(Resources.COLOR_DRAG_ENTER));
589
    }
590

    
591
    @Override
592
    public void dragLeft() {
593
        getControl().setBackground(AbstractUtility.getColor(Resources.COLOR_COMPOSITE_BACKGROUND));
594
    }
595

    
596
    public void setMisapplicationsGroup(MisappliedGroupE4 misappliedGroup) {
597
        this.misappliedGroup = misappliedGroup;
598
    }
599

    
600
    public FormToolkit getToolkit() {
601
        return managedForm.getToolkit();
602
    }
603

    
604
    public List<HomotypicalSynonymGroupE4> getHeterotypicSynonymGroups() {
605
        return heterotypicSynonymGroups;
606
    }
607

    
608
    public void addHeterotypicSynonymGroup(HomotypicalSynonymGroupE4 group) {
609
        heterotypicSynonymGroups.add(group);
610
    }
611

    
612
    public AcceptedGroupE4 getAcceptedGroup() {
613
        return acceptedGroup;
614
    }
615

    
616
    public void setAcceptedGroup(AcceptedGroupE4 acceptedGroup) {
617
        this.acceptedGroup = acceptedGroup;
618
    }
619

    
620
    public MisappliedGroupE4 getMisappliedGroup() {
621
        return misappliedGroup;
622
    }
623

    
624
    public boolean isActive() {
625
        return this.equals(AbstractUtility.getActivePart());
626
    }
627

    
628
    @Override
629
    public boolean onComplete() {
630
        getContainer(objectAffectedByLastOperation).setSelected();
631
        return true;
632
    }
633

    
634
    /** {@inheritDoc} */
635
    @Override
636
    public void partChanged(Integer eventType, IWorkbenchPartReference partRef) {
637
        if (!partRef.getPart(false).equals(this)) {
638

    
639

    
640
        }
641
    }
642

    
643
    public void removeGroup(AbstractGroupE4 group) {
644
        if (group != null) {
645
            group.dispose();
646

    
647
            // if (heterotypicSynonymGroups != null) {
648
            heterotypicSynonymGroups.remove(group);
649
            // }
650
        }
651
    }
652

    
653
    public AbstractGroupedContainerE4 getContainer(TaxonBase taxonBase) {
654
        @SuppressWarnings("rawtypes")
655
        List<AbstractGroupedContainerE4> groupedContainers = getGroupedContainers();
656
        for (AbstractGroupedContainerE4 container : groupedContainers) {
657
            if (container.getData().equals(taxonBase) && container.getNameViewer().getTextWidget() != null) {
658
                return container;
659
            }
660
        }
661
        return getAcceptedNameContainer();
662
    }
663

    
664
    public void setOnError() {
665
        Color disabledColor = AbstractUtility.getColor(Resources.COLOR_EDITOR_ERROR);
666
        setEnabled(false, disabledColor);
667
    }
668

    
669
    public void setDisabled() {
670
        Color disabledColor = AbstractUtility.getColor(Resources.COLOR_TEXT_DISABLED_BACKGROUND);
671
        setEnabled(false, disabledColor);
672
    }
673

    
674
    protected void setEnabled(boolean enabled, Color background) {
675

    
676
        for (AbstractGroupedContainerE4 groupedContainer : getGroupedContainers()) {
677
            groupedContainer.setEnabled(enabled);
678
        }
679

    
680
        // send an empty selection to the current provider - TODO only on error
681
        // ???
682
        if (!enabled) {
683
            getManagedForm().setInput(null);
684

    
685
            for (AbstractGroupedContainerE4 groupedContainer : getGroupedContainers()) {
686
                groupedContainer.setBackground(background);
687
            }
688
        }
689
        getControl().setBackground(background);
690
    }
691

    
692
    @Override
693
    public void changed(Object element) {
694
        // setDirty(true);
695
        // if the attribute is null then do not set the dirty flag -> hotfix for
696
        // the problem that for tasks done in service methods the changes are
697
        // saved automatically
698
        if (element != null) {
699
            dirty.setDirty(true);
700
            // refresh part title
701
            // TODO: refresh taxon node in taxon navigator
702
            setPartName();
703
        }
704

    
705
        if (element instanceof TaxonBase) {
706
            AbstractGroupedContainerE4 container = getContainer((TaxonBase) element);
707
            if (container != null) {
708
                container.refresh();
709
            }
710
        }
711

    
712
        if (element instanceof TaxonRelationship) {
713
            AbstractGroupedContainerE4 container = getContainer(((TaxonRelationship) element).getFromTaxon());
714
            if (container != null) {
715
                container.refresh();
716
            }
717
        }
718
    }
719

    
720
    public void setPartName() {
721
        // FIXME: temporary fix for #6437 to avoid outdated title caches
722
        thisPart.setLabel(this.taxon.getName().generateFullTitle());
723
        // thisPart.setLabel(this.taxon.getName().getFullTitleCache());
724
    }
725

    
726
    @Override
727
    public void forceDirty() {
728
        setDirty();
729
    }
730

    
731
    public IUndoContext getUndoContext() {
732
        return undoContext;
733
    }
734

    
735
    @Override
736
    public Composite getControl() {
737
        return managedForm.getForm().getBody();
738
    }
739

    
740
    public EMenuService getMenuService() {
741
        return menuService;
742
    }
743

    
744
    public ESelectionService getSelectionService() {
745
        return selService;
746
    }
747

    
748
    /**
749
     * {@inheritDoc}
750
     */
751
    @Override
752
    public boolean canAttachMedia() {
753
        return true;
754
    }
755

    
756
    public TaxonEditorInputE4 getEditorInput() {
757
        return input;
758
    }
759

    
760
    /**
761
     * {@inheritDoc}
762
     */
763
    @Override
764
    public TaxonNameEditorE4 getEditor() {
765
        return this;
766
    }
767

    
768
    @Inject
769
    @Optional
770
    private void updateView(@UIEventTopic(WorkbenchEventConstants.REFRESH_NAME_EDITOR) CdmBase cdmbase) {
771

    
772
        if ((EventUtility.getTaxonEditor() != null && EventUtility.getTaxonEditor().equals(this))
773
                || (this.taxon != null && (this.taxon.equals(cdmbase)
774
                        || (this.taxon.getName() != null && this.taxon.getName().equals(cdmbase))))) {
775
            this.redraw(false, true, true, true);
776
            this.setDirty();
777
            if (cdmbase instanceof TaxonBase) {
778
                this.selection = (TaxonBase) cdmbase;
779
            }
780

    
781
        }
782
    }
783

    
784
    @Inject
785
    @Optional
786
    private void updateView(@UIEventTopic(WorkbenchEventConstants.REFRESH_NAME_EDITOR) List<CdmBase> cdmBases) {
787

    
788
        for (CdmBase cdmBase: cdmBases){
789
            if (cdmBase instanceof Taxon || cdmBase instanceof TaxonName){
790
                if ((EventUtility.getTaxonEditor() != null && EventUtility.getTaxonEditor().equals(this))
791
                        || (this.taxon != null && (this.taxon.equals(cdmBase)
792
                                || (this.taxon.getName() != null && this.taxon.getName().equals(cdmBase))))) {
793
                    EPartService partService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EPartService.class);
794
                    EModelService modelService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EModelService.class);
795
                    EditorUtil.openTaxonBaseE4(this.getTaxon().getUuid(), modelService, partService, application);
796

    
797
                }
798
            }
799
        }
800
    }
801

    
802
    @Inject
803
    @Optional
804
    private void updateView(@UIEventTopic(WorkbenchEventConstants.REFRESH_NAME_EDITOR) UUID cdmbaseUuid) {
805

    
806
        if (this.taxon.getUuid().equals(cdmbaseUuid)) {
807
            TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstanceFromTaxonBase(cdmbaseUuid);
808
            init(input);
809
        }
810
    }
811

    
812
    @Inject
813
    @Optional
814
    private void updatefromDelete(@UIEventTopic(WorkbenchEventConstants.DELETE_DERIVATIVE) DeleteResult result) {
815
        if (taxon.getName() == null) {
816
            return;
817
        }
818
        Set<DerivedUnit> typeDesignationSpecimens = new HashSet<>();
819
        this.taxon.getName().getSpecimenTypeDesignations()
820
                .forEach(designation -> typeDesignationSpecimens.add(designation.getTypeSpecimen()));
821
        // check if any deleted object was a type specimen
822
        if (result.getUpdatedObjects().stream()
823
                // filter only DerivedUnits
824
                .filter(cdmBase -> cdmBase.isInstanceOf(DerivedUnit.class))
825
                // deproxy from CdmBase to DerivedUnit
826
                .map(unit -> HibernateProxyHelper.deproxy(unit, DerivedUnit.class))
827
                // check for match in type designations
828
                .anyMatch(unit -> typeDesignationSpecimens.contains(unit))) {
829
            EditorUtil.updateEditor(this.input.getTaxonNode(), this);
830

    
831
        }
832

    
833
    }
834

    
835
    /**
836
     * {@inheritDoc}
837
     */
838
    @Override
839
    public void update() {
840
        EPartService partService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EPartService.class);
841
        EModelService modelService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EModelService.class);
842
        EditorUtil.openTaxonBaseE4(this.getTaxon().getUuid(), modelService, partService, application);
843

    
844
    }
845

    
846
    @Override
847
    public TaxonNode getTaxonNode() {
848
        return input.getTaxonNode();
849
    }
850

    
851
}
(2-2/2)