KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > filter > FilterList


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.undation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16
package org.columba.core.filter;
17
18 import org.columba.core.config.DefaultItem;
19 import org.columba.core.xml.XmlElement;
20
21 /**
22  * A list of filters.
23  *
24  */

25 public class FilterList extends DefaultItem implements IFilterList {
26     /**
27      * Creates a FilterList with the specified element as the root.
28      *
29      * @param root
30      * the element to use as the root.
31      */

32     public FilterList(XmlElement root) {
33         super(root);
34     }
35
36     /**
37      * Creates an empty filter list.
38      */

39     public FilterList() {
40         super(new XmlElement(FilterList.XML_NAME));
41     }
42
43     /**
44      * Returns an empty default filter.
45      *
46      * @return an empty default Filter.
47      * @deprecated
48      */

49     public static IFilter createDefaultFilter() {
50         XmlElement filter = new XmlElement("filter");
51         filter.addAttribute("description", "new filter");
52         filter.addAttribute("enabled", "true");
53
54         XmlElement rules = new XmlElement("rules");
55         rules.addAttribute("condition", "matchall");
56
57         XmlElement criteria = new XmlElement("criteria");
58         criteria.addAttribute("type", "Subject");
59         criteria.addAttribute("headerfield", "Subject");
60         criteria.addAttribute("criteria", "contains");
61         criteria.addAttribute("pattern", "pattern");
62         rules.addElement(criteria);
63         filter.addElement(rules);
64
65         XmlElement actionList = new XmlElement("actionlist");
66         XmlElement action = new XmlElement("action");
67
68         /*
69          * action.addAttribute( "class",
70          * "org.columba.mail.filter.action.MarkMessageAsReadFilterAction");
71          */

72         action.addAttribute("type", "Mark Message");
73         action.addAttribute("markvariant", "read");
74
75         actionList.addElement(action);
76         filter.addElement(actionList);
77
78         // XmlElement.printNode(getRoot(),"");
79
return new Filter(filter);
80
81         /*
82          * //AdapterNode filterListNode = getFilterListNode();
83          *
84          * AdapterNode node =
85          * MailInterface.config.getFolderConfig().addEmptyFilterNode(
86          * getFolder().getNode() ); Filter filter = new Filter( node );
87          *
88          * add( filter );
89          *
90          * return filter;
91          */

92     }
93
94     /* (non-Javadoc)
95    * @see org.columba.core.filter.IFilterList#add(org.columba.core.filter.Filter)
96    */

97     public void add(IFilter f) {
98         if (f != null) {
99             getRoot().addElement(f.getRoot());
100         }
101
102         // list.add(f);
103
}
104
105     /* (non-Javadoc)
106    * @see org.columba.core.filter.IFilterList#addAll(org.columba.core.filter.FilterList)
107    */

108     public void addAll(IFilterList list) {
109         int size = list.count();
110
111         for (int i = 0; i < size; i++) {
112             IFilter newFilter = list.get(i);
113             add(newFilter);
114         }
115     }
116
117     /* (non-Javadoc)
118    * @see org.columba.core.filter.IFilterList#remove(org.columba.core.filter.Filter)
119    */

120     public void remove(IFilter f) {
121         if (f != null) {
122             getRoot().getElements().remove(f.getRoot());
123         }
124     }
125
126     /* (non-Javadoc)
127    * @see org.columba.core.filter.IFilterList#insert(org.columba.core.filter.Filter, int)
128    */

129     public void insert(IFilter filter, int index) {
130         if (filter != null) {
131             getRoot().insertElement(filter.getRoot(), index);
132         }
133     }
134
135     /* (non-Javadoc)
136    * @see org.columba.core.filter.IFilterList#moveUp(org.columba.core.filter.Filter)
137    */

138     public void moveUp(IFilter filter) {
139         move(indexOf(filter), -1);
140     }
141
142     /* (non-Javadoc)
143    * @see org.columba.core.filter.IFilterList#moveDown(org.columba.core.filter.Filter)
144    */

145     public void moveDown(IFilter filter) {
146         move(indexOf(filter), 1);
147     }
148
149     /* (non-Javadoc)
150    * @see org.columba.core.filter.IFilterList#move(org.columba.core.filter.Filter, int)
151    */

152     public void move(IFilter filter, int nrOfPositions) {
153         move(indexOf(filter), nrOfPositions);
154     }
155
156     /* (non-Javadoc)
157    * @see org.columba.core.filter.IFilterList#move(int, int)
158    */

159     public void move(int filterIndex, int nrOfPositions) {
160         if ((filterIndex >= 0) && (filterIndex < count())) {
161             XmlElement filterXML = getRoot().getElement(filterIndex);
162             int newFilterIndex = filterIndex + nrOfPositions;
163
164             if (newFilterIndex < 0) {
165                 newFilterIndex = 0;
166             }
167
168             getRoot().removeElement(filterIndex);
169
170             if (newFilterIndex > count()) {
171                 getRoot().addElement(filterXML);
172             } else {
173                 getRoot().insertElement(filterXML, newFilterIndex);
174             }
175         }
176     }
177
178     /* (non-Javadoc)
179    * @see org.columba.core.filter.IFilterList#indexOf(org.columba.core.filter.Filter)
180    */

181     public int indexOf(IFilter filter) {
182         int index = -1;
183
184         if (filter != null) {
185             int childCount = getChildCount();
186
187             for (int i = 0; (index == -1) && (i < childCount); i++) {
188                 if (getRoot().getElement(i).equals(filter.getRoot())) {
189                     index = i;
190                 }
191             }
192         }
193
194         return index;
195     }
196
197     /* (non-Javadoc)
198    * @see org.columba.core.filter.IFilterList#count()
199    */

200     public int count() {
201         return getChildCount();
202     }
203
204     /* (non-Javadoc)
205    * @see org.columba.core.filter.IFilterList#get(int)
206    */

207     public IFilter get(int index) {
208         Filter filter = new Filter(getRoot().getElement(index));
209
210         return filter;
211     }
212
213     /* (non-Javadoc)
214    * @see org.columba.core.filter.IFilterList#remove(int)
215    */

216     public void remove(int index) {
217         getRoot().removeElement(index);
218     }
219 }
220
Popular Tags