All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.javascript.TooltipJS Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.javascript;

import java.util.Map;
import java.util.HashMap;

import com.hfg.html.HTML;
import com.hfg.util.StringUtil;
import com.hfg.xml.XMLNode;
import com.hfg.svg.SvgNode;

//------------------------------------------------------------------------------
/**
  Generic tooltip generation javascript.
  Tooltips can be added to any tag which supports onmouseover and onmouseout.
  You will also need to add generateJS() to the javascript in the page's <head> tag.
  

See this page for tooltip examples.

Simple code example:
       TooltipJS tooltip = new TooltipJS();

       HTMLDoc htmlDoc = new HTMLDoc();

       // Keep IE from going into quirks mode.
       htmlDoc.setDoctype(Doctype.HTML_4_01_TRANSITIONAL_NO_URL);

       HTML html = new HTML();
       htmlDoc.setRootTag(html);
       html.getHead().addSubtag(Meta.CONTENT_TYPE_ISO_8859_1);

       // Add the necessary javascript to the page.
       html.getHead().addJavascript(tooltip.generateJS());

       Body body = html.getBody();
       body.br(2);

       // Create the target tag (in this case a span) the tooltip will function on.
       Span testSpan = body.addSpan("Test span");

       // Create the tooltip content
       Pre tooltip1Content = new Pre("foobar and all that stuff\n");
       tooltip1Content.addSpan("just grinds my axe!\n").setStyle(CSS.BOLD + CSS.fontSize(24));

       // Attach the tooltip to the target.
       tooltip.addTooltip(testSpan, tooltip1Content.toHTML());

  
@author J. Alex Taylor, hairyfatguy.com */ //------------------------------------------------------------------------------ // com.hfg XML/HTML Coding Library // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com // [email protected] //------------------------------------------------------------------------------ public class TooltipJS { //########################################################################## // PRIVATE FIELDS //########################################################################## private int mDelay = 500; private int mXOffset = 10; private int mYOffset = 20; private int mMaxWidth = 300; private String mBorder = "1px solid #666666"; private String mFont = "arial,sans-serif"; private String mFontSize = "11px"; private String mFontColor = "#006600"; private String mBgColor = "#eeffee"; private int mPadding = 3; private String mShadowColor = "#000000"; private int mShadowOffset = 3; private boolean mAddOnMouseMove = false; private String mCachedJs; //########################################################################## // CONSTRUCTORS //########################################################################## //-------------------------------------------------------------------------- public TooltipJS() { } //########################################################################## // PUBLIC METHODS //########################################################################## //-------------------------------------------------------------------------- public TooltipJS setDelay(int inValue) { if (inValue != mDelay) { mDelay = inValue; mCachedJs = null; // Invalidate the cache. } return this; } //-------------------------------------------------------------------------- public TooltipJS setXOffset(int inValue) { if (inValue != mXOffset) { mXOffset = inValue; mCachedJs = null; // Invalidate the cache. } return this; } //-------------------------------------------------------------------------- public TooltipJS setYOffset(int inValue) { if (inValue != mYOffset) { mYOffset = inValue; mCachedJs = null; // Invalidate the cache. } return this; } //-------------------------------------------------------------------------- public TooltipJS setBorder(String inValue) { if (null == inValue) { throw new RuntimeException("The border cannot be set to null!"); } if (! inValue.equals(mBorder)) { mBorder = inValue; mCachedJs = null; // Invalidate the cache. } return this; } //-------------------------------------------------------------------------- public TooltipJS setFont(String inValue) { if (null == inValue) { throw new RuntimeException("The font cannot be set to null!"); } if (! inValue.equals(mFont)) { mFont = inValue; mCachedJs = null; // Invalidate the cache. } return this; } //-------------------------------------------------------------------------- public TooltipJS setFontSize(String inValue) { if (null == inValue) { throw new RuntimeException("The font size cannot be set to null!"); } if (! inValue.equals(mFontSize)) { mFontSize = inValue; mCachedJs = null; // Invalidate the cache. } return this; } //-------------------------------------------------------------------------- public TooltipJS setFontColor(String inValue) { if (null == inValue) { throw new RuntimeException("The font color cannot be set to null!"); } if (! inValue.equals(mFontColor)) { mFontColor = inValue; mCachedJs = null; // Invalidate the cache. } return this; } //-------------------------------------------------------------------------- public TooltipJS setBackgroundColor(String inValue) { if (null == inValue) { throw new RuntimeException("The background color cannot be set to null!"); } if (! inValue.equals(mBgColor)) { mBgColor = inValue; mCachedJs = null; // Invalidate the cache. } return this; } //-------------------------------------------------------------------------- public TooltipJS setPadding(int inValue) { if (inValue != mPadding) { mPadding = inValue; mCachedJs = null; // Invalidate the cache. } return this; } //-------------------------------------------------------------------------- public TooltipJS setShadowColor(String inValue) { if (null == inValue) { throw new RuntimeException("The shadow color cannot be set to null!"); } if (! inValue.equals(mShadowColor)) { mShadowColor = inValue; mCachedJs = null; // Invalidate the cache. } return this; } //-------------------------------------------------------------------------- /** Sets the suggested maximum width for tooltips. If content cannot be squeezed into this width, such as is sometimes the case with Pre's, tables, etc., the the tooltip will grow. It will always enclose all of the content. * @param inValue */ public TooltipJS setMaxWidth(int inValue) { if (inValue != mMaxWidth) { mMaxWidth = inValue; mCachedJs = null; // Invalidate the cache. } return this; } //-------------------------------------------------------------------------- public TooltipJS setShadowOffset(int inValue) { if (inValue != mShadowOffset) { mShadowOffset = inValue; mCachedJs = null; // Invalidate the cache. } return this; } //-------------------------------------------------------------------------- /** Move the tooltip via onmousemove(). */ // Changing the value does not affect the cached js. public TooltipJS setMouseMove(boolean inValue) { mAddOnMouseMove = inValue; return this; } //-------------------------------------------------------------------------- /** * Tooltips can be added to any tag which supports onmouseover and onmouseout. * You will also need to add generateJS() to the javascript in the page's <head> tag. * @param inTag the tag to which the onmouseover and onmouseout attributes will * be set to display a popup tooltip. * @param inValue the text or HTML to display in the tooltip. */ public void addTooltip(XMLNode inTag, String inValue) { // String safeString = StringUtil.replaceAll(inValue, "'", "\\'"); // String safeString = XMLUtil.escapeAttributeValue(inValue); String safeString = StringUtil.replaceAll(inValue, "\n", "\\n"); safeString = StringUtil.replaceAll(safeString, "\r", "\\r"); safeString = StringUtil.replaceAll(safeString, "'", "'"); safeString = StringUtil.replaceAll(safeString, "\"", """); // Firefox (as of 3.0.*) still only recognized 'evt' and not 'event' // Safari seems to recognize both. String eventName = (inTag instanceof SvgNode ? "evt" : "event"); String currentValue = inTag.getAttributeValue(HTML.ONMOUSEOVER); inTag.setAttribute(HTML.ONMOUSEOVER, (currentValue != null ? currentValue + ";" : "") + "showTooltip(" + eventName + ", '" + safeString + "');"); if (mAddOnMouseMove) { currentValue = inTag.getAttributeValue(HTML.ONMOUSEMOVE); inTag.setAttribute(HTML.ONMOUSEMOVE, (currentValue != null ? currentValue + ";" : "") + "moveTooltip(" + eventName + ");"); } currentValue = inTag.getAttributeValue(HTML.ONMOUSEOUT); inTag.setAttribute(HTML.ONMOUSEOUT, (currentValue != null ? currentValue + ";" : "") + "hideTooltip();"); } //-------------------------------------------------------------------------- /** * Creates a tooltip using the tag's 'title' attribute as the tooltip text. * @param inTag the tag to which the onmouseover and onmouseout attributes will * be added in order to display the popup tooltip. */ public void addTitleTooltip(XMLNode inTag) { String currentValue = inTag.getAttributeValue(HTML.ONMOUSEOVER); inTag.setAttribute(HTML.ONMOUSEOVER, (currentValue != null ? currentValue + ";" : "") + "showTitleTooltip(event, this)"); if (mAddOnMouseMove) { currentValue = inTag.getAttributeValue(HTML.ONMOUSEMOVE); inTag.setAttribute(HTML.ONMOUSEMOVE, (currentValue != null ? currentValue + ";" : "") + "moveTooltip(event)"); } currentValue = inTag.getAttributeValue(HTML.ONMOUSEOUT); inTag.setAttribute(HTML.ONMOUSEOUT, (currentValue != null ? currentValue + ";" : "") + "hideTitleTooltip()"); } //-------------------------------------------------------------------------- public String generateJS() { if (null == mCachedJs) { Map tokenSubstitutionMap = new HashMap(); tokenSubstitutionMap.put("TOOLTIP_DELAY", mDelay + ""); tokenSubstitutionMap.put("TOOLTIP_XOFFSET", mXOffset + ""); tokenSubstitutionMap.put("TOOLTIP_YOFFSET", mYOffset + ""); tokenSubstitutionMap.put("TOOLTIP_BORDER", mBorder + ""); tokenSubstitutionMap.put("TOOLTIP_FONT", mFont + ""); tokenSubstitutionMap.put("TOOLTIP_FONT_SIZE", mFontSize + ""); tokenSubstitutionMap.put("TOOLTIP_FONT_COLOR", mFontColor + ""); tokenSubstitutionMap.put("TOOLTIP_BGCOLOR", mBgColor + ""); tokenSubstitutionMap.put("TOOLTIP_PADDING", mPadding + ""); tokenSubstitutionMap.put("TOOLTIP_MAX_WIDTH", mMaxWidth + ""); tokenSubstitutionMap.put("TOOLTIP_SHADOW_COLOR", mShadowColor + ""); tokenSubstitutionMap.put("TOOLTIP_SHADOW_OFFSET", mShadowOffset + ""); mCachedJs = JsUtil.getJSResourceAsString("tooltip.js", tokenSubstitutionMap); } return mCachedJs; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy