Project

General

Profile

Download (3.63 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.ui.basic.MPart;
17
import org.eclipse.e4.ui.services.IServiceConstants;
18
import org.eclipse.jface.viewers.ISelection;
19
import org.eclipse.ui.PartInitException;
20

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

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

    
41
    @Execute
42
    public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
43
            @Optional@Named(IServiceConstants.ACTIVE_SELECTION) TaxonRelationship taxonRelationship) {
44

    
45
        MultiPageTaxonEditor editor = null;
46
        BulkEditor bulkEditor = null;
47

    
48
        ConceptViewPartE4 conceptView = (ConceptViewPartE4) activePart.getObject();
49

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

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

    
80
    /**
81
     * @param relatedTaxon
82
     */
83
    protected void openConcept(TaxonBase<?> relatedTaxon) {
84
        try {
85
            EditorUtil.openTaxonBaseE4(relatedTaxon.getUuid());
86
        } catch (PartInitException e) {
87
            MessagingUtils.messageDialog(Messages.OpenRelatedConceptHandler_COULD_NOT_OPEN, getClass(),
88
                    String.format(Messages.OpenRelatedConceptHandler_COULD_NOT_OPEN_MESSAGE ,relatedTaxon), e);
89
        }
90
    }
91

    
92
	private Taxon getRelatedTaxon(TaxonRelationship relationship, Taxon taxon){
93
		if (relationship.getFromTaxon().equals(taxon)){
94
			return relationship.getToTaxon();
95
		}
96
		else if(relationship.getToTaxon().equals(taxon)){
97
			return relationship.getFromTaxon();
98
		}
99
		else{
100
			return null;
101
		}
102
	}
103
}
(3-3/4)