001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.rest.widget;
014
015import static org.apache.juneau.dto.html5.HtmlBuilder.*;
016
017import java.util.*;
018
019import org.apache.juneau.*;
020import org.apache.juneau.dto.html5.*;
021
022/**
023 * Simple template for adding tooltips to HTML5 bean constructs, typically in menu item widgets.
024 * 
025 * <p>
026 * Tooltips depend on the existence of the <code>tooltip</code> and <code>tooltiptext</code> styles that should be
027 * present in the stylesheet for the document.
028 * 
029 * <p>
030 * The following examples shows how tooltips can be added to a menu item widget.
031 * 
032 * <p class='bcode'>
033 * <jk>public class</jk> MyFormMenuItem <jk>extends</jk> MenuItemWidget {
034 * 
035 *    <ja>@Override</ja>
036 *    <jk>public</jk> String getLabel(RestRequest req) <jk>throws</jk> Exception {
037 *       <jk>return</jk> <js>"myform"</js>;
038 *    }
039 * 
040 *    <ja>@Override</ja>
041 *    <jk>public</jk> Object getContent(RestRequest req) <jk>throws</jk> Exception {
042 *       <jk>return</jk> div(
043 *          <jsm>form</jsm>().id(<js>"form"</js>).action(<js>"servlet:/form"</js>).method(<jsf>POST</jsf>).children(
044 *             <jsm>table</jsm>(
045 *                <jsm>tr</jsm>(
046 *                   <jsm>th</jsm>(<js>"Field 1:"</js>),
047 *                   <jsm>td</jsm>(<jsm>input</jsm>().name(<js>"field1"</js>).type(<js>"text"</js>)),
048 *                   <jsm>td</jsm>(<jk>new</jk> Tooltip(<js>"(?)"</js>, <js>"This is field #1!"</js>, br(), <js>"(e.g. '"</js>, code(<js>"Foo"</js>), <js>"')"</js>))
049 *                ),
050 *                <jsm>tr</jsm>(
051 *                   <jsm>th</jsm>(<js>"Field 2:"</js>),
052 *                   <jsm>td</jsm>(<jsm>input</jsm>().name(<js>"field2"</js>).type(<js>"text"</js>)),
053 *                   <jsm>td</jsm>(<jk>new</jk> Tooltip(<js>"(?)"</js>, <js>"This is field #2!"</js>, br(), <js>"(e.g. '"</js>, code(<js>"Bar"</js>), <js>"')"</js>))
054 *                )
055 *             )
056 *          )
057 *       );
058 *    }
059 * }
060 * </p>
061 * 
062 * <h5 class='section'>See Also:</h5>
063 * <ul>
064 *    <li class='link'><a class="doclink" href="../../../../../overview-summary.html#juneau-rest-server.Widgets">Overview &gt; juneau-rest-server &gt; Widgets</a>
065 * </ul>
066 */
067public class Tooltip {
068
069   private final String display;
070   private final List<Object> content;
071
072   /**
073    * Constructor.
074    *
075    * @param display
076    *    The normal display text.
077    *    This is what gets rendered normally.
078    * @param content
079    *    The hover contents.
080    *    Typically a list of strings, but can also include any HTML5 beans as well.
081    */
082   public Tooltip(String display, Object...content) {
083      this.display = display;
084      this.content = new ArrayList<>(Arrays.asList(content));
085   }
086
087   /**
088    * The swap method.
089    *
090    * <p>
091    * Converts this bean into a div tag with contents.
092    *
093    * @param session The bean session.
094    * @return The swapped contents of this bean.
095    */
096   public Div swap(BeanSession session) {
097      return div(
098         small(display),
099         span()._class("tooltiptext").children(content)
100      )._class("tooltip");
101   }
102}