-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathswing.lisp
581 lines (479 loc) · 20 KB
/
swing.lisp
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
;; (require :abcl-contrib)
;; (require :jss)
;; (setf jss:*muffle-warnings* nil)
;; ;; Follows the following SWING tutorial:
;; ;; https://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html#programmatic
;; easy action listener creator
(defun handle-java-error (e)
"Stub for handling a JavaException thrown within the Swing UI"
(format t "~40a~%~40a~%" "handle-java-error:" e)
(force-output)) ;important for showing the message ASAP
(defun actionlistener (handler-function)
"Easy creation of action listener, handler function requires to be provided.
Function shall take one parameter -- event (the event)"
;; instantiate proxy that implements actionListener
(java:jinterface-implementation
"java.awt.event.ActionListener"
"actionPerformed"
;; wrap the handler function
(lambda (e)
(handler-case
(funcall handler-function e)
(java:java-exception (x)
(handle-java-error x))))))
;; helper to test if it works
(defun %test-actionlistener (listener)
(#"actionPerformed" listener +null+))
;; easy button creator
(defun button (text &optional handler-function)
"Create a button (with text).
Handler-function is a function that works as the action listener for the button."
(let ((b (jss:new 'JButton text)))
(when handler-function
(#"addActionListener" b (actionlistener handler-function)))
b))
(defun pack (component)
"Apply Pack method to component."
(#"pack" component))
(defun set-visible (component &optional (visible? +true+) )
"Set component visible (or not). Use Java booleans +true+ + +false+!!"
(#"setVisible" component visible?))
(defun set-size (component size-x size-y)
"Set size to component."
(#"setSize" component size-x size-y))
;; frame creator
(defun frame (title &optional size-x size-y)
"Create a new frame. And make it visible."
(let ((f (jss:new 'JFrame title)))
(when (and size-x size-y)
(set-size f size-x size-y))
(set-visible f)
f))
(defmacro %add-to-component (component-var items-var)
"Utility macro. Adds to component/container."
`(loop for i in ,items-var
do
(#"add" ,component-var i)))
(defun add-to (container items)
"Add the list of components (items) to the container."
(%add-to-component container items))
(defun add-with-constraints (container list)
"Add the list of components (items in alist) to the container.
Each item of the list should be a CONS or list where the
CAR is the component to be added, the (optional) CDR should be
the constraints specified (i.e. a string)."
(loop for i in list do
(if (cdr i) (#"add" container (car i) (cdr i))
(#"add" container (car i)))))
;; add things to frame
;; TODO: rewrite as macro.
;; (defun frame-add (frame items &key (pack T))
;; "Add items to the JFrame frame."
;; (%add-to-component frame items)
;; (when pack
;; (pack frame)))
;; JPanel
(defun panel (&optional layout-manager)
"Create a new JPanel."
(if layout-manager
(jss:new 'JPanel layout-manager)
(jss:new 'JPanel)))
;; (defun panel-add (panel items)
;; "Add items to the JPanel panel."
;; (%add-to-component panel items))
;; JTabbedPane
(defun tabbedpane ()
"JTabbedPane constructor."
(jss:new 'JTabbedPane))
;; (defun tabbedpane-add (tp items)
;; "Add items to tabbedpane"
;; (%add-to-component tp items))
;; JDialog - modeless dialog
(defun dialog (&optional parent-frame (title "") (is-modal +false+))
"Create a JDialog."
(if parent-frame
(jss:new 'JDialog parent-frame title is-modal)
(jss:new 'JDialog)))
;; horizontal alignment from SwingConstants
(defparameter +align-right+ (jss:get-java-field 'SwingConstants "RIGHT"))
(defparameter +align-left+ (jss:get-java-field 'SwingConstants "LEFT"))
(defparameter +align-center+ (jss:get-java-field 'SwingConstants "CENTER"))
;; and more
(defparameter +align-top+ (jss:get-java-field 'SwingConstants "TOP"))
(defparameter +align-bottom+ (jss:get-java-field 'SwingConstants "BOTTOM"))
(defparameter +align-leading+ (jss:get-java-field 'SwingConstants "LEADING"))
(defparameter +align-trailing+ (jss:get-java-field 'SwingConstants "TRAILING"))
(defun label (text &optional horizontal-alignment)
"Create a new JLabel."
(let ((l (jss:new 'JLabel text)))
(when horizontal-alignment
(#"setHorizontalAlignment" l horizontal-alignment))
l))
(defun textfield (&optional (text "")
columns
(editable +true+))
"Create a new JTextField"
(let ((tx
(if columns
(jss:new 'JTextField text columns)
(jss:new 'JTextField text))))
(#"setEditable" tx editable)
tx))
(defun textarea (&optional (text "")
rows
columns)
"Create a JTextarea."
(let ((ta
(jss:new 'JTextArea text)))
(when rows
(#"setRows" ta rows))
(when columns
(#"setColumns" ta columns))
ta))
;; text area font example
(defun font-fixed (size)
"Get fixed spacing font of required size"
(jss:new "java.awt.Font" "Courier New"
(jss:get-java-field 'java.awt.Font "PLAIN") size))
(defun textarea-set-font (tx font)
(#"setFont" tx font))
(defun add-caretlistener (text-component caret-update-function)
"Create and add a CaretListener for a text component,
which calls caret-update-function (sending event e) on change in the caret position of a text component."
(#"addCaretListener" text-component
;; caretListener implementation
(java:jinterface-implementation
"javax.swing.event.CaretListener"
"caretUpdate"
;; impl for caretUpdate
caret-update-function)))
(defun scrollpane (control)
"Create scrollpane for supplied control"
(jss:new 'JScrollPane control))
;; TODO: JPasswordField
;; TODO: JCheckBox
;; TODO: JCheckBox : ItemListener
;; TODO: JRadioButton
(defun listselectionlistener (handler-function)
"Easy creation of ListSelectionListener, handler function requires to be provided.
Function shall take one parameter -- event (ListSelectionListener event)"
;; instantiate proxy that implements actionListener
(java:jinterface-implementation
"javax.swing.event.ListSelectionListener"
"valueChanged"
handler-function))
(defun defaultlistmodel (&optional elements)
"Creates a new DefaultListModel. Optionally, adds elements from the supplied list.."
(let ((l
(jss:new 'DefaultListModel)))
(when elements
(loop for i in elements
do
(#"addElement" l i)))
l))
(defun defaultlistmodel-add (list-model element)
"Add element to listmodel."
(#"addElement" list-model element))
(defun defaultlistmodel-get-element-at (list-model index)
"Obtain element at index. (Index is zero-based)"
(#"elementAt" list-model index))
(defun defaultlistmodel-insert-element-at (list-model element index)
"Insert element at index. (Index is zero-based)"
(#"insertElementAt" list-model element index))
(defun defaultlistmodel-remove-element-at (list-model index)
"Remove element at index and return it. (Index is zero-based)"
(#"remove" list-model index))
;; (defun defaultlistmodel-move-element (list-model index offset)
;; "Move up or down element at index. (Index is zero-based)
;; Offest: -1 = move up, +1 = move down"
;; (let ((new-index (+ index offset))
;; (elem (defaultlistmodel-get-element-at list-model index))
;; (all (defaultlistmodel-getlist list-model)))
;; (when (and elem
;; (>= new-index 0)
;; (< new-index (defaultlistmodel-size list-model)))
;; ;; clear list, create it again...
;; (#"clear" list-model)
;; (loop for x from 0 to (1- (length all))
;; do
;; (cond
;; ((equal x new-index)
;; (#"addElement" list-model elem))
;; ((equal x index) nil)
;; (t (#"addElement" list-model (elt all index)))))
;; (defaultlistmodel-insert-element-at list-model elem new-index)
;; (defaultlistmodel-remove-element-at list-model index))))
(defun defaultlistmodel-size (list-model)
"Obtain number of elements"
(#"getSize" list-model))
(defun defaultlistmodel-getlist (list-model)
"Get all elements"
(loop for x from 0 to (1- (defaultlistmodel-size list-model))
collecting (defaultlistmodel-get-element-at list-model x)))
(defun jlist (&optional list-model selection-listener-function)
"Creats a new JList. Optionally uses a list handler function,
which shall receive one parameter, the event (ListSelectionListener event)"
(let ((l (if list-model (jss:new 'JList list-model)
(jss:new 'JList))))
(when selection-listener-function
(#"addListSelectionListener" l (listselectionlistener selection-listener-function)))
l))
;; JScrollBar??
;;DIALOGS
;; constants for dialogs
(defun %option (str)
(jss:get-java-field 'JOptionPane str))
;; Return values for dialogs.
(defparameter +dialog-yes+ (%option "YES_OPTION"))
(defparameter +dialog-no+ (%option "NO_OPTION"))
(defparameter +dialog-cancel+ (%option "CANCEL_OPTION"))
(defparameter +dialog-ok+ (%option "OK_OPTION"))
(defparameter +dialog-closed+ (%option "CLOSED_OPTION"))
(defun show-message-dialog (parent-component text)
"Shows a message dialog."
(java:jstatic "showMessageDialog"
"javax.swing.JOptionPane"
parent-component
text))
(defun show-confirm-dialog (parent-component text)
"Shows a confirm dialog."
(java:jstatic "showConfirmDialog"
"javax.swing.JOptionPane"
parent-component
text))
(defun show-warning-message (parent-component text &optional (dialog-title "Warning"))
"Shows a warning message dialog."
(java:jstatic "showMessageDialog"
"javax.swing.JOptionPane"
parent-component
text
dialog-title
(jss:get-java-field 'JOptionPane "WARNING_MESSAGE")))
(defun show-input-dialog (parent-component message)
(java:jstatic "showInputDialog"
"javax.swing.JOptionPane"
parent-component
message))
;; MENU ITEMS !
(defun menu (title menu-items)
"Creates JMenu object. Allows a list of submenus (menu items)."
(let ((m (jss:new 'JMenu title)))
(%add-to-component m menu-items)
m))
(defun menuitem (title &optional handler-function)
"Creates a menu item with its corresponding handler function."
(let ((m (jss:new 'JMenuItem title)))
(when handler-function
(#"addActionListener" m (actionlistener handler-function)))
m))
(defun set-menu-bar (frame menu-list)
"Create a menu bar for the supplied menu list.
Then set the frame to that menu bar."
(let ((mb (jss:new 'JMenuBar)))
;; add items in menu-list to mb
(%add-to-component mb menu-list)
;; set this menubar on the supplied frame
(#"setJMenuBar" frame mb)))
(defun %menu-test ()
(let ((f (frame "Menu test!!")))
(set-menu-bar f
(list
(menu "Menu1"
(list
(menuitem "MenuItem1"
(lambda (e)
(declare (ignore e))
(show-warning-message f
"OYE NO ME APRIETES!!" "Alertita")))
(menuitem "Aprietame"
(lambda (e)
(declare (ignore e))
(show-warning-message f
"ESO, ESO, ESO" "Alertita")))))
(menu "Misc"
(list
(menuitem "About"
(lambda (e)
(declare (ignore e))
(show-message-dialog f "(c) 2018 Defunkydrummer.")))
))))
;;(set-motif-laf f)
))
;; Popup Menu
(defun popupmenu (title menu-items)
"Creates JPopupMenu object. Allows a list of submenus (menu items)."
(let ((m (jss:new 'JPopupMenu title)))
(%add-to-component m menu-items)
m))
;; add menu to containter
(defun add-popupmenu-to-container (container popup-menu)
"Add the popup-menu to the frame or container.
If right-clicked, the popup menu will appear...
NOTE: This will set/reset the mouseClicked event handler on the frame."
;;TODO: Must be RIGHT click.
(#"addMouseListener"
container
(java:jinterface-implementation
"java.awt.event.MouseListener"
;; implementation of method...
"mouseClicked"
;; handler for right-click.
(lambda (e)
(if (equal (#"getButton" e)
(jss:get-java-field 'java.awt.event.MouseEvent "BUTTON3"))
;; show menu at point.
(#"show" popup-menu
container
(#"getX" e)
(#"getY" e)))))))
(defun %popupmenu-test ()
(let* ((f (frame "Hello World" 640 480))
(p (popupmenu "Menu"
(list (menuitem "HELLO"
(lambda (e)
(declare (ignore e))
(show-message-dialog f "Hello my friends!")))
(menuitem "GOODBYE")))))
(add-popupmenu-to-container f p)))
;; TODO: JCheckBoxMenuItem (bueno, es para agregar checkboxes en menus, bien especializado no?)
(defun separator (&optional orientation)
"JSeparator"
(declare (type (or fixnum null) orientation))
(if orientation
(jss:new 'JSeparator orientation)
(jss:new 'JSeparator)))
;; ------------------------------------------------------------------------
;; JTABLE
;; ------------------------------------------------------------------------
;; helper
(defun list-to-jarray (list)
"Convert list to java array if necessary."
(cond
((consp list) (java:jarray-from-list list))
((java:java-object-p list) list)
(t (error "Can't conver this list."))))
(defun defaulttablemodel (column-names)
"Create DefaultTableModel, column names not optional..."
(let ((cols (list-to-jarray column-names)))
(jss:new 'DefaultTableModel
(java:jnew-array "java.lang.String" 0 0) ;data is Object[][]
cols))) ;String[] columns
(defun jtable (table-model)
"Create table based on DefaultTableModel"
(jss:new 'JTable table-model))
;; ------------------------------------------------------------------------
;; COMBO BOX
;; ------------------------------------------------------------------------
(defun jcombobox (items)
"Create a JComboBox with the following items (list of strings)"
(jss:new 'JComboBox
(list-to-jarray items)))
(defun jcombobox-selecteditem (cb)
"Get the selected item of the combo box."
(#"getSelectedItem" cb))
;; TODO: JProgressbar?
;; TODO: JTree?
;; and other other componnents.
;; TODO: JEditorPane
;; File chooser
(defun file-chooser ()
"Create a JFileChooser"
(jss:new 'JFileChooser))
(defun file-chooser-add-extension (chooser description extensions)
"Add the supplied extension(s) (and description) to what the file chooser shall accept."
(declare (type string description)
(type cons extensions))
(let ((filter (jss:new 'FileNameExtensionFilter
description
;; lisp list to java array.
(java:jarray-from-list extensions))))
(#"addChoosableFileFilter" chooser filter)))
(defun get-file-info (file)
"Obtain info from a Java.io.File object as PLIST"
(list :absolute-file (#"getAbsoluteFile" file) ;Java.io.File
:path (#"getAbsolutePath" file)
:name (#"getName" file)
:parent (#"getParent" file)))
(defun open-file-chooser (chooser parent-component)
"Open file chooser.
If file was approved, return file properties as plist."
(let ((retval (#"showOpenDialog" chooser parent-component)))
(if (equal retval
(jss:get-java-field 'JFileChooser "APPROVE_OPTION"))
;; return the chosen filename
(let ((file (#"getSelectedFile" chooser)))
(get-file-info file)))))
(defun open-file-chooser-save (chooser parent-component)
"Open file chooser for SAVE.
If file was approved, return file properties as plist."
(let ((retval (#"showSaveDialog" chooser parent-component)))
(if (equal retval
(jss:get-java-field 'JFileChooser "APPROVE_OPTION"))
;; return the chosen filename
(let ((file (#"getSelectedFile" chooser)))
(get-file-info file)))))
(defun open-file-chooser-multiple-selection (chooser parent-component)
"Open file chooser using the selected extensions (list of strings).
If file was approved, return list of file paths."
(#"setMultiSelectionEnabled" chooser +true+)
(let ((retval (#"showOpenDialog" chooser parent-component)))
(if (equal retval
(jss:get-java-field 'JFileChooser "APPROVE_OPTION"))
;; return the chosen filenames (ALL)
(let ((files (#"getSelectedFiles" chooser)))
(loop for f across files
collecting (get-file-info f))))))
;; -------------------------------------------------------------------------------------
;; LAYOUTS
;; -------------------------------------------------------------------------------------
(defun add-using-borderlayout (parent-container
&key north south east west center)
"Add components to container using BorderLayout. Each one is added in the direction specified.
I.e. :north xx adds xx in the NORTH direction.
You don't need to specify all directions."
(let ((n (jss:get-java-field 'BorderLayout "NORTH"))
(s (jss:get-java-field 'BorderLayout "SOUTH"))
(e (jss:get-java-field 'BorderLayout "EAST"))
(w (jss:get-java-field 'BorderLayout "WEST"))
(c (jss:get-java-field 'BorderLayout "CENTER")))
(if north (#"add" parent-container north n))
(if south (#"add" parent-container south s))
(if east (#"add" parent-container east e))
(if west (#"add" parent-container west w))
(if center (#"add" parent-container center c))))
(defun set-layout (container layout)
"Set container to the required layout object."
(#"setLayout" container layout))
(defun gridlayout (rows columns &optional horizontal-gap vertical-gap)
"Create GridLayout with specified number of rows and columns."
(if (and horizontal-gap vertical-gap)
(jss:new 'GridLayout rows columns horizontal-gap vertical-gap)
(jss:new 'GridLayout rows columns)))
;; flow layout alignments
;; TODO: See if we should use SwingConstants (defined earlier, above)
(defparameter +flow-layout-left+ (jss:get-java-field 'FlowLayout "LEFT"))
(defparameter +flow-layout-right+ (jss:get-java-field 'FlowLayout "RIGHT"))
(defparameter +flow-layout-center+ (jss:get-java-field 'FlowLayout "CENTER"))
(defparameter +flow-layout-leading+ (jss:get-java-field 'FlowLayout "LEADING"))
(defparameter +flow-layout-trailing+ (jss:get-java-field 'FlowLayout "TRAILING"))
(defun flowlayout (flow-layout-alignment &optional horizontal-gap vertical-gap)
"Create FlowLayout with the specified alignment (use the defined constants for alignment.)"
(if (and horizontal-gap vertical-gap)
(jss:new 'FlowLayout flow-layout-alignment horizontal-gap vertical-gap)
(jss:new 'FlowLayout flow-layout-alignment)))
;; TEMP: Load MigLayout
(defvar *miglayout-loaded* nil)
(defun load-miglayout ()
(unless *miglayout-loaded*
(java:add-to-classpath #P"java_libs/miglayout-3.5.5.jar")
(jss:jar-import "java_libs/miglayout-3.5.5.jar")
(setf *miglayout-loaded* T)))
(defun miglayout (&optional (layout-constraints "")
(column-constraints "")
(row-constraints ""))
"Constructor for a MigLayout."
(load-miglayout)
(jss:new "net.miginfocom.swing.MigLayout"
layout-constraints
column-constraints
row-constraints))