;;;; *************************************************************************
;;;; FILE IDENTIFICATION
;;;;
-;;;; Name: views.lisp
-;;;; Purpose: View methods for Hyperobjects
-;;;; Programmer: Kevin M. Rosenberg
-;;;; Date Started: Apr 2000
+;;;; Name: views.lisp
+;;;; Purpose: View methods for Hyperobjects
+;;;; Author: Kevin M. Rosenberg
+;;;; Created: Apr 2000
;;;;
-;;;; $Id: views.lisp,v 1.59 2003/06/29 16:21:09 kevin Exp $
+;;;; $Id$
;;;;
-;;;; This file is Copyright (c) 2000-2003 by Kevin M. Rosenberg
+;;;; This file is Copyright (c) 2000-2004 by Kevin M. Rosenberg
;;;; *************************************************************************
-
+
(in-package #:hyperobject)
(defclass object-view ()
- ((object-class-name :initform nil :initarg :object-class-name
- :accessor object-class-name
- :documentation "Name of class of object to be viewed.")
- (object-class :initform nil :initarg :object-class
+ ((object-class :initform nil :initarg :object-class
:accessor object-class
:documentation "Class of object to be viewed.")
(slots :initform nil :initarg :slots :accessor slots
:documentation "List of effective slots for object to be viewed.")
- (name :initform nil :initarg :name :accessor name
- :documentation "Name for this view.")
- (category :initform nil :initarg :category :accessor category
- :documentation "Category for view. Helpful when want to find a view corresponding to a particular category.")
- (source-code :initform nil :initarg :source-code :accessor source-code
+ (id :initform nil :initarg :id :accessor id
+ :documentation "id for this view.")
+ (source-code :initform nil :initarg :source-code :accessor source-code
:documentation "Source code for generating view.")
(country-language :initform :en :initarg :country-language
:documentation "Country's Language for this view.")
+ (printer :initform nil :initarg :printer :accessor printer
+ :documentation "default function that prints the object")
;;
(file-start-str :type (or string null) :initform nil :initarg :file-start-str
:accessor file-start-str)
:accessor link-href-end)
(link-ampersand :type (or string null) :initform nil :initarg :link-ampersand
:accessor link-ampersand))
- (:default-initargs :link-page "lookup-func1")
+ (:default-initargs :link-page "meta-search.html")
(:documentation "View class for a hyperobject"))
-
-(defun get-category-view (obj category &optional slots)
+(defun get-default-view-id (obj-cl)
+ (aif (views obj-cl)
+ (id (car it))
+ :compact-text))
+
+(defun find-view-id-in-class-precedence (obj-cl vid)
+ "Looks for a view in class and parent classes"
+ (when (typep obj-cl 'hyperobject-class)
+ (aif (find vid (views obj-cl) :key #'id :test #'eq)
+ it
+ (let (cpl)
+ (handler-case
+ (setq cpl (class-precedence-list obj-cl))
+ (error (e)
+ (declare (ignore e))
+ ;; can't get cpl unless class finalized
+ (make-instance (class-name obj-cl))
+ (setq cpl (class-precedence-list obj-cl))))
+ (find-view-id-in-class-precedence (second cpl) vid)))))
+
+
+(defun get-view-id (obj vid &optional slots)
"Find or make a category view for an object"
- (let ((obj-class (class-of obj)))
- (if (null category)
- (default-view obj-class)
- (aif (find category (views obj-class) :key #'category)
- it
- (let ((view
- (make-instance 'object-view
- :object-class-name (class-name obj-class)
- :object-class obj-class
- :category category
- :slots slots)))
- (push view (views obj-class))
- view)))))
-
+ (let ((obj-cl (class-of obj)))
+ (unless vid
+ (setq vid (get-default-view-id obj-cl)))
+ (aif (find-view-id-in-class-precedence obj-cl vid)
+ it
+ (let ((view
+ (make-instance 'object-view
+ :object-class (class-name obj-cl)
+ :id vid
+ :slots slots)))
+ (push view (views obj-cl))
+ view))))
+
;;;; *************************************************************************
;;;; Metaclass Intialization
;;;; *************************************************************************
(unless (default-print-slots cl)
(setf (default-print-slots cl)
(mapcar #'slot-definition-name (class-slots cl))))
- (let ((views '()))
- (dolist (view-def (views cl))
- (push (make-object-view cl view-def) views))
- (setf (views cl) (nreverse views)))
- (cond
- ((default-view cl)
- (setf (default-view cl) (make-object-view cl (default-view cl))))
- ((car (views cl))
- (setf (default-view cl) (make-object-view cl (car (views cl)))))
- (t
- (setf (default-view cl) (make-object-view cl :default)))))
+ (setf (views cl)
+ (loop for view-def in (direct-views cl)
+ collect (make-object-view cl view-def))))
(defun make-object-view (cl view-def)
"Make an object view from a definition. Do nothing if a class is passed so that reinitialization will be a no-op"
((typep view-def 'object-view)
view-def)
((eq view-def :default)
- (let* ((name (class-name cl))
- (view (make-instance 'object-view :name "automatic"
- :object-class-name name
- :object-class cl
- :category :compact-text)))
- view))
+ (make-instance 'object-view
+ :object-class (class-name cl)
+ :id :compact-text))
((consp view-def)
- (apply #'make-instance 'object-view view-def))
+ (make-instance 'object-view
+ :object-class (class-name cl)
+ :id (getf view-def :id)
+ :slots (getf view-def :slots)
+ :source-code (getf view-def :source-code)))
(t
(error "Invalid parameter to make-object-view: ~S" view-def))))
-(defmethod initialize-instance :after ((view object-view)
- &rest initargs &key &allow-other-keys)
- (initialize-view (object-class view) view))
-
-(defun initialize-view (obj-cl view)
+(defmethod initialize-instance :after ((self object-view)
+ &rest initargs
+ &key
+ &allow-other-keys)
+ (initialize-view self))
+
+(defun initialize-view (view)
"Calculate all view slots for a hyperobject class"
- (cond
- ((category view)
- (initialize-view-by-category obj-cl view))
- ((source-code view)
- (initialize-view-by-source-code obj-cl view))
- (t
- (setf (category view) :compact-text)
- (initialize-view-by-category obj-cl view))))
+ (let ((obj-cl (find-class (object-class view))))
+ (cond
+ ((source-code view)
+ (initialize-view-by-source-code view))
+ ((id view)
+ (initialize-view-by-id obj-cl view))
+ (t
+ (setf (id view) :compact-text)
+ (initialize-view-by-id obj-cl view)))))
-(defun initialize-view-by-source-code (obj-cl view)
+
+
+(defun initialize-view-by-source-code (view)
"Initialize a view based upon a source code"
- (let ((source-code (source-code view)))
- (warn "source code compilation is not implemented")
- )
- )
+ (let* ((source-code (source-code view))
+ (printer `(lambda
+ (,(intern (symbol-name '#:self)
+ (symbol-package (object-class view)))
+ ,(intern (symbol-name '#:s)
+ (symbol-package (object-class view))))
+ (declare (ignorable
+ ,(intern (symbol-name '#:self)
+ (symbol-package (object-class view)))
+ ,(intern (symbol-name '#:s)
+ (symbol-package (object-class view)))))
+ (with-slots ,(slots view)
+ ,(intern (symbol-name '#:self)
+ (symbol-package (object-class view)))
+ ,@source-code))))
+ (setf (printer view)
+ (compile nil (eval printer)))))
(defmacro write-simple (v s)
`(typecase ,v
(funcall formatter slot-data)
slot-data)))
(if cdata
- (write-xml-cdata fmt-data strm)
+ (write-cdata fmt-data strm)
(write-simple fmt-data strm))))
(defun ppfc-html (title name type formatter cdata print-func)
(vector-push-extend `(write-string ,tag s) print-func)
(vector-push-extend '(write-char #\> s) print-func))
+(defun ppfc-display-table (title name type formatter cdata print-func)
+ (vector-push-extend '(write-string "<td>" s) print-func)
+ (ppfc-html title name type formatter cdata print-func)
+ (vector-push-extend '(write-string "</td>" s) print-func))
+
(defun ppfc-html-labels (label name type formatter cdata print-func)
(vector-push-extend '(write-string "<span class=\"label\">" s) print-func)
(vector-push-extend `(write-string ,label s) print-func)
(defun ppfc-html-link (name type formatter cdata nlink print-func)
(declare (fixnum nlink))
(vector-push-extend '(write-char #\< s) print-func)
- (vector-push-extend `(write-string (nth ,(+ nlink nlink) links) s) print-func)
+ (vector-push-extend `(write-string (nth ,(+ nlink nlink) links) s) print-func)
(vector-push-extend '(write-char #\> s) print-func)
(vector-push-extend `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func)
(vector-push-extend '(write-string "</" s) print-func)
- (vector-push-extend `(write-string (nth ,(+ nlink nlink 1) links) s) print-func)
+ (vector-push-extend `(write-string (nth ,(+ nlink nlink 1) links) s) print-func)
(vector-push-extend '(write-char #\> s) print-func))
(defun ppfc-html-link-labels (label name type formatter cdata nlink print-func)
(vector-push-extend '(write-string "</span> " s) print-func)
(ppfc-html-link name type formatter cdata nlink print-func))
-(defun push-print-fun-code (category slot nlink print-func)
+(defun push-print-fun-code (vid slot nlink print-func)
(let* ((formatter (esd-print-formatter slot))
(name (slot-definition-name slot))
(user-name (esd-user-name slot))
(xml-user-name (escape-xml-string user-name))
(xml-tag (escape-xml-string user-name))
- (type (slot-value slot 'type))
+ (type (slot-value slot #-sbcl 'type
+ #+sbcl 'sb-pcl::%type))
(cdata (not (null
- (and (in category :xml :xhtml :xml-link :xhtml-link
+ (and (in vid :xml :xhtml :xml-link :xhtml-link
:xml-labels :ie-xml-labels
:xhtml-link-labels :xml-link-labels :ie-xml-link
:ie-xml-link-labels)
(or formatter
(lisp-type-is-a-string type))))))
(hyperlink (esd-hyperlink slot)))
-
- (case category
+
+ (case vid
(:compact-text
(vector-push-extend
`(write-ho-value x ',name ',type ',formatter ,cdata s) print-func))
(ppfc-html-labels user-name name type formatter cdata print-func))
(:xhtml-labels
(ppfc-xhtml-labels xml-user-name user-name name type formatter cdata print-func))
+ ((:display-table :display-table-labels)
+ (ppfc-display-table user-name name type formatter cdata print-func))
(:xml-labels
(ppfc-xml-labels xml-user-name xml-tag name type formatter cdata print-func))
((or :html-link :xhtml-link)
(defun view-has-links-p (view)
- (in (category view) :html-link :xhtml-link :xml-link :ie-xml-link
+ (in (id view) :html-link :xhtml-link :xml-link :ie-xml-link
:html-link-labels :xhtml-link-labels :xml-link-labels
:ie-xml-link-labels))
-(defun initialize-view-by-category (obj-cl view)
- "Initialize a view based upon a preset category"
- (unless (in (category view) :compact-text :compact-text-labels
- :html :html-labels :html-link-labels
- :xhtml :xhtml-labels :xhtml-link-labels
- :xhtml-link :html-link
- :xml :xml-labels :xml-link :ie-xml-link
- :xml-link-labels :ie-xml-link-labels
- :display-table :edit-table)
- (error "Unknown view category ~A" (category view)))
-
+(defun creatable-view-id-p (obj-cl vid)
+ "Returns T if a view id can be created for this class"
+ (declare (ignore obj-cl))
+ (in vid :compact-text :compact-text-labels
+ :html :html-labels :html-link-labels
+ :xhtml :xhtml-labels :xhtml-link-labels
+ :xhtml-link :html-link
+ :xml :xml-labels :xml-link :ie-xml-link
+ :xml-link-labels :ie-xml-link-labels
+ :display-table :display-table-labels :edit-table :edit-table-labels))
+
+(defun initialize-view-by-id (obj-cl view)
+ "Initialize a view based upon a preset vid"
+ (unless (creatable-view-id-p obj-cl (id view))
+ (error "Unable to automatically create view id ~A" (id view)))
+
(unless (slots view) (setf (slots view) (default-print-slots obj-cl)))
(let ((links '())
- (print-func (make-array 10 :fill-pointer 0 :adjustable t)))
+ (print-func (make-array 20 :fill-pointer 0 :adjustable t)))
(do* ((slots (slots view) (cdr slots))
(slot-name (car slots) (car slots))
((null slots))
(unless slot
(error "Slot ~A is not found in class ~S" slot-name obj-cl))
-
- (push-print-fun-code (category view) slot (length links) print-func)
+
+ (push-print-fun-code (id view) slot (length links) print-func)
(when (> (length slots) 1)
(vector-push-extend '(write-char #\space s) print-func))
,@(map 'list #'identity print-func)))
(setf (obj-data-printer view)
(compile nil (eval (obj-data-print-code view))))
-
+
(setf (link-slots view) (nreverse links)))
- (finalize-view-by-category view)
+ (finalize-view-by-id view)
view)
-(defun finalize-view-by-category (view)
- (case (category view)
+(defun finalize-view-by-id (view)
+ (case (id view)
((or :compact-text :compact-text-labels)
(initialize-text-view view))
((or :html :html-labels)
(setf (link-href-start view) "a href=")
(setf (link-href-end view) "a")
(setf (link-ampersand view) "&"))
+ ((or :display-table :display-table-labels :edit-tables)
+ (initialize-table-view view)
+ (when (in (id view) :display-table-labels :edit-table-labels)
+ (setf (list-start-printer view) #'table-label-list-start-func))
+ (setf (link-href-start view) "a href=")
+ (setf (link-href-end view) "a")
+ (setf (link-ampersand view) "&"))
((or :xml-link :xml-link-labels)
(initialize-xml-view view)
(setf (link-href-start view)
(setf (link-ampersand view) "&"))))
-(defun make-std-object-slots-view (class-name slots)
- #'(lambda (obj strm)
- )
-
- )
-
;;;; *************************************************************************
;;;; View Data Format Section
;;;; *************************************************************************
(setf (indenter view) #'indent-spaces))
(defun html-list-start-func (obj nitems indent strm)
- (write-string "<div class=\"ho-username\" :style=\"margin-left:" strm)
+ (write-string "<div class=\"ho-username\" style=\"margin-left:" strm)
(write-fixnum (+ indent indent) strm)
(write-string "em;\">" strm)
(write-user-name-maybe-plural obj nitems strm)
(setf (obj-data-indent view) nil))
(defun xhtml-list-start-func (obj nitems indent strm)
- (write-string "<div class=\"ho-username\" :style=\"margin-left:" strm)
+ (write-string "<div class=\"ho-username\" style=\"margin-left:" strm)
+ (write-fixnum (+ indent indent) strm)
+ (write-string "em;\">" strm)
+ (write-user-name-maybe-plural obj nitems strm)
+ (write-string "</div>" strm)
+ (write-string "<div style=\"margin-left:" strm)
+ (write-fixnum (+ indent indent) strm)
+ (write-string "em;\">" strm)
+ (write-char #\newline strm))
+
+(defun table-list-start-func (obj nitems indent strm)
+ (write-string "<div class=\"ho-username\" style=\"margin-left:" strm)
(write-fixnum (+ indent indent) strm)
(write-string "em;\">" strm)
(write-user-name-maybe-plural obj nitems strm)
(write-string "</div>" strm)
- (write-string "<div :style=\"margin-left:" strm)
+ (write-char #\newline strm)
+ (write-string "<table style=\"margin-left:" strm)
(write-fixnum (+ indent indent) strm)
(write-string "em;\">" strm)
+ (write-string "<tbody>" strm)
+ (write-char #\newline strm))
+
+(defun table-label-list-start-func (obj nitems indent strm)
+ (write-string "<div class=\"ho-username\" style=\"margin-left:" strm)
+ (write-fixnum (+ indent indent) strm)
+ (write-string "em;\">" strm)
+ (write-user-name-maybe-plural obj nitems strm)
+ (write-string "</div>" strm)
+ (write-char #\newline strm)
+ (write-string "<table style=\"margin-left:" strm)
+ (write-fixnum (+ indent indent) strm)
+ (write-string "em;\">" strm)
+ (write-string "<thead>" strm)
+ (dolist (slot (default-print-slots (class-of obj)))
+ (write-string "<th>" strm)
+ (write-string (write-to-string slot) strm)
+ (write-string "</th>" strm))
+ (write-string "</thead>" strm)
+ (write-char #\newline strm)
+ (write-string "<tbody>" strm)
(write-char #\newline strm))
(defun html-obj-start (obj indent strm)
(setf (obj-end-printer view) (format nil "</div>~%"))
(setf (obj-data-indent view) nil))
+(defun initialize-table-view (view)
+ (initialize-text-view view)
+ (setf (indenter view) #'indent-spaces)
+ (setf (file-start-str view) (format nil "<html><body>~%"))
+ (setf (file-end-str view) (format nil "</body><html>~%"))
+ (setf (list-start-indent view) nil)
+ (setf (list-start-printer view) #'table-list-start-func)
+ (setf (list-end-printer view) (format nil "</tbody>~%</table>~%"))
+ (setf (list-end-indent view) nil)
+ (setf (obj-start-indent view) nil)
+ (setf (obj-start-printer view) #'html-obj-start)
+ (setf (obj-start-printer view) (format nil "<tr>"))
+ (setf (obj-end-printer view) (format nil "</tr>~%"))
+ (setf (obj-data-indent view) nil))
+
(defun xmlformat-list-end-func (x strm)
(write-string "</" strm)
(write-string (class-name-of x) strm)
(write-char #\newline strm))
(defun initialize-xml-view (view)
- (setf (file-start-str view) "") ; (std-xml-header)
- (setf (list-start-indent view) t)
- (setf (list-start-printer view) #'xmlformat-list-start-func)
- (setf (list-end-indent view) t)
- (setf (list-end-printer view) #'xmlformat-list-end-func)
- (setf (obj-start-printer view) (format nil "<~(~a~)>" (object-class-name view)))
- (setf (obj-start-indent view) t)
- (setf (subobj-end-printer view) (format nil "</~(~a~)>~%" (object-class-name view)))
- (setf (subobj-end-indent view) nil)
- (setf (obj-data-indent view) nil))
+ (let ((name (string-downcase (symbol-name (object-class view)))))
+ (setf (file-start-str view) "") ; (std-xml-header)
+ (setf (list-start-indent view) t)
+ (setf (list-start-printer view) #'xmlformat-list-start-func)
+ (setf (list-end-indent view) t)
+ (setf (list-end-printer view) #'xmlformat-list-end-func)
+ (setf (obj-start-printer view) (format nil "<~(~a~)>" name))
+ (setf (obj-start-indent view) t)
+ (setf (obj-end-printer view) (format nil "</~(~a~)>~%" name))
+ (setf (subobj-end-printer view) (format nil "</~(~a~)>~%" name))
+ (setf (subobj-end-indent view) nil)
+ (setf (obj-data-indent view) nil)))
;;; File Start and Ends
(defun fmt-obj-end (obj view strm indent)
(when (obj-end-indent view)
(awhen (indenter view)
- (funcall it indent strm)))
+ (funcall it indent strm)))
(awhen (obj-end-printer view)
(if (stringp it)
(write-string it strm)
(defun fmt-subobj-end (obj view strm indent)
(when (subobj-end-indent view)
(awhen (indenter view)
- (funcall it indent strm)))
+ (funcall it indent strm)))
(awhen (subobj-end-printer view)
(if (stringp it)
(write-string it strm)
(funcall it obj strm))))
-
-;;; Object Data
+
+;;; Object Data
(defun make-link-start (view fieldfunc fieldvalue refvars link-printer)
(link-printer
(funcall link-printer link-page fieldfunc fieldvalue refvars s))))
(write-char #\" s)))
-
+
(defun make-link-end (obj view fieldname)
(declare (ignore obj fieldname))
(link-href-end view))
(load-all-subobjects it))))
objs)
-(defun view-hyperobject (objs view category strm &optional (indent 0) filter
+(defun view-subobjects (obj strm &optional vid (indent 0) filter
+ subobjects refvars link-printer)
+ (when (hyperobject-class-subobjects obj)
+ (dolist (subobj (hyperobject-class-subobjects obj))
+ (aif (slot-value obj (name-slot subobj))
+ (view-hyperobject
+ it (get-view-id (car (mklist it)) vid)
+ strm vid (1+ indent) filter subobjects refvars
+ link-printer)))))
+
+
+(defun view-hyperobject (objs view strm &optional vid (indent 0) filter
subobjects refvars link-printer)
"Display a single or list of hyperobject-class instances and their subobjects"
(let-when (objlist (mklist objs))
(*print-level* nil))
(fmt-list-start (car objlist) view strm indent nobjs)
(dolist (obj objlist)
- (unless (and filter (not (funcall filter obj)))
- (fmt-obj-start obj view strm indent)
- (fmt-obj-data obj view strm (1+ indent) refvars link-printer)
- (fmt-obj-end obj view strm indent)
- (fmt-subobj-start obj view strm indent)
- (when (and subobjects (hyperobject-class-subobjects obj))
- (dolist (subobj (hyperobject-class-subobjects obj))
- (aif (slot-value obj (name-slot subobj))
- (view-hyperobject it
- (get-category-view (car (mklist it))
- category)
- category strm (1+ indent) filter
- subobjects refvars link-printer))))
- (fmt-subobj-end obj view strm indent)))
+ (awhen (printer view)
+ (funcall it obj strm))
+ (unless (and filter (not (funcall filter obj)))
+ (fmt-obj-start obj view strm indent)
+ (fmt-obj-data obj view strm (1+ indent) refvars link-printer)
+ (fmt-obj-end obj view strm indent)
+ (if subobjects
+ (progn
+ (fmt-subobj-start obj view strm indent)
+ (view-subobjects obj strm vid indent filter subobjects
+ refvars link-printer)
+ (fmt-subobj-end obj view strm indent))
+ (fmt-subobj-start obj view strm indent))))
(fmt-list-end (car objlist) view strm indent nobjs)))
objs)
-(defun view (objs &key (stream *standard-output*) category view
+(defun view (objs &key (stream *standard-output*) vid view
filter subobjects refvars file-wrapper link-printer)
"EXPORTED Function: prints hyperobject-class objects. Calls view-hyperobject"
(let-when (objlist (mklist objs))
- (when category
- (setq view (get-category-view (car objlist) category)))
(unless view
- (setq view (default-view (class-of (car objlist)))))
+ (setq view (get-view-id (car objlist) vid)))
(when file-wrapper
(fmt-file-start view stream))
- (view-hyperobject objlist view category stream 0 filter subobjects refvars
+ (view-hyperobject objlist view stream vid 0 filter subobjects refvars
link-printer)
(when file-wrapper
(fmt-file-end view stream)))