KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > STemplateLayout


1 /*
2  * $Id: STemplateLayout.java,v 1.4 2004/12/01 07:54:07 hengels Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings;
15
16 import org.wings.template.*;
17 import org.wings.template.parser.PageParser;
18
19 import java.io.File JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24
25 /*
26  * STemplateLayout.java
27  *
28  * Diese Layout ist dazu gedacht HTML Componenten in ein
29  * vordefiniertes HTML Layout (Template) einzusetzten. In dem Template
30  * sollten Schluesselwoerter (Template-Tag:
31  * <COMPONENT NAME=constraint ICON="image.gif"></COMPONENT> )
32  * gesetzt sein, die mit der Componente
33  * ersetzt werden, die mit dem Schluesselwort als Constraint
34  * zum Layouter hinzugefuegt wurden. <br>
35  * Beispiel: template<br>
36  * <CODE>
37  * &lt;HTML&gt;&lt;BODY&gt;
38  * Name der Person: &lt;COMPONENT NAME=NAME&gt;&lt;BR&gt;
39  * Vorname der Person: &lt;COMPONENT NAME=VORNAME ICON="vorname.gif"&gt;&lt;BR&gt;
40  * Wohnort der Person: &lt;COMPONENT NAME=WOHNORT&gt;&lt;BR&gt;
41  * &lt;/BODY&gt;&lt;/HTML&gt;
42  * </CODE>
43  * <BR>
44  * Fuellen dieses Templates schaut dann so aus:<BR>
45  * <CODE>
46  * templateContainer.addComponent(new SLabel("Haaf"), "NAME");
47  * templateContainer.addComponent(new SButton("Armin"), "VORNAME");
48  * templateContainer.addComponent(new SLabel("Neu-Ulm"), "WOHNORT");
49  * </CODE>
50  *
51  * @author Armin Haaf
52  * @author Henner Zeller
53  */

54
55 /**
56  * @author <a HREF="mailto:haaf@mercatis.de">Armin Haaf</a>
57  * @author Jochen Woehrle
58  * @author <a HREF="mailto:H.Zeller@acm.org">Henner Zeller</a>
59  * @version $Revision: 1.4 $
60  */

