KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > portal > BasePortletSkin


1 /*
2  * Copyright 2000-2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.jetspeed.portal;
18
19 import java.io.File JavaDoc;
20 import java.util.StringTokenizer JavaDoc;
21
22 import org.apache.jetspeed.capability.CapabilityMap;
23 import org.apache.turbine.services.servlet.TurbineServlet;
24
25 /**
26  * This default implementation of PortletSkin stores every property
27  * as a Map of text properties
28  *
29  * @author <a HREF="mailto:raphael@apache.org">Raphaël Luta</a>
30  * @author <a HREF="mailto:paulsp@apache.org">Paul Spencer</a>
31  * @author <a HREF="mailto:weaver@apache.org">Scott T. Weaver</a>
32  * @version $Id: BasePortletSkin.java,v 1.7 2004/02/23 04:05:35 jford Exp $
33  */

34 public class BasePortletSkin extends java.util.HashMap JavaDoc implements PortletSkin
35 {
36
37     public String JavaDoc name = null;
38     private CapabilityMap cm;
39     private static final String JavaDoc[] VALID_EXTENSIONS = new String JavaDoc[] { "gif", "jpg", "png" };
40
41     /**
42      * Returns the name of this color scheme
43      * @return the color scheme name
44      */

45     public String JavaDoc getName()
46     {
47         return this.name;
48     }
49
50     /** Sets the name of this Skin
51      */

52     public void setName(String JavaDoc name)
53     {
54         this.name = name;
55     }
56
57     /**
58      * Returns the color to use for displaying the portlet text
59      * @return the text color value in HTML format (#RRGGBB)
60      */

61     public String JavaDoc getTextColor()
62     {
63         return (String JavaDoc)get(TEXT_COLOR);
64     }
65
66     /**
67      * Sets the color to use for displaying the portlet text
68      * @param color the text color value in HTML format (#RRGGBB)
69      */

70     public void setTextColor(String JavaDoc color)
71     {
72         if (color!=null)
73         {
74             put(TEXT_COLOR,color);
75         }
76     }
77
78     /**
79      * Returns the color to use for displaying the portlet background
80      * @return the text color value in HTML format (#RRGGBB)
81      */

82     public String JavaDoc getBackgroundColor()
83     {
84         return (String JavaDoc)get(BACKGROUND_COLOR);
85     }
86
87     /**
88      * Sets the color to use for displaying the portlet background
89      * @param backgroundColor the background color value in HTML format (#RRGGBB)
90      */

91     public void setBackgroundColor(String JavaDoc color)
92     {
93         if (color!=null)
94         {
95             put(BACKGROUND_COLOR,color);
96         }
97     }
98
99
100     /**
101      * Returns the color to use for displaying the portlet title text
102      * @return the text color value in HTML format (#RRGGBB)
103      */

104     public String JavaDoc getTitleTextColor()
105     {
106         return (String JavaDoc)get(TITLE_TEXT_COLOR);
107     }
108
109
110     /**
111      * Sets the color to use for displaying the portlet title text
112      * @param titleColor the title color value in HTML format (#RRGGBB)
113      */

114     public void setTitleTextColor(String JavaDoc color)
115     {
116         if (color!=null)
117         {
118             put(TITLE_TEXT_COLOR,color);
119         }
120     }
121
122     /**
123      * Returns the color to use for displaying the portlet title background
124      * @return the background color value in HTML format (#RRGGBB)
125      */

126     public String JavaDoc getTitleBackgroundColor()
127     {
128         return (String JavaDoc)get(TITLE_BACKGROUND_COLOR);
129     }
130
131     /**
132      * Sets the color to use for displaying the portlet title background
133      * @param titleColor the title color value in HTML format (#RRGGBB)
134      */

135     public void setTitleBackgroundColor(String JavaDoc color)
136     {
137         if (color!=null)
138         {
139             put(TITLE_BACKGROUND_COLOR,color);
140         }
141     }
142
143     /**
144      * Returns the color to use for displaying an highlighted text
145      * @return the text color value in HTML format (#RRGGBB)
146      */

147     public String JavaDoc getHighlightTextColor()
148     {
149         return (String JavaDoc)get(HIGHLIGHT_TEXT_COLOR);
150     }
151
152     /**
153      * Sets the color to use for displaying an highlighted text
154      * @param titleColor a color value in HTML format (#RRGGBB)
155      */

156     public void setHighlightTextColor(String JavaDoc color)
157     {
158         if (color!=null)
159         {
160             put(HIGHLIGHT_TEXT_COLOR,color);
161         }
162     }
163
164     /**
165      * Returns the color to use for displaying an highlighted background
166      * @return the background color value in HTML format (#RRGGBB)
167      */

168     public String JavaDoc getHighlightBackgroundColor()
169     {
170         return (String JavaDoc)get(HIGHLIGHT_BACKGROUND_COLOR);
171     }
172
173     /**
174      * Sets the color to use for displaying an highlighted background
175      * @param titleColor the title color value in HTML format (#RRGGBB)
176      */

177     public void setHighlightBackgroundColor(String JavaDoc color)
178     {
179         if (color!=null)
180         {
181             put(HIGHLIGHT_BACKGROUND_COLOR,color);
182         }
183     }
184
185     /**
186      * Returns the CSS class to use for the portlet overall
187      * @return the CSS class to use (PortletStyleClass)
188      */

189     public String JavaDoc getPortletStyleClass()
190     {
191         return (String JavaDoc)get(PORTLET_STYLE_CLASS);
192     }
193
194     /**
195      * Sets the CSS class to use for the portlet overall
196      * @param portletStyleClass the new class to be used
197      */

198     public void setPortletStyleClass(String JavaDoc portletStyleClass)
199     {
200         if (portletStyleClass!=null)
201         {
202             put(PORTLET_STYLE_CLASS,portletStyleClass);
203         }
204     }
205
206     /**
207      * Returns the CSS class to use for the portlet title
208      * @return the CSS class to use (TitleStyleClass)
209      */

210     public String JavaDoc getTitleStyleClass()
211     {
212         return (String JavaDoc)get(TITLE_STYLE_CLASS);
213     }
214
215     /**
216      * Sets the CSS class to use for the portlet title
217      * @param titleStyleClass the new class to be used
218      */

219     public void setTitleStyleClass(String JavaDoc titleStyleClass)
220     {
221         if (titleStyleClass!=null)
222         {
223             put(TITLE_STYLE_CLASS,titleStyleClass);
224         }
225     }
226
227     /**
228      * Returns the CSS class to use for the portlet content
229      * @return the CSS class to use (ContentStyleClass)
230      */

231     public String JavaDoc getContentStyleClass()
232     {
233         return (String JavaDoc)get(CONTENT_STYLE_CLASS);
234     }
235
236     /**
237      * Sets the CSS class to use for the portlet content
238      * @param contentStyleClass the new class to be used
239      */

240     public void setContentStyleClass(String JavaDoc contentStyleClass)
241     {
242         if (contentStyleClass!=null)
243         {
244             put(CONTENT_STYLE_CLASS,contentStyleClass);
245         }
246     }
247
248     /**
249      * Returns the CSS class to use overall for the tabbed control
250      * @return the CSS class to use (TabStyleClass)
251      */

252     public String JavaDoc getTabStyleClass()
253     {
254         return (String JavaDoc)get(TAB_STYLE_CLASS);
255     }
256
257     /**
258      * Sets the CSS class to use for overall for the tabbed control
259      * @param tabStyleClass the new class to be used
260      */

261     public void setTabStyleClass(String JavaDoc tabStyleClass)
262     {
263         if (tabStyleClass!=null)
264         {
265             put(TAB_STYLE_CLASS,tabStyleClass);
266         }
267     }
268
269     /**
270      * Returns the CSS class to use on the title of the tabbed control
271      * @return the CSS class to use (TabTitleStyleClass)
272      */

273     public String JavaDoc getTabTitleStyleClass()
274     {
275         return (String JavaDoc)get(TAB_TITLE_STYLE_CLASS);
276     }
277
278     /**
279      * Sets the CSS class to use on the title of the tabbed control
280      * @param tabTitleStyleClass the new class to be used
281      */

282     public void setTabTitleStyleClass(String JavaDoc tabTitleStyleClass)
283     {
284         if (tabTitleStyleClass!=null)
285         {
286             put(TAB_TITLE_STYLE_CLASS,tabTitleStyleClass);
287         }
288     }
289
290     /**
291      * Returns the CSS class to use on the control of the tabbed control
292      * @return the CSS class to use (TabContentStyleClass)
293      */

294     public String JavaDoc getTabContentStyleClass()
295     {
296         return (String JavaDoc)get(TAB_CONTENT_STYLE_CLASS);
297     }
298
299     /**
300      * Sets the CSS class to use on the control of the tabbed control
301      * @param tabContentStyleClass the new class to be used
302      */

303     public void setTabContentStyleClass(String JavaDoc tabContentStyleClass)
304     {
305         if (tabContentStyleClass!=null)
306         {
307             put(TAB_CONTENT_STYLE_CLASS,tabContentStyleClass);
308         }
309     }
310
311     /**
312      * Returns the CSS class to use on the control of the Highlighted title tab or menu item
313      * @return the CSS class to use (HighlightTitleStyleClass)
314      */

315     public String JavaDoc getHighlightTitleStyleClass()
316     {
317         return (String JavaDoc)get(HIGHLIGHT_TITLE_STYLE_CLASS);
318     }
319
320     /**
321      * Sets the CSS class to use on the control of the Highlighted title tab or menu item
322      * @param highlightTitleStyleClass the new class to be used
323      */

324     public void setHighlightTitleStyleClass(String JavaDoc highlightTitleStyleClass)
325     {
326         if (highlightTitleStyleClass!=null)
327         {
328             put(HIGHLIGHT_TITLE_STYLE_CLASS, highlightTitleStyleClass);
329         }
330     }
331
332     /**
333      * Returns the CSS class to use for the controller overall
334      * @return the CSS class to use (ControllerStyleClass)
335      */

336     public String JavaDoc getControllerStyleClass()
337     {
338         return (String JavaDoc)get(CONTROLLER_STYLE_CLASS);
339     }
340
341     /**
342      * Sets the CSS class to use for the controller overall
343      * @param controllerStyleClass the new class to be used
344      */

345     public void setControllerStyleClass(String JavaDoc controllerStyleClass)
346     {
347         if (controllerStyleClass!=null)
348         {
349             put(CONTROLLER_STYLE_CLASS,controllerStyleClass);
350         }
351     }
352
353     /**
354      * Returns the CSS class to use for the global skin rendering
355      * @see org.apache.jetspeed.portal.PortletSkin#getPortletSkinClass()
356      */

357     public String JavaDoc getPortletSkinClass()
358     {
359         return (String JavaDoc)get(PORTLET_SKIN_CLASS);
360     }
361
362     /**
363      * Sets the CSS class to use for the global skin rendering
364      * @param portletSkinClass the new class to be used
365      */

366     public void setPortletSkinClass(String JavaDoc portletSkinClass)
367     {
368         if (portletSkinClass!=null)
369         {
370             put(PORTLET_SKIN_CLASS,portletSkinClass);
371         }
372     }
373
374     /**
375      * @see org.apache.jetspeed.portal.PortletSkin#getImage(String, String)
376      */

377     public String JavaDoc getImage(String JavaDoc name, String JavaDoc dftPath)
378     {
379
380         if (containsKey("image-" + name))
381         {
382             return buildMediaTypeSpecificPath((String JavaDoc) get("image-" + name));
383         }
384
385         String JavaDoc path = imageDiscovery(name);
386         if (path != null)
387         {
388             return path;
389         }
390         else
391         {
392             return dftPath;
393         }
394     }
395
396
397     /**
398     * This allows the PortalToolKit to make the PortletSkin aware
399     * of the current user-agents's capabilities
400     * @param CapabilityMap cm Current capaibilities of the user-agent
401     * @author <a HREF="mailto:weaver@apache.org">Scott T. Weaver</a>
402     */

403     public void setCapabilityMap(CapabilityMap cm)
404     {
405         this.cm = cm;
406     }
407
408     /**
409      * builds a media type specific path for the relative path provided
410      */

411     private String JavaDoc buildMediaTypeSpecificPath(String JavaDoc relativePath)
412     {
413         String JavaDoc path = "images/" + cm.getPreferredMediaType() + "/skins/" + relativePath;
414         return path;
415     }
416
417     /**
418      * builds a media type specific path using this skin's name.
419      */

420     private String JavaDoc buildMediaTypeSpecificPath()
421     {
422         return buildMediaTypeSpecificPath(name);
423     }
424
425     private String JavaDoc imageDiscovery(String JavaDoc imageName)
426     {
427         String JavaDoc imagePathes = (String JavaDoc) get("image.paths");
428         boolean hasExtension = hasImageExtension(imageName);
429         String JavaDoc fullPath = null;
430         if (imagePathes != null)
431         {
432             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(imagePathes, ",");
433             while (tokenizer.hasMoreTokens())
434             {
435                 fullPath =
436                     buildValidImage(
437                         buildMediaTypeSpecificPath(tokenizer.nextToken()),
438                         imageName,
439                         hasExtension);
440                 if (fullPath != null)
441                 {
442                     return fullPath;
443                 }
444             }
445         }
446
447         if (fullPath == null)
448         {
449             String JavaDoc skinBasedPath = buildMediaTypeSpecificPath();
450             fullPath = buildValidImage(skinBasedPath, imageName, hasExtension);
451         }
452         return fullPath;
453     }
454
455     /**
456      * Does the path contain a valid image extension?
457      */

458     private boolean hasImageExtension(String JavaDoc path)
459     {
460         return (path.indexOf(".gif") > -1)
461             || (path.indexOf(".jpg") > -1)
462             || (path.indexOf(".png") > -1);
463     }
464
465     /**
466      * makes every attempt to locate a valid image based on the combination
467      * of an absoulte path and relative path or name. The relPath may pr may not
468      * contain a valid image extension (.gif, .png, .jpg).
469      */

470     private String JavaDoc buildValidImage(String JavaDoc absPath, String JavaDoc relPath, boolean hasExtension)
471     {
472         String JavaDoc path = null;
473
474         if (hasExtension)
475         {
476             path = absPath + "/" + relPath;
477             if (fileExists(path))
478             {
479                 return path;
480             }
481         }
482         else
483         {
484             for (int i = 0; i < VALID_EXTENSIONS.length; i++)
485             {
486                 path = absPath + "/" + relPath + "." + VALID_EXTENSIONS[i];
487                 if (fileExists(path))
488                 {
489                     return path;
490                 }
491             }
492         }
493
494         return null;
495     }
496
497     /**
498      * Does this <code>path</code> exist in the current file system.
499      */

500     private boolean fileExists(String JavaDoc path)
501     {
502         File JavaDoc testPath = null;
503         testPath = new File JavaDoc(TurbineServlet.getRealPath(path));
504         if (testPath.exists())
505         {
506             testPath = null;
507             return true;
508         }
509         else
510         {
511             testPath = null;
512             return false;
513         }
514     }
515
516 }
517
Popular Tags