Project

General

Profile

Download (4.2 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2017 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
package eu.etaxonomy.taxeditor.workbench;
10

    
11
import java.io.File;
12
import java.net.URL;
13
import java.util.Collection;
14
import java.util.List;
15

    
16
import org.eclipse.core.runtime.Platform;
17
import org.eclipse.core.runtime.URIUtil;
18
import org.eclipse.e4.ui.model.application.MApplication;
19
import org.eclipse.e4.ui.model.application.ui.advanced.MArea;
20
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
21
import org.eclipse.e4.ui.model.application.ui.basic.MPartSashContainerElement;
22
import org.eclipse.e4.ui.model.application.ui.basic.MPartStack;
23
import org.eclipse.e4.ui.workbench.modeling.EModelService;
24
import org.eclipse.e4.ui.workbench.modeling.EPartService;
25
import org.eclipse.swt.program.Program;
26

    
27
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
28
import eu.etaxonomy.taxeditor.workbench.part.ISelectionElementEditingPart;
29

    
30
/**
31
 * Utility class for e4 workbench related operations
32
 * @author pplitzner
33
 * @since Jun 27, 2017
34
 *
35
 */
36
public class WorkbenchUtility {
37

    
38
    public static MPart getActiveEditorPart(EPartService partService){
39
        Collection<MPart> parts = partService.getParts();
40
        for (MPart part : parts) {
41
            if(part.getObject()!=null && part.getObject() instanceof IE4SavablePart
42
                    && part.isVisible()){
43
                return part;
44
            }
45
        }
46
        return null;
47
    }
48

    
49
    /**
50
     * Checks if the activePart is an E4 wrapper for a legacy part and returns
51
     * that part
52
     *
53
     * @param activePart the e4 wrapper
54
     * @return the wrapped legacy part or <code>null</code>
55
     */
56
    public static Object getE4WrappedPart(Object activePart){
57
        //FIXME E4 can be removed when E4 migration is complete
58

    
59
        Object object = activePart;
60
        if(object instanceof MPart){
61
            object = ((MPart) activePart).getObject();
62
        }
63
        return object;
64
    }
65

    
66
    public static File getBaseLocation() {
67
        File baseLocation;
68
        try {
69
            baseLocation = new File(URIUtil.toURI(Platform.getInstallLocation().getURL()));
70
        } catch (Exception e) {
71
            throw new RuntimeException(e);
72
        }
73
        baseLocation = new File(baseLocation, "configuration"); //$NON-NLS-1$
74
        return baseLocation;
75
    }
76

    
77
    public static MPart findSavablePart(ISelectionElementEditingPart part){
78
        MPart selectionProvidingPart = part.getSelectionProvidingPart();
79
        if(selectionProvidingPart!=null){
80
            if(selectionProvidingPart.getObject() instanceof ISelectionElementEditingPart){
81
                return findSavablePart((ISelectionElementEditingPart) selectionProvidingPart.getObject());
82
            }
83
            return selectionProvidingPart;
84
        }
85
        return null;
86
    }
87

    
88

    
89
    public static MPartStack getPartStack(String stackId, MApplication application, EModelService modelService){
90
        if(application!=null){
91
            List<MPartStack> stacks = modelService.findElements(application, stackId, MPartStack.class, null);
92
            if(!stacks.isEmpty()){
93
                return stacks.iterator().next();
94
            }
95
        }
96
        return null;
97
    }
98

    
99
    public static MPartStack getEditorAreaPartStack(MApplication application, EModelService modelService){
100
        //FIXME E4 hack for opening in e3 editor area
101
        if(application!=null){
102
            //FIXME E4 hack for opening in e3 editor area
103
            List<MArea> elements = modelService.findElements(application, "org.eclipse.ui.editorss", MArea.class, null);
104
            for (MArea mArea : elements) {
105
                List<MPartSashContainerElement> children = mArea.getChildren();
106
                for (MPartSashContainerElement mPartSashContainerElement : children) {
107
                    if(mPartSashContainerElement instanceof MPartStack){
108
                        return (MPartStack) mPartSashContainerElement;
109
                    }
110
                }
111
            }
112
        }
113
        return null;
114
    }
115

    
116
    public static boolean openWebpage(URL url){
117
        return Program.launch(url.toString());
118
    }
119
}
(8-8/8)