KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search2 > internal > ui > QueryManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.search2.internal.ui;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.LinkedList JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import org.eclipse.search.ui.IQueryListener;
21 import org.eclipse.search.ui.ISearchQuery;
22
23 class QueryManager {
24     private List JavaDoc fQueries;
25     private List JavaDoc fListeners;
26     
27     public QueryManager() {
28         super();
29         // an ArrayList should be plenty fast enough (few searches).
30
fListeners= new ArrayList JavaDoc();
31         fQueries= new LinkedList JavaDoc();
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 JavaDoc copiedListeners= new HashSet JavaDoc();
82         synchronized (fListeners) {
83             copiedListeners.addAll(fListeners);
84         }
85         Iterator JavaDoc 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 JavaDoc copiedListeners= new HashSet JavaDoc();
94         synchronized (fListeners) {
95             copiedListeners.addAll(fListeners);
96         }
97         Iterator JavaDoc 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 JavaDoc copiedListeners= new HashSet JavaDoc();
106         synchronized (fListeners) {
107             copiedListeners.addAll(fListeners);
108         }
109         Iterator JavaDoc 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 JavaDoc copiedListeners= new HashSet JavaDoc();
118         synchronized (fListeners) {
119             copiedListeners.addAll(fListeners);
120         }
121         Iterator JavaDoc 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 JavaDoc old= fQueries;
131             fQueries= new LinkedList JavaDoc();
132             Iterator JavaDoc 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