KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > web > html > Composite


1 // ===========================================================================
2
// Copyright (c) 1996 Mort Bay Consulting Pty. Ltd. All rights reserved.
3
// $Id: Composite.java,v 1.1 2004/03/23 13:59:49 laurent Exp $
4
// ---------------------------------------------------------------------------
5

6 package org.objectweb.jac.aspects.gui.web.html;
7
8 // Laurent Martelli <laurent@aopsys.com> - 21 mar. 2004:
9
// Added add(int position, Object element) method
10

11 import java.io.IOException JavaDoc;
12 import java.io.OutputStream JavaDoc;
13 import java.io.OutputStreamWriter JavaDoc;
14 import java.io.Writer JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import org.mortbay.util.Code;
17 import org.mortbay.html.Page;
18
19 /* -------------------------------------------------------------------- */
20 /** HTML Composite Element.
21  * <p>This class is can be used a either an abstract or concrete
22  * holder of other HTML elements.
23  * Used directly, it allow multiple HTML Elements to be added which
24  * are produced sequentially.
25  * Derived used of Composite may wrap each contain Element in
26  * special purpose HTML tags (e.g. list).
27  *
28  * <p>Notes<br>
29  * Elements are added to the Composite either as HTML Elements or as
30  * Strings. Other objects added to the Composite are converted to Strings
31  * @see Element
32  * @version $Id: Composite.java,v 1.1 2004/03/23 13:59:49 laurent Exp $
33  * @author Greg Wilkins
34 */

