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    @Override
060    public void doFinally()
061    {
062        super.doFinally();
063
064        m_accesskey = null;
065        m_tabTitle  = null;
066        m_url       = null;
067    }
068
069    /**
070     * Sets the tab title.
071     * @param aTabTitle the tab title
072     */
073    public void setTitle(final String aTabTitle)
074    {
075        m_tabTitle = TextUtil.replaceEntities( aTabTitle );
076    }
077
078    /**
079     * Sets the tab access key.
080     * @param anAccesskey the access key
081     */
082    public void setAccesskey(final String anAccesskey)
083    {
084        m_accesskey = TextUtil.replaceEntities( anAccesskey ); //take only the first char
085    }
086
087    /**
088     * Sets the tab URL.
089     * @param url the URL
090     */
091    public void setUrl(final String url )
092    {
093        m_url = TextUtil.replaceEntities( url );
094    }
095
096    // insert <u> ..accesskey.. </u> in title
097    private boolean handleAccesskey()
098    {
099        if( (m_tabTitle == null) || (m_accesskey == null) ) return false;
100
101        final int pos = m_tabTitle.toLowerCase().indexOf( m_accesskey.toLowerCase() );
102        if( pos > -1 )
103        {
104            m_tabTitle = m_tabTitle.substring( 0, pos ) + "<span class='accesskey'>"
105                       + m_tabTitle.charAt( pos ) + "</span>" + m_tabTitle.substring( pos+1 );
106        }
107        return true;
108    }
109
110    /**
111     * {@inheritDoc}
112     */
113    @Override
114    public int doWikiStartTag() throws JspTagException
115    {
116        final TabbedSectionTag parent=(TabbedSectionTag)findAncestorWithClass( this, TabbedSectionTag.class );
117
118        //
119        //  Sanity checks
120        //
121        if( getId() == null )
122        {
123            throw new JspTagException("Tab Tag without \"id\" attribute");
124        }
125        if( m_tabTitle == null )
126        {
127            throw new JspTagException("Tab Tag without \"tabTitle\" attribute");
128        }
129        if( parent == null )
130        {
131            throw new JspTagException("Tab Tag without parent \"TabbedSection\" Tag");
132        }
133
134        if( !parent.isStateGenerateTabBody() ) return SKIP_BODY;
135
136        final StringBuilder sb = new StringBuilder(32);
137
138        sb.append( "<div id=\"" ).append( getId() ).append( "\"" );
139
140        if( !parent.validateDefaultTab( getId()) )
141        {
142            sb.append( " class=\"hidetab\"" );
143        }
144        sb.append( " >\n" );
145
146        try
147        {
148            pageContext.getOut().write( sb.toString() );
149        }
150        catch( final java.io.IOException e )
151        {
152            throw new JspTagException( "IO Error: " + e.getMessage() );
153        }
154
155        return EVAL_BODY_INCLUDE;
156    }
157
158    /**
159     * {@inheritDoc}
160     */
161    @Override
162    public int doEndTag() throws JspTagException
163    {
164        final TabbedSectionTag parent=(TabbedSectionTag)findAncestorWithClass( this, TabbedSectionTag.class );
165
166        final StringBuilder sb = new StringBuilder();
167
168        if( parent.isStateFindDefaultTab() )
169        {
170            //inform the parent of each tab
171            parent.validateDefaultTab( getId() );
172        }
173        else if( parent.isStateGenerateTabBody() )
174        {
175            sb.append( "</div>\n" );
176        }
177        else if( parent.isStateGenerateTabMenu() )
178        {
179            sb.append( "<a" );
180
181            if( parent.validateDefaultTab( getId() ) )
182            {
183                sb.append( " class=\"activetab\"" );
184            }
185
186            sb.append( " id=\"menu-" ).append( getId() ).append( "\"" );
187
188            if( m_url != null )
189            {
190                sb.append( " href='" ).append( m_url ).append( "'" );
191            }
192
193            if( handleAccesskey() )
194            {
195                sb.append( " accesskey=\"" ).append( m_accesskey ).append( "\"" );
196            }
197
198            sb.append( " >" );
199            sb.append( m_tabTitle );
200            sb.append( "</a>" );
201        }
202
203        try
204        {
205            pageContext.getOut().write( sb.toString() );
206        }
207        catch( final IOException e )
208        {
209            throw new JspTagException( "IO Error: " + e.getMessage() );
210        }
211
212        return EVAL_PAGE;
213    }
214}