-
Notifications
You must be signed in to change notification settings - Fork 0
/
StyledTextGazeHandler.java
164 lines (142 loc) · 5.42 KB
/
StyledTextGazeHandler.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
package edu.ysu.itrace.gaze.handlers;
//import org.eclipse.jface.text.source.projection.ProjectionViewer;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Point;
import edu.ysu.itrace.AstManager;
import edu.ysu.itrace.AstManager.SourceCodeEntity;
import edu.ysu.itrace.ControlView;
import edu.ysu.itrace.Gaze;
import edu.ysu.itrace.gaze.IGazeHandler;
import edu.ysu.itrace.gaze.IStyledTextGazeResponse;
/**
* Implements the gaze handler interface for a StyledText widget.
*/
public class StyledTextGazeHandler implements IGazeHandler {
private StyledText targetStyledText;
//private ProjectionViewer projectionViewer;
/**
* Constructs a new gaze handler for the target StyledText object
*/
public StyledTextGazeHandler(Object target) {
this.targetStyledText = (StyledText) target;
}
@Override
public IStyledTextGazeResponse handleGaze(int absoluteX, int absoluteY,
int relativeX, int relativeY, final Gaze gaze) {
final int lineIndex;
final int col;
final Point absoluteLineAnchorPosition;
final String name;
final int lineHeight;
final int fontHeight;
final AstManager.SourceCodeEntity[] entities;
final String path;
try {
if (targetStyledText.getData(ControlView.KEY_AST) == null)
return null;
AstManager astManager = (AstManager) targetStyledText
.getData(ControlView.KEY_AST);
//projectionViewer = astManager.getProjectionViewer();
lineIndex = targetStyledText.getLineIndex(relativeY);
int lineOffset = targetStyledText.getOffsetAtLine(lineIndex);
//int lineOffset = targetStyledText.getOffsetAtLine(targetStyledText.getLineIndex(relativeY));
int offset;
try{
offset = targetStyledText.getOffsetAtLocation(new Point(relativeX, relativeY));
}catch(IllegalArgumentException ex){
return null;
}
col = offset - lineOffset;
//lineIndex = projectionViewer.widgetLine2ModelLine(targetStyledText.getLineIndex(relativeY));
// (0, 0) relative to the control in absolute screen
// coordinates.
Point relativeRoot = new Point(absoluteX - relativeX, absoluteY
- relativeY);
// Top-left position of the first character on the line in
// relative coordinates.
Point lineAnchorPosition = targetStyledText
.getLocationAtOffset(lineOffset);
// To absolute.
absoluteLineAnchorPosition = new Point(lineAnchorPosition.x
+ relativeRoot.x, lineAnchorPosition.y + relativeRoot.y);
lineHeight = targetStyledText.getLineHeight();
fontHeight = targetStyledText.getFont().getFontData()[0]
.getHeight();
entities = astManager.getSCEs(lineIndex + 1, col);
path = astManager.getPath();
int splitLength = path.split("\\\\").length;
name = path.split("\\\\")[splitLength-1];
} catch (IllegalArgumentException e) {
/* An IllegalArgumentException SHOULD mean that the gaze fell
* outside the valid text area, so just drop this one.
*/
e.printStackTrace();
return null;
}
/*
* This anonymous class just grabs the variables marked final
* in the enclosing method and returns them.
*/
return new IStyledTextGazeResponse() {
@Override
public String getName() {
return name;
}
@Override
public String getGazeType() {
/*String[] splitPath = path.split("\\.");
String type = splitPath[splitPath.length-1];*/
String type = path;
int dotIndex;
for(dotIndex=0; dotIndex<type.length();dotIndex++)
if(path.charAt(dotIndex) == '.')
break;
if(dotIndex+1 == type.length())
return "text";
type = type.substring(dotIndex+1);
return type;
}
@Override
public int getLineHeight() {
return lineHeight;
}
@Override
public int getFontHeight() {
return fontHeight;
}
@Override
public Gaze getGaze() {
return gaze;
}
public IGazeHandler getGazeHandler() {
return StyledTextGazeHandler.this;
}
@Override
public int getLine() {
return lineIndex + 1;
}
@Override
public int getCol() {
return col;
}
// Write out the position at the top-left of the first
// character in absolute screen coordinates.
@Override
public int getLineBaseX() {
return absoluteLineAnchorPosition.x;
}
@Override
public int getLineBaseY() {
return absoluteLineAnchorPosition.y;
}
@Override
public SourceCodeEntity[] getSCEs() {
return entities;
}
@Override
public String getPath() {
return path;
}
};
}
}