KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > webui > list > WebList


1 /*
2 Copyright (c) 2003 eInnovation Inc. All rights reserved
3
4 This library is free software; you can redistribute it and/or modify it under the terms
5 of the GNU Lesser General Public License as published by the Free Software Foundation;
6 either version 2.1 of the License, or (at your option) any later version.
7
8 This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the GNU Lesser General Public License for more details.
11 */

12
13 package com.openedit.webui.list;
14
15 import javax.swing.ListModel JavaDoc;
16
17 import org.dom4j.Document;
18 import org.dom4j.DocumentFactory;
19 import org.dom4j.Element;
20 import org.dom4j.Namespace;
21 import org.dom4j.QName;
22
23
24 /**
25  * This is a Web list widget analogous to Swing's <code>JList</code>.
26  *
27  * @author Eric and Matt
28  */

29 public class WebList
30 {
31     public static final Namespace WEB_LIST_NAMESPACE = Namespace.get(
32             "list", "http://www.einnovation.com/xmlns/WebUI/List");
33     public static final QName LIST_QNAME = new QName("list", WEB_LIST_NAMESPACE);
34     protected ListModel JavaDoc fieldModel;
35     protected String JavaDoc fieldName;
36     protected WebListCellRenderer fieldCellRenderer;
37
38     /**
39      * Create a new WebList which listens to the given model.
40      *
41      * @param inModel DOCUMENT ME!
42      */

43     public WebList(ListModel JavaDoc inModel)
44     {
45         setModel(inModel);
46     }
47
48     /**
49      * Sets the renderer that will render each list item.
50      *
51      * @param cellRenderer The cell renderer to set
52      */

53     public void setCellRenderer(WebListCellRenderer cellRenderer)
54     {
55         fieldCellRenderer = cellRenderer;
56     }
57
58     /**
59      * Returns the renderer that will render each list item.
60      *
61      * @return WebListCellRenderer
62      */

63     public WebListCellRenderer getCellRenderer()
64     {
65         return fieldCellRenderer;
66     }
67
68     /**
69      * Sets the model for this list.
70      *
71      * @param model The model to set
72      */

73     public void setModel(ListModel JavaDoc model)
74     {
75         fieldModel = model;
76     }
77
78     /**
79      * Returns the model for this list.
80      *
81      * @return WebListModel
82      */

83     public ListModel JavaDoc getModel()
84     {
85         return fieldModel;
86     }
87
88     /**
89      * Sets the name of this <code>WebList</code>.
90      *
91      * @param name The new name
92      */

93     public void setName(String JavaDoc name)
94     {
95         fieldName = name;
96     }
97
98     /**
99      * Returns the name of this <code>WebList</code>, which may be <code>null</code>.
100      *
101      * @return The name of this <code>WebList</code>
102      */

103     public String JavaDoc getName()
104     {
105         return fieldName;
106     }
107
108     /**
109      * Render this list as an XML document by calling the list cell renderer on each node.
110      *
111      * @return The XML version of this list
112      *
113      * @see #setCellRenderer
114      */

115     public Document render()
116     {
117         DocumentFactory factory = DocumentFactory.getInstance();
118         Element rootElem = factory.createElement(LIST_QNAME);
119
120         if (getName() != null)
121         {
122             rootElem.addAttribute("name", getName());
123         }
124
125         // Render all the items in the list and add them to the root element.
126
for (int i = 0; i < getModel().getSize(); i++)
127         {
128             rootElem.add(
129                 getCellRenderer().getListCellElement(this, getModel().getElementAt(i), factory));
130         }
131
132         return factory.createDocument(rootElem);
133     }
134 }
135
Popular Tags