001 /*
002 * Apache License
003 * Version 2.0, January 2004
004 * http://www.apache.org/licenses/
005 *
006 * Copyright 2008-2010 by chenillekit.org
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 */
014
015 package org.chenillekit.tapestry.core.components;
016
017 import org.apache.tapestry5.BindingConstants;
018 import org.apache.tapestry5.ComponentResources;
019 import org.apache.tapestry5.MarkupWriter;
020 import org.apache.tapestry5.annotations.AfterRender;
021 import org.apache.tapestry5.annotations.BeginRender;
022 import org.apache.tapestry5.annotations.Environmental;
023 import org.apache.tapestry5.annotations.Import;
024 import org.apache.tapestry5.annotations.Parameter;
025 import org.apache.tapestry5.ioc.annotations.Inject;
026 import org.apache.tapestry5.services.Environment;
027 import org.apache.tapestry5.services.javascript.JavaScriptSupport;
028
029 /**
030 * shows an tooltip if mouse slides over the declared content.
031 *
032 * @version $Id: Tooltip.java 674 2010-07-29 12:47:25Z homburgs $
033 */
034 @Import(library = {"../Chenillekit.js", "Tooltip.js"}, stylesheet = {"Tooltip.css"})
035 public class Tooltip
036 {
037 /**
038 * The id used to generate a page-unique client-side identifier for the component. If a component renders multiple
039 * times, a suffix will be appended to the to id to ensure uniqueness.
040 */
041 @Parameter(value = "prop:componentResources.id", defaultPrefix = BindingConstants.LITERAL)
042 private String clientId;
043
044 /**
045 * the tooltip title.
046 */
047 @Parameter(value = "", required = false, defaultPrefix = "literal")
048 private String title;
049
050 /**
051 * the tooltip content.
052 */
053 @Parameter(value = "", required = false, defaultPrefix = "literal")
054 private String value;
055
056 /**
057 * the tooltip effect ("blind", "appear", "slide").
058 */
059 @Parameter(required = false, defaultPrefix = "literal")
060 private String effect;
061
062 @Inject
063 private ComponentResources resources;
064
065 @Environmental
066 private JavaScriptSupport javascriptSupport;
067
068 @Inject
069 private Environment environment;
070
071 private String assignedClientId;
072
073 void setupRender()
074 {
075 assignedClientId = javascriptSupport.allocateClientId(clientId);
076 }
077
078 @BeginRender
079 void beginRender(MarkupWriter writer)
080 {
081 writer.element("span",
082 "id", assignedClientId);
083 }
084
085 @AfterRender
086 void afterRender(MarkupWriter writer)
087 {
088 writer.end();
089
090 String jsCommand = "new Ck.Tip('%s', '%s'";
091 jsCommand += ", {className: 'ck_tooltip'";
092
093 if (title != null)
094 jsCommand += ", title: '" + replaceJSChar(title) + "'";
095
096 if (effect != null)
097 jsCommand += ", effect: '" + effect + "'";
098
099 jsCommand += "});";
100 javascriptSupport.addScript(jsCommand, assignedClientId, replaceJSChar(value));
101 }
102
103 /**
104 * replace the ' char with the " char and '%' with '%%'.
105 */
106 private String replaceJSChar(String value)
107 {
108 if (value == null)
109 return "";
110
111 return value.replaceAll("'", "\"").replaceAll("%", "%%");
112 }
113 }