001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // 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
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.html;
016    
017    import org.apache.tapestry.BaseComponent;
018    import org.apache.tapestry.IMarkupWriter;
019    import org.apache.tapestry.IRequestCycle;
020    import org.apache.tapestry.bean.EvenOdd;
021    import org.apache.tapestry.util.exception.ExceptionDescription;
022    
023    import java.util.List;
024    
025    /**
026     * Component used to display an already formatted exception. [ <a
027     * href="../../../../../ComponentReference/ExceptionDisplay.html">Component
028     * Reference </a>]
029     * 
030     * @author Howard Lewis Ship
031     */
032    
033    public abstract class ExceptionDisplay extends BaseComponent
034    {
035        private ExceptionDescription _exception;
036    
037        private EvenOdd _evenOdd;
038    
039        public abstract int getIndex();
040    
041        public abstract int getCount();
042    
043        public abstract void setCount(int count);
044    
045        public abstract ExceptionDescription[] getExceptions();
046        
047        /** @since 4.1.4 */
048        public abstract List getPackages();
049        
050        public abstract String getTrace();
051        
052        /**
053         * Each time the current exception is set, as a side effect, the evenOdd
054         * helper bean is reset to even.
055         */
056    
057        public void setException(ExceptionDescription value)
058        {
059            _exception = value;
060    
061            _evenOdd.setEven(true);
062        }
063    
064        public ExceptionDescription getException()
065        {
066            return _exception;
067        }
068    
069        protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
070        {
071            ExceptionDescription[] exceptions = getExceptions();
072    
073            setCount(exceptions.length);
074    
075            try
076            {
077                _evenOdd = (EvenOdd) getBeans().getBean("evenOdd");
078    
079                super.renderComponent(writer, cycle);
080            }
081            finally
082            {
083                _exception = null;
084                _evenOdd = null;
085            }
086        }
087    
088        public boolean isLast()
089        {
090            return getIndex() == (getCount() - 1);
091        }
092        
093        public boolean isInPackage() {
094            List packages = getPackages();
095            if (packages == null) 
096                return false;
097            
098            String trace = getTrace();
099            for (int i=0; i<packages.size(); i++)
100            {
101                if (trace.startsWith((String)packages.get(i)))
102                    return true;
103            }
104            
105            return false;
106        }
107        
108        public String getStackClass()
109        {
110            return isInPackage() ? "stackSelected" : null;
111        }
112    }