Project

General

Profile

Download (22.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.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.ESelectionService;
38
import org.eclipse.jface.dialogs.MessageDialog;
39
import org.eclipse.jface.viewers.ISelection;
40
import org.eclipse.jface.viewers.ISelectionProvider;
41
import org.eclipse.jface.viewers.StructuredSelection;
42
import org.eclipse.swt.dnd.DND;
43
import org.eclipse.swt.dnd.DropTarget;
44
import org.eclipse.swt.dnd.Transfer;
45
import org.eclipse.swt.graphics.Color;
46
import org.eclipse.swt.widgets.Composite;
47
import org.eclipse.ui.ISelectionListener;
48
import org.eclipse.ui.IWorkbenchPart;
49
import org.eclipse.ui.IWorkbenchPartReference;
50
import org.eclipse.ui.forms.ManagedForm;
51
import org.eclipse.ui.forms.widgets.FormToolkit;
52
import org.eclipse.ui.forms.widgets.ScrolledForm;
53
import org.eclipse.ui.forms.widgets.TableWrapLayout;
54

    
55
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
56
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
57
import eu.etaxonomy.cdm.api.service.DeleteResult;
58
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
59
import eu.etaxonomy.cdm.model.common.CdmBase;
60
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
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.hibernate.CdmDataChangeMap;
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.TaxonEditorInputE4;
72
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
73
import eu.etaxonomy.taxeditor.editor.name.e4.container.AbstractGroupE4;
74
import eu.etaxonomy.taxeditor.editor.name.e4.container.AbstractGroupedContainerE4;
75
import eu.etaxonomy.taxeditor.editor.name.e4.container.AcceptedGroupE4;
76
import eu.etaxonomy.taxeditor.editor.name.e4.container.AcceptedNameContainerE4;
77
import eu.etaxonomy.taxeditor.editor.name.e4.container.ConceptContainerE4;
78
import eu.etaxonomy.taxeditor.editor.name.e4.container.ContainerFactoryE4;
79
import eu.etaxonomy.taxeditor.editor.name.e4.container.HomotypicalSynonymGroupE4;
80
import eu.etaxonomy.taxeditor.editor.name.e4.container.MisappliedGroupE4;
81
import eu.etaxonomy.taxeditor.editor.name.e4.dnd.NameEditorDropTargetListenerE4;
82
import eu.etaxonomy.taxeditor.event.EventUtility;
83
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
84
import eu.etaxonomy.taxeditor.model.AbstractUtility;
85
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
86
import eu.etaxonomy.taxeditor.model.IPartChangeListener;
87
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
88
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
89
import eu.etaxonomy.taxeditor.model.IPartContentHasMedia;
90
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
91
import eu.etaxonomy.taxeditor.model.MessagingUtils;
92
import eu.etaxonomy.taxeditor.model.TaxeditorPartService;
93
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
94
import eu.etaxonomy.taxeditor.preference.Resources;
95
import eu.etaxonomy.taxeditor.security.RequiredPermissions;
96
import eu.etaxonomy.taxeditor.store.CdmStore;
97
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
98

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

    
109
	private Taxon taxon;
110

    
111
	private ManagedForm managedForm;
112
	private ScrolledForm scrolledForm;
113
	private Composite parent;
114
	private ISelectionProvider simpleSelectionProvider;
115

    
116
	private TaxonBase selection;
117

    
118
	private ConversationHolder conversation;
119

    
120
	private AcceptedGroupE4 acceptedGroup;
121
	private List<HomotypicalSynonymGroupE4> heterotypicSynonymGroups = new ArrayList<>();
122
	private MisappliedGroupE4 misappliedGroup;
123

    
124
	private DropTarget target;
125

    
126
	@Inject
127
	UISynchronize sync;
128

    
129
	private TaxonBase objectAffectedByLastOperation;
130

    
131
	@Inject
132
	private EMenuService menuService;
133

    
134
	@Inject
135
	private ESelectionService selService;
136

    
137
	@Inject
138
	private IEclipseContext context;
139

    
140
	@Inject
141
	private MDirtyable dirty;
142

    
143
	private MPart thisPart;
144

    
145
    private TaxonEditorInputE4 input;
146

    
147
    private UndoContext undoContext;
148

    
149
    @Inject
150
    private IEventBroker eventBroker;
151

    
152
	@Inject
153
	public TaxonNameEditorE4() {
154
	    undoContext = new UndoContext();
155
	}
156

    
157

    
158
	@PostConstruct
159
    public void createPartControl(Composite parent, MPart thisPart) {
160
	    this.thisPart = thisPart;
161

    
162
        createManagedForm(parent);
163

    
164
		TaxeditorPartService.getInstance().addListener(
165
				TaxeditorPartService.PART_ACTIVATED, this);
166

    
167
	}
168

    
169
	protected void createManagedForm(Composite composite) {
170

    
171
		managedForm = new ManagedForm(composite) {
172

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

    
178
			@Override
179
			public boolean setInput(Object input) {
180
				if (input instanceof AbstractGroupedContainerE4) {
181
				    TaxonBase newSelection = ((AbstractGroupedContainerE4) input).getData();
182
				    if(selection!=newSelection || TaxonNameEditorE4.this.isDirty()){
183
				        selection = newSelection;
184
				        selService.setSelection(new StructuredSelection(selection));
185
				    }
186
				}else if(input == null){
187
					selection = null;
188
                    selService.setSelection(new StructuredSelection());
189
				}
190

    
191

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

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

    
199
		parent.setData(taxon);
200

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

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

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

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

    
217

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

    
229

    
230

    
231
		// Redraw composite
232
	    parent.pack();
233
		managedForm.refresh();
234
		managedForm.reflow(true);
235

    
236

    
237
	}
238

    
239
	@Override
240
    public Taxon getTaxon() {
241
		return HibernateProxyHelper.deproxy(taxon);
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 unbound
251
        if(conversation!=null && !conversation.isBound()){
252
            conversation.bind();
253
        }
254

    
255
	    if(input!=null){
256
	        if (getSelectedContainer() == null) {
257
	            throw new IllegalStateException(
258
	                    Messages.TaxonNameEditor_THERE_SHOULD_ALWAYS_BE);
259
	        }
260
	        getSelectedContainer().setSelected();
261
	        if (!input.getCdmEntitySession().isActive()){
262
	            input.bind();
263
	        }
264

    
265
	        // check permissions
266
	        boolean doEnable = permissionsSatisfied();
267
	        managedForm.getForm().setEnabled(doEnable);
268
	    }
269
        eventBroker.post(WorkbenchEventConstants.CURRENT_ACTIVE_EDITOR, this);
270
	}
271

    
272
	@Override
273
	public boolean permissionsSatisfied() {
274
		TaxonNode taxonNode = input.getTaxonNode();
275
		boolean doEnable = CdmStore.currentAuthentiationHasPermission(taxonNode.getTaxon(), RequiredPermissions.TAXON_EDIT);
276
		return doEnable;
277
	}
278

    
279
	@Override
280
    public ConversationHolder getConversationHolder() {
281
		return conversation;
282
	}
283

    
284
	/** {@inheritDoc} */
285
	@Override
286
    public void update(CdmDataChangeMap events) {
287
		// redraw();
288
	}
289

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

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

    
306
		createOrUpdateNameComposites(accepted, heterotypic, misappliedNames);
307

    
308
		if (focus) {
309
			setFocus();
310
		}
311

    
312
		return true;
313
	}
314

    
315
	@Override
316
    public boolean postOperation(Object objectAffectedByOperation) {
317

    
318
		changed(objectAffectedByOperation);
319

    
320
		redraw(true, true,true, true);
321

    
322
		if (objectAffectedByOperation instanceof TaxonBase) {
323
			objectAffectedByLastOperation = (TaxonBase) objectAffectedByOperation;
324
		}
325

    
326
		return true;
327
	}
328

    
329
	public ManagedForm getManagedForm() {
330
		return managedForm;
331
	}
332

    
333

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

    
351
	public Set<AbstractGroupedContainerE4> getEmptyContainers() {
352
		Set<AbstractGroupedContainerE4> containersWithEmptyNames = new HashSet<>();
353

    
354
		for (AbstractGroupedContainerE4 container : getGroupedContainers()) {
355
			if (container.getName() == null
356
					|| StringUtils.isEmpty(container.getName().getTitleCache())) {
357
				containersWithEmptyNames.add(container);
358
			}
359
		}
360

    
361
		return containersWithEmptyNames;
362
	}
363

    
364
	/** {@inheritDoc} */
365
	@Override
366
    @Persist
367
	public void save(IProgressMonitor monitor) {
368

    
369
	    monitor.beginTask(Messages.TaxonNameEditor_SAVING_NAMES, getGroupedContainers().size());
370
	    if (!conversation.isBound()) {
371
	        conversation.bind();
372

    
373
	    }
374
	    conversation.commit(true);
375
	    monitor.worked(1);
376

    
377
	    // check for empty names
378
	    if (checkForEmptyNames()) {
379
	        MessageDialog.openWarning(AbstractUtility.getShell(), Messages.MultiPageTaxonEditor_NO_NAME_SPECIFIED,
380
	                Messages.MultiPageTaxonEditor_NO_NAME_SPECIFIED_MESSAGE);
381
	        return;
382
	    }
383
	    for (AbstractGroupedContainerE4 container : getGroupedContainers()) {
384

    
385
	        monitor.subTask(Messages.TaxonNameEditor_SAVING_COMPOSITES
386
	                + container.getTaxonBase().getTitleCache());
387
	        container.persistName();
388
	        //because of missing cascading the concepts need to be saved separately
389
	        if (container instanceof ConceptContainerE4){
390
	            input.addToSaveNewConcept((Taxon)container.getData());
391
	        }
392
	        // In case the progress monitor was canceled throw an exception.
393
	        if (monitor.isCanceled()) {
394
	            throw new OperationCanceledException();
395
	        }
396

    
397
	     // Otherwise declare this step as done.
398
	        monitor.worked(1);
399

    
400
	    }
401
	    input.setSync(sync);
402
	    input.merge();
403
	    // commit the conversation and start a new transaction immediately
404
        conversation.commit(true);
405

    
406

    
407
        dirty.setDirty(false);
408
        EventUtility.postEvent(WorkbenchEventConstants.REFRESH_NAVIGATOR, true);
409
	    // Stop the progress monitor.
410
	    monitor.done();
411
	}
412

    
413
	public void init(TaxonEditorInputE4 input) {
414
		if (this.input != null){
415
			this.input.dispose();
416
//			this.acceptedGroup = null;
417
//			this.heterotypicSynonymGroups.clear();
418
//			this.misappliedGroup = null;
419
		}
420
		if (!(input != null)) {
421
            MessagingUtils.error(this.getClass(), new Exception(Messages.TaxonNameEditor_INVALID_INPUT));
422
            return;
423
        }
424

    
425
		if (input.getAdapter(Taxon.class) != null) {
426
			taxon = CdmBase.deproxy(input.getAdapter(Taxon.class), Taxon.class);
427
		} else {
428
		    MessagingUtils.error(this.getClass(), new Exception(Messages.TaxonNameEditor_INVALID_INPUT_TAXON_NULL));
429
		    return;
430
		}
431

    
432
		this.input = input;
433
		conversation = input.getConversationHolder();
434

    
435
        createOrUpdateNameComposites(true, true, true);
436

    
437
        createDragSupport();
438

    
439
        setPartName();
440

    
441
        //set initial selection
442
        TaxonBase initiallySelectedTaxonBase = input.getInitiallySelectedTaxonBase();
443
        if(initiallySelectedTaxonBase!=null){
444
            selService.setSelection(new StructuredSelection(initiallySelectedTaxonBase));
445
            getContainer(initiallySelectedTaxonBase).setSelected();
446
        }
447
	}
448

    
449
   private void createDragSupport() {
450
       // Listen for names being dragged outside of existing homotypic groups -
451
       // user wants to create a new group
452
       Transfer[] types = new Transfer[] { CdmDataTransfer.getInstance() };
453
       int operations = DND.DROP_MOVE;
454
       if (target == null) {
455
           target = new DropTarget(parent, operations);
456
           target.setTransfer(types);
457
           target.addDropListener(new NameEditorDropTargetListenerE4(this));
458
       }
459
   }
460

    
461
	public AcceptedNameContainerE4 getAcceptedNameContainer() {
462
		return getAcceptedGroup().getAcceptedNameContainer();
463
	}
464

    
465
	public HomotypicalSynonymGroupE4 getHomotypicalGroupContainer(
466
			HomotypicalGroup homotypicalGroup) {
467
		for (HomotypicalSynonymGroupE4 group : getHeterotypicSynonymGroups()) {
468
			if (group.getGroup().equals(homotypicalGroup)) {
469
				return group;
470
			}
471
		}
472

    
473
		return null;
474
	}
475

    
476
	/**
477
	 * <p>
478
	 * getDirtyNames
479
	 * </p>
480
	 *
481
	 * @return a Set containing all composites that have been edited
482
	 */
483
	public Set<AbstractGroupedContainerE4> getDirtyNames() {
484
		Set<AbstractGroupedContainerE4> dirtyNames = new HashSet<>();
485

    
486
		for (AbstractGroupedContainerE4 composite : getGroupedContainers()) {
487
			if (composite.isDirty()) {
488
				dirtyNames.add(composite);
489
			}
490
		}
491

    
492
		return dirtyNames;
493
	}
494

    
495
	public List<AbstractGroupedContainerE4> getGroupedContainers() {
496
		List<AbstractGroupedContainerE4> groupedComposites = new ArrayList<>();
497

    
498
		for (AbstractGroupE4 group : getAllGroups()) {
499
		    if (group!= null){
500
		        groupedComposites.addAll(group.getGroupedContainers());
501
		    }
502
		}
503

    
504
		return groupedComposites;
505
	}
506

    
507
	public List<AbstractGroupE4> getAllGroups() {
508
		List<AbstractGroupE4> allGroups = new ArrayList<>();
509

    
510
		allGroups.add(getAcceptedGroup());
511

    
512
		heterotypicSynonymGroups = getHeterotypicSynonymGroups();
513

    
514
		if (heterotypicSynonymGroups != null) {
515
			allGroups.addAll(heterotypicSynonymGroups);
516
		}
517

    
518
		if (misappliedGroup != null) {
519
			allGroups.add(misappliedGroup);
520
		}
521

    
522
		return allGroups;
523
	}
524

    
525
	@Override
526
	public IEclipseContext getContext() {
527
	    return context;
528
	}
529

    
530
	public boolean isDirty() {
531
		return dirty.isDirty();
532
	}
533

    
534
	@PreDestroy
535
	public void dispose() {
536
        if(conversation!=null){
537
            conversation.unregisterForDataStoreChanges(this);
538
            conversation.close();
539
        }
540
        if(input!=null){
541
            input.dispose();
542
        }
543
        dirty.setDirty(false);
544
        eventBroker.post(WorkbenchEventConstants.CURRENT_ACTIVE_EDITOR, null);
545
	}
546

    
547
	/** {@inheritDoc} */
548
	@Override
549
    public void selectionChanged(IWorkbenchPart part, ISelection selection) {
550
	    System.err.println("Bla");
551
	}
552

    
553
	public AbstractGroupedContainerE4 getSelectedContainer() {
554
		return (selection != null) ? getContainer(selection)
555
				: getAcceptedNameContainer();
556
	}
557

    
558
    @Override
559
    public void dragEntered() {
560
        // TODO change this
561
        getControl().setBackground(
562
                AbstractUtility.getColor(Resources.COLOR_DRAG_ENTER));
563
    }
564

    
565
    @Override
566
    public void dragLeft() {
567
        getControl().setBackground(
568
                AbstractUtility.getColor(Resources.COLOR_COMPOSITE_BACKGROUND));
569
    }
570

    
571

    
572
	public void setMisapplicationsGroup(MisappliedGroupE4 misappliedGroup) {
573
		this.misappliedGroup = misappliedGroup;
574
	}
575

    
576
	public FormToolkit getToolkit() {
577
		return managedForm.getToolkit();
578
	}
579

    
580
	public List<HomotypicalSynonymGroupE4> getHeterotypicSynonymGroups() {
581
		return heterotypicSynonymGroups;
582
	}
583

    
584
	public void addHeterotypicSynonymGroup(HomotypicalSynonymGroupE4 group) {
585
		heterotypicSynonymGroups.add(group);
586
	}
587

    
588
	public AcceptedGroupE4 getAcceptedGroup() {
589
		return acceptedGroup;
590
	}
591

    
592
	public void setAcceptedGroup(AcceptedGroupE4 acceptedGroup) {
593
		this.acceptedGroup = acceptedGroup;
594
	}
595

    
596
	public MisappliedGroupE4 getMisappliedGroup() {
597
		return misappliedGroup;
598
	}
599

    
600
	public boolean isActive() {
601
		return this.equals(AbstractUtility.getActivePart());
602
	}
603

    
604
    @Override
605
    public boolean onComplete() {
606
		getContainer(objectAffectedByLastOperation).setSelected();
607
		return true;
608
	}
609

    
610
	/** {@inheritDoc} */
611
	@Override
612
    public void partChanged(Integer eventType, IWorkbenchPartReference partRef) {
613
		if (!partRef.getPart(false).equals(this)) {
614
			// getSelectedObject().colorSelected(AbstractGroupedContainer.SELECTED_NO_FOCUS);
615
		}
616
	}
617

    
618
	public void removeGroup(AbstractGroupE4 group) {
619
		if (group != null) {
620
			group.dispose();
621

    
622
			//if (heterotypicSynonymGroups != null) {
623
			heterotypicSynonymGroups.remove(group);
624
			//}
625
		}
626
	}
627

    
628
	public AbstractGroupedContainerE4 getContainer(TaxonBase taxonBase) {
629
		@SuppressWarnings("rawtypes")
630
        List<AbstractGroupedContainerE4> groupedContainers = getGroupedContainers();
631
		for (AbstractGroupedContainerE4 container : groupedContainers) {
632
			if (container.getData().equals(taxonBase)
633
					&& container.getNameViewer().getTextWidget() != null) {
634
				return container;
635
			}
636
		}
637
		return getAcceptedNameContainer();
638
	}
639

    
640
    public void setOnError() {
641
		Color disabledColor =  AbstractUtility.getColor(Resources.COLOR_EDITOR_ERROR);
642
		setEnabled(false, disabledColor);
643
	}
644

    
645
	public void setDisabled(){
646
		Color disabledColor =  AbstractUtility.getColor(Resources.COLOR_TEXT_DISABLED_BACKGROUND);
647
		setEnabled(false, disabledColor);
648
	}
649

    
650
	protected void setEnabled(boolean enabled, Color background) {
651

    
652
		for(AbstractGroupedContainerE4 groupedContainer : getGroupedContainers()){
653
			groupedContainer.setEnabled(enabled);
654
		}
655

    
656
		// send an empty selection to the current provider - TODO only on error ???
657
		if (!enabled) {
658
			getManagedForm().setInput(null);
659

    
660
			for (AbstractGroupedContainerE4 groupedContainer : getGroupedContainers()) {
661
				groupedContainer.setBackground(background);
662
			}
663
		}
664
		getControl().setBackground(background);
665
	}
666

    
667
    @Override
668
    public void changed(Object element) {
669
        // setDirty(true);
670
        // if the attribute is null then do not set the dirty flag -> hotfix for the problem that for tasks done in service methods the changes are saved automatically
671
        if (element != null){
672
            dirty.setDirty(true);
673
            //refresh part title
674
            //TODO: refresh taxon node in taxon navigator
675
            setPartName();
676
        }
677

    
678
        if (element instanceof TaxonBase) {
679
            AbstractGroupedContainerE4 container = getContainer((TaxonBase) element);
680
            if (container != null) {
681
                container.refresh();
682
            }
683
        }
684

    
685
        if (element instanceof TaxonRelationship) {
686
            AbstractGroupedContainerE4 container = getContainer(((TaxonRelationship) element).getFromTaxon());
687
            if (container != null) {
688
                container.refresh();
689
            }
690
        }
691
    }
692

    
693
    public void setPartName(){
694
        //FIXME: temporary fix for #6437 to avoid outdated title caches
695
        thisPart.setLabel(this.taxon.getName().generateFullTitle());
696
//        thisPart.setLabel(this.taxon.getName().getFullTitleCache());
697
    }
698

    
699
    @Override
700
    public void forceDirty() {
701
        setDirty();
702
    }
703

    
704

    
705
    public IUndoContext getUndoContext() {
706
        return undoContext;
707
    }
708

    
709
    @Override
710
    public Composite getControl(){
711
        return managedForm.getForm().getBody();
712
    }
713

    
714
    public EMenuService getMenuService() {
715
        return menuService;
716
    }
717

    
718
    public ESelectionService getSelectionService() {
719
        return selService;
720
    }
721

    
722

    
723
    /**
724
     * {@inheritDoc}
725
     */
726
    @Override
727
    public boolean canAttachMedia() {
728
        return true;
729
    }
730

    
731
    public TaxonEditorInputE4 getEditorInput() {
732
        return input;
733
    }
734

    
735
    /**
736
     * {@inheritDoc}
737
     */
738
    @Override
739
    public TaxonNameEditorE4 getEditor() {
740
        return this;
741
    }
742

    
743
    @Inject
744
    @Optional
745
    private void updateView(@UIEventTopic(WorkbenchEventConstants.REFRESH_NAME_EDITOR)CdmBase cdmbase){
746

    
747
        if (EventUtility.getTaxonEditor().equals(this) || (this.taxon.equals(cdmbase) || this.taxon.getName().equals(cdmbase))){
748
            this.redraw(false, true, true, true);
749
            this.setDirty();
750
            if (cdmbase instanceof TaxonBase){
751
                this.selection = (TaxonBase) cdmbase;
752
            }
753

    
754

    
755
        }
756
    }
757

    
758
    @Inject
759
    @Optional
760
    private void updateView(@UIEventTopic(WorkbenchEventConstants.REFRESH_NAME_EDITOR)UUID cdmbaseUuid){
761

    
762
        if (this.taxon.getUuid().equals(cdmbaseUuid)){
763
            TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstanceFromTaxonBase(cdmbaseUuid);
764
            init(input);
765
        }
766
    }
767

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

    
786
        }
787

    
788

    
789

    
790

    
791
    }
792

    
793
}
(2-2/2)