61 public class STemplateLayout
62         extends SAbstractLayoutManager {
63     /*
64      * Dieser PropertyManager behandelt alle Properties, zu denen kein eigener
65      * PropertyManager gefunden wurde.
66      */

67     private static final PropertyManager defaultPropertyManager =
68             new PropertyManager() {
69                 final Class JavaDoc[] empty = new Class JavaDoc[0];
70
71                 public void setProperty(SComponent c, String JavaDoc name,
72                                         String JavaDoc value) {
73                 }
74
75                 public Class JavaDoc[] getSupportedClasses() {
76                     return empty;
77                 }
78             };
79
80     /*
81      * Alle Property Manager
82      */

83     private static final HashMap JavaDoc propertyManager = new HashMap JavaDoc();
84
85     /*
86      * some default property Managers. Sets properties of components
87      * from parameters given in the template page.
88      */

89     static {
90         addPropertyManager(new SComponentPropertyManager());
91         addPropertyManager(new SAbstractIconTextCompoundPropertyManager());
92         addPropertyManager(new SAbstractButtonPropertyManager());
93         addPropertyManager(new SLabelPropertyManager());
94         addPropertyManager(new STextFieldPropertyManager());
95         addPropertyManager(new STextAreaPropertyManager());
96         addPropertyManager(new STablePropertyManager());
97         addPropertyManager(new SFileChooserPropertyManager());
98         addPropertyManager(new SListPropertyManager());
99     }
100
101     /**
102      * Abstraction of the template source (file, resource, ..)
103      */

104     protected HashMap JavaDoc components = new HashMap JavaDoc();
105     private TemplateSource templateSource = null;
106
107     /**
108      * PageParser to use
109      */

110     protected PageParser pageParser = PageParser.getInstance();
111
112
113     public STemplateLayout() {}
114
115     /**
116      * Create a TemplateLayout that reads its content from the generic
117      * {@link TemplateSource}. The template source can be implemented
118      * to be read from any source you want to, e.g. database BLOBs. Whenever
119      * the source changes (i.e. lastModified() returns a different
120      * modification time the last time this source has been parsed), the
121      * template is reparsed.
122      *
123      * @param source the TemplateSource this template is to be read from.
124      */

125     public STemplateLayout(TemplateSource source) {
126         setTemplate(source);
127     }
128
129     /**
130      * Open a template from a file with the given name.
131      * Whenever the file changes, the Template is reloaded.
132      *
133      * @param tmplFileName the filename to read the file from.
134      * @throws java.io.IOException
135      */

136     public STemplateLayout(String JavaDoc tmplFileName) throws java.io.IOException JavaDoc {
137         setTemplate(new File JavaDoc(tmplFileName));
138     }
139
140     /**
141      * Read the template from a file.
142      * Open a template from a file.
143      * Whenever the file changes, the Template is reloaded.
144      *
145      * @param tmplFile the File to read the template from.
146      * @throws java.io.IOException
147      */

148     public STemplateLayout(File JavaDoc tmplFile) throws java.io.IOException JavaDoc {
149         setTemplate(tmplFile);
150     }
151
152     /**
153      * Read the template from an URL.
154      * The content is cached.
155      *
156      * @param url the URL to read the template from.
157      * @throws java.io.IOException
158      */

159     public STemplateLayout(URL JavaDoc url) throws java.io.IOException JavaDoc {
160         setTemplate(url);
161     }
162
163     /**
164      * Determines appropriate property manager for the given SComponent or
165      * derived class. Goes up the hierarchy.
166      */

167     public static final PropertyManager getPropertyManager(Class JavaDoc c) {
168         if (c == null)
169             return defaultPropertyManager;
170
171         PropertyManager p = (PropertyManager) propertyManager.get(c);
172
173         if (p == null)
174             return getPropertyManager(c.getSuperclass());
175
176         return p;
177     }
178
179     /**
180      * Adds a PropertyManager.
181      * A Property Manager provides a mapping from properties given in
182      * template tags to properties of components.
183      * PropertyManager are responsible for a number of component classes; if
184      * there is already a mapping for a certain class, then this mapping
185      * is <em>not</em> added.
186      */

187     public static final void addPropertyManager(PropertyManager p) {
188         if (p == null)
189             return;
190
191         Class JavaDoc[] cl = p.getSupportedClasses();
192         if (cl == null)
193             return;
194
195         for (int i = 0; i < cl.length; i++) {
196             if (!propertyManager.containsKey(cl[i]))
197                 propertyManager.put(cl[i], p);
198         }
199     }
200
201     /**
202      * Set the template to the template given as file name.
203      *
204      * @throws java.io.IOException
205      */

206     public void setTemplate(String JavaDoc templateFileName)
207             throws java.io.IOException JavaDoc {
208         setTemplate(new File JavaDoc(templateFileName));
209     }
210
211     /**
212      * Set the template to the template stored in the given file.
213      *
214      * @throws java.io.IOException
215      */

216     public void setTemplate(File JavaDoc templateFile) throws java.io.IOException JavaDoc {
217         setTemplate(new CachedFileTemplateSource(templateFile));
218     }
219
220     /**
221      * Set the template to the template which can be retrieved from the
222      * given URL.
223      *
224      * @throws java.io.IOException
225      */

226     public void setTemplate(URL JavaDoc templateURL) throws java.io.IOException JavaDoc {
227         if ("file".equals(templateURL.getProtocol())) {
228             setTemplate(new File JavaDoc(templateURL.getFile()));
229         } else {
230             setTemplate(new CachedFileTemplateSource(templateURL));
231         }
232     }
233
234     /**
235      * Sets the template from the DataSource. Use this, if you hold your
236      * templates in databases etc. and write your own DataSource.
237      *
238      * @param source the source this template is to be read.
239      * @see org.wings.template.TemplateSource
240      */

241     public void setTemplate(TemplateSource source) {
242         templateSource = source;
243     }
244
245     /**
246      * add a component with the given constraint. The contstraint in the
247      * TemplateLayout is the value of the name attribute of the object in
248      * the HTML-template.
249      *
250      * @param c the component to be added
251      * @param constraint the string describing the
252      */

253     public void addComponent(SComponent c, Object JavaDoc constraint, int index) {
254         if (constraint == null)
255             throw new IllegalArgumentException JavaDoc("null constraint not allowed here");
256         components.put(constraint.toString(), c);
257     }
258
259     /**
260      * removes the given component.
261      *
262      * @param comp the component to be removed.
263      */

264     public void removeComponent(SComponent comp) {
265         Iterator JavaDoc it = components.entrySet().iterator();
266         while (it.hasNext()) {
267             Map.Entry JavaDoc e = (Map.Entry JavaDoc) it.next();
268             if (e.getValue() == comp)
269                 it.remove();
270         }
271     }
272
273     /**
274      * returns a map of the constraint/component.
275      */

276     public SComponent getComponent(String JavaDoc name) {
277         return (SComponent) components.get(name);
278     }
279
280     public TemplateSource getTemplateSource() {
281         return templateSource;
282     }
283
284     /**
285      * Retrieve the PageParser of this instance
286      *
287      * @return the current PageParser
288      */

289     public PageParser getPageParser() {
290         return pageParser;
291     }
292
293     /**
294      * Set the PageParser for this instance
295      *
296      * @param pageParser the new PageParser
297      */

298     public void setPageParser(PageParser pageParser) {
299         this.pageParser = pageParser;
300     }
301 }
302
303
304
Popular Tags