64d83ce02970ba98f3830751418912b7812059c2
[cdm-vaadin.git] / src / main / java / eu / etaxonomy / cdm / vaadin / debug / EntityCacheDebugger.java
1 /**
2 * Copyright (C) 2018 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.cdm.vaadin.debug;
10
11 import java.lang.reflect.InvocationTargetException;
12 import java.lang.reflect.Method;
13 import java.util.Optional;
14
15 import org.apache.logging.log4j.LogManager;
16 import org.apache.logging.log4j.Logger;
17 import org.springframework.beans.factory.annotation.Autowired;
18 import org.springframework.context.annotation.Profile;
19 import org.springframework.stereotype.Component;
20 import org.vaadin.spring.events.Event;
21 import org.vaadin.spring.events.EventBus;
22 import org.vaadin.spring.events.EventBus.UIEventBus;
23 import org.vaadin.spring.events.EventBusListener;
24
25 import com.vaadin.event.ShortcutAction;
26 import com.vaadin.event.ShortcutListener;
27 import com.vaadin.navigator.View;
28 import com.vaadin.navigator.ViewChangeListener;
29 import com.vaadin.spring.annotation.UIScope;
30 import com.vaadin.ui.UI;
31 import com.vaadin.ui.Window;
32
33 import eu.etaxonomy.cdm.vaadin.view.name.CachingPresenter;
34 import eu.etaxonomy.vaadin.mvp.AbstractCdmPopupEditor;
35 import eu.etaxonomy.vaadin.mvp.AbstractPopupView;
36 import eu.etaxonomy.vaadin.mvp.AbstractPresenter;
37 import eu.etaxonomy.vaadin.mvp.AbstractView;
38 import eu.etaxonomy.vaadin.ui.view.PopEditorOpenedEvent;
39 import eu.etaxonomy.vaadin.ui.view.PopupView;
40
41 /**
42 * @author a.kohlbecker
43 * @since Jan 22, 2018
44 */
45 @Component
46 @UIScope
47 @Profile("debug")
48 public class EntityCacheDebugger implements ViewChangeListener, EventBusListener<PopEditorOpenedEvent> {
49
50 private final static Logger logger = LogManager.getLogger();
51
52 private UIEventBus uiEventBus;
53
54
55 @Autowired
56 protected final void setUIEventBus(EventBus.UIEventBus uiEventBus){
57 this.uiEventBus = uiEventBus;
58 uiEventBus.subscribe(this);
59 }
60
61 EntityCacheDebuggerShortcutListener shortcutListener;
62
63 public EntityCacheDebugger(){
64 shortcutListener = new EntityCacheDebuggerShortcutListener("Debug Entities",
65 ShortcutAction.KeyCode.SPACEBAR,
66 ShortcutAction.ModifierKey.CTRL);
67 }
68
69 public void openFor(AbstractView view){
70
71 if(view != null){
72
73 try {
74 AbstractPresenter<?> presenter;
75 Method getPresenterMethod = AbstractView.class.getDeclaredMethod("getPresenter");
76 getPresenterMethod.setAccessible(true);
77 presenter = (AbstractPresenter<?>) getPresenterMethod.invoke(view);
78 if(CachingPresenter.class.isAssignableFrom(presenter.getClass())){
79 open(view, (CachingPresenter)presenter);
80 } else {
81 logger.warn("can only operate on CachingPresenters");
82 }
83 } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
84 | InvocationTargetException e) {
85 logger.error(e);
86 }
87
88 } else {
89 logger.warn("view is null");
90 }
91
92 }
93
94 private void open(AbstractView view, CachingPresenter presenter) {
95
96 EntityCacheDebuggerComponent content = new EntityCacheDebuggerComponent(presenter);
97
98 if(view instanceof AbstractCdmPopupEditor){
99 findWindow((AbstractCdmPopupEditor)view).setModal(false);
100 }
101 Window window = new Window();
102 window.setCaption("Entity Cache Debugger");
103 window.setResizable(true);
104 window.setModal(false);
105 content.setSizeFull();
106 window.setContent(content);
107 window.setWidth("800px");
108 window.setHeight("600px");
109 UI.getCurrent().addWindow(window);
110
111 }
112
113 /**
114 * {@inheritDoc}
115 */
116 @Override
117 public boolean beforeViewChange(ViewChangeEvent event) {
118 return true;
119 }
120
121 /**
122 * {@inheritDoc}
123 */
124 @Override
125 public void afterViewChange(ViewChangeEvent event) {
126 View newView = event.getNewView();
127 if(newView instanceof AbstractView){
128 ((AbstractView)newView).addShortcutListener(shortcutListener);
129 }
130 if(event.getOldView() instanceof AbstractView){
131 ((AbstractView)event.getOldView()).removeShortcutListener(shortcutListener);
132 }
133 }
134
135 @Override
136 public void onEvent(Event<PopEditorOpenedEvent> event){
137 PopupView popupView = event.getPayload().getPopupView();
138 if(popupView != null && popupView instanceof AbstractPopupView){
139 findWindow((AbstractPopupView)popupView).addShortcutListener(shortcutListener);
140 }
141
142 }
143
144 private Window findWindow(AbstractPopupView view){
145 Optional<Window> popUpWindow = UI.getCurrent().getWindows().stream().filter(w -> w.getContent().equals(view)).findFirst();
146 if(popUpWindow.isPresent()){
147 return popUpWindow.get();
148 } else {
149 return null;
150 }
151
152 }
153
154 /**
155 * @return the shortcutListener
156 */
157 public EntityCacheDebuggerShortcutListener getShortcutListener() {
158 return shortcutListener;
159 }
160
161
162 private class EntityCacheDebuggerShortcutListener extends ShortcutListener {
163
164 private static final long serialVersionUID = -8727949764189908851L;
165
166 /**
167 * @param caption
168 * @param keyCode
169 * @param modifierKeys
170 */
171 public EntityCacheDebuggerShortcutListener(
172 String caption,
173 int keyCode,
174 int modifierKey) {
175 super(caption, keyCode, new int[]{modifierKey});
176 }
177
178
179 public EntityCacheDebuggerShortcutListener(
180 String caption,
181 int keyCode) {
182 super(caption, new int[]{keyCode});
183 }
184
185 @Override
186 public void handleAction(Object sender, Object target) {
187 if(sender instanceof AbstractView) {
188 EntityCacheDebugger.this.openFor((AbstractView)sender);
189 }
190 if(sender instanceof Window && ((Window)sender).getContent() instanceof AbstractPopupView) {
191 EntityCacheDebugger.this.openFor((AbstractPopupView)((Window)sender).getContent());
192 }
193
194 }
195 };
196
197 }