Project

General

Profile

Download (27.8 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
        if (EditorUtil.isFactsVisible()){
268
            EditorUtil.showFacts(modelService, partService);
269
        }
270
        if (EditorUtil.isMediaVisible()){
271
            EditorUtil.showMedia(modelService, partService);
272
        }
273
        if (input != null) {
274
            if (getSelectedContainer() == null) {
275
                throw new IllegalStateException(Messages.TaxonNameEditor_THERE_SHOULD_ALWAYS_BE);
276
            }
277
            getSelectedContainer().setSelected();
278

    
279
            if (!input.getCdmEntitySession().isActive()) {
280
                input.bind();
281
            }
282

    
283

    
284

    
285

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

    
304
    @Override
305
    public boolean permissionsSatisfied() {
306
        boolean doEnable = false;
307
        if (input != null){
308
            TaxonNode taxonNode = input.getTaxonNode();
309
            doEnable = CdmStore.currentAuthentiationHasPermission(taxonNode,
310
                    RequiredPermissions.TAXON_EDIT);
311

    
312
        }
313
        return doEnable;
314
    }
315

    
316
    @Override
317
    public ConversationHolder getConversationHolder() {
318
        return conversation;
319
    }
320

    
321
    /** {@inheritDoc} */
322
    @Override
323
    public void update(CdmDataChangeMap events) {
324
        // redraw();
325
    }
326

    
327
    /**
328
     * Redraws this editor return true on success
329
     *
330
     * @return a boolean.
331
     */
332
    public boolean redraw() {
333
        return redraw(true, true, true, true);
334
    }
335

    
336
    /**
337
     * {@inheritDoc}
338
     *
339
     * Redraws the editor controls
340
     */
341
    public boolean redraw(boolean focus, boolean accepted, boolean heterotypic, boolean misappliedNames) {
342

    
343
        createOrUpdateNameComposites(accepted, heterotypic, misappliedNames);
344

    
345
        if (focus) {
346
            setFocus();
347
        }
348

    
349
        return true;
350
    }
351

    
352
    @Override
353
    public boolean postOperation(Object objectAffectedByOperation) {
354
        if (objectAffectedByOperation instanceof TaxonBase) {
355
            objectAffectedByLastOperation = (TaxonBase) objectAffectedByOperation;
356
        }
357

    
358
        redraw(true, true, true, true);
359
        changed(objectAffectedByOperation);
360
        onComplete();
361
        return true;
362
    }
363

    
364
    public ManagedForm getManagedForm() {
365
        return managedForm;
366
    }
367

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

    
385
    public Set<AbstractGroupedContainerE4> getEmptyContainers() {
386
        Set<AbstractGroupedContainerE4> containersWithEmptyNames = new HashSet<>();
387

    
388
        for (AbstractGroupedContainerE4 container : getGroupedContainers()) {
389
            if (container.getName() == null || StringUtils.isEmpty(container.getName().getTitleCache())) {
390
                containersWithEmptyNames.add(container);
391
            }
392
        }
393

    
394
        return containersWithEmptyNames;
395
    }
396

    
397
    /** {@inheritDoc} */
398
    @Override
399
    @Persist
400
    public void save(IProgressMonitor monitor) {
401

    
402
        monitor.beginTask(Messages.TaxonNameEditor_SAVING_NAMES, getGroupedContainers().size());
403
        if (!conversation.isBound()) {
404
            conversation.bind();
405

    
406
        }
407
        conversation.commit(true);
408
        monitor.worked(1);
409

    
410
        // check for empty names
411
        if (checkForEmptyNames()) {
412
            MessageDialog.openWarning(AbstractUtility.getShell(), Messages.MultiPageTaxonEditor_NO_NAME_SPECIFIED,
413
                    Messages.MultiPageTaxonEditor_NO_NAME_SPECIFIED_MESSAGE);
414
            return;
415
        }
416
        for (AbstractGroupedContainerE4 container : getGroupedContainers()) {
417

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

    
430
            // Otherwise declare this step as done.
431
            monitor.worked(1);
432

    
433
        }
434
        input.setSync(sync);
435
        input.merge();
436
        // commit the conversation and start a new transaction immediately
437
        conversation.commit(true);
438

    
439
        dirty.setDirty(false);
440
        EventUtility.postEvent(WorkbenchEventConstants.REFRESH_NAVIGATOR, new TaxonNodeDto(input.getTaxonNode()));
441
        EventUtility.postEvent(WorkbenchEventConstants.SAVE_TAXON, true);
442
        // Stop the progress monitor.
443
        monitor.done();
444
    }
445

    
446
    public void init(TaxonEditorInputE4 input) {
447
        if (this.input != null) {
448
            this.input.dispose();
449
            // this.acceptedGroup = null;
450
            // this.heterotypicSynonymGroups.clear();
451
            // this.misappliedGroup = null;
452
        }
453
        if (!(input != null)) {
454
            MessagingUtils.error(this.getClass(), new Exception(Messages.TaxonNameEditor_INVALID_INPUT));
455
            return;
456
        }
457

    
458
        if (input.getAdapter(Taxon.class) != null) {
459
            taxon = CdmBase.deproxy(input.getAdapter(Taxon.class), Taxon.class);
460
        } else {
461
            MessagingUtils.error(this.getClass(), new Exception(Messages.TaxonNameEditor_INVALID_INPUT_TAXON_NULL));
462
            return;
463
        }
464

    
465
        this.input = input;
466
        conversation = input.getConversationHolder();
467

    
468
        createOrUpdateNameComposites(true, true, true);
469

    
470
        createDragSupport();
471

    
472
        setPartName();
473

    
474
        // set initial selection
475
        TaxonBase initiallySelectedTaxonBase = input.getInitiallySelectedTaxonBase();
476
        if (initiallySelectedTaxonBase != null) {
477
            selService.setSelection(new StructuredSelection(initiallySelectedTaxonBase));
478
            getContainer(initiallySelectedTaxonBase).setSelected();
479
        }
480
    }
481

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

    
494
    public AcceptedNameContainerE4 getAcceptedNameContainer() {
495
        return getAcceptedGroup().getAcceptedNameContainer();
496
    }
497

    
498
    public HomotypicalSynonymGroupE4 getHomotypicalGroupContainer(HomotypicalGroup homotypicalGroup) {
499
        for (HomotypicalSynonymGroupE4 group : getHeterotypicSynonymGroups()) {
500
            if (group.getGroup().equals(homotypicalGroup)) {
501
                return group;
502
            }
503
        }
504

    
505
        return null;
506
    }
507

    
508
    /**
509
     * <p>
510
     * getDirtyNames
511
     * </p>
512
     *
513
     * @return a Set containing all composites that have been edited
514
     */
515
    public Set<AbstractGroupedContainerE4> getDirtyNames() {
516
        Set<AbstractGroupedContainerE4> dirtyNames = new HashSet<>();
517

    
518
        for (AbstractGroupedContainerE4 composite : getGroupedContainers()) {
519
            if (composite.isDirty()) {
520
                dirtyNames.add(composite);
521
            }
522
        }
523

    
524
        return dirtyNames;
525
    }
526

    
527
    public List<AbstractGroupedContainerE4> getGroupedContainers() {
528
        List<AbstractGroupedContainerE4> groupedComposites = new ArrayList<>();
529

    
530
        for (AbstractGroupE4 group : getAllGroups()) {
531
            if (group != null) {
532
                groupedComposites.addAll(group.getGroupedContainers());
533
            }
534
        }
535

    
536
        return groupedComposites;
537
    }
538

    
539
    public List<AbstractGroupE4> getAllGroups() {
540
        List<AbstractGroupE4> allGroups = new ArrayList<>();
541

    
542
        allGroups.add(getAcceptedGroup());
543

    
544
        heterotypicSynonymGroups = getHeterotypicSynonymGroups();
545

    
546
        if (heterotypicSynonymGroups != null) {
547
            allGroups.addAll(heterotypicSynonymGroups);
548
        }
549

    
550
        if (misappliedGroup != null) {
551
            allGroups.add(misappliedGroup);
552
        }
553

    
554
        return allGroups;
555
    }
556

    
557
    @Override
558
    public IEclipseContext getContext() {
559
        return context;
560
    }
561

    
562
    @Override
563
    public boolean isDirty() {
564
        return dirty.isDirty();
565
    }
566

    
567
    @PreDestroy
568
    public void dispose() {
569
        if (conversation != null) {
570
            conversation.unregisterForDataStoreChanges(this);
571
            conversation.close();
572
        }
573
        if (input != null) {
574
            input.dispose();
575
        }
576
        dirty.setDirty(false);
577
        eventBroker.post(WorkbenchEventConstants.CURRENT_ACTIVE_EDITOR, null);
578
    }
579

    
580
    /** {@inheritDoc} */
581
    @Override
582
    public void selectionChanged(IWorkbenchPart part, ISelection selection) {
583
        System.err.println("Bla");
584
    }
585

    
586
    public AbstractGroupedContainerE4 getSelectedContainer() {
587
        return (selection != null) ? getContainer(selection) : getAcceptedNameContainer();
588
    }
589

    
590
    @Override
591
    public void dragEntered() {
592
        // TODO change this
593
        getControl().setBackground(AbstractUtility.getColor(Resources.COLOR_DRAG_ENTER));
594
    }
595

    
596
    @Override
597
    public void dragLeft() {
598
        getControl().setBackground(AbstractUtility.getColor(Resources.COLOR_COMPOSITE_BACKGROUND));
599
    }
600

    
601
    public void setMisapplicationsGroup(MisappliedGroupE4 misappliedGroup) {
602
        this.misappliedGroup = misappliedGroup;
603
    }
604

    
605
    public FormToolkit getToolkit() {
606
        return managedForm.getToolkit();
607
    }
608

    
609
    public List<HomotypicalSynonymGroupE4> getHeterotypicSynonymGroups() {
610
        return heterotypicSynonymGroups;
611
    }
612

    
613
    public void addHeterotypicSynonymGroup(HomotypicalSynonymGroupE4 group) {
614
        heterotypicSynonymGroups.add(group);
615
    }
616

    
617
    public AcceptedGroupE4 getAcceptedGroup() {
618
        return acceptedGroup;
619
    }
620

    
621
    public void setAcceptedGroup(AcceptedGroupE4 acceptedGroup) {
622
        this.acceptedGroup = acceptedGroup;
623
    }
624

    
625
    public MisappliedGroupE4 getMisappliedGroup() {
626
        return misappliedGroup;
627
    }
628

    
629
    public boolean isActive() {
630
        return this.equals(AbstractUtility.getActivePart());
631
    }
632

    
633
    @Override
634
    public boolean onComplete() {
635
        getContainer(objectAffectedByLastOperation).setSelected();
636
        return true;
637
    }
638

    
639
    /** {@inheritDoc} */
640
    @Override
641
    public void partChanged(Integer eventType, IWorkbenchPartReference partRef) {
642
        if (!partRef.getPart(false).equals(this)) {
643

    
644

    
645
        }
646
    }
647

    
648
    public void removeGroup(AbstractGroupE4 group) {
649
        if (group != null) {
650
            group.dispose();
651

    
652
            // if (heterotypicSynonymGroups != null) {
653
            heterotypicSynonymGroups.remove(group);
654
            // }
655
        }
656
    }
657

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

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

    
674
    public void setDisabled() {
675
        Color disabledColor = AbstractUtility.getColor(Resources.COLOR_TEXT_DISABLED_BACKGROUND);
676
        setEnabled(false, disabledColor);
677
    }
678

    
679
    protected void setEnabled(boolean enabled, Color background) {
680

    
681
        for (AbstractGroupedContainerE4 groupedContainer : getGroupedContainers()) {
682
            groupedContainer.setEnabled(enabled);
683
        }
684

    
685
        // send an empty selection to the current provider - TODO only on error
686
        // ???
687
        if (!enabled) {
688
            getManagedForm().setInput(null);
689

    
690
            for (AbstractGroupedContainerE4 groupedContainer : getGroupedContainers()) {
691
                groupedContainer.setBackground(background);
692
            }
693
        }
694
        getControl().setBackground(background);
695
    }
696

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

    
710
        if (element instanceof TaxonBase) {
711
            AbstractGroupedContainerE4 container = getContainer((TaxonBase) element);
712
            if (container != null) {
713
                container.refresh();
714
            }
715
        }
716

    
717
        if (element instanceof TaxonRelationship) {
718
            AbstractGroupedContainerE4 container = getContainer(((TaxonRelationship) element).getFromTaxon());
719
            if (container != null) {
720
                container.refresh();
721
            }
722
        }
723
    }
724

    
725
    public void setPartName() {
726
        // FIXME: temporary fix for #6437 to avoid outdated title caches
727
        thisPart.setLabel(this.taxon.getName().generateFullTitle());
728
        // thisPart.setLabel(this.taxon.getName().getFullTitleCache());
729
    }
730

    
731
    @Override
732
    public void forceDirty() {
733
        setDirty();
734
    }
735

    
736
    public IUndoContext getUndoContext() {
737
        return undoContext;
738
    }
739

    
740
    @Override
741
    public Composite getControl() {
742
        return managedForm.getForm().getBody();
743
    }
744

    
745
    public EMenuService getMenuService() {
746
        return menuService;
747
    }
748

    
749
    public ESelectionService getSelectionService() {
750
        return selService;
751
    }
752

    
753
    /**
754
     * {@inheritDoc}
755
     */
756
    @Override
757
    public boolean canAttachMedia() {
758
        return true;
759
    }
760

    
761
    public TaxonEditorInputE4 getEditorInput() {
762
        return input;
763
    }
764

    
765
    /**
766
     * {@inheritDoc}
767
     */
768
    @Override
769
    public TaxonNameEditorE4 getEditor() {
770
        return this;
771
    }
772

    
773
    @Inject
774
    @Optional
775
    private void updateView(@UIEventTopic(WorkbenchEventConstants.REFRESH_NAME_EDITOR) CdmBase cdmbase) {
776

    
777
        if ((EventUtility.getTaxonEditor() != null && EventUtility.getTaxonEditor().equals(this))
778
                || (this.taxon != null && (this.taxon.equals(cdmbase)
779
                        || (this.taxon.getName() != null && this.taxon.getName().equals(cdmbase))))) {
780
            this.redraw(false, true, true, true);
781
            this.setDirty();
782
            if (cdmbase instanceof TaxonBase) {
783
                this.selection = (TaxonBase) cdmbase;
784
            }
785

    
786
        }
787
    }
788

    
789
    @Inject
790
    @Optional
791
    private void updateView(@UIEventTopic(WorkbenchEventConstants.REFRESH_NAME_EDITOR) List<CdmBase> cdmBases) {
792

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

    
802
                }
803
            }
804
        }
805
    }
806

    
807
    @Inject
808
    @Optional
809
    private void updateView(@UIEventTopic(WorkbenchEventConstants.REFRESH_NAME_EDITOR) UUID cdmbaseUuid) {
810

    
811
        if (this.taxon.getUuid().equals(cdmbaseUuid)) {
812
            TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstanceFromTaxonBase(cdmbaseUuid);
813
            init(input);
814
        }
815
    }
816

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

    
836
        }
837

    
838
    }
839

    
840
    /**
841
     * {@inheritDoc}
842
     */
843
    @Override
844
    public void update() {
845
        EPartService partService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EPartService.class);
846
        EModelService modelService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EModelService.class);
847
        EditorUtil.openTaxonBaseE4(this.getTaxon().getUuid(), modelService, partService, application);
848

    
849
    }
850

    
851
    @Override
852
    public TaxonNode getTaxonNode() {
853
        return input.getTaxonNode();
854
    }
855

    
856
}
(2-2/2)