KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > mlw > vlh > web > tag > support > GroupingCellInterceptor


1 /**
2  * Copyright (c) 2003 held jointly by the individual authors.
3  *
4  * This library is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; either version 2.1 of the License, or
7  * (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; with out even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation,
16  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  *
18  * > http://www.gnu.org/copyleft/lesser.html
19  * > http://www.opensource.org/licenses/lgpl-license.php
20  */

21 package net.mlw.vlh.web.tag.support;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import javax.servlet.jsp.PageContext JavaDoc;
30
31 /**
32  * @author mwilson
33  *
34  * Groups cell by using lastRow.property.equsls(currentRow.property)
35  */

36 public class GroupingCellInterceptor implements CellInterceptor
37 {
38    /**
39     * @see net.mlw.vlh.web.tag.support.CellInterceptor#isHidden(javax.servlet.jsp.PageContext, java.lang.String, java.lang.String, java.lang.Object)
40     */

41    public boolean isHidden(PageContext JavaDoc pageContext, String JavaDoc key, String JavaDoc name,
42          Object JavaDoc value)
43    {
44       if( key == null )
45       {
46          return false;
47       }
48       
49       GroupingInfo info = (GroupingInfo)pageContext.getAttribute(key + "GroupingInfo");
50       if( info == null )
51       {
52          pageContext.setAttribute(key + "GroupingInfo", info = new GroupingInfo());
53       }
54       
55       return info.sameAsLastValue(name, value);
56    }
57    
58    private static class GroupingInfo
59    {
60       private List JavaDoc names = new ArrayList JavaDoc();
61       private Map JavaDoc lastValues = new HashMap JavaDoc();
62        
63       public boolean sameAsLastValue(String JavaDoc name, Object JavaDoc value)
64       {
65          if(!names.contains(name))
66          {
67             names.add(name);
68             lastValues.put(name, value);
69             return false;
70          }
71          
72          Object JavaDoc lastValue = lastValues.get(name);
73          lastValues.put(name, value);
74          
75          if(lastValue==null)
76          {
77             return false;
78          }
79          else
80          {
81             if(lastValue.equals(value))
82             {
83                 return true;
84             }
85             else
86             {
87                boolean nullOut = false;
88                for (Iterator JavaDoc iter = names.iterator(); iter.hasNext();)
89                {
90                   String JavaDoc currentName = (String JavaDoc) iter.next();
91                   
92                   if(nullOut)
93                   {
94                      lastValues.remove(currentName);
95                   }
96
97                   if( currentName.equals(name))
98                   {
99                      nullOut = true;
100                   }
101                }
102                return false;
103             }
104          }
105       }
106    }
107 }
Popular Tags