1 package info.magnolia.cms.gui.controlx.search; 2 3 import info.magnolia.cms.gui.controlx.list.ListModelIterator; 4 import info.magnolia.cms.gui.query.SearchQuery; 5 6 import java.util.Collection ; 7 import java.util.NoSuchElementException ; 8 9 public class MultipleSearchListModel extends AbstractSearchableListModel { 10 11 protected final AbstractSearchableListModel[] models; 12 protected final String [] modelNames; 13 14 public MultipleSearchListModel(AbstractSearchableListModel[] models, String [] modelNames) { 15 this.models = models; 16 this.modelNames = modelNames; 17 } 18 19 public ListModelIterator getListModel() { 20 return new MultipleListIterator(); 21 } 22 23 private class MultipleListIterator implements ListModelIterator { 24 25 private ListModelIterator current, previous; 26 private int pos; 27 28 public Object getValue(String name) { 29 if(previous != null){ 30 return previous.getValue(name); 31 } 32 return current.getValue(name); 33 } 34 35 public Object getValueObject() { 36 if(previous != null){ 37 return previous.getValueObject(); 38 } 39 return current.getValueObject(); 40 } 41 42 public String getGroupName() { 43 if(previous != null){ 44 return previous.getGroupName(); 45 } 46 return current.getGroupName(); 47 } 48 49 public Object nextGroup() { 50 if(previous != null){ 51 return previous.nextGroup(); 52 } 53 return current.nextGroup(); 54 } 55 56 public boolean hasNextInGroup() { 57 if(previous != null){ 58 return previous.hasNext(); 59 } 60 return current.hasNextInGroup(); 61 } 62 63 public void remove() { 64 current.remove(); 65 } 66 67 public boolean hasNext() { 68 if(current == null && models.length > 0){ 69 current = models[pos].getListModelIterator(); 70 } 71 while(!current.hasNext()){ 72 if(++pos < models.length){ 73 previous = current; 74 current = models[pos].getListModelIterator(); 75 }else{ 76 return false; 77 } 78 } 79 return true; 80 } 81 82 public Object next() { 83 previous = null; 84 if(current == null) { 85 throw new NoSuchElementException (); 86 } 87 current.next(); 88 return getName(); 89 } 90 91 public String getName() { 92 return modelNames[pos]; 93 } 94 95 } 96 97 public void setGroupBy(String name, String order) { 98 super.setGroupBy(name, order); 99 for(int i = 0; i < models.length; i++) { 100 models[i].setGroupBy(name, order); 101 } 102 } 103 104 public void setGroupBy(String name) { 105 super.setGroupBy(name); 106 for(int i = 0; i < models.length; i++) { 107 models[i].setGroupBy(name); 108 } 109 } 110 111 public void setSortBy(String name, String order) { 112 super.setSortBy(name, order); 113 for(int i = 0; i < models.length; i++) { 114 models[i].setSortBy(name, order); 115 } 116 } 117 118 public void setSortBy(String name) { 119 super.setSortBy(name); 120 for(int i = 0; i < models.length; i++) { 121 models[i].setSortBy(name); 122 } 123 } 124 125 public void setQuery(SearchQuery query) { 126 super.setQuery(query); 127 for(int i = 0; i < models.length; i++) { 128 models[i].setQuery(query); 129 } 130 } 131 132 protected Collection getResult() throws Exception { 133 throw new RuntimeException ("this method should never be called since the iterator creation was overwritten"); 134 } 135 } 136 | Popular Tags |