FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
processing/java/src/processing/mode/java/pdex/ShowUsage.java at master · KeesR/processing · GitHub
KeesR
processing
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
processing
/
java
/
src
/
processing
/
mode
/
java
/
pdex
/
ShowUsage.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
329 lines (266 loc) · 10.7 KB
Breadcrumbs
processing
/
java
/
src
/
processing
/
mode
/
java
/
pdex
/
ShowUsage.java
Copy path
File metadata and controls
329 lines (266 loc) · 10.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package
processing
.
mode
.
java
.
pdex
;
import
static
processing
.
mode
.
java
.
pdex
.
ASTUtils
.
findAllOccurrences
;
import
java
.
awt
.
Color
;
import
java
.
awt
.
EventQueue
;
import
java
.
awt
.
GraphicsDevice
;
import
java
.
awt
.
GraphicsEnvironment
;
import
java
.
awt
.
Rectangle
;
import
java
.
awt
.
event
.
ComponentAdapter
;
import
java
.
awt
.
event
.
ComponentEvent
;
import
java
.
util
.
Comparator
;
import
java
.
util
.
List
;
import
java
.
util
.
Map
;
import
java
.
util
.
function
.
Consumer
;
import
java
.
util
.
stream
.
Collectors
;
import
javax
.
swing
.
JDialog
;
import
javax
.
swing
.
JMenuItem
;
import
javax
.
swing
.
JScrollPane
;
import
javax
.
swing
.
JTree
;
import
javax
.
swing
.
WindowConstants
;
import
javax
.
swing
.
tree
.
DefaultMutableTreeNode
;
import
javax
.
swing
.
tree
.
DefaultTreeModel
;
import
javax
.
swing
.
tree
.
TreeModel
;
import
org
.
eclipse
.
jdt
.
core
.
dom
.
IBinding
;
import
org
.
eclipse
.
jdt
.
core
.
dom
.
IMethodBinding
;
import
org
.
eclipse
.
jdt
.
core
.
dom
.
IVariableBinding
;
import
org
.
eclipse
.
jdt
.
core
.
dom
.
SimpleName
;
import
processing
.
app
.
Language
;
import
processing
.
app
.
ui
.
EditorStatus
;
import
processing
.
app
.
ui
.
Toolkit
;
import
processing
.
app
.
ui
.
ZoomTreeCellRenderer
;
import
processing
.
mode
.
java
.
JavaEditor
;
import
processing
.
mode
.
java
.
pdex
.
PreprocessedSketch
.
SketchInterval
;
class
ShowUsage
{
final
JDialog
window
;
final
JTree
tree
;
final
JavaEditor
editor
;
final
PreprocessingService
pps
;
final
Consumer
<
PreprocessedSketch
>
reloadListener
;
IBinding
binding
;
ShowUsage
(
JavaEditor
editor
,
PreprocessingService
pps
) {
this
.
editor
=
editor
;
this
.
pps
=
pps
;
// Add show usage option
JMenuItem
showUsageItem
=
new
JMenuItem
(
Language
.
text
(
"editor.popup.show_usage"
));
showUsageItem
.
addActionListener
(
e
->
handleShowUsage
());
editor
.
getTextArea
().
getRightClickPopup
().
add
(
showUsageItem
);
reloadListener
=
this
::
reloadShowUsage
;
{
// Show Usage window
window
=
new
JDialog
(
editor
);
window
.
setDefaultCloseOperation
(
WindowConstants
.
HIDE_ON_CLOSE
);
window
.
setAutoRequestFocus
(
false
);
window
.
addComponentListener
(
new
ComponentAdapter
() {
@
Override
public
void
componentHidden
(
ComponentEvent
e
) {
binding
=
null
;
tree
.
setModel
(
null
);
pps
.
unregisterListener
(
reloadListener
);
}
@
Override
public
void
componentShown
(
ComponentEvent
e
) {
pps
.
registerListener
(
reloadListener
);
}
});
window
.
setSize
(
Toolkit
.
zoom
(
300
,
400
));
window
.
setFocusableWindowState
(
false
);
Toolkit
.
setIcon
(
window
);
JScrollPane
sp2
=
new
JScrollPane
();
tree
=
new
JTree
();
ZoomTreeCellRenderer
renderer
=
new
ZoomTreeCellRenderer
(
editor
.
getMode
());
tree
.
setCellRenderer
(
renderer
);
renderer
.
setLeafIcon
(
null
);
renderer
.
setClosedIcon
(
null
);
renderer
.
setOpenIcon
(
null
);
renderer
.
setBackgroundSelectionColor
(
new
Color
(
228
,
248
,
246
));
renderer
.
setBorderSelectionColor
(
new
Color
(
0
,
0
,
0
,
0
));
renderer
.
setTextSelectionColor
(
Color
.
BLACK
);
sp2
.
setViewportView
(
tree
);
window
.
add
(
sp2
);
}
tree
.
addTreeSelectionListener
(
e
-> {
if
(
tree
.
getLastSelectedPathComponent
() !=
null
) {
DefaultMutableTreeNode
tnode
=
(
DefaultMutableTreeNode
)
tree
.
getLastSelectedPathComponent
();
if
(
tnode
.
getUserObject
()
instanceof
ShowUsageTreeNode
) {
ShowUsageTreeNode
node
= (
ShowUsageTreeNode
)
tnode
.
getUserObject
();
editor
.
highlight
(
node
.
tabIndex
,
node
.
startTabOffset
,
node
.
stopTabOffset
);
}
}
});
}
// Thread: EDT
void
handleShowUsage
() {
int
startOffset
=
editor
.
getSelectionStart
();
int
stopOffset
=
editor
.
getSelectionStop
();
int
tabIndex
=
editor
.
getSketch
().
getCurrentCodeIndex
();
pps
.
whenDoneBlocking
(
ps
->
handleShowUsage
(
ps
,
tabIndex
,
startOffset
,
stopOffset
));
}
// Thread: worker
void
handleShowUsage
(
PreprocessedSketch
ps
,
int
tabIndex
,
int
startTabOffset
,
int
stopTabOffset
) {
// Map offsets
int
startJavaOffset
=
ps
.
tabOffsetToJavaOffset
(
tabIndex
,
startTabOffset
);
int
stopJavaOffset
=
ps
.
tabOffsetToJavaOffset
(
tabIndex
,
stopTabOffset
);
// Find the node
SimpleName
name
=
ASTUtils
.
getSimpleNameAt
(
ps
.
compilationUnit
,
startJavaOffset
,
stopJavaOffset
);
if
(
name
==
null
) {
editor
.
statusMessage
(
"Cannot find any name under cursor"
,
EditorStatus
.
NOTICE
);
return
;
}
// Find binding
IBinding
binding
=
ASTUtils
.
resolveBinding
(
name
);
if
(
binding
==
null
) {
editor
.
statusMessage
(
"Cannot find usages, try to fix errors in your code first"
,
EditorStatus
.
NOTICE
);
return
;
}
findUsageAndUpdateTree
(
ps
,
binding
);
}
// Thread: worker
void
findUsageAndUpdateTree
(
PreprocessedSketch
ps
,
IBinding
binding
) {
this
.
binding
=
binding
;
// Get label
String
bindingType
=
""
;
switch
(
binding
.
getKind
()) {
case
IBinding
.
METHOD
:
IMethodBinding
method
= (
IMethodBinding
)
binding
;
if
(
method
.
isConstructor
())
bindingType
=
"Constructor"
;
else
bindingType
=
"Method"
;
break
;
case
IBinding
.
TYPE
:
bindingType
=
"Type"
;
break
;
case
IBinding
.
VARIABLE
:
IVariableBinding
variable
= (
IVariableBinding
)
binding
;
if
(
variable
.
isField
())
bindingType
=
"Field"
;
else
if
(
variable
.
isParameter
())
bindingType
=
"Parameter"
;
else
if
(
variable
.
isEnumConstant
())
bindingType
=
"Enum constant"
;
else
bindingType
=
"Local variable"
;
break
;
}
// Find usages, map to tree nodes, add to root node
String
bindingKey
=
binding
.
getKey
();
List
<
SketchInterval
>
intervals
=
findAllOccurrences
(
ps
.
compilationUnit
,
bindingKey
).
stream
()
.
map
(
ps
::
mapJavaToSketch
)
// remove occurrences which fall into generated header
.
filter
(
ps
::
inRange
)
// remove empty intervals (happens when occurence was inserted)
.
filter
(
in
->
in
.
startPdeOffset
<
in
.
stopPdeOffset
)
.
collect
(
Collectors
.
toList
());
int
usageCount
=
intervals
.
size
();
// Get element name from PDE code if possible, otherwise use one from Java
String
elementName
=
intervals
.
stream
()
.
findAny
()
.
map
(
si
->
ps
.
pdeCode
.
substring
(
si
.
startPdeOffset
,
si
.
stopPdeOffset
))
.
orElseGet
(
binding
::
getName
);
// Create root node
DefaultMutableTreeNode
rootNode
=
new
DefaultMutableTreeNode
(
bindingType
+
": "
+
elementName
);
intervals
.
stream
()
// Convert to TreeNodes
.
map
(
in
->
ShowUsageTreeNode
.
fromSketchInterval
(
ps
,
in
))
// Group by tab index
.
collect
(
Collectors
.
groupingBy
(
node
->
node
.
tabIndex
))
// Stream Map Entries of (tab index) <-> (List<ShowUsageTreeNode>)
.
entrySet
().
stream
()
// Sort by tab index
.
sorted
(
Comparator
.
comparing
(
Map
.
Entry
::
getKey
))
.
map
(
entry
-> {
Integer
tabIndex
=
entry
.
getKey
();
List
<
ShowUsageTreeNode
>
nodes
=
entry
.
getValue
();
int
count
=
nodes
.
size
();
String
usageLabel
=
count
==
1
?
"usage"
:
"usages"
;
// Create new DefaultMutableTreeNode for this tab
String
tabLabel
=
"<html><font color=#222222>"
+
ps
.
sketch
.
getCode
(
tabIndex
).
getPrettyName
() +
"</font> <font color=#999999>"
+
count
+
" "
+
usageLabel
+
"</font></html>"
;
DefaultMutableTreeNode
tabNode
=
new
DefaultMutableTreeNode
(
tabLabel
);
// Stream nodes belonging to this tab
nodes
.
stream
()
// Convert TreeNodes to DefaultMutableTreeNodes
.
map
(
DefaultMutableTreeNode
::
new
)
// Add all as children of tab node
.
forEach
(
tabNode
::
add
);
return
tabNode
;
})
// Add all tab nodes as children of root node
.
forEach
(
rootNode
::
add
);
TreeModel
treeModel
=
new
DefaultTreeModel
(
rootNode
);
// Update tree
EventQueue
.
invokeLater
(() -> {
tree
.
setModel
(
treeModel
);
// Expand all nodes
for
(
int
i
=
0
;
i
<
tree
.
getRowCount
();
i
++) {
tree
.
expandRow
(
i
);
}
tree
.
setRootVisible
(
true
);
if
(!
window
.
isVisible
()) {
window
.
setVisible
(
true
);
GraphicsEnvironment
ge
=
GraphicsEnvironment
.
getLocalGraphicsEnvironment
();
GraphicsDevice
defaultScreen
=
ge
.
getDefaultScreenDevice
();
Rectangle
rect
=
defaultScreen
.
getDefaultConfiguration
().
getBounds
();
int
maxX
= (
int
)
rect
.
getMaxX
() -
window
.
getWidth
();
int
x
=
Math
.
min
(
editor
.
getX
() +
editor
.
getWidth
(),
maxX
);
int
y
= (
x
==
maxX
) ?
10
:
editor
.
getY
();
window
.
setLocation
(
x
,
y
);
}
window
.
toFront
();
window
.
setTitle
(
"Usage of
\"
"
+
elementName
+
"
\"
: "
+
usageCount
+
" time(s)"
);
});
}
// Thread: worker
void
reloadShowUsage
(
PreprocessedSketch
ps
) {
if
(
binding
!=
null
) {
findUsageAndUpdateTree
(
ps
,
binding
);
}
}
void
hide
() {
window
.
setVisible
(
false
);
}
void
dispose
() {
if
(
window
!=
null
) {
window
.
dispose
();
}
}
}
class
ShowUsageTreeNode
{
final
int
tabIndex
;
final
int
startTabOffset
;
final
int
stopTabOffset
;
final
String
text
;
ShowUsageTreeNode
(
int
tabIndex
,
int
startTabOffset
,
int
stopTabOffset
,
String
text
) {
this
.
tabIndex
=
tabIndex
;
this
.
startTabOffset
=
startTabOffset
;
this
.
stopTabOffset
=
stopTabOffset
;
this
.
text
=
text
;
}
static
ShowUsageTreeNode
fromSketchInterval
(
PreprocessedSketch
ps
,
SketchInterval
in
) {
int
lineStartPdeOffset
=
ps
.
pdeCode
.
lastIndexOf
(
'\n'
,
in
.
startPdeOffset
) +
1
;
int
lineStopPdeOffset
=
ps
.
pdeCode
.
indexOf
(
'\n'
,
in
.
stopPdeOffset
);
if
(
lineStopPdeOffset
== -
1
)
lineStopPdeOffset
=
ps
.
pdeCode
.
length
();
int
highlightStartOffset
=
in
.
startPdeOffset
-
lineStartPdeOffset
;
int
highlightStopOffset
=
in
.
stopPdeOffset
-
lineStartPdeOffset
;
int
tabLine
=
ps
.
tabOffsetToTabLine
(
in
.
tabIndex
,
in
.
startTabOffset
);
// TODO: what a mess
String
line
=
ps
.
pdeCode
.
substring
(
lineStartPdeOffset
,
lineStopPdeOffset
);
String
pre
=
line
.
substring
(
0
,
highlightStartOffset
)
.
replace
(
"&"
,
"&"
).
replace
(
">"
,
">"
).
replace
(
"<"
,
"<"
);
String
highlight
=
line
.
substring
(
highlightStartOffset
,
highlightStopOffset
)
.
replace
(
"&"
,
"&"
).
replace
(
">"
,
">"
).
replace
(
"<"
,
"<"
);
String
post
=
line
.
substring
(
highlightStopOffset
)
.
replace
(
"&"
,
"&"
).
replace
(
">"
,
">"
).
replace
(
"<"
,
"<"
);
line
=
pre
+
"<font color=#222222><b>"
+
highlight
+
"</b></font>"
+
post
;
line
=
line
.
trim
();
String
text
=
"<html><font color=#bbbbbb>"
+
(
tabLine
+
1
) +
"</font> <font color=#777777>"
+
line
+
"</font></html>"
;
return
new
ShowUsageTreeNode
(
in
.
tabIndex
,
in
.
startTabOffset
,
in
.
stopTabOffset
,
text
);
}
@
Override
public
String
toString
() {
return
text
;
}
}
Back
|
FazBrowse Home
|
New Git URL