QueryResponses.cs, DumpIndex.cs, IQueryResult.cs, QueryExecutor.cs, QueryResult.cs...
[beagle.git] / Tiles / TileHitCollection.cs
blobe63ca3828ac55f8296db08c81e5a5a632fca75c2
1 //
2 // TileHitCollection.cs
3 //
4 // Copyright (C) 2004 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
27 using System;
28 using System.Collections;
29 using Beagle.Util;
31 namespace Beagle.Tile {
33 [TileStyle (Resource="template-page.css")]
34 public class TileHitCollection : Tile {
36 public TileHitCollection () {
39 private class HitTilePair : IComparable {
40 Hit hit;
41 Tile tile;
43 public HitTilePair (Hit _hit, Tile _tile)
45 hit = _hit;
46 tile = _tile;
49 public Beagle.Hit Hit {
50 get { return hit; }
53 public Tile Tile {
54 get { return tile; }
57 public int CompareTo (object obj)
59 HitTilePair other = (HitTilePair) obj;
60 return hit.CompareTo (other.hit);
64 private ArrayList all_hits = new ArrayList ();
65 private ArrayList hits = new ArrayList ();
66 int firstDisplayed = 0;
67 int maxDisplayed = Conf.Searching.MaxDisplayed;
69 Template head_template;
70 Template foot_template;
72 public double MaxScore {
73 get {
74 if (all_hits.Count == 0)
75 return 0;
76 HitTilePair pair = (HitTilePair) all_hits [0];
77 return pair.Hit.Score;
81 public int FirstDisplayed {
82 get { return firstDisplayed; }
85 public int LastDisplayed {
86 get { return Math.Min (firstDisplayed + maxDisplayed, hits.Count) - 1; }
89 public int NumDisplayableResults {
90 get { return hits.Count; }
93 public int NumResults {
94 get { return all_hits.Count; }
97 public bool CanPageForward {
98 get { return LastDisplayed != (hits.Count - 1); }
101 [TileAction]
102 public void PageFirst ()
104 firstDisplayed = 0;
105 Changed ();
108 [TileAction]
109 public void PageForward ()
111 firstDisplayed += maxDisplayed;
112 if (firstDisplayed < 0)
113 firstDisplayed = 0;
114 Changed ();
117 public bool CanPageBack {
118 get { return FirstDisplayed > 0; }
121 [TileAction]
122 public void PageBack ()
124 firstDisplayed -= maxDisplayed;
125 if (firstDisplayed < 0)
126 firstDisplayed = 0;
127 Changed ();
130 public void Clear ()
132 bool changed = false;
133 lock (this) {
134 if (all_hits.Count > 0) {
135 all_hits.Clear ();
136 firstDisplayed = 0;
137 changed = true;
139 if (hits.Count > 0) {
140 hits.Clear ();
141 firstDisplayed = 0;
142 changed = true;
145 if (changed)
146 Changed ();
149 private bool InsertDisplayable (HitTilePair pair)
151 int i = hits.BinarySearch (pair);
153 hits.Insert (i < 0 ? ~i : i, pair);
154 if (i == 0 || i < LastDisplayed) {
155 Changed ();
156 return true;
159 return false;
162 public bool Add (Hit hit, Tile tile)
164 bool changed = false;
166 HitTilePair pair = new HitTilePair (hit, tile);
167 int i = all_hits.BinarySearch (pair);
168 all_hits.Insert (i < 0 ? ~i : i, pair);
169 if (i == 0 || i < LastDisplayed) {
170 Changed ();
171 changed = true;
174 if (SourceIsDisplayable (hit)) {
175 if (InsertDisplayable (pair))
176 changed = true;
179 return changed;
182 public bool Subtract (Uri uri)
184 bool removed = false;
186 for (int i = 0; i < all_hits.Count; ++i) {
187 HitTilePair pair = (HitTilePair) all_hits [i];
188 if (pair.Hit.Uri.Equals (uri) && pair.Hit.Uri.Fragment == uri.Fragment) {
189 all_hits.Remove (pair);
190 removed = true;
191 break;
195 for (int i = 0; i < hits.Count; ++i) {
196 HitTilePair pair = (HitTilePair) hits [i];
197 if (pair.Hit.Uri.Equals (uri) && pair.Hit.Uri.Fragment == uri.Fragment) {
198 hits.Remove (pair);
199 break;
203 return removed;
206 public bool IsEmpty {
207 get { return all_hits.Count == 0; }
210 private ArrayList hitSources = new ArrayList ();
212 public void SetSource (string source)
214 Console.WriteLine ("SetSource: {0}", source);
216 hitSources = new ArrayList ();
218 if (source != null)
219 hitSources.Add (source);
221 hits = new ArrayList ();
222 for (int i = 0; i < NumResults; i ++) {
223 HitTilePair pair = (HitTilePair) all_hits [i];
225 if (SourceIsDisplayable (pair.Hit)) {
226 InsertDisplayable (pair);
227 } else
228 Console.WriteLine ("{0} -- {1}", pair.Hit.Type, pair.Hit.Uri);
231 Changed ();
234 public void AddSource (string source)
236 hitSources.Add (source);
238 for (int i = 0; i < NumDisplayableResults; i ++) {
239 HitTilePair pair = (HitTilePair) hits [i];
241 if (! SourceIsDisplayable (pair.Hit))
242 hits.RemoveAt (i);
246 public void SubtractSource (string source)
248 hitSources.Remove (source);
250 for (int i = 0; i < NumResults; i ++) {
251 HitTilePair pair = (HitTilePair) hits [i];
253 if (pair.Hit.Source != source)
254 continue;
256 int j = hits.BinarySearch (pair);
258 hits.Insert (j < 0 ? ~j : j, pair);
259 if (j == 0 || j < LastDisplayed)
260 Changed ();
264 public void ClearSources (string source)
266 if (hitSources.Count == 0)
267 return;
269 hitSources.Clear ();
271 Changed ();
274 public bool SourceIsDisplayable (Hit hit)
276 if (hitSources.Count == 0)
277 return true;
279 if (hitSources.IndexOf (hit.Type) >= 0)
280 return true;
282 return false;
285 private void RenderTiles (TileRenderContext ctx)
287 int i = FirstDisplayed;
288 int i1 = LastDisplayed;
290 while (i <= i1 && i < NumResults) {
291 HitTilePair pair = (HitTilePair) hits [i];
292 ctx.Tile (pair.Tile);
293 ++i;
297 private void PopulateTemplate (Template t)
301 public override void Render (TileRenderContext ctx)
303 if (head_template == null) {
304 head_template = new Template ("template-head.html");
305 PopulateTemplate (head_template);
308 ctx.Write (head_template.ToString ());
310 RenderTiles (ctx);
312 if (foot_template == null) {
313 foot_template = new Template ("template-foot.html");
314 PopulateTemplate (foot_template);
316 ctx.Write (foot_template.ToString ());