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