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 */
019package org.apache.wiki.tags;
020
021import jakarta.servlet.jsp.*;
022import jakarta.servlet.jsp.tagext.*;
023
024/**
025 *  Generates tabbed page section: container for the Tab tag.
026 *  Works together with the tabbedSection javacript.
027 *
028 *  <P><B>Attributes</B></P>
029 *  <UL>
030 *    <LI>defaultTab - Page name to refer to.  Default is the current page.
031 *  </UL>
032 *
033 *  @since v2.3.63
034 */
035// FIXME: Needs a bit more of explaining how this tag works.
036public class TabbedSectionTag extends BodyTagSupport
037{
038    private static final long serialVersionUID = 1702437933960026481L;
039    private String       m_defaultTabId;
040    private String       m_firstTabId;
041    private boolean      m_defaultTabFound;
042
043    private StringBuffer m_buffer = new StringBuffer(BUFFER_SIZE);
044
045    private static final int FIND_DEFAULT_TAB = 0;
046    private static final int GENERATE_TABMENU = 1;
047    private static final int GENERATE_TABBODY = 2;
048
049    private static final int BUFFER_SIZE      = 1024;
050
051    private              int m_state            = FIND_DEFAULT_TAB;
052
053    /**
054     *  {@inheritDoc}
055     */
056    @Override
057    public void release()
058    {
059        super.release();
060        m_defaultTabId = m_firstTabId = null;
061        m_defaultTabFound = false;
062        m_buffer = new StringBuffer();
063        m_state = FIND_DEFAULT_TAB;
064    }
065
066    /**
067     *  Set the id of the default tab (the tab which should be shown when
068     *  the page is first loaded).
069     *  
070     *  @param anDefaultTabId ID attribute of the default tab.
071     */
072    public void setDefaultTab(final String anDefaultTabId)
073    {
074        m_defaultTabId = anDefaultTabId;
075    }
076
077    /**
078     *  Called by every tab each time the section goes through its tabs. While the default tab is being looked for, it records the first
079     *  tab and whether the requested default tab exists; if it does not, the first tab becomes the default one. In the other phases it
080     *  tells whether the tab is the default one, which is the tab that is shown and highlighted in the menu.
081     *
082     *  @param aTabId ID attribute of the tab.
083     *  @return True, if the tab is the default tab.
084     */
085    public boolean validateDefaultTab(final String aTabId )
086    {
087        if( m_firstTabId == null ) m_firstTabId = aTabId;
088        if( aTabId.equals( m_defaultTabId ) ) m_defaultTabFound = true;
089
090        return aTabId.equals( m_defaultTabId );
091    }
092
093    /**
094     *  {@inheritDoc}
095     */
096    @Override
097    public int doStartTag() throws JspTagException
098    {
099        return EVAL_BODY_BUFFERED; /* always look inside */
100    }
101
102    /**
103     *  Returns true, if the tab system is currently trying to
104     *  figure out which is the default tab.
105     *  
106     *  @return True, if finding the default tab.
107     */
108    public boolean isStateFindDefaultTab()
109    {
110        return m_state == FIND_DEFAULT_TAB;
111    }
112
113    /**
114     *  Returns true, if the tab system is currently generating
115     *  the tab menu.
116     *  
117     *  @return True, if currently generating the menu itself.
118     */
119    public boolean isStateGenerateTabMenu()
120    {
121        return m_state == GENERATE_TABMENU;
122    }
123
124    /**
125     *  Returns true, if the tab system is currently generating
126     *  the tab body.
127     *  
128     *  @return True, if the tab system is currently generating the tab body.
129     */
130    public boolean isStateGenerateTabBody()
131    {
132        return m_state == GENERATE_TABBODY;
133    }
134
135
136    /**
137     *  The tabbed section iterates 3 time through the underlying Tab tags
138     * - first it identifies the default tab (displayed by default)
139     * - second it generates the tabmenu markup (displays all tab-titles)
140     * - finally it generates the content of each tab.
141     * 
142     * @return {@inheritDoc}
143     * @throws {@inheritDoc}
144     */
145    @Override
146    public int doAfterBody() throws JspTagException
147    {
148        if( isStateFindDefaultTab() )
149        {
150            if( !m_defaultTabFound )
151            {
152                m_defaultTabId = m_firstTabId;
153            }
154            m_state = GENERATE_TABMENU;
155            return EVAL_BODY_BUFFERED;
156        }
157        else if( isStateGenerateTabMenu() )
158        {
159            if( bodyContent != null )
160            {
161                m_buffer.append( "<div class=\"tabmenu\">" );
162                m_buffer.append( bodyContent.getString() );
163                bodyContent.clearBody();
164                m_buffer.append( "</div>\n" );
165            }
166            m_state = GENERATE_TABBODY;
167            return EVAL_BODY_BUFFERED;
168        }
169        else if( isStateGenerateTabBody() )
170        {
171            if( bodyContent != null )
172            {
173                m_buffer.append( "<div class=\"tabs\">" );
174                m_buffer.append( bodyContent.getString() );
175                bodyContent.clearBody();
176                m_buffer.append( "<div style=\"clear:both;\" ></div>\n</div>\n" );
177            }
178            return SKIP_BODY;
179        }
180        return SKIP_BODY;
181    }
182
183    /**
184     *  {@inheritDoc}
185     */
186    @Override
187    public int doEndTag() throws JspTagException
188    {
189        try
190        {
191            if( m_buffer.length() > 0 )
192            {
193                getPreviousOut().write( m_buffer.toString() );
194            }
195        }
196        catch(final java.io.IOException e)
197        {
198            throw new JspTagException( "IO Error: " + e.getMessage() );
199        }
200
201        //now reset some stuff for the next run -- ugh.
202        m_buffer    = new StringBuffer(BUFFER_SIZE);
203        m_state = FIND_DEFAULT_TAB;
204        m_defaultTabId    = null;
205        m_firstTabId      = null;
206        m_defaultTabFound = false;
207        return EVAL_PAGE;
208    }
209
210}