KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > internal > ui > text > FileTableContentProvider


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.search.internal.ui.text;
12
13 import org.eclipse.jface.viewers.IStructuredContentProvider;
14 import org.eclipse.jface.viewers.TableViewer;
15 import org.eclipse.jface.viewers.Viewer;
16
17 import org.eclipse.search.ui.text.AbstractTextSearchResult;
18
19 public class FileTableContentProvider implements IStructuredContentProvider, IFileSearchContentProvider {
20     
21     private final Object JavaDoc[] EMPTY_ARR= new Object JavaDoc[0];
22     
23     private FileSearchPage fPage;
24     private AbstractTextSearchResult fResult;
25
26     public FileTableContentProvider(FileSearchPage page) {
27         fPage= page;
28     }
29     
30     public void dispose() {
31         // nothing to do
32
}
33     
34     public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
35         if (inputElement instanceof FileSearchResult) {
36             int elementLimit= getElementLimit();
37             Object JavaDoc[] elements= ((FileSearchResult)inputElement).getElements();
38             if (elementLimit != -1 && elements.length > elementLimit) {
39                 Object JavaDoc[] shownElements= new Object JavaDoc[elementLimit];
40                 System.arraycopy(elements, 0, shownElements, 0, elementLimit);
41                 return shownElements;
42             }
43             return elements;
44         }
45         return EMPTY_ARR;
46     }
47     
48     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
49         if (newInput instanceof FileSearchResult) {
50             fResult= (FileSearchResult) newInput;
51         }
52     }
53     
54     public void elementsChanged(Object JavaDoc[] updatedElements) {
55         TableViewer viewer= getViewer();
56         int elementLimit= getElementLimit();
57         boolean tableLimited= elementLimit != -1;
58         for (int i= 0; i < updatedElements.length; i++) {
59             if (fResult.getMatchCount(updatedElements[i]) > 0) {
60                 if (viewer.testFindItem(updatedElements[i]) != null)
61                     viewer.update(updatedElements[i], null);
62                 else {
63                     if (!tableLimited || viewer.getTable().getItemCount() < elementLimit)
64                         viewer.add(updatedElements[i]);
65                 }
66             } else
67                 viewer.remove(updatedElements[i]);
68         }
69     }
70
71     private int getElementLimit() {
72         return fPage.getElementLimit().intValue();
73     }
74
75     private TableViewer getViewer() {
76         return (TableViewer) fPage.getViewer();
77     }
78     
79     public void clear() {
80         getViewer().refresh();
81     }
82 }
83
Popular Tags