KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > search > JavaSearchTableContentProvider


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.jdt.internal.ui.search;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.swt.widgets.Table;
17
18 import org.eclipse.jface.viewers.IStructuredContentProvider;
19 import org.eclipse.jface.viewers.TableViewer;
20
21 public class JavaSearchTableContentProvider extends JavaSearchContentProvider implements IStructuredContentProvider {
22     public JavaSearchTableContentProvider(JavaSearchResultPage page) {
23         super(page);
24     }
25     public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
26         if (inputElement instanceof JavaSearchResult) {
27             Set JavaDoc filteredElements= new HashSet JavaDoc();
28             Object JavaDoc[] rawElements= ((JavaSearchResult)inputElement).getElements();
29             int limit= getPage().getElementLimit().intValue();
30             for (int i= 0; i < rawElements.length; i++) {
31                 if (getPage().getDisplayedMatchCount(rawElements[i]) > 0) {
32                     filteredElements.add(rawElements[i]);
33                     if (limit != -1 && limit < filteredElements.size()) {
34                         break;
35                     }
36                 }
37             }
38             return filteredElements.toArray();
39         }
40         return EMPTY_ARR;
41     }
42
43     public void elementsChanged(Object JavaDoc[] updatedElements) {
44         if (getSearchResult() == null)
45             return;
46         
47         int addCount= 0;
48         int removeCount= 0;
49         int addLimit= getAddLimit();
50         
51         TableViewer viewer= (TableViewer) getPage().getViewer();
52         Set JavaDoc updated= new HashSet JavaDoc();
53         Set JavaDoc added= new HashSet JavaDoc();
54         Set JavaDoc removed= new HashSet JavaDoc();
55         for (int i= 0; i < updatedElements.length; i++) {
56             if (getPage().getDisplayedMatchCount(updatedElements[i]) > 0) {
57                 if (viewer.testFindItem(updatedElements[i]) != null)
58                     updated.add(updatedElements[i]);
59                 else {
60                     if (addLimit > 0) {
61                         added.add(updatedElements[i]);
62                         addLimit--;
63                         addCount++;
64                     }
65                 }
66             } else {
67                 removed.add(updatedElements[i]);
68                 removeCount++;
69             }
70         }
71         
72         viewer.add(added.toArray());
73         viewer.update(updated.toArray(), new String JavaDoc[] { SearchLabelProvider.PROPERTY_MATCH_COUNT });
74         viewer.remove(removed.toArray());
75     }
76
77     private int getAddLimit() {
78         int limit= getPage().getElementLimit().intValue();
79         if (limit != -1) {
80             Table table= (Table) getPage().getViewer().getControl();
81             int itemCount= table.getItemCount();
82             if (itemCount >= limit) {
83                 return 0;
84             }
85             return limit - itemCount;
86         }
87         return Integer.MAX_VALUE;
88     }
89     
90     public void clear() {
91         getPage().getViewer().refresh();
92     }
93
94 }
95
Popular Tags