1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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 }