KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > viewers > deferred > DeferredContentProvider


1 /*******************************************************************************
2  * Copyright (c) 2004, 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.jface.viewers.deferred;
12
13 import java.util.Comparator JavaDoc;
14
15 import org.eclipse.core.runtime.Assert;
16 import org.eclipse.jface.viewers.AcceptAllFilter;
17 import org.eclipse.jface.viewers.IFilter;
18 import org.eclipse.jface.viewers.ILazyContentProvider;
19 import org.eclipse.jface.viewers.TableViewer;
20 import org.eclipse.jface.viewers.Viewer;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.swt.widgets.Table;
23
24 /**
25  * Content provider that performs sorting and filtering in a background thread.
26  * Requires a <code>TableViewer</code> created with the <code>SWT.VIRTUAL</code>
27  * flag and an <code>IConcurrentModel</code> as input.
28  * <p>
29  * The sorter and filter must be set directly on the content provider.
30  * Any sorter or filter on the TableViewer will be ignored.
31  * </p>
32  *
33  * <p>
34  * The real implementation is in <code>BackgroundContentProvider</code>. This
35  * object is a lightweight wrapper that adapts the algorithm to work with
36  * <code>TableViewer</code>.
37  * </p>
38  *
39  * @since 3.1
40  */

41 public class DeferredContentProvider implements ILazyContentProvider {
42
43     private int limit = -1;
44     private BackgroundContentProvider provider;
45     private Comparator JavaDoc sortOrder;
46     private IFilter filter = AcceptAllFilter.getInstance();
47     private AbstractVirtualTable table;
48     
49     private static final class TableViewerAdapter extends AbstractVirtualTable {
50         
51         private TableViewer viewer;
52         
53         /**
54          * @param viewer
55          */

56         public TableViewerAdapter(TableViewer viewer) {
57             this.viewer = viewer;
58         }
59         
60         /* (non-Javadoc)
61          * @see org.eclipse.jface.viewers.deferred.AbstractVirtualTable#flushCache(java.lang.Object)
62          */

63         public void clear(int index) {
64             viewer.clear(index);
65         }
66         
67         /* (non-Javadoc)
68          * @see org.eclipse.jface.viewers.deferred.AbstractVirtualTable#replace(java.lang.Object, int)
69          */

70         public void replace(Object JavaDoc element, int itemIndex) {
71             viewer.replace(element, itemIndex);
72         }
73         
74         /* (non-Javadoc)
75          * @see org.eclipse.jface.viewers.deferred.AbstractVirtualTable#setItemCount(int)
76          */

77         public void setItemCount(int total) {
78             viewer.setItemCount(total);
79         }
80         
81         /* (non-Javadoc)
82          * @see org.eclipse.jface.viewers.deferred.AbstractVirtualTable#getItemCount()
83          */

84         public int getItemCount() {
85             return viewer.getTable().getItemCount();
86         }
87         
88         /* (non-Javadoc)
89          * @see org.eclipse.jface.viewers.deferred.AbstractVirtualTable#getTopIndex()
90          */

91         public int getTopIndex() {
92             return Math.max(viewer.getTable().getTopIndex() - 1, 0);
93         }
94         
95         /* (non-Javadoc)
96          * @see org.eclipse.jface.viewers.deferred.AbstractVirtualTable#getVisibleItemCount()
97          */

98         public int getVisibleItemCount() {
99             int start = getTopIndex();
100             int itemCount = getItemCount();
101             Table table = viewer.getTable();
102             return Math.min(table.getBounds().height / table.getItemHeight() + 2,
103                     itemCount - start);
104         }
105         
106         /* (non-Javadoc)
107          * @see org.eclipse.jface.viewers.deferred.AbstractVirtualTable#getControl()
108          */

109         public Control getControl() {
110             return viewer.getControl();
111         }
112         
113     }
114
115     /**
116      * Create a DeferredContentProvider with the given sort order.
117      * @param sortOrder a comparator that sorts the content.
118      */

119     public DeferredContentProvider(Comparator JavaDoc sortOrder) {
120         this.sortOrder = sortOrder;
121     }
122
123     /* (non-Javadoc)
124      * @see org.eclipse.jface.viewers.IContentProvider#dispose()
125      */

126     public void dispose() {
127         setProvider(null);
128     }
129
130     /* (non-Javadoc)
131      * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
132      */

133     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
134         if (newInput == null) {
135             setProvider(null);
136             return;
137         }
138         
139         Assert.isTrue(newInput instanceof IConcurrentModel);
140         Assert.isTrue(viewer instanceof TableViewer);
141         IConcurrentModel model = (IConcurrentModel)newInput;
142         
143         this.table = new TableViewerAdapter((TableViewer)viewer);
144         
145         BackgroundContentProvider newProvider = new BackgroundContentProvider(
146                 table,
147                 model, sortOrder);
148         
149         setProvider(newProvider);
150         
151         newProvider.setLimit(limit);
152         newProvider.setFilter(filter);
153     }
154     
155     /**
156      * Sets the sort order for this content provider. This sort order takes priority
157      * over anything that was supplied to the <code>TableViewer</code>.
158      *
159      * @param sortOrder new sort order. The comparator must be able to support being
160      * used in a background thread.
161      */

162     public void setSortOrder(Comparator JavaDoc sortOrder) {
163         Assert.isNotNull(sortOrder);
164         this.sortOrder = sortOrder;
165         if (provider != null) {
166             provider.setSortOrder(sortOrder);
167         }
168     }
169     
170     /**
171      * Sets the filter for this content provider. This filter takes priority over
172      * anything that was supplied to the <code>TableViewer</code>. The filter
173      * must be capable of being used in a background thread.
174      *
175      * @param toSet filter to set
176      */

177     public void setFilter(IFilter toSet) {
178         this.filter = toSet;
179         if (provider != null) {
180             provider.setFilter(toSet);
181         }
182     }
183     
184     /**
185      * Sets the maximum number of rows in the table. If the model contains more
186      * than this number of elements, only the top elements will be shown based on
187      * the current sort order.
188      *
189      * @param limit maximum number of rows to show or -1 if unbounded
190      */

191     public void setLimit(int limit) {
192         this.limit = limit;
193         if (provider != null) {
194             provider.setLimit(limit);
195         }
196     }
197     
198     /**
199      * Returns the current maximum number of rows or -1 if unbounded
200      *
201      * @return the current maximum number of rows or -1 if unbounded
202      */

203     public int getLimit() {
204         return limit;
205     }
206     
207     /* (non-Javadoc)
208      * @see org.eclipse.jface.viewers.ILazyContentProvider#updateElement(int)
209      */

210     public void updateElement(int element) {
211         if (provider != null) {
212             provider.checkVisibleRange(element);
213         }
214     }
215     
216     private void setProvider(BackgroundContentProvider newProvider) {
217         if (provider != null) {
218             provider.dispose();
219         }
220         
221         provider = newProvider;
222     }
223
224 }
225
Popular Tags