1 11 package org.eclipse.search2.internal.ui; 12 13 import java.util.ArrayList ; 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 import java.util.LinkedList ; 17 import java.util.List ; 18 import java.util.Set ; 19 20 import org.eclipse.search.ui.IQueryListener; 21 import org.eclipse.search.ui.ISearchQuery; 22 23 class QueryManager { 24 private List fQueries; 25 private List fListeners; 26 27 public QueryManager() { 28 super(); 29 fListeners= new ArrayList (); 31 fQueries= new LinkedList (); 32 } 33 34 public boolean hasQueries() { 35 synchronized (this) { 36 return !fQueries.isEmpty(); 37 } 38 } 39 40 public int getSize() { 41 synchronized (this) { 42 return fQueries.size(); 43 } 44 } 45 46 public ISearchQuery[] getQueries() { 47 synchronized (this) { 48 return (ISearchQuery[]) fQueries.toArray(new ISearchQuery[fQueries.size()]); 49 } 50 } 51 52 public void removeQuery(ISearchQuery query) { 53 synchronized (this) { 54 fQueries.remove(query); 55 } 56 fireRemoved(query); 57 } 58 59 public void addQuery(ISearchQuery query) { 60 synchronized (this) { 61 if (fQueries.contains(query)) 62 return; 63 fQueries.add(0, query); 64 } 65 fireAdded(query); 66 } 67 68 public void addQueryListener(IQueryListener l) { 69 synchronized (fListeners) { 70 fListeners.add(l); 71 } 72 } 73 74 public void removeQueryListener(IQueryListener l) { 75 synchronized (fListeners) { 76 fListeners.remove(l); 77 } 78 } 79 80 public void fireAdded(ISearchQuery query) { 81 Set copiedListeners= new HashSet (); 82 synchronized (fListeners) { 83 copiedListeners.addAll(fListeners); 84 } 85 Iterator listeners= copiedListeners.iterator(); 86 while (listeners.hasNext()) { 87 IQueryListener l= (IQueryListener) listeners.next(); 88 l.queryAdded(query); 89 } 90 } 91 92 public void fireRemoved(ISearchQuery query) { 93 Set copiedListeners= new HashSet (); 94 synchronized (fListeners) { 95 copiedListeners.addAll(fListeners); 96 } 97 Iterator listeners= copiedListeners.iterator(); 98 while (listeners.hasNext()) { 99 IQueryListener l= (IQueryListener) listeners.next(); 100 l.queryRemoved(query); 101 } 102 } 103 104 public void fireStarting(ISearchQuery query) { 105 Set copiedListeners= new HashSet (); 106 synchronized (fListeners) { 107 copiedListeners.addAll(fListeners); 108 } 109 Iterator listeners= copiedListeners.iterator(); 110 while (listeners.hasNext()) { 111 IQueryListener l= (IQueryListener) listeners.next(); 112 l.queryStarting(query); 113 } 114 } 115 116 public void fireFinished(ISearchQuery query) { 117 Set copiedListeners= new HashSet (); 118 synchronized (fListeners) { 119 copiedListeners.addAll(fListeners); 120 } 121 Iterator listeners= copiedListeners.iterator(); 122 while (listeners.hasNext()) { 123 IQueryListener l= (IQueryListener) listeners.next(); 124 l.queryFinished(query); 125 } 126 } 127 128 public void removeAll() { 129 synchronized (this) { 130 List old= fQueries; 131 fQueries= new LinkedList (); 132 Iterator iter= old.iterator(); 133 while (iter.hasNext()) { 134 ISearchQuery element= (ISearchQuery) iter.next(); 135 fireRemoved(element); 136 } 137 } 138 } 139 140 public void queryFinished(ISearchQuery query) { 141 fireFinished(query); 142 } 143 144 public void queryStarting(ISearchQuery query) { 145 fireStarting(query); 146 } 147 148 public void touch(ISearchQuery query) { 149 synchronized (this) { 150 if (fQueries.contains(query)) { 151 fQueries.remove(query); 152 fQueries.add(0, query); 153 } 154 } 155 } 156 157 public ISearchQuery getOldestQuery() { 158 synchronized (this) { 159 if (!fQueries.isEmpty()) { 160 return (ISearchQuery) fQueries.get(fQueries.size()-1); 161 } 162 } 163 return null; 164 } 165 166 } 167 | Popular Tags |