blob: 84df7e7c7c2d1821d82775cb636a8b43538913d9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
/*
@VaadinApache2LicenseForJavaFiles@
*/
package com.vaadin.ui;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.vaadin.terminal.gwt.server.AbstractCommunicationManager;
import com.vaadin.terminal.gwt.server.ClientConnector;
import com.vaadin.ui.Component.RepaintRequestEvent;
import com.vaadin.ui.Component.RepaintRequestListener;
/**
* A class that tracks dirty {@link ClientConnector}s. A {@link ClientConnector}
* is dirty when an operation has been performed on it on the server and as a
* result of this operation new information needs to be sent to its client side
* counterpart.
*
* @author Vaadin Ltd
* @version @VERSION@
* @since 7.0.0
*
*/
public class DirtyConnectorTracker implements RepaintRequestListener {
private Set<Component> dirtyComponents = new HashSet<Component>();
private Root root;
/**
* Gets a logger for this class
*
* @return A logger instance for logging within this class
*
*/
public static Logger getLogger() {
return Logger.getLogger(DirtyConnectorTracker.class.getName());
}
public DirtyConnectorTracker(Root root) {
this.root = root;
}
public void repaintRequested(RepaintRequestEvent event) {
markDirty((Component) event.getConnector());
}
public void componentAttached(Component component) {
component.addListener(this);
markDirty(component);
}
private void markDirty(Component component) {
if (getLogger().isLoggable(Level.FINE)) {
if (!dirtyComponents.contains(component)) {
getLogger()
.fine(getDebugInfo(component) + " " + "is now dirty");
}
}
dirtyComponents.add(component);
}
private void markClean(Component component) {
if (getLogger().isLoggable(Level.FINE)) {
if (dirtyComponents.contains(component)) {
getLogger().fine(
getDebugInfo(component) + " " + "is no longer dirty");
}
}
dirtyComponents.remove(component);
}
private String getDebugInfo(Component component) {
String message = getObjectString(component);
if (component.getParent() != null) {
message += " (parent: " + getObjectString(component.getParent())
+ ")";
}
return message;
}
private String getObjectString(Object component) {
return component.getClass().getName() + "@"
+ Integer.toHexString(component.hashCode());
}
public void componentDetached(Component component) {
component.removeListener(this);
markClean(component);
}
public void markAllComponentsDirty() {
markComponentsDirtyRecursively(root);
getLogger().fine("All components are now dirty");
}
public void markAllComponentsClean() {
dirtyComponents.clear();
getLogger().fine("All components are now clean");
}
/**
* Marks all visible components dirty, starting from the given component and
* going downwards in the hierarchy.
*
* @param c
* The component to start iterating downwards from
*/
private void markComponentsDirtyRecursively(Component c) {
if (!c.isVisible()) {
return;
}
markDirty(c);
if (c instanceof HasComponents) {
HasComponents container = (HasComponents) c;
for (Component child : AbstractCommunicationManager
.getChildComponents(container)) {
markComponentsDirtyRecursively(child);
}
}
}
public Collection<Component> getDirtyComponents() {
return dirtyComponents;
}
}
|