35 public class Composite extends Element
36 {
37     /* ----------------------------------------------------------------- */
38     /** The vector of elements in this Composite.
39      */

40     protected ArrayList JavaDoc elements= new ArrayList JavaDoc(8);
41
42     /* ----------------------------------------------------------------- */
43     protected Composite nest=null;
44
45     /* ----------------------------------------------------------------- */
46     /** Default constructor.
47      */

48     public Composite()
49     {}
50     
51     /* ----------------------------------------------------------------- */
52     /** Default constructor.
53      */

54     public Composite(String JavaDoc attributes)
55     {
56         super(attributes);
57     }
58
59     /* ----------------------------------------------------------------- */
60     /** Add an Object to the Composite by converting it to a Element or.
61      * String
62      * @param o The Object to add. If it is a String or Element, it is
63      * added directly, otherwise toString() is called.
64      * @return This Composite (for chained commands)
65      */

66     public Composite add(Object JavaDoc o)
67     {
68         return add(elements.size(),o);
69     }
70
71
72     /* ----------------------------------------------------------------- */
73     /** Add an Object to the Composite by converting it to a Element or.
74      * String
75      * @param o The Object to add. If it is a String or Element, it is
76      * added directly, otherwise toString() is called.
77      * @param position the position at which to insert the object
78      * @return This Composite (for chained commands)
79      */

80     public Composite add(int position, Object JavaDoc o)
81     {
82         if (nest!=null)
83             nest.add(position,o);
84         else
85         {
86             if (o!=null)
87             {
88                 if (o instanceof Element)
89                 {
90                     Code.assertTrue(!(o instanceof Page),
91                                     "Can't insert Page in Composite");
92                     elements.add(position,o);
93                 }
94                 else if (o instanceof String JavaDoc)
95                     elements.add(position,o);
96                 else
97                     elements.add(position,o.toString());
98             }
99         }
100         return this;
101     }
102     
103     /* ----------------------------------------------------------------- */
104     /** Nest a Composite within a Composite.
105      * The passed Composite is added to this Composite. Adds to
106      * this composite are actually added to the nested Composite.
107      * Calls to nest are passed the nested Composite
108      * @return The Composite to unest on to return to the original
109      * state.
110      */

111     public Composite nest(Composite c)
112     {
113         if (nest!=null)
114             return nest.nest(c);
115         else
116         {
117             add(c);
118             nest=c;
119         }
120         return this;
121     }
122
123     /* ----------------------------------------------------------------- */
124     /** Explicit set of the Nested component.
125      * No add is performed. setNest() obeys any current nesting and
126      * sets the nesting of the nested component.
127      */

128     public Composite setNest(Composite c)
129     {
130         if (nest!=null)
131             nest.setNest(c);
132         else
133             nest=c;
134         return this;
135     }
136     
137     /* ----------------------------------------------------------------- */
138     /** Recursively unnest the composites.
139      */

140     public Composite unnest()
141     {
142         if (nest!=null)
143             nest.unnest();
144         nest = null;
145         return this;
146     }
147
148
149     /* ----------------------------------------------------------------- */
150     /** The number of Elements in this Composite.
151      * @return The number of elements in this Composite
152      */

153     public int size()
154     {
155         return elements.size();
156     }
157     
158     /* ----------------------------------------------------------------- */
159     /** Write the composite.
160      * The default implementation writes the elements sequentially. May
161      * be overridden for more specialized behaviour.
162      * @param out Writer to write the element to.
163      */

164     public void write(Writer JavaDoc out)
165          throws IOException JavaDoc
166     {
167         for (int i=0; i <elements.size() ; i++)
168         {
169             Object JavaDoc element = elements.get(i);
170           
171             if (element instanceof Element)
172                 ((Element)element).write(out);
173             else if (element==null)
174                 out.write("null");
175             else
176                 out.write(element.toString());
177         }
178     }
179     
180     /* ----------------------------------------------------------------- */
181     /** Contents of the composite.
182      */

183     public String JavaDoc contents()
184     {
185         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
186         synchronized(buf)
187         {
188             for (int i=0; i <elements.size() ; i++)
189             {
190                 Object JavaDoc element = elements.get(i);
191                 if (element==null)
192                     buf.append("null");
193                 else
194                     buf.append(element.toString());
195             }
196         }
197         return buf.toString();
198     }
199
200     /* ------------------------------------------------------------ */
201     /** Empty the contents of this Composite .
202      */

203     public Composite reset()
204     {
205         elements.clear();
206         return unnest();
207     }
208     
209     /* ----------------------------------------------------------------- */
210     /* Flush is a package method used by Page.flush() to locate the
211      * most nested composite, write out and empty its contents.
212      */

213     void flush(Writer JavaDoc out)
214          throws IOException JavaDoc
215     {
216         if (nest!=null)
217             nest.flush(out);
218         else
219         {
220             write(out);
221             elements.clear();
222         }
223     }
224     
225     /* ----------------------------------------------------------------- */
226     /* Flush is a package method used by Page.flush() to locate the
227      * most nested composite, write out and empty its contents.
228      */

229     void flush(OutputStream JavaDoc out)
230          throws IOException JavaDoc
231     {
232         flush(new OutputStreamWriter JavaDoc(out));
233     }
234     
235     /* ----------------------------------------------------------------- */
236     /* Flush is a package method used by Page.flush() to locate the
237      * most nested composite, write out and empty its contents.
238      */

239     void flush(OutputStream JavaDoc out, String JavaDoc encoding)
240          throws IOException JavaDoc
241     {
242         flush(new OutputStreamWriter JavaDoc(out,encoding));
243     }
244
245     /* ------------------------------------------------------------ */
246     /** Replace an object within the composite.
247      */

248     public boolean replace(Object JavaDoc oldObj, Object JavaDoc newObj)
249     {
250         if (nest != null)
251         {
252             return nest.replace(oldObj, newObj);
253         }
254         else
255         {
256             int sz = elements.size();
257             for (int i = 0; i < sz; i++)
258             {
259                 if (elements.get(i) == oldObj)
260                 {
261                     elements.set(i,newObj);
262                     return true;
263                 }
264             }
265         }
266         
267         return false;
268     }
269
270 }
271
Popular Tags