001/* 
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.  
018 */
019
020package org.apache.wiki.tags;
021
022import java.io.IOException;
023
024import javax.servlet.jsp.JspTagException;
025
026import org.apache.wiki.util.TextUtil;
027
028/**
029 *  Generates single tabbed page layout.
030 *  Works together with the tabbedSection javascript.  Note that if you do not
031 *  specify an url, the body contents of the tag are loaded by the tag itself.
032 *
033 *  <P><B>Attributes</B></P>
034 *  <UL>
035 *    <LI>id - ID for this tab. (mandatory)
036 *    <LI>title - Title of this tab. (mandatory)
037 *    <LI>accesskey - Single char usable as quick accesskey (alt- or ctrl-) (optional)
038 *    <li>url - If you <i>don't</i> want to create a Javascript-enabled tag, you can use this
039 *              to make the tab look just the usual tag, but instead, it will actually link
040 *              to that page.  This can be useful in certain cases where you have something
041 *              that you want to look like a part of a tag, but for example, due to it being
042 *              very big in size, don't want to include it as a part of the page content
043 *              every time.
044 *  </UL>
045 *
046 *  @since v2.3.63
047 */
048
049public class TabTag extends WikiTagBase
050{
051    private static final long serialVersionUID = -8534125226484616489L;
052    private String m_accesskey;
053    private String m_tabTitle;
054    private String m_url;
055
056    /**
057     * {@inheritDoc}
058     */
059    public void doFinally()
060    {
061        super.doFinally();
062
063        m_accesskey = null;
064        m_tabTitle  = null;
065        m_url       = null;
066    }
067
068    /**
069     * Sets the tab title.
070     * @param aTabTitle the tab title
071     */
072    public void setTitle(String aTabTitle)
073    {
074        m_tabTitle = TextUtil.replaceEntities( aTabTitle );
075    }
076
077    /**
078     * Sets the tab access key.
079     * @param anAccesskey the access key
080     */
081    public void setAccesskey(String anAccesskey)
082    {
083        m_accesskey = TextUtil.replaceEntities( anAccesskey ); //take only the first char
084    }
085
086    /**
087     * Sets the tab URL.
088     * @param url the URL
089     */
090    public void setUrl( String url )
091    {
092        m_url = TextUtil.replaceEntities( url );
093    }
094
095    // insert <u> ..accesskey.. </u> in title
096    private boolean handleAccesskey()
097    {
098        if( (m_tabTitle == null) || (m_accesskey == null) ) return false;
099
100        int pos = m_tabTitle.toLowerCase().indexOf( m_accesskey.toLowerCase() );
101        if( pos > -1 )
102        {
103            m_tabTitle = m_tabTitle.substring( 0, pos ) + "<span class='accesskey'>"
104                       + m_tabTitle.charAt( pos ) + "</span>" + m_tabTitle.substring( pos+1 );
105        }
106        return true;
107    }
108
109    /**
110     * {@inheritDoc}
111     */
112    public int doWikiStartTag() throws JspTagException
113    {
114        TabbedSectionTag parent=(TabbedSectionTag)findAncestorWithClass( this, TabbedSectionTag.class );
115
116        //
117        //  Sanity checks
118        //
119        if( getId() == null )
120        {
121            throw new JspTagException("Tab Tag without \"id\" attribute");
122        }
123        if( m_tabTitle == null )
124        {
125            throw new JspTagException("Tab Tag without \"tabTitle\" attribute");
126        }
127        if( parent == null )
128        {
129            throw new JspTagException("Tab Tag without parent \"TabbedSection\" Tag");
130        }
131
132        if( !parent.isStateGenerateTabBody() ) return SKIP_BODY;
133
134        StringBuilder sb = new StringBuilder(32);
135
136        sb.append( "<div id=\""+ getId() + "\"" );
137
138        if( !parent.validateDefaultTab( getId()) )
139        {
140            sb.append( " class=\"hidetab\"" );
141        }
142        sb.append( " >\n" );
143
144        try
145        {
146            pageContext.getOut().write( sb.toString() );
147        }
148        catch( java.io.IOException e )
149        {
150            throw new JspTagException( "IO Error: " + e.getMessage() );
151        }
152
153        return EVAL_BODY_INCLUDE;
154    }
155
156    /**
157     * {@inheritDoc}
158     */
159    public int doEndTag() throws JspTagException
160    {
161        TabbedSectionTag parent=(TabbedSectionTag)findAncestorWithClass( this, TabbedSectionTag.class );
162
163        StringBuilder sb = new StringBuilder();
164
165        if( parent.isStateFindDefaultTab() )
166        {
167            //inform the parent of each tab
168            parent.validateDefaultTab( getId() );
169        }
170        else if( parent.isStateGenerateTabBody() )
171        {
172            sb.append( "</div>\n" );
173        }
174        else if( parent.isStateGenerateTabMenu() )
175        {
176            sb.append( "<a" );
177
178            if( parent.validateDefaultTab( getId() ) )
179            {
180                sb.append( " class=\"activetab\"" );
181            }
182
183            sb.append( " id=\"menu-" + getId() + "\"" );
184
185            if( m_url != null )
186            {
187                sb.append( " href='"+m_url+"'" );
188            }
189
190            if( handleAccesskey() )
191            {
192                sb.append( " accesskey=\"" + m_accesskey + "\"" );
193            }
194
195            sb.append( " >" );
196            sb.append( m_tabTitle );
197            sb.append( "</a>" );
198        }
199
200        try
201        {
202            pageContext.getOut().write( sb.toString() );
203        }
204        catch( IOException e )
205        {
206            throw new JspTagException( "IO Error: " + e.getMessage() );
207        }
208
209        return EVAL_PAGE;
210    }
211}