-
Notifications
You must be signed in to change notification settings - Fork 0
/
HandlerBindManager.java
165 lines (144 loc) · 6.03 KB
/
HandlerBindManager.java
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package edu.ysu.itrace;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartReference;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.navigator.resources.ProjectExplorer;
import edu.ysu.itrace.gaze.GazeHandlerFactory;
import edu.ysu.itrace.gaze.IGazeHandler;
/**
* Binds on-screen elements to gaze handlers.
*/
public class HandlerBindManager {
public static final String KEY_HANDLER = "gazeHandler";
/**
* Binds all controls in an IWorkbenchPartReference that is
* an instance of IEditorPartReference to their appropriate
* gaze handlers if the handler exists.
* Binds the IWorkbenchPartReference that is an instance of
* IViewPartReference to the appropriate gaze handler if the
* handler exists.
* @param partRef Workbench part from which to get controls.
*/
public static void bind(IWorkbenchPartReference partRef) {
IWorkbenchPart part = partRef.getPart(true);
Control control = part.getAdapter(Control.class);
System.out.println(control);
//is an EditorPart
if (control != null) {
bindControl(partRef, control, false);
//is a ViewPart
} else {
//must be handled on a case to case basis
//Browser - always look through all controls in the shell for browsers and bind them
//regardless of the partRef that has become visible
//not possible to get a Browser control from a partRef
Shell workbenchShell = partRef.getPage().getWorkbenchWindow().getShell();
for (Control ctrl : workbenchShell.getChildren()) {
bind(ctrl); //call recursive helper function to find all browser controls
}
//Project Explorer
if (part.getAdapter(ProjectExplorer.class) != null) {
ProjectExplorer explorer = part.getAdapter(ProjectExplorer.class);
//this control is the primary control associated with a ProjectExplorer
Control viewControl = explorer.getCommonViewer().getControl();
bindControl(partRef, viewControl, false);
}
}
}
/**
* Recursive helper function to find and bind Browser controls
* @param control
*/
private static void bind(Control control) {
if (control instanceof Browser) {
bindControl(null, control, false);
}
//If composite, look through children.
if (control instanceof Composite) {
Composite composite = (Composite) control;
Control[] children = composite.getChildren();
if (children.length > 0 && children[0] != null) {
for (Control curControl : children)
bind(curControl);
}
}
}
/**
* Unbinds all controls in an IWorkbenchPartReference that is an instance
* of IEditorPartReference which are currently bound to a gaze handler.
* Unbinds an IWorkbenchPartReference that is an instance of IViewPartReference
* which is currently bound to a gaze handler.
* @param partRef Workbench part from which to get controls.
*/
public static void unbind(IWorkbenchPartReference partRef) {
IWorkbenchPart part = partRef.getPart(true);
Control control = part.getAdapter(Control.class);
//is an EditorPart
if (control != null) {
bindControl(partRef, control, true);
//is a ViewPart
} else {
//must be handled on a case to case basis
//Browser - always look through all controls in the shell for browsers and unbind them
//regardless of the partRef that has been hidden
//not possible to get Browser control from a partRef
Shell workbenchShell = partRef.getPage().getWorkbenchWindow().getShell();
for (Control ctrl : workbenchShell.getChildren()) {
unbind(ctrl);
}
//Project Explorer
if (part.getAdapter(ProjectExplorer.class) != null) {
ProjectExplorer explorer = part.getAdapter(ProjectExplorer.class);
//this control is the primary control associated with a ProjectExplorer
Control viewControl = explorer.getCommonViewer().getControl();
bindControl(partRef, viewControl, true);
}
}
}
/**
* Recursive helper function to find and unbind Browser controls
* @param control
*/
private static void unbind(Control control) {
if (control instanceof Browser) {
bindControl(null, control, true);
}
//If composite, look through children
if (control instanceof Composite) {
Composite composite = (Composite) control;
Control[] children = composite.getChildren();
if (children.length > 0 && children[0] != null) {
for (Control curControl : children)
unbind(curControl);
}
}
}
/**
* Bind a control. If it is a composite, also bind all of its children.
* @param control Highest level control.
* @param unbind If true, unbind instead of bind.
*/
private static void bindControl(IWorkbenchPartReference partRef,
Control control, boolean unbind) {
//If composite, bind children.
if (control instanceof Composite) {
Composite composite = (Composite) control;
Control[] children = composite.getChildren();
if (children.length > 0 && children[0] != null) {
for (Control curControl : children)
bindControl(partRef, curControl, unbind);
}
}
//control should not have any data set
//upon reaching this part of the method
IGazeHandler handler = GazeHandlerFactory.
createHandler(control, partRef);
if (handler != null && !unbind)
control.setData(KEY_HANDLER, handler);
else
control.setData(KEY_HANDLER, null);
}
}