2 * Copyright (C) 2018 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.vaadin
.debug
;
11 import java
.lang
.reflect
.InvocationTargetException
;
12 import java
.lang
.reflect
.Method
;
13 import java
.util
.Optional
;
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
;
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
;
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
;
42 * @author a.kohlbecker
48 public class EntityCacheDebugger
implements ViewChangeListener
, EventBusListener
<PopEditorOpenedEvent
> {
50 private final static Logger logger
= LogManager
.getLogger();
52 private UIEventBus uiEventBus
;
56 protected final void setUIEventBus(EventBus
.UIEventBus uiEventBus
){
57 this.uiEventBus
= uiEventBus
;
58 uiEventBus
.subscribe(this);
61 EntityCacheDebuggerShortcutListener shortcutListener
;
63 public EntityCacheDebugger(){
64 shortcutListener
= new EntityCacheDebuggerShortcutListener("Debug Entities",
65 ShortcutAction
.KeyCode
.SPACEBAR
,
66 ShortcutAction
.ModifierKey
.CTRL
);
69 public void openFor(AbstractView view
){
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
);
81 logger
.warn("can only operate on CachingPresenters");
83 } catch (NoSuchMethodException
| SecurityException
| IllegalAccessException
| IllegalArgumentException
84 | InvocationTargetException e
) {
89 logger
.warn("view is null");
94 private void open(AbstractView view
, CachingPresenter presenter
) {
96 EntityCacheDebuggerComponent content
= new EntityCacheDebuggerComponent(presenter
);
98 if(view
instanceof AbstractCdmPopupEditor
){
99 findWindow((AbstractCdmPopupEditor
)view
).setModal(false);
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
);
117 public boolean beforeViewChange(ViewChangeEvent event
) {
125 public void afterViewChange(ViewChangeEvent event
) {
126 View newView
= event
.getNewView();
127 if(newView
instanceof AbstractView
){
128 ((AbstractView
)newView
).addShortcutListener(shortcutListener
);
130 if(event
.getOldView() instanceof AbstractView
){
131 ((AbstractView
)event
.getOldView()).removeShortcutListener(shortcutListener
);
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
);
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();
155 * @return the shortcutListener
157 public EntityCacheDebuggerShortcutListener
getShortcutListener() {
158 return shortcutListener
;
162 private class EntityCacheDebuggerShortcutListener
extends ShortcutListener
{
164 private static final long serialVersionUID
= -8727949764189908851L;
169 * @param modifierKeys
171 public EntityCacheDebuggerShortcutListener(
175 super(caption
, keyCode
, new int[]{modifierKey
});
179 public EntityCacheDebuggerShortcutListener(
182 super(caption
, new int[]{keyCode
});
186 public void handleAction(Object sender
, Object target
) {
187 if(sender
instanceof AbstractView
) {
188 EntityCacheDebugger
.this.openFor((AbstractView
)sender
);
190 if(sender
instanceof Window
&& ((Window
)sender
).getContent() instanceof AbstractPopupView
) {
191 EntityCacheDebugger
.this.openFor((AbstractPopupView
)((Window
)sender
).getContent());