ref #7268 Implement own extension point for preferences
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / event / EventUtility.java
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.commands.ECommandService;
18 import org.eclipse.e4.core.di.annotations.Optional;
19 import org.eclipse.e4.core.services.events.IEventBroker;
20 import org.eclipse.e4.ui.di.UIEventTopic;
21 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
22 import org.eclipse.e4.ui.services.IServiceConstants;
23 import org.eclipse.e4.ui.workbench.modeling.EPartService;
24 import org.eclipse.swt.widgets.Shell;
25
26 import eu.etaxonomy.cdm.model.taxon.Taxon;
27 import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
28 import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
29
30 /**
31 * @author pplitzner
32 * @since Sep 4, 2017
33 *
34 */
35 public class EventUtility {
36
37 private static ITaxonEditor editor;
38
39 private static MPart activePart;
40
41 private static Shell shell;
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 ECommandService getCommandService() {
59 return commandService;
60 }
61
62 @Inject
63 @Optional
64 private void updateCurrentActiveShell(@Named(IServiceConstants.ACTIVE_SHELL)Shell shell){
65 EventUtility.shell = shell;
66 }
67
68 @Inject
69 @Optional
70 private void updateCurrentActivePart(@Named(IServiceConstants.ACTIVE_PART)MPart activePart){
71 if(activePart!=null){
72 EventUtility.activePart = activePart;
73 }
74 }
75
76 @Inject
77 @Optional
78 private void updateCurrentTaxon(@UIEventTopic(WorkbenchEventConstants.CURRENT_ACTIVE_EDITOR)ITaxonEditor editor){
79 EventUtility.editor = editor;
80 }
81
82 public static Taxon getCurrentTaxon() {
83 if(editor!=null){
84 return editor.getTaxon();
85 }
86 return null;
87 }
88
89 public static ITaxonEditor getTaxonEditor() {
90 return editor;
91 }
92
93 public static MPart getActivePart() {
94 return activePart;
95 }
96
97 public static Shell getShell() {
98 return shell;
99 }
100
101 public static MPart getActiveEditorPart(EPartService partService){
102 Collection<MPart> parts = partService.getParts();
103 for (MPart part : parts) {
104 if(part.getObject()!=null && part.getObject() instanceof IE4SavablePart
105 && part.isVisible()){
106 return part;
107 }
108 }
109 return null;
110 }
111
112 // private EventHandler testHandler;
113 //
114 // @Inject
115 // public EventUtility(IEventBroker eventBroker) {
116 // testHandler = new EventHandler() {
117 //
118 // @Override
119 // public void handleEvent(Event event) {
120 // Object part = event.getProperty(UIEvents.EventTags.ELEMENT);
121 // boolean tbr =(Boolean) event.getProperty(UIEvents.EventTags.NEW_VALUE);
122 // if (part instanceof MPart){
123 // System.out.println("Part "+((MPart)part).getElementId()+" is "+(!tbr?"NOT":"")+" visible");
124 // }
125 // else if(part instanceof MPlaceholder){
126 // System.out.println("Part "+((MPlaceholder)part).getRef().getElementId()+" is "+(!tbr?"NOT":"")+" visible");
127 // }
128 // }
129 // };
130 // eventBroker.subscribe(UIEvents.UIElement.TOPIC_TOBERENDERED, testHandler);
131 // }
132
133 }