-
Notifications
You must be signed in to change notification settings - Fork 55
/
jdee-widgets.el
581 lines (512 loc) · 21.5 KB
/
jdee-widgets.el
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
;;; jdee-widgets.el -- Custom-style widgets used by the JDE
;; $Id$
;; Author: Paul Kinnucan <[email protected]>
;; Maintainer: Paul Landes <landes <at> mailc dt net>
;; Keywords: java, tools
;; Copyright (C) 1997-2004 Paul Kinnucan.
;; Copyright (C) 2009 by Paul Landes
;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;;; Code:
(require 'wid-edit)
(require 'eieio)
(require 'efc)
(require 'tree-widget)
;; FIXME: refactor
;; jdee-dbs-java-udci-p, jdee-dbs-get-string, jdee-dbs-cmd-exec,
;; jdee-dbo-locals-open-p, jdee-dbs-java-array-p, jdee-dbs-java-primitive-p,
;; jdee-dbs-java-null-p, jdee-dbs-get-object, jdee-dbs-get-array
;; ----------------------------------------------------------------------
;; The Tree Widget Code:
;;; The `tree' Widget.
(define-widget 'jdee-widget-tree-open-button 'item
"Open node in `jdee-tree' widget."
:button-prefix "["
:button-suffix "]"
:tag "+"
:action 'jdee-widget-tree-open-button-callback
:help-echo "Show subtree."
:format "%[%t%]")
(defun jdee-widget-tree-open-button-callback (widget &optional event)
;; Set parent state to open.
(widget-value-set (widget-get widget :parent) t))
(define-widget 'jdee-widget-tree-close-button 'item
"Close node in `tree' widget."
:button-prefix "["
:button-suffix "]"
:tag "-"
:action 'jdee-widget-tree-close-button-callback
:help-echo "Hide subtree."
:format "%[%t%]")
(defun jdee-widget-tree-close-button-callback (widget &optional event)
;; Set parent state to closed.
(let* ((parent (widget-get widget :parent))
(entries (widget-get parent :args))
(children (widget-get parent :children)))
(while (and entries children)
(widget-put (car entries) :value (widget-value (car children)))
(setq entries (cdr entries)
children (cdr children)))
(widget-value-set parent nil)))
(define-widget 'jdee-widget-tree 'default
"A tree structure widget."
:convert-widget 'widget-types-convert-widget
:format "%v"
:prefix ""
:prefix-extra " "
:prefix-empty " |--- "
:value-get 'widget-value-value-get
:value-create 'jdee-widget-tree-value-create-callback
:value-delete 'widget-children-value-delete)
(defun jdee-widget-tree-value-create-callback (widget)
;; Insert all values
(let ((open (widget-value widget))
(tag (widget-get widget :tag))
(entries (widget-get widget :args))
children buttons)
(cond ((null entries)
;; Empty node.
(insert (widget-get widget :prefix-empty) tag "\n"))
(open
;; Open node.
(push
(widget-create-child-and-convert widget 'jdee-widget-tree-close-button)
buttons)
(insert "-\\ " tag "\n")
(let ((prefix (concat (widget-get widget :prefix)
(widget-get widget :prefix-extra)))
entry)
(while entries
(setq entry (car entries)
entries (cdr entries))
(insert prefix)
(push (if entries
(widget-create-child-and-convert widget entry
:prefix prefix
:prefix-extra " | ")
;; Last entry uses a different prefix.
(widget-create-child-and-convert
widget entry
:prefix prefix
:prefix-empty " `--- "))
children))))
(t
;; Closed node.
(push (widget-create-child-and-convert widget 'jdee-widget-tree-open-button)
buttons)
(insert "-- " tag "\n")))
(widget-put widget :children children)
(widget-put widget :buttons buttons)))
;;----------------------------------------------------------------------
;; Eval this to create a small tree.
(defun test-tree ()
(interactive)
(switch-to-buffer "*Tree Example*")
(kill-all-local-variables)
;; (make-local-variable 'widget-example-repeat)
(let ((inhibit-read-only t))
(erase-buffer))
(let ((all (overlay-lists)))
(mapc 'delete-overlay (car all))
(mapc 'delete-overlay (cdr all)))
(widget-insert "Test tree widget. \n\n")
; (setq tree (widget-create 'tree
; :tag "Foo"
; '(tree :tag "First")
; '(tree :tag "Second"
; :value nil
; (tree :tag "Nested"))
; '(tree :tag "Third")))
(setq tree (widget-create 'jdee-widget-tree
:tag "<test.Foo:139>"
'(jdee-widget-tree :tag "n int 0")
;; '(jdee-widget-tree :tag '(jdee-widget-tree :tag "n int 0"))
'(jdee-widget-tree :tag "a double 5.5")
'(jdee-widget-tree :tag "s S <test.S:145>"
(jdee-widget-tree :tag "b boolean true"))))
; (let* ((threads
; (list
; (list "ThreadGroup" 189 "system"
; (list
; (list "Thread" 190 "Signal dispatcher" "runnable" "suspended by debugger")
; (list "Thread" 191 "Reference Handler" "waiting" "suspended by debugger")
; (list "Thread" 192 "Finalizer" "waiting" "suspended by debugger")))
; (list "ThreadGroup" 193 "main"
; (list
; (list "Thread" 1 "main" "runnable" "suspended at breakpoint"))
; nil)))
; (tree (jdee-dbs-map-threads-to-tree threads)))
; (apply 'widget-create tree))
(use-local-map widget-keymap)
(widget-setup))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; Dynamic tree widget ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-widget 'jdee-widget-dtree 'default
"A widget whose nodes are generated on demand.
The first time the user expands the tree, the tree invokes a function that
generates the nodes. The tree then caches the nodes.
Thereafter, the node uses the cached nodes when the
user closes and then reopens the tree. Use the syntax
(widget-create 'jdee-widget-dtree :tag NAME :node-fcn NODE-FUNCTION)
to create the widget where NAME is the tree name and NODE-FUNCTION
is a function that takes one argument, the tree itself, and
returns a list of widgets that are the nodes of the expanded
tree."
:format "%v"
:prefix-extra " "
:value-get 'widget-value-value-get
:value-create 'jdee-widget-dtree-create-callback
:value-delete 'widget-children-value-delete
:has-nodes t)
(defun jdee-widget-dtree-create-callback (widget)
(let ((open (widget-value widget))
(tag (widget-get widget :tag))
children buttons)
(cond
(open
(push (widget-create-child-and-convert widget 'jdee-widget-tree-close-button)
buttons)
(insert "-\\ " tag "\n")
(let ((prefix (concat (widget-get widget :prefix)
(widget-get widget :prefix-extra)))
(nodes (widget-get widget :nodes))
node)
(when (and (widget-get widget :has-nodes)
(not nodes))
(setq nodes
(funcall (widget-get widget :node-fcn) widget))
(if nodes
(widget-put widget :nodes nodes)
(widget-put widget :has-nodes nil)))
(while nodes
(setq node (car nodes)
nodes (cdr nodes))
(insert prefix)
(push
(if nodes
(widget-create-child-and-convert widget node
:prefix prefix
:prefix-extra " | ")
(widget-create-child-and-convert
widget node
:prefix prefix
:prefix-empty " `--- "))
children))))
(t
;; Closed node.
(push (widget-create-child-and-convert widget 'jdee-widget-tree-open-button)
buttons)
(insert "-- " tag "\n")))
(widget-put widget :children children)
(widget-put widget :buttons buttons)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; Java object widget ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun jdee-test-get-fields (process object-id)
(list
(cons (list "sum" "double") (list "double" 0.0))
(cons (list "r" "int") (list "int" 1))
(cons (list "z" "java.lang.String") (list "java.lang.String" 229 nil))
(cons (list "B" "double[]") (list "double[]" 228 nil))
(cons (list "A" "double[][]") (list "double[][]" 227 nil))
(cons (list "args" "java.lang.String[]") (list "java.lang.String[]" 226 nil))))
(defun jdee-widget-java-var-to-tree (process var)
(let* ((var-name (oref var name))
(var-type (oref var jtype))
(var-value (oref var value))
(var-tag (format "%s %s" var-type var-name)))
(cond
((typep var-value 'jdee-dbs-java-udci)
(setq var-tag (format "%s [id: %d]" var-tag (oref var-value :id)))
(if (string= (oref var-value :jtype) "java.lang.String")
(let* ((cmd (jdee-dbs-get-string
"get string"
:process process
:object-id (oref var-value id)))
(str-val (jdee-dbs-cmd-exec cmd)))
(list 'tree-widget
:tag var-tag
:node-name var-tag
:open (jdee-dbo-locals-open-p var-tag)
:value t
(list 'tree-widget :tag str-val)))
(list 'jdee-widget-java-obj
:tag var-tag
:node-name var-tag
:open (jdee-dbo-locals-open-p var-tag)
:process process
:object-id (oref var-value :id))))
((typep var-value 'jdee-dbs-java-array)
(setq var-tag (format "%s [id: %d]" var-tag (oref var-value :id)))
(list 'jdee-widget-java-array
:tag var-tag
:node-name var-tag
:open (jdee-dbo-locals-open-p var-tag)
:process process :object var-value))
((typep var-value 'jdee-dbs-java-primitive)
(list 'tree-widget
:tag var-tag
:node-name var-tag
:open (jdee-dbo-locals-open-p var-tag)
:value t
(list 'tree-widget
:tag (format "%s" (oref var-value value)))))
((typep var-value 'jdee-dbs-java-null)
(list 'tree-widget
:tag var-tag
:node-name var-tag
:open (jdee-dbo-locals-open-p var-tag)
:value t
(list 'tree-widget :tag "null")))
(t
(error "Unidentified type of local variable: %s" var-tag)))))
(defun jdee-widget-java-obj-get-fields (obj-widget)
(if (widget-get obj-widget :args)
(widget-get obj-widget :args)
(let* ((process (widget-get obj-widget :process))
(object-id (widget-get obj-widget :object-id))
(cmd
(jdee-dbs-get-object
(format "get_object %d" object-id)
:process process
:object-id object-id))
(object
(jdee-dbs-cmd-exec cmd))
(fields (oref object fields))
field
nodes)
(while fields
(setq field (car fields) fields (cdr fields))
(setq field (cdr field))
(push
(jdee-widget-java-var-to-tree process field)
nodes))
nodes)))
(define-widget 'jdee-widget-java-obj 'tree-widget
"A widget that represents a Java object.
This widget is essentially a tree node whose entries are the fields
of the corresponding object. The first time the user expands the node,
the node retrieves the fields of the object from the debugger and
caches them. Thereafter, the node uses the cached values when the
user closes and then reopens the node. Use the syntax
(widget-create 'jdee-widget-java-obj
:tag NAME :process PROCESS :object-id OBJ-ID) to create the widget where
NAME is the object's name, PROCESS is the process in which
the object exists, and ID is the debugger id for the object."
:dynargs 'jdee-widget-java-obj-get-fields
:has-children t)
(defun jdee-widget-java-array-element-to-tree (process element index)
(cond
((typep element 'jdee-dbs-java-udci)
(if (string= (oref element :jtype) "java.lang.String")
(let* ((cmd (jdee-dbs-get-string
"get string"
:process process
:object-id (oref element id)))
(str-val (jdee-dbs-cmd-exec cmd)))
(list 'tree-widget
:tag (format "[%d] %s" index str-val)
:node-name (format "[%d] %s" index str-val)
:open (jdee-dbo-locals-open-p (format "[%d] %s" index str-val))
))
(list 'jdee-widget-java-obj
:tag (format "[%d] %s" index (oref element jtype))
:node-name (format "[%d] %s" index (oref element jtype))
:open (jdee-dbo-locals-open-p (format "[%d] %s" index (oref element jtype)))
:process process
:object-id (oref element id))))
((typep element 'jdee-dbs-java-array)
(list 'jdee-widget-java-array
:tag (format "[%d] %s" index (oref element jtype))
:node-name (format "[%d] %s" index (oref element jtype))
:open (jdee-dbo-locals-open-p (format "[%d] %s" index (oref element jtype)))
:process process
:object element))
((typep element 'jdee-dbs-java-primitive)
(list 'tree-widget :tag (format "[%d] %s" index (oref element value))))
((typep element 'jdee-dbs-java-null)
(list 'tree-widget :tag (format "[%d] null" index)))
(t
(error "Unidentified type of object: <%s|%s>" (oref element jtype)
(oref element id)))))
(defun jdee-widget-java-array-get-elements (array-widget)
(if (widget-get array-widget :args)
(widget-get array-widget :args)
(let* ((process (widget-get array-widget :process))
(array (widget-get array-widget :object))
cmd array-length)
(setq cmd
(jdee-dbs-get-array
(format "get_array_length %d" (oref array id))
:process process
:array array))
(jdee-dbs-cmd-exec cmd)
(setq array-length
(if (slot-boundp array 'length)
(oref array length)
0))
(when (> array-length 0)
(setq cmd
(jdee-dbs-get-array
(format "get_array_elements %d" (oref array id))
:process process
:array array
:index 0
:length array-length))
(jdee-dbs-cmd-exec cmd)
(let ((elements (oref array elements))
element
nodes
(index 0))
(while elements
(setq element (car elements) elements (cdr elements))
(setq nodes
(append nodes
(list (jdee-widget-java-array-element-to-tree process element index))))
(setq index (1+ index)))
nodes)))))
(define-widget 'jdee-widget-java-array 'tree-widget
"A widget that represents a Java array. Clicking on the widget's
expand button causes the widget to display the values of the array."
:dynargs 'jdee-widget-java-array-get-elements
:has-children t)
(defun test-obj ()
(interactive)
(switch-to-buffer "*Java Object Example*")
(kill-all-local-variables)
;; (make-local-variable 'widget-example-repeat)
(let ((inhibit-read-only t))
(erase-buffer))
(let ((all (overlay-lists)))
(mapc 'delete-overlay (car all))
(mapc 'delete-overlay (cdr all)))
(widget-insert "Test object tree. \n\n")
(widget-create 'jdee-widget-java-obj :tag "jmath.System s" :process "process" :object-id 1)
(widget-create 'jdee-widget-java-obj :tag "java.awt.Frame frame1" :process "process" :object-id 1)
(use-local-map widget-keymap)
(widget-setup))
;; ----------------------------------------------------------------------
;; Option Tree Widget
(defun jdee-widget-option-tree-open-button-callback (widget &optional event)
;; Set parent state to open.
(widget-value-set (widget-get widget :parent) t)
(widget-setup))
(define-widget 'jdee-widget-option-tree-open-button 'item
"Button to open an option tree."
:button-prefix "["
:button-suffix "]"
:tag "+"
:action 'jdee-widget-option-tree-open-button-callback
:help-echo "Show option tree."
:format "%[%t%]")
(define-widget 'jdee-widget-option-tree-close-button 'item
"Close node in `jdee-widget-option-tree' widget."
:button-prefix "["
:button-suffix "]"
:tag "-"
:action 'jdee-widget-option-tree-close-button-callback
:help-echo "Hide panel."
:format "%[%t%]")
(defun jdee-widget-option-tree-close-button-callback (widget &optional event)
;; Set parent state to closed.
(let* ((parent (widget-get widget :parent))
(entries (widget-get parent :args))
(group (car (widget-get parent :children)))
(children (widget-get group :children)))
;; Get values entered by user from children and
;; insert them in the corresponding widget definitions
;; so that they appear the next time the user expands
;; the tree.
(while (and entries children)
(widget-put (car entries) :value (widget-value (car children)))
(setq entries (cdr entries)
children (cdr children)))
(widget-value-set parent nil)))
(defun jdee-widget-option-tree-value-create-callback (widget)
(let ((open-widget-p (widget-value widget))
(tag (widget-get widget :tag))
(entries (widget-get widget :args))
entry children buttons)
(cond (open-widget-p
;; Wrap widgets in this tree in a group widget
;; to ensure proper formatting.
(let ((group-type
(list 'group :args entries))
group-widget)
(push
(widget-create-child-and-convert
widget
'jdee-widget-option-tree-close-button)
buttons)
(insert "-\\ " tag "\n")
(push (widget-create-child-and-convert widget group-type) children)))
(t
(push (widget-create 'jdee-widget-option-tree-open-button
:parent widget)
buttons)
(insert "-- " tag "\n")))
(widget-put widget :children children)
(widget-put widget :buttons buttons)))
(define-widget 'jdee-widget-option-tree 'default
"A panel containing widgets."
:convert-widget 'widget-types-convert-widget
:format "%v"
:value-get 'widget-value-value-get
:value-create 'jdee-widget-option-tree-value-create-callback
:value-delete 'widget-children-value-delete)
(defun test-option-tree ()
(interactive)
(switch-to-buffer "*Panel Example*")
(kill-all-local-variables)
(let ((inhibit-read-only t))
(erase-buffer))
(let ((all (overlay-lists)))
(mapc 'delete-overlay (car all))
(mapc 'delete-overlay (cdr all)))
(widget-insert "Test panel widget. \n\n")
(let ((panel (widget-create
'jdee-widget-option-tree
:tag "Compile Options"
'(cons :tag "Debugger Options"
(radio-button-choice :format "%t \n%v"
:tag "Debugger "
(const "jdb")
(const "oldjdb")
(const "Other"))
(cons :tag "Other Debugger Info"
(string :tag "Path")
(radio-button-choice :format "%t \n%v"
:tag "Type "
(const "Executable")
(const "Class"))))
'(repeat (string :tag "Path"))
'(editable-field :tag "classpath"
:format " %t: %v\n %h \n\n"
:size 40
:doc "Name of project.")
'(editable-field :tag "compiler"
:format " %t: %v\n %h \n\n"
:size 40
:doc "Name of project.")
'(jdee-widget-option-tree :tag "Debugger Options"
(repeat (string :tag "Path"))))))
(use-local-map widget-keymap)
(widget-setup)))
(provide 'jdee-widgets)
;; End of jdee-widgets.el