Project

General

Profile

Download (3.56 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 javax.annotation.PostConstruct;
12
import javax.inject.Inject;
13
import javax.inject.Named;
14

    
15
import org.eclipse.e4.core.commands.ECommandService;
16
import org.eclipse.e4.core.di.annotations.Optional;
17
import org.eclipse.e4.core.services.events.IEventBroker;
18
import org.eclipse.e4.ui.di.UIEventTopic;
19
import org.eclipse.e4.ui.di.UISynchronize;
20
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
21
import org.eclipse.e4.ui.services.IServiceConstants;
22
import org.eclipse.swt.widgets.Shell;
23

    
24
import eu.etaxonomy.cdm.model.taxon.Taxon;
25
import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
26

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

    
34
    private static ITaxonEditor editor;
35

    
36
    private static MPart activePart;
37

    
38
    private static Shell shell;
39

    
40
    @Inject
41
    private static UISynchronize sync;
42

    
43
    @Inject
44
    private static IEventBroker eventBroker;
45

    
46
    @Inject
47
    private static ECommandService commandService;
48

    
49
    @PostConstruct
50
    public void create(){
51
        //nothing
52
    }
53

    
54
    public static void postEvent(String topic, Object data){
55
        eventBroker.post(topic, data);
56
    }
57

    
58
    public static void postSyncEvent(String topic, Object data){
59
        sync.syncExec(()->eventBroker.post(topic, data));
60
    }
61

    
62
    public static void postAsyncEvent(String topic, Object data){
63
        sync.asyncExec(()->eventBroker.post(topic, data));
64
    }
65

    
66
    public static ECommandService getCommandService() {
67
        return commandService;
68
    }
69

    
70
    @Inject
71
    @Optional
72
    private void updateCurrentActiveShell(@Named(IServiceConstants.ACTIVE_SHELL)Shell shell){
73
        EventUtility.shell = shell;
74
    }
75

    
76
    @Inject
77
    @Optional
78
    private void updateCurrentActivePart(@Named(IServiceConstants.ACTIVE_PART)MPart activePart){
79
        if(activePart!=null){
80
            EventUtility.activePart = activePart;
81
        }
82
    }
83

    
84
    @Inject
85
    @Optional
86
    private void updateCurrentTaxon(@UIEventTopic(WorkbenchEventConstants.CURRENT_ACTIVE_EDITOR)ITaxonEditor editor){
87
        EventUtility.editor = editor;
88
    }
89

    
90
    public static Taxon getCurrentTaxon() {
91
        if(editor!=null){
92
            return editor.getTaxon();
93
        }
94
        return null;
95
    }
96

    
97
    public static ITaxonEditor getTaxonEditor() {
98
        return editor;
99
    }
100

    
101
    public static MPart getActivePart() {
102
        return activePart;
103
    }
104

    
105
    public static Shell getShell() {
106
        return shell;
107
    }
108

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

    
130
}
(1-1/2)