4358148e |
1 | (in-package :lisp-on-lines) |
2 | |
4358148e |
3 | (define-description editable () |
4 | () |
5 | (:mixinp t)) |
6 | |
f4efa7ff |
7 | (define-layered-class standard-attribute |
8 | :in-layer #.(defining-description 'editable) |
9 | () |
10 | ((edit-attribute-p |
11 | :initform :inherit |
12 | :layered-accessor attribute-editp |
13 | :initarg :editp |
14 | :layered t) |
15 | (setter |
16 | :initarg :setter |
17 | :layered t |
18 | :accessor attribute-setter |
19 | :initform nil) |
20 | (attribute-editor |
2548f054 |
21 | :initarg :editor |
f4efa7ff |
22 | :layered t |
23 | :accessor attribute-editor |
2548f054 |
24 | :initform (make-instance 'attribute-editor) |
f4efa7ff |
25 | :documentation "This ones a bit odd"))) |
26 | |
2548f054 |
27 | (defmethod shared-initialize :after ((object standard-attribute) |
28 | slots &rest args &key input &allow-other-keys) |
29 | |
30 | (when input |
31 | (setf (attribute-editor object) |
32 | (apply #'make-instance (find-editor-class input) |
33 | input)))) |
34 | |
35 | |
36 | (defun find-editor-class (spec) |
37 | (let ((class (getf spec :class)) |
38 | (type (getf spec :type))) |
39 | (or class (when |
40 | (and type (symbolp type)) |
41 | (let ((name (format nil "~A-~A" type 'attribute-editor))) |
42 | (or (find-class (intern name (symbol-package type)) nil) |
43 | (find-class (intern name) nil) |
44 | 'string-attribute-editor)))))) |
f4efa7ff |
45 | |
46 | (defclass attribute-editor () |
47 | ((type :initarg :type |
2548f054 |
48 | :initform 'string |
49 | :accessor attribute-editor-type) |
f4efa7ff |
50 | (parser :initarg :parse-using |
51 | :initform 'identity |
52 | :accessor attribute-editor-parsing-function) |
53 | (prompt :initarg :prompt |
2548f054 |
54 | :initform nil) |
55 | (unbound-value |
56 | :initarg :unbound-value |
57 | :initform ""))) |
58 | |
59 | |
f4efa7ff |
60 | |
61 | (defclass string-attribute-editor (attribute-editor) ()) |
62 | (defclass text-attribute-editor (string-attribute-editor) ()) |
2548f054 |
63 | |
64 | (deftype password () 'string) |
65 | |
f4efa7ff |
66 | (defclass password-attribute-editor (string-attribute-editor) ()) |
67 | |
68 | (defclass number-attribute-editor (attribute-editor) () |
69 | (:default-initargs |
70 | :parse-using 'parse-number:PARSE-NUMBER |
71 | :type 'number)) |
72 | |
73 | (defun parse-attribute-value (attribute value) |
74 | (funcall (attribute-editor-parsing-function |
75 | (attribute-editor attribute)) |
76 | value)) |
77 | |
78 | (define-layered-function display-attribute-editor (attribute) |
79 | (:method (attribute) |
80 | (setf (attribute-value attribute) |
81 | (funcall (attribute-editor-parsing-function |
82 | (attribute-editor attribute)) |
83 | (read-line))))) |
84 | |
4358148e |
85 | (define-description T () |
86 | ((editp :label "Edit by Default?" |
87 | :value nil |
88 | :editp nil) |
89 | (identity :editp nil) |
90 | (type :editp nil) |
91 | (class :editp nil)) |
92 | (:in-description editable)) |
93 | |
b7657b86 |
94 | (define-layered-method (setf attribute-value-using-object) |
95 | :in-layer #.(defining-description 'editable)(value object attribute) |
96 | |
97 | (let ((setter (attribute-setter attribute))) |
98 | (if setter |
99 | (funcall setter value object) |
100 | (error "No setter in ~A for ~A" attribute object)))) |
4358148e |
101 | |
4358148e |
102 | |
f4efa7ff |
103 | (define-layered-function attribute-editp (attribute) |
104 | (:method (attribute) nil)) |
4358148e |
105 | |
106 | (define-layered-method attribute-editp |
107 | :in-layer #.(defining-description 'editable) |
f4efa7ff |
108 | ((attribute standard-attribute)) |
2548f054 |
109 | (let ((value (attribute-value attribute))) |
110 | (unless (or (unbound-slot-value-p value) |
111 | (typep value |
112 | (attribute-editor-type |
113 | (attribute-editor attribute)))) |
114 | (return-from attribute-editp nil))) |
f4efa7ff |
115 | (let ((edit? (call-next-method))) |
116 | (if (eq :inherit edit?) |
117 | (attribute-value (find-attribute |
118 | (attribute-description attribute) |
119 | 'editp)) |
120 | edit?))) |
4358148e |
121 | |
122 | |
f4efa7ff |
123 | (define-layered-method display-attribute-value |
4358148e |
124 | :in-layer #.(defining-description 'editable) |
f4efa7ff |
125 | ((attribute standard-attribute)) |
126 | (if (attribute-editp attribute) |
127 | (display-attribute-editor attribute) |
e8d4fa45 |
128 | (call-next-method))) |
4358148e |
129 | |
130 | |
f4efa7ff |
131 | |
132 | |
133 | |
4358148e |
134 | |