2 // TileHitCollection.cs
4 // Copyright (C) 2004 Novell, Inc.
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.
28 using System
.Collections
;
31 namespace Beagle
.Tile
{
33 [TileStyle (Resource
="template-page.css")]
34 public class TileHitCollection
: Tile
{
36 public TileHitCollection () {
39 private class HitTilePair
: IComparable
{
43 public HitTilePair (Hit _hit
, Tile _tile
)
49 public Beagle
.Hit Hit
{
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
{
74 if (all_hits
.Count
== 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); }
102 public void PageFirst ()
109 public void PageForward ()
111 firstDisplayed
+= maxDisplayed
;
112 if (firstDisplayed
< 0)
117 public bool CanPageBack
{
118 get { return FirstDisplayed > 0; }
122 public void PageBack ()
124 firstDisplayed
-= maxDisplayed
;
125 if (firstDisplayed
< 0)
132 bool changed
= false;
134 if (all_hits
.Count
> 0) {
139 if (hits
.Count
> 0) {
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
) {
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
) {
174 if (SourceIsDisplayable (hit
)) {
175 if (InsertDisplayable (pair
))
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
);
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
) {
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 ();
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
);
228 Console
.WriteLine ("{0} -- {1}", pair
.Hit
.Type
, pair
.Hit
.Uri
);
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
))
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
)
256 int j
= hits
.BinarySearch (pair
);
258 hits
.Insert (j
< 0 ? ~j
: j
, pair
);
259 if (j
== 0 || j
< LastDisplayed
)
264 public void ClearSources (string source
)
266 if (hitSources
.Count
== 0)
274 public bool SourceIsDisplayable (Hit hit
)
276 if (hitSources
.Count
== 0)
279 if (hitSources
.IndexOf (hit
.Type
) >= 0)
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
);
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 ());
312 if (foot_template
== null) {
313 foot_template
= new Template ("template-foot.html");
314 PopulateTemplate (foot_template
);
316 ctx
.Write (foot_template
.ToString ());