KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > html > Theme


1 /*
2  * Coefficient - facilitates project based collaboration
3  * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
4  * PO Box 395
5  * Pretoria 0001, RSA
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package za.org.coefficient.html;
21
22 import za.org.coefficient.interfaces.ThemeLocalIf;
23 import za.org.coefficient.util.ejb.VelocityScreenUtil;
24
25 import java.util.HashMap JavaDoc;
26 import java.util.Properties JavaDoc;
27
28 /**
29  * DOCUMENT ME!
30  *
31  * @author $author$
32  * @version $Revision: 1.16 $
33  */

34 public abstract class Theme implements ThemeLocalIf {
35     //~ Instance fields ========================================================
36

37     public static final String JavaDoc WEST_POSITION = "WEST";
38     public static final String JavaDoc EAST_POSITION = "EAST";
39     public static final String JavaDoc NORTH_POSITION = "NORTH";
40     public static final String JavaDoc SOUTH_POSITION = "SOUTH";
41     public static final String JavaDoc CENTER_POSITION = "CENTER";
42     public static final String JavaDoc NULL_POSITION = "null";
43
44     public String JavaDoc TABLEWIDTH = new String JavaDoc("100%");
45     public String JavaDoc BGCOLOUR = new String JavaDoc("ffffff");
46     public String JavaDoc CENTERBGCOLOUR = new String JavaDoc("ffffff");
47     public String JavaDoc CENTERPERCENTAGE = "60%";
48     public String JavaDoc EAST = "east.vm";
49     public String JavaDoc EASTBGCOLOUR = new String JavaDoc("ffffff");
50     public String JavaDoc EASTPERCENTAGE = "20%";
51     public String JavaDoc HEADER = "header.vm";
52     public String JavaDoc NORTH = "north.vm";
53     public String JavaDoc NORTHBGCOLOUR = new String JavaDoc("ffffff");
54     public String JavaDoc SOUTH = "south.vm";
55     public String JavaDoc SOUTHBGCOLOUR = new String JavaDoc("ffffff");
56     public String JavaDoc WELCOME = "welcome.vm";
57     public String JavaDoc WEST = "west.vm";
58     public String JavaDoc WESTBGCOLOUR = new String JavaDoc("ffffff");
59     public String JavaDoc WESTPERCENTAGE = "20%";
60     public String JavaDoc CELLPADDING = "1";
61     public String JavaDoc CELLSPACING = "1";
62     public String JavaDoc themeName = null;
63     Properties JavaDoc props = new Properties JavaDoc();
64     VelocityScreenUtil velocityScreenUtil = null;
65
66     protected String JavaDoc pathToThemeResource;
67
68     public void init(String JavaDoc themeName) {
69         this.themeName = themeName;
70         pathToThemeResource = this.getClass().getName();
71         // remove class name
72
int lastPos = pathToThemeResource.lastIndexOf(".");
73         if (lastPos>0) {
74           pathToThemeResource = pathToThemeResource.substring(0, lastPos).replaceAll("\\.","/")+"/resource";
75         } else {
76           pathToThemeResource="resource";
77         }
78         try {
79             VelocityScreenUtil velocityScreenUtil = new VelocityScreenUtil();
80
81             java.io.InputStream JavaDoc propStream = null;
82             propStream =
83                 Theme.class.getResourceAsStream("/"+pathToThemeResource+"/"+themeName+".properties");
84             if (propStream == null) {
85                 System.out.println(themeName + " no properties");
86             }
87
88             if (propStream != null) {
89                 props.load(propStream);
90             }
91
92             java.lang.reflect.Field JavaDoc[] fields = this.getClass()
93                                                    .getFields();
94             int i;
95             for (i = 0; i < fields.length; i++) {
96                 String JavaDoc name = fields[i].getName();
97                 String JavaDoc value = props.getProperty(name);
98                 if (value != null) {
99                     fields[i].set(this, value.trim());
100                 }
101             }
102         } catch (Throwable JavaDoc t) {
103             System.out.println(themeName + "Theme throws " + t);
104         }
105     }
106
107     //~ Methods ================================================================
108

109     public String JavaDoc getTableWidth(Page page) {
110         return TABLEWIDTH;
111     }
112     public String JavaDoc getBackGround(Page page) {
113         return BGCOLOUR;
114     }
115
116     public String JavaDoc getPathToThemeResource() {
117         return this.pathToThemeResource;
118     }
119
120     public String JavaDoc getCenter(Page page) {
121         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
122         //sb.append("<table border=\"0\" cellpadding=\"0\" ");
123
sb.append("<table width=\"100%\" border=\"0\" cellpadding=\"0\" ");
124         sb.append("cellspacing=\"0\" ");
125         sb.append(BGCOLOUR);
126         //sb.append("\" width=\"" + CENTERPERCENTAGE + "\">\n");
127
sb.append(">\n");
128         sb.append("<tr><td>\n");
129         sb.append(page.getCenterContent());
130         sb.append("</table>");
131
132         return sb.toString();
133     }
134
135     public String JavaDoc getCenterBackGround(Page page) {
136         return CENTERBGCOLOUR;
137     }
138
139     public String JavaDoc getCenterPercentage(Page page) {
140         return CENTERPERCENTAGE;
141     }
142
143     public String JavaDoc getEast(Page page) {
144         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
145         HashMap JavaDoc hash = new HashMap JavaDoc();
146         hash.put("content", page.getEastContent());
147     hash.put("width", EASTPERCENTAGE);
148         sb.append(velocityScreenUtil.getProcessedScreenFullyQualified(pathToThemeResource + "/" +EAST, hash));
149
150         return sb.toString();
151     }
152
153     public String JavaDoc getEastBackGround(Page page) {
154         return EASTBGCOLOUR;
155     }
156
157     public String JavaDoc getEastPercentage(Page page) {
158         return EASTPERCENTAGE;
159     }
160
161     public String JavaDoc getFooter(Page page) {
162         return "";
163     }
164
165     public String JavaDoc getHeader(Page page) {
166         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
167         sb.append(velocityScreenUtil.getProcessedScreenFullyQualified(pathToThemeResource + "/" +HEADER,
168                 new HashMap JavaDoc()));
169
170         return sb.toString();
171     }
172
173     public String JavaDoc getName(Page page) {
174         return themeName + "Theme";
175     }
176
177     public String JavaDoc getName() {
178         return getName(null);
179     }
180
181
182     public String JavaDoc getNorth(Page page) {
183         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
184         HashMap JavaDoc hash = new HashMap JavaDoc();
185         hash.put("content", page.getNorthContent());
186         hash.put("project", page.getProject());
187         sb.append(velocityScreenUtil.getProcessedScreenFullyQualified(pathToThemeResource + "/" +NORTH,
188                 hash));
189
190         return sb.toString();
191     }
192
193     public String JavaDoc getNorthBackGround(Page page) {
194         return NORTHBGCOLOUR;
195     }
196
197     public String JavaDoc getPosition(String JavaDoc moduleName) {
198         //System.out.println("theme properties are");
199
//props.list(System.out);
200
String JavaDoc position = props.getProperty(moduleName.trim());
201         if (position == null) {
202             position = "CENTER";
203         }
204
205         return position;
206     }
207
208     public String JavaDoc getSouth(Page page) {
209         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
210         HashMap JavaDoc hash = new HashMap JavaDoc();
211         hash.put("content", page.getSouthContent());
212         sb.append(velocityScreenUtil.getProcessedScreenFullyQualified(pathToThemeResource + "/" +SOUTH,
213                 hash));
214
215         return sb.toString();
216     }
217
218     public String JavaDoc getSouthBackGround(Page page) {
219         return SOUTHBGCOLOUR;
220     }
221
222     public String JavaDoc getWelcomePageContent() {
223         String JavaDoc content = null;
224         HashMap JavaDoc hash = new HashMap JavaDoc();
225         try {
226             content =
227                 velocityScreenUtil.getProcessedScreenFullyQualified(pathToThemeResource + "/" +WELCOME,
228                     hash)
229                                   .toString();
230         } catch (Throwable JavaDoc t) {
231             // if page is not there return empty
232
}
233
234         return content;
235     }
236
237     public String JavaDoc getWest(Page page) {
238         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
239         HashMap JavaDoc hash = new HashMap JavaDoc();
240         hash.put("content", page.getWestContent());
241         hash.put("project", page.getProject());
242         hash.put("width", WESTPERCENTAGE);
243         sb.append(velocityScreenUtil.getProcessedScreenFullyQualified(pathToThemeResource + "/" +WEST, hash));
244
245         return sb.toString();
246     }
247
248     public String JavaDoc getWestBackGround(Page page) {
249         return WESTBGCOLOUR;
250     }
251
252     public String JavaDoc getWestPercentage(Page page) {
253         return WESTPERCENTAGE;
254     }
255
256     public String JavaDoc getCellPadding(Page page) {
257         return CELLPADDING;
258     }
259
260     public String JavaDoc getCellSpacing(Page page) {
261         return CELLSPACING;
262     }
263
264     public void format(Page page, String JavaDoc moduleName, String JavaDoc html) {
265         // if the modulename contains spaces, take the spaces out
266
// Properties do not seem to work right when there are spaces
267
// to the left of the = sign.
268
//
269
// NB: If you have two modules names which only differ with
270
// spaces, there will be problems!
271
//
272
String JavaDoc position = getPosition(moduleName.replaceAll(" ", ""));
273
274         // if the position is specifically set to the word "null"
275
// then the module is not displayed. NB: if the position
276
// is not indicated in the properties file, then the
277
// position defaults to centre
278
//
279
if (position.toLowerCase().equals(NULL_POSITION)) {
280             return;
281         }
282         
283         int idx = position.indexOf(",");
284         Integer JavaDoc index = null;
285         if (idx > 0) {
286             index = Integer.decode(position.substring(idx + 1, position.length()).trim());
287             position = position.substring(0, idx).trim();
288         }
289         if(position.equals(WEST_POSITION)) {
290             page.setWestContent(html, index, moduleName);
291         } else if(position.equals(EAST_POSITION)) {
292             page.setEastContent(html, index, moduleName);
293         } else if(position.equals(NORTH_POSITION)) {
294             page.setNorthContent(html, index, moduleName);
295         } else if(position.equals(SOUTH_POSITION)) {
296             page.setSouthContent(html, index, moduleName);
297         } else if(position.equals(CENTER_POSITION)) {
298             page.setCenterContent(html, index, moduleName);
299         }
300     }
301
302     public String JavaDoc getPositionForModule(String JavaDoc moduleName) {
303         // if the modulename contains spaces, take the spaces out
304
// Properties do not seem to work right when there are spaces
305
// to the left of the = sign.
306
//
307
// NB: If you have two modules names which only differ with
308
// spaces, there will be problems!
309
//
310
String JavaDoc position = getPosition(moduleName.replaceAll(" ", ""));
311
312         // if the position is specifically set to the word "null"
313
// then the module is not displayed. NB: if the position
314
// is not indicated in the properties file, then the
315
// position defaults to centre
316
//
317
if(position.toLowerCase().equals(NULL_POSITION)) {
318             return NULL_POSITION;
319         }
320         
321         int idx = position.indexOf(",");
322         if (idx > 0) {
323             position = position.substring(0, idx).trim();
324         }
325         if(position == null) {
326             position = CENTER_POSITION;
327         }
328         return position;
329     }
330 }
331
Popular Tags