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
public class EventUtility {
32

    
33
    private static ITaxonEditor editor;
34

    
35
    private static MPart activePart;
36

    
37
    private static Shell shell;
38

    
39
    @Inject
40
    private static UISynchronize sync;
41

    
42
    @Inject
43
    private static IEventBroker eventBroker;
44

    
45
    @Inject
46
    private static ECommandService commandService;
47

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

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

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

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

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

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

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

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

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

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

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

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

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

    
129
}
(1-1/2)