View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.gui.controlx.search;
35  
36  import info.magnolia.cms.gui.controlx.list.ListModelIterator;
37  import info.magnolia.cms.gui.query.SearchQuery;
38  
39  import java.util.Collection;
40  import java.util.NoSuchElementException;
41  
42  public class MultipleSearchListModel extends AbstractSearchableListModel {
43  
44      protected final AbstractSearchableListModel[] models;
45      protected final String[] modelNames;
46  
47      public MultipleSearchListModel(AbstractSearchableListModel[] models, String[] modelNames) {
48          this.models = models;
49          this.modelNames = modelNames;
50      }
51  
52      public ListModelIterator getListModel() {
53          return new MultipleListIterator();
54      }
55  
56      private class MultipleListIterator implements ListModelIterator {
57  
58          private ListModelIterator current, previous;
59          private int pos;
60  
61          public Object getValue(String name) {
62              if(previous != null){
63                  return previous.getValue(name);
64              }
65              return current.getValue(name);
66          }
67  
68          public Object getValueObject() {
69              if(previous != null){
70                  return previous.getValueObject();
71              }
72              return current.getValueObject();
73          }
74  
75          public String getId() {
76              if(previous != null){
77                  return previous.getId();
78              }
79              return current.getId();
80          }
81  
82          public String getGroupName() {
83              if(previous != null){
84                  return previous.getGroupName();
85              }
86              return current.getGroupName();
87          }
88  
89          public Object nextGroup() {
90              if(previous != null){
91                  return previous.nextGroup();
92              }
93              return current.nextGroup();
94          }
95  
96          public boolean hasNextInGroup() {
97              if(previous != null){
98                  return previous.hasNext();
99              }
100             return current.hasNextInGroup();
101         }
102 
103         public void remove() {
104             current.remove();
105         }
106 
107         public boolean hasNext() {
108             if(current == null && models.length > 0){
109                 current = models[pos].getListModelIterator();
110             }
111             while(!current.hasNext()){
112                 if(++pos < models.length){
113                     previous = current;
114                     current = models[pos].getListModelIterator();
115                 }else{
116                     return false;
117                 }
118             }
119             return true;
120         }
121 
122         public Object next() {
123             previous = null;
124             if(current == null) {
125                 throw new NoSuchElementException();
126             }
127             current.next();
128             return getName();
129         }
130 
131         public String getName() {
132             return modelNames[pos];
133         }
134 
135     }
136 
137     public void setGroupBy(String name, String order) {
138         super.setGroupBy(name, order);
139         for(int i = 0; i < models.length; i++) {
140             models[i].setGroupBy(name, order);
141         }
142     }
143 
144     public void setGroupBy(String name) {
145         super.setGroupBy(name);
146         for(int i = 0; i < models.length; i++) {
147             models[i].setGroupBy(name);
148         }
149     }
150 
151     public void setSortBy(String name, String order) {
152         super.setSortBy(name, order);
153         for(int i = 0; i < models.length; i++) {
154             models[i].setSortBy(name, order);
155         }
156     }
157 
158     public void setSortBy(String name) {
159         super.setSortBy(name);
160         for(int i = 0; i < models.length; i++) {
161             models[i].setSortBy(name);
162         }
163     }
164 
165     public void setQuery(SearchQuery query) {
166         super.setQuery(query);
167         for(int i = 0; i < models.length; i++) {
168             models[i].setQuery(query);
169         }
170     }
171 
172     protected Collection getResult() throws Exception {
173         throw new RuntimeException("this method should never be called since the iterator creation was overwritten");
174     }
175 }