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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.portal;
1819import java.io.File;
20import java.util.StringTokenizer;
2122import org.apache.jetspeed.capability.CapabilityMap;
23import org.apache.turbine.services.servlet.TurbineServlet;
2425/***26 * This default implementation of PortletSkin stores every property27 * as a Map of text properties28 *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 */34publicclassBasePortletSkinextends java.util.HashMap implements PortletSkin35 {
3637public String name = null;
38privateCapabilityMap cm;
39privatestaticfinal String[] VALID_EXTENSIONS = new String[] { "gif", "jpg", "png" };
4041/***42 * Returns the name of this color scheme43 * @return the color scheme name44 */45public String getName()
46 {
47returnthis.name;
48 }
4950/*** Sets the name of this Skin51 */52publicvoid setName(String name)
53 {
54this.name = name;
55 }
5657/***58 * Returns the color to use for displaying the portlet text59 * @return the text color value in HTML format (#RRGGBB)60 */61public String getTextColor()
62 {
63return (String)get(TEXT_COLOR);
64 }
6566/***67 * Sets the color to use for displaying the portlet text68 * @param color the text color value in HTML format (#RRGGBB)69 */70publicvoid setTextColor(String color)
71 {
72if (color!=null)
73 {
74 put(TEXT_COLOR,color);
75 }
76 }
7778/***79 * Returns the color to use for displaying the portlet background80 * @return the text color value in HTML format (#RRGGBB)81 */82public String getBackgroundColor()
83 {
84return (String)get(BACKGROUND_COLOR);
85 }
8687/***88 * Sets the color to use for displaying the portlet background89 * @param backgroundColor the background color value in HTML format (#RRGGBB)90 */91publicvoid setBackgroundColor(String color)
92 {
93if (color!=null)
94 {
95 put(BACKGROUND_COLOR,color);
96 }
97 }
9899100/***101 * Returns the color to use for displaying the portlet title text102 * @return the text color value in HTML format (#RRGGBB)103 */104public String getTitleTextColor()
105 {
106return (String)get(TITLE_TEXT_COLOR);
107 }
108109110/***111 * Sets the color to use for displaying the portlet title text112 * @param titleColor the title color value in HTML format (#RRGGBB)113 */114publicvoid setTitleTextColor(String color)
115 {
116if (color!=null)
117 {
118 put(TITLE_TEXT_COLOR,color);
119 }
120 }
121122/***123 * Returns the color to use for displaying the portlet title background124 * @return the background color value in HTML format (#RRGGBB)125 */126public String getTitleBackgroundColor()
127 {
128return (String)get(TITLE_BACKGROUND_COLOR);
129 }
130131/***132 * Sets the color to use for displaying the portlet title background133 * @param titleColor the title color value in HTML format (#RRGGBB)134 */135publicvoid setTitleBackgroundColor(String color)
136 {
137if (color!=null)
138 {
139 put(TITLE_BACKGROUND_COLOR,color);
140 }
141 }
142143/***144 * Returns the color to use for displaying an highlighted text145 * @return the text color value in HTML format (#RRGGBB)146 */147public String getHighlightTextColor()
148 {
149return (String)get(HIGHLIGHT_TEXT_COLOR);
150 }
151152/***153 * Sets the color to use for displaying an highlighted text154 * @param titleColor a color value in HTML format (#RRGGBB)155 */156publicvoid setHighlightTextColor(String color)
157 {
158if (color!=null)
159 {
160 put(HIGHLIGHT_TEXT_COLOR,color);
161 }
162 }
163164/***165 * Returns the color to use for displaying an highlighted background166 * @return the background color value in HTML format (#RRGGBB)167 */168public String getHighlightBackgroundColor()
169 {
170return (String)get(HIGHLIGHT_BACKGROUND_COLOR);
171 }
172173/***174 * Sets the color to use for displaying an highlighted background175 * @param titleColor the title color value in HTML format (#RRGGBB)176 */177publicvoid setHighlightBackgroundColor(String color)
178 {
179if (color!=null)
180 {
181 put(HIGHLIGHT_BACKGROUND_COLOR,color);
182 }
183 }
184185/***186 * Returns the CSS class to use for the portlet overall187 * @return the CSS class to use (PortletStyleClass)188 */189public String getPortletStyleClass()
190 {
191return (String)get(PORTLET_STYLE_CLASS);
192 }
193194/***195 * Sets the CSS class to use for the portlet overall196 * @param portletStyleClass the new class to be used197 */198publicvoid setPortletStyleClass(String portletStyleClass)
199 {
200if (portletStyleClass!=null)
201 {
202 put(PORTLET_STYLE_CLASS,portletStyleClass);
203 }
204 }
205206/***207 * Returns the CSS class to use for the portlet title208 * @return the CSS class to use (TitleStyleClass)209 */210public String getTitleStyleClass()
211 {
212return (String)get(TITLE_STYLE_CLASS);
213 }
214215/***216 * Sets the CSS class to use for the portlet title217 * @param titleStyleClass the new class to be used218 */219publicvoid setTitleStyleClass(String titleStyleClass)
220 {
221if (titleStyleClass!=null)
222 {
223 put(TITLE_STYLE_CLASS,titleStyleClass);
224 }
225 }
226227/***228 * Returns the CSS class to use for the portlet content229 * @return the CSS class to use (ContentStyleClass)230 */231public String getContentStyleClass()
232 {
233return (String)get(CONTENT_STYLE_CLASS);
234 }
235236/***237 * Sets the CSS class to use for the portlet content238 * @param contentStyleClass the new class to be used239 */240publicvoid setContentStyleClass(String contentStyleClass)
241 {
242if (contentStyleClass!=null)
243 {
244 put(CONTENT_STYLE_CLASS,contentStyleClass);
245 }
246 }
247248/***249 * Returns the CSS class to use overall for the tabbed control250 * @return the CSS class to use (TabStyleClass)251 */252public String getTabStyleClass()
253 {
254return (String)get(TAB_STYLE_CLASS);
255 }
256257/***258 * Sets the CSS class to use for overall for the tabbed control259 * @param tabStyleClass the new class to be used260 */261publicvoid setTabStyleClass(String tabStyleClass)
262 {
263if (tabStyleClass!=null)
264 {
265 put(TAB_STYLE_CLASS,tabStyleClass);
266 }
267 }
268269/***270 * Returns the CSS class to use on the title of the tabbed control271 * @return the CSS class to use (TabTitleStyleClass)272 */273public String getTabTitleStyleClass()
274 {
275return (String)get(TAB_TITLE_STYLE_CLASS);
276 }
277278/***279 * Sets the CSS class to use on the title of the tabbed control280 * @param tabTitleStyleClass the new class to be used281 */282publicvoid setTabTitleStyleClass(String tabTitleStyleClass)
283 {
284if (tabTitleStyleClass!=null)
285 {
286 put(TAB_TITLE_STYLE_CLASS,tabTitleStyleClass);
287 }
288 }
289290/***291 * Returns the CSS class to use on the control of the tabbed control292 * @return the CSS class to use (TabContentStyleClass)293 */294public String getTabContentStyleClass()
295 {
296return (String)get(TAB_CONTENT_STYLE_CLASS);
297 }
298299/***300 * Sets the CSS class to use on the control of the tabbed control301 * @param tabContentStyleClass the new class to be used302 */303publicvoid setTabContentStyleClass(String tabContentStyleClass)
304 {
305if (tabContentStyleClass!=null)
306 {
307 put(TAB_CONTENT_STYLE_CLASS,tabContentStyleClass);
308 }
309 }
310311/***312 * Returns the CSS class to use on the control of the Highlighted title tab or menu item313 * @return the CSS class to use (HighlightTitleStyleClass)314 */315public String getHighlightTitleStyleClass()
316 {
317return (String)get(HIGHLIGHT_TITLE_STYLE_CLASS);
318 }
319320/***321 * Sets the CSS class to use on the control of the Highlighted title tab or menu item322 * @param highlightTitleStyleClass the new class to be used323 */324publicvoid setHighlightTitleStyleClass(String highlightTitleStyleClass)
325 {
326if (highlightTitleStyleClass!=null)
327 {
328 put(HIGHLIGHT_TITLE_STYLE_CLASS, highlightTitleStyleClass);
329 }
330 }
331332/***333 * Returns the CSS class to use for the controller overall334 * @return the CSS class to use (ControllerStyleClass)335 */336public String getControllerStyleClass()
337 {
338return (String)get(CONTROLLER_STYLE_CLASS);
339 }
340341/***342 * Sets the CSS class to use for the controller overall343 * @param controllerStyleClass the new class to be used344 */345publicvoid setControllerStyleClass(String controllerStyleClass)
346 {
347if (controllerStyleClass!=null)
348 {
349 put(CONTROLLER_STYLE_CLASS,controllerStyleClass);
350 }
351 }
352353/***354 * Returns the CSS class to use for the global skin rendering355 * @see org.apache.jetspeed.portal.PortletSkin#getPortletSkinClass()356 */357public String getPortletSkinClass()
358 {
359return (String)get(PORTLET_SKIN_CLASS);
360 }
361362/***363 * Sets the CSS class to use for the global skin rendering364 * @param portletSkinClass the new class to be used365 */366publicvoid setPortletSkinClass(String portletSkinClass)
367 {
368if (portletSkinClass!=null)
369 {
370 put(PORTLET_SKIN_CLASS,portletSkinClass);
371 }
372 }
373374/***375 * @see org.apache.jetspeed.portal.PortletSkin#getImage(String, String)376 */377public String getImage(String name, String dftPath)
378 {
379380if (containsKey("image-" + name))
381 {
382return buildMediaTypeSpecificPath((String) get("image-" + name));
383 }
384385 String path = imageDiscovery(name);
386if (path != null)
387 {
388return path;
389 }
390else391 {
392return dftPath;
393 }
394 }
395396397/***398 * This allows the PortalToolKit to make the PortletSkin aware399 * of the current user-agents's capabilities400 * @param CapabilityMap cm Current capaibilities of the user-agent401 * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>402 */403publicvoid setCapabilityMap(CapabilityMap cm)
404 {
405this.cm = cm;
406 }
407408/***409 * builds a media type specific path for the relative path provided410 */411private String buildMediaTypeSpecificPath(String relativePath)
412 {
413 String path = "images/" + cm.getPreferredMediaType() + "/skins/" + relativePath;
414return path;
415 }
416417/***418 * builds a media type specific path using this skin's name.419 */420private String buildMediaTypeSpecificPath()
421 {
422return buildMediaTypeSpecificPath(name);
423 }
424425private String imageDiscovery(String imageName)
426 {
427 String imagePathes = (String) get("image.paths");
428boolean hasExtension = hasImageExtension(imageName);
429 String fullPath = null;
430if (imagePathes != null)
431 {
432 StringTokenizer tokenizer = new StringTokenizer(imagePathes, ",");
433while (tokenizer.hasMoreTokens())
434 {
435 fullPath =
436 buildValidImage(
437 buildMediaTypeSpecificPath(tokenizer.nextToken()),
438 imageName,
439 hasExtension);
440if (fullPath != null)
441 {
442return fullPath;
443 }
444 }
445 }
446447if (fullPath == null)
448 {
449 String skinBasedPath = buildMediaTypeSpecificPath();
450 fullPath = buildValidImage(skinBasedPath, imageName, hasExtension);
451 }
452return fullPath;
453 }
454455/***456 * Does the path contain a valid image extension?457 */458privateboolean hasImageExtension(String path)
459 {
460return (path.indexOf(".gif") > -1)
461 || (path.indexOf(".jpg") > -1)
462 || (path.indexOf(".png") > -1);
463 }
464465/***466 * makes every attempt to locate a valid image based on the combination467 * of an absoulte path and relative path or name. The relPath may pr may not468 * contain a valid image extension (.gif, .png, .jpg).469 */470private String buildValidImage(String absPath, String relPath, boolean hasExtension)
471 {
472 String path = null;
473474if (hasExtension)
475 {
476 path = absPath + "/" + relPath;
477if (fileExists(path))
478 {
479return path;
480 }
481 }
482else483 {
484for (int i = 0; i < VALID_EXTENSIONS.length; i++)
485 {
486 path = absPath + "/" + relPath + "." + VALID_EXTENSIONS[i];
487if (fileExists(path))
488 {
489return path;
490 }
491 }
492 }
493494returnnull;
495 }
496497/***498 * Does this <code>path</code> exist in the current file system.499 */500privateboolean fileExists(String path)
501 {
502 File testPath = null;
503 testPath = new File(TurbineServlet.getRealPath(path));
504if (testPath.exists())
505 {
506 testPath = null;
507returntrue;
508 }
509else510 {
511 testPath = null;
512return false;
513 }
514 }
515516 }