Project

General

Profile

Download (3.67 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.view.concept.e4.handler;
11

    
12
import javax.inject.Named;
13

    
14
import org.eclipse.e4.core.di.annotations.Execute;
15
import org.eclipse.e4.core.di.annotations.Optional;
16
import org.eclipse.e4.ui.model.application.MApplication;
17
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
18
import org.eclipse.e4.ui.services.IServiceConstants;
19
import org.eclipse.e4.ui.workbench.modeling.EModelService;
20
import org.eclipse.e4.ui.workbench.modeling.EPartService;
21
import org.eclipse.jface.viewers.ISelection;
22

    
23
import eu.etaxonomy.cdm.model.taxon.Taxon;
24
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
25
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
26
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
27
import eu.etaxonomy.taxeditor.editor.EditorUtil;
28
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
29
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
30
import eu.etaxonomy.taxeditor.editor.view.concept.e4.ConceptViewPartE4;
31
import eu.etaxonomy.taxeditor.model.LineSelection;
32
import eu.etaxonomy.taxeditor.model.MessagingUtils;
33
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
34

    
35
/**
36
 *
37
 * @author pplitzner
38
 * @date 21.08.2017
39
 *
40
 */
41
public class OpenRelatedConceptHandlerE4  {
42

    
43
    @Execute
44
    public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
45
            @Optional@Named(IServiceConstants.ACTIVE_SELECTION) TaxonRelationship taxonRelationship, EModelService modelService, EPartService partService, MApplication application) {
46

    
47
        TaxonNameEditorE4 editor = null;
48
        BulkEditor bulkEditor = null;
49

    
50
        ConceptViewPartE4 conceptView = (ConceptViewPartE4) activePart.getObject();
51

    
52
        Object e4WrappedPart = WorkbenchUtility.getE4WrappedPart(conceptView.getSelectionProvidingPart());
53
        if(e4WrappedPart instanceof TaxonNameEditorE4){
54
            editor = (TaxonNameEditorE4) e4WrappedPart;
55
        }
56
        else if(e4WrappedPart instanceof BulkEditor){
57
            bulkEditor = (BulkEditor) e4WrappedPart;
58
        }
59
        TaxonBase<?> relatedTaxon = null;
60
        if(editor!=null){
61
            relatedTaxon = getRelatedTaxon(taxonRelationship, editor.getTaxon());
62
        }
63
        else if(bulkEditor!=null){
64
            ISelection bulkSelection = bulkEditor.getSelectionProvider().getSelection();
65
            if(bulkSelection instanceof LineSelection){
66
                Object firstElement = ((LineSelection)bulkSelection).getFirstElement();
67
                if(firstElement instanceof Taxon){
68
                    relatedTaxon = getRelatedTaxon(taxonRelationship, (Taxon) firstElement);
69
                }
70
            }
71
        }
72

    
73
		if(relatedTaxon==null){
74
		    MessagingUtils.messageDialog(Messages.OpenRelatedConceptHandler_COULD_NOT_OPEN, getClass(),
75
		            String.format(Messages.OpenRelatedConceptHandler_COULD_NOT_OPEN_MESSAGE ,relatedTaxon), null);
76
		}
77
		else{
78
		    openConcept(relatedTaxon, modelService, partService, application);
79
		}
80
	}
81

    
82
    protected void openConcept(TaxonBase<?> relatedTaxon, EModelService modelService, EPartService partService, MApplication application) {
83
        EditorUtil.openTaxonBaseE4(relatedTaxon.getUuid(), modelService, partService, application);
84
    }
85

    
86
	private Taxon getRelatedTaxon(TaxonRelationship relationship, Taxon taxon){
87
		if (relationship.getFromTaxon().equals(taxon)){
88
			return relationship.getToTaxon();
89
		}
90
		else if(relationship.getToTaxon().equals(taxon)){
91
			return relationship.getFromTaxon();
92
		}
93
		else{
94
			return null;
95
		}
96
	}
97
}
(3-3/4)