View Javadoc
1   /**
2    * This file Copyright (c) 2003-2014 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          @Override
62          public Object getValue(String name) {
63              if(previous != null){
64                  return previous.getValue(name);
65              }
66              return current.getValue(name);
67          }
68  
69          @Override
70          public Object getValueObject() {
71              if(previous != null){
72                  return previous.getValueObject();
73              }
74              return current.getValueObject();
75          }
76  
77          @Override
78          public String getId() {
79              if(previous != null){
80                  return previous.getId();
81              }
82              return current.getId();
83          }
84  
85          @Override
86          public String getGroupName() {
87              if(previous != null){
88                  return previous.getGroupName();
89              }
90              return current.getGroupName();
91          }
92  
93          @Override
94          public Object nextGroup() {
95              if(previous != null){
96                  return previous.nextGroup();
97              }
98              return current.nextGroup();
99          }
100 
101         @Override
102         public boolean hasNextInGroup() {
103             if(previous != null){
104                 return previous.hasNext();
105             }
106             return current.hasNextInGroup();
107         }
108 
109         @Override
110         public void remove() {
111             current.remove();
112         }
113 
114         @Override
115         public boolean hasNext() {
116             if(current == null && models.length > 0){
117                 current = models[pos].getListModelIterator();
118             }
119             while(!current.hasNext()){
120                 if(++pos < models.length){
121                     previous = current;
122                     current = models[pos].getListModelIterator();
123                 }else{
124                     return false;
125                 }
126             }
127             return true;
128         }
129 
130         @Override
131         public Object next() {
132             previous = null;
133             if(current == null) {
134                 throw new NoSuchElementException();
135             }
136             current.next();
137             return getName();
138         }
139 
140         public String getName() {
141             return modelNames[pos];
142         }
143 
144     }
145 
146     @Override
147     public void setGroupBy(String name, String order) {
148         super.setGroupBy(name, order);
149         for(int i = 0; i < models.length; i++) {
150             models[i].setGroupBy(name, order);
151         }
152     }
153 
154     @Override
155     public void setGroupBy(String name) {
156         super.setGroupBy(name);
157         for(int i = 0; i < models.length; i++) {
158             models[i].setGroupBy(name);
159         }
160     }
161 
162     @Override
163     public void setSortBy(String name, String order) {
164         super.setSortBy(name, order);
165         for(int i = 0; i < models.length; i++) {
166             models[i].setSortBy(name, order);
167         }
168     }
169 
170     @Override
171     public void setSortBy(String name) {
172         super.setSortBy(name);
173         for(int i = 0; i < models.length; i++) {
174             models[i].setSortBy(name);
175         }
176     }
177 
178     @Override
179     public void setQuery(SearchQuery query) {
180         super.setQuery(query);
181         for(int i = 0; i < models.length; i++) {
182             models[i].setQuery(query);
183         }
184     }
185 
186     @Override
187     protected Collection getResult() throws Exception {
188         throw new RuntimeException("this method should never be called since the iterator creation was overwritten");
189     }
190 }