KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > addressbook > gui > table > model > FilterDecorator


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.addressbook.gui.table.model;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import javax.swing.event.TableModelEvent JavaDoc;
26
27 import org.columba.addressbook.model.ContactModelPartial;
28 import org.columba.addressbook.model.IContactModelPartial;
29
30 /**
31  * @author fdietz
32  *
33  */

34 public class FilterDecorator extends TableModelDecorator {
35
36     private List JavaDoc list;
37
38     private String JavaDoc pattern = "";
39
40     /**
41      * @param model
42      */

43     public FilterDecorator(ContactItemTableModel model) {
44         super(model);
45
46         list = new ArrayList JavaDoc();
47
48     }
49
50     public void tableChanged(TableModelEvent JavaDoc e) {
51         filter();
52     }
53     
54
55     /* (non-Javadoc)
56      * @see org.columba.addressbook.gui.table.model.TableModelDecorator#getRowCount()
57      */

58     @Override JavaDoc
59     public int getRowCount() {
60         return list.size();
61     }
62
63     /**
64      * @see org.columba.addressbook.gui.table.model.ContactItemTableModel#setContactItemMap(org.columba.addressbook.model.ContactItemMap)
65      */

66     public void setContactItemMap(Map JavaDoc<String JavaDoc,IContactModelPartial> list) {
67         super.setContactItemMap(list);
68         
69         if ( list != null)
70             filter();
71     }
72
73     protected void filter() {
74         this.list = new ArrayList JavaDoc();
75
76         pattern = pattern.toLowerCase();
77
78         Iterator JavaDoc it = getRealModel().getContactItemMap().values().iterator();
79         int i = 0;
80         while (it.hasNext()) {
81             ContactModelPartial item = (ContactModelPartial) it.next();
82             String JavaDoc name = item.getName();
83             String JavaDoc adr = item.getAddress();
84
85             name = name.toLowerCase();
86             adr = adr.toLowerCase();
87
88             if ((name.indexOf(pattern) != -1) || (adr.indexOf(pattern) != -1)) {
89
90                 this.list.add(new Integer JavaDoc(i));
91
92             }
93             i++;
94         }
95     }
96
97     /**
98      * @see javax.swing.table.TableModel#getValueAt(int, int)
99      */

100     public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
101         if ( getRealModel().getRowCount() == 0 ) return null;
102         return getRealModel().getValueAt(
103                 ((Integer JavaDoc) list.get(rowIndex)).intValue(), columnIndex);
104     }
105
106     /**
107      * @see javax.swing.table.TableModel#setValueAt(java.lang.Object, int, int)
108      */

109     public void setValueAt(Object JavaDoc aValue, int rowIndex, int columnIndex) {
110         getRealModel().setValueAt(aValue,
111                 ((Integer JavaDoc) list.get(rowIndex)).intValue(), columnIndex);
112     }
113
114     /**
115      * @return Returns the pattern.
116      */

117     public String JavaDoc getPattern() {
118         return pattern;
119     }
120
121     /**
122      * @param pattern
123      * The pattern to set.
124      */

125     public void setPattern(String JavaDoc pattern) {
126         this.pattern = pattern;
127     }
128
129     /**
130      * @see org.columba.addressbook.gui.table.model.ContactItemTableModel#getContactItem(int)
131      */

132     public IContactModelPartial getContactItem(int index) {
133         return getRealModel().getContactItem(
134                 ((Integer JavaDoc) list.get(index)).intValue());
135     }
136 }
Popular Tags