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.log4j.Logger;
|
16
|
import org.springframework.beans.factory.annotation.Autowired;
|
17
|
import org.springframework.context.annotation.Profile;
|
18
|
import org.springframework.stereotype.Component;
|
19
|
import org.vaadin.spring.events.Event;
|
20
|
import org.vaadin.spring.events.EventBus;
|
21
|
import org.vaadin.spring.events.EventBus.UIEventBus;
|
22
|
import org.vaadin.spring.events.EventBusListener;
|
23
|
|
24
|
import com.vaadin.event.ShortcutAction;
|
25
|
import com.vaadin.event.ShortcutListener;
|
26
|
import com.vaadin.navigator.View;
|
27
|
import com.vaadin.navigator.ViewChangeListener;
|
28
|
import com.vaadin.spring.annotation.UIScope;
|
29
|
import com.vaadin.ui.UI;
|
30
|
import com.vaadin.ui.Window;
|
31
|
|
32
|
import eu.etaxonomy.cdm.vaadin.view.name.CachingPresenter;
|
33
|
import eu.etaxonomy.vaadin.mvp.AbstractCdmPopupEditor;
|
34
|
import eu.etaxonomy.vaadin.mvp.AbstractPopupView;
|
35
|
import eu.etaxonomy.vaadin.mvp.AbstractPresenter;
|
36
|
import eu.etaxonomy.vaadin.mvp.AbstractView;
|
37
|
import eu.etaxonomy.vaadin.ui.view.PopEditorOpenedEvent;
|
38
|
import eu.etaxonomy.vaadin.ui.view.PopupView;
|
39
|
|
40
|
/**
|
41
|
* @author a.kohlbecker
|
42
|
* @since Jan 22, 2018
|
43
|
*
|
44
|
*/
|
45
|
@Component
|
46
|
@UIScope
|
47
|
@Profile("debug")
|
48
|
public class EntityCacheDebugger implements ViewChangeListener, EventBusListener<PopEditorOpenedEvent> {
|
49
|
|
50
|
Logger logger = Logger.getLogger(EntityCacheDebugger.class);
|
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
|
/**
|
95
|
* @param view
|
96
|
* @param presenter
|
97
|
*/
|
98
|
private void open(AbstractView view, CachingPresenter presenter) {
|
99
|
|
100
|
EntityCacheDebuggerComponent content = new EntityCacheDebuggerComponent(presenter);
|
101
|
|
102
|
if(view instanceof AbstractCdmPopupEditor){
|
103
|
findWindow((AbstractCdmPopupEditor)view).setModal(false);
|
104
|
}
|
105
|
Window window = new Window();
|
106
|
window.setCaption("Entity Cache Debugger");
|
107
|
window.setResizable(true);
|
108
|
window.setModal(false);
|
109
|
content.setSizeFull();
|
110
|
window.setContent(content);
|
111
|
window.setWidth("800px");
|
112
|
window.setHeight("600px");
|
113
|
UI.getCurrent().addWindow(window);
|
114
|
|
115
|
}
|
116
|
|
117
|
/**
|
118
|
* {@inheritDoc}
|
119
|
*/
|
120
|
@Override
|
121
|
public boolean beforeViewChange(ViewChangeEvent event) {
|
122
|
return true;
|
123
|
}
|
124
|
|
125
|
/**
|
126
|
* {@inheritDoc}
|
127
|
*/
|
128
|
@Override
|
129
|
public void afterViewChange(ViewChangeEvent event) {
|
130
|
View newView = event.getNewView();
|
131
|
if(newView instanceof AbstractView){
|
132
|
((AbstractView)newView).addShortcutListener(shortcutListener);
|
133
|
}
|
134
|
if(event.getOldView() instanceof AbstractView){
|
135
|
((AbstractView)event.getOldView()).removeShortcutListener(shortcutListener);
|
136
|
}
|
137
|
}
|
138
|
|
139
|
@Override
|
140
|
public void onEvent(Event<PopEditorOpenedEvent> event){
|
141
|
PopupView popupView = event.getPayload().getPopupView();
|
142
|
if(popupView != null && popupView instanceof AbstractPopupView){
|
143
|
findWindow((AbstractPopupView)popupView).addShortcutListener(shortcutListener);
|
144
|
}
|
145
|
|
146
|
}
|
147
|
|
148
|
private Window findWindow(AbstractPopupView view){
|
149
|
Optional<Window> popUpWindow = UI.getCurrent().getWindows().stream().filter(w -> w.getContent().equals(view)).findFirst();
|
150
|
if(popUpWindow.isPresent()){
|
151
|
return popUpWindow.get();
|
152
|
} else {
|
153
|
return null;
|
154
|
}
|
155
|
|
156
|
}
|
157
|
|
158
|
/**
|
159
|
* @return the shortcutListener
|
160
|
*/
|
161
|
public EntityCacheDebuggerShortcutListener getShortcutListener() {
|
162
|
return shortcutListener;
|
163
|
}
|
164
|
|
165
|
|
166
|
private class EntityCacheDebuggerShortcutListener extends ShortcutListener {
|
167
|
|
168
|
private static final long serialVersionUID = -8727949764189908851L;
|
169
|
|
170
|
/**
|
171
|
* @param caption
|
172
|
* @param keyCode
|
173
|
* @param modifierKeys
|
174
|
*/
|
175
|
public EntityCacheDebuggerShortcutListener(
|
176
|
String caption,
|
177
|
int keyCode,
|
178
|
int modifierKey) {
|
179
|
super(caption, keyCode, new int[]{modifierKey});
|
180
|
}
|
181
|
|
182
|
|
183
|
public EntityCacheDebuggerShortcutListener(
|
184
|
String caption,
|
185
|
int keyCode) {
|
186
|
super(caption, new int[]{keyCode});
|
187
|
}
|
188
|
|
189
|
@Override
|
190
|
public void handleAction(Object sender, Object target) {
|
191
|
if(sender instanceof AbstractView) {
|
192
|
EntityCacheDebugger.this.openFor((AbstractView)sender);
|
193
|
}
|
194
|
if(sender instanceof Window && ((Window)sender).getContent() instanceof AbstractPopupView) {
|
195
|
EntityCacheDebugger.this.openFor((AbstractPopupView)((Window)sender).getContent());
|
196
|
}
|
197
|
|
198
|
}
|
199
|
};
|
200
|
|
201
|
}
|