Project

General

Profile

Download (3.52 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.event;
10

    
11
import java.util.Collection;
12

    
13
import javax.annotation.PostConstruct;
14
import javax.inject.Inject;
15
import javax.inject.Named;
16

    
17
import org.eclipse.e4.core.di.annotations.Optional;
18
import org.eclipse.e4.core.services.events.IEventBroker;
19
import org.eclipse.e4.ui.di.UIEventTopic;
20
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
21
import org.eclipse.e4.ui.services.IServiceConstants;
22
import org.eclipse.e4.ui.workbench.modeling.EPartService;
23
import org.eclipse.swt.widgets.Shell;
24

    
25
import eu.etaxonomy.cdm.model.taxon.Taxon;
26
import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
27
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
28

    
29
/**
30
 * @author pplitzner
31
 * @since Sep 4, 2017
32
 *
33
 */
34
public class EventUtility {
35

    
36
    private static ITaxonEditor editor;
37

    
38
    private static MPart activePart;
39

    
40
    private static Shell shell;
41

    
42
    @Inject
43
    private static IEventBroker eventBroker;
44

    
45
    @PostConstruct
46
    public void create(){
47
        //nothing
48
    }
49

    
50
    public static void postEvent(String topic, Object data){
51
        eventBroker.post(topic, data);
52
    }
53

    
54
    @Inject
55
    @Optional
56
    private void updateCurrentActiveShell(@Named(IServiceConstants.ACTIVE_SHELL)Shell shell){
57
        EventUtility.shell = shell;
58
    }
59

    
60
    @Inject
61
    @Optional
62
    private void updateCurrentActivePart(@Named(IServiceConstants.ACTIVE_PART)MPart activePart){
63
        if(activePart!=null){
64
            EventUtility.activePart = activePart;
65
        }
66
    }
67

    
68
    @Inject
69
    @Optional
70
    private void updateCurrentTaxon(@UIEventTopic(WorkbenchEventConstants.CURRENT_ACTIVE_EDITOR)ITaxonEditor editor){
71
        EventUtility.editor = editor;
72
    }
73

    
74
    public static Taxon getCurrentTaxon() {
75
        if(editor!=null){
76
            return editor.getTaxon();
77
        }
78
        return null;
79
    }
80

    
81
    public static ITaxonEditor getTaxonEditor() {
82
        return editor;
83
    }
84

    
85
    public static MPart getActivePart() {
86
        return activePart;
87
    }
88

    
89
    public static Shell getShell() {
90
        return shell;
91
    }
92

    
93
    public static MPart getActiveEditorPart(EPartService partService){
94
        Collection<MPart> parts = partService.getParts();
95
        for (MPart part : parts) {
96
            if(part.getObject()!=null && part.getObject() instanceof IE4SavablePart
97
                    && part.isVisible()){
98
                return part;
99
            }
100
        }
101
        return null;
102
    }
103

    
104
//    private EventHandler testHandler;
105
//
106
//    @Inject
107
//    public EventUtility(IEventBroker eventBroker) {
108
//        testHandler = new EventHandler() {
109
//
110
//            @Override
111
//            public void handleEvent(Event event) {
112
//                Object part = event.getProperty(UIEvents.EventTags.ELEMENT);
113
//                boolean tbr =(Boolean) event.getProperty(UIEvents.EventTags.NEW_VALUE);
114
//                if (part instanceof MPart){
115
//                    System.out.println("Part "+((MPart)part).getElementId()+" is "+(!tbr?"NOT":"")+" visible");
116
//                }
117
//                else if(part instanceof MPlaceholder){
118
//                    System.out.println("Part "+((MPlaceholder)part).getRef().getElementId()+" is "+(!tbr?"NOT":"")+" visible");
119
//                }
120
//            }
121
//        };
122
//        eventBroker.subscribe(UIEvents.UIElement.TOPIC_TOBERENDERED, testHandler);
123
//    }
124

    
125
}
(1-1/2)