View Javadoc

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;
20  import java.util.StringTokenizer;
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 implements PortletSkin
35  {
36  
37      public String name = null;
38      private CapabilityMap cm;
39      private static final String[] VALID_EXTENSIONS = new String[] { "gif", "jpg", "png" };
40  
41      /***
42       * Returns the name of this color scheme
43       * @return the color scheme name
44       */
45      public String getName()
46      {
47          return this.name;
48      }
49  
50      /*** Sets the name of this Skin
51       */
52      public void setName(String 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 getTextColor()
62      {
63          return (String)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 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 getBackgroundColor()
83      {
84          return (String)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 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 getTitleTextColor()
105     {
106         return (String)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 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 getTitleBackgroundColor()
127     {
128         return (String)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 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 getHighlightTextColor()
148     {
149         return (String)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 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 getHighlightBackgroundColor()
169     {
170         return (String)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 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 getPortletStyleClass()
190     {
191         return (String)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 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 getTitleStyleClass()
211     {
212         return (String)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 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 getContentStyleClass()
232     {
233         return (String)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 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 getTabStyleClass()
253     {
254         return (String)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 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 getTabTitleStyleClass()
274     {
275         return (String)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 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 getTabContentStyleClass()
295     {
296         return (String)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 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 getHighlightTitleStyleClass()
316     {
317         return (String)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 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 getControllerStyleClass()
337     {
338         return (String)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 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 getPortletSkinClass()
358     {
359         return (String)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 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 getImage(String name, String dftPath)
378     {
379 
380         if (containsKey("image-" + name))
381         {
382             return buildMediaTypeSpecificPath((String) get("image-" + name));
383         }
384 
385         String 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 buildMediaTypeSpecificPath(String relativePath)
412     {
413         String 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 buildMediaTypeSpecificPath()
421     {
422         return buildMediaTypeSpecificPath(name);
423     }
424 
425     private  String imageDiscovery(String imageName)
426     {
427         String imagePathes = (String) get("image.paths");
428         boolean hasExtension = hasImageExtension(imageName);
429         String fullPath = null;
430         if (imagePathes != null)
431         {
432             StringTokenizer tokenizer = new StringTokenizer(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 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 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 buildValidImage(String absPath, String relPath, boolean hasExtension)
471     {
472         String 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 path)
501     {
502         File testPath = null;
503         testPath = new File(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 }