1 11 package org.eclipse.jface.viewers.deferred; 12 13 import java.util.Comparator ; 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 41 public class DeferredContentProvider implements ILazyContentProvider { 42 43 private int limit = -1; 44 private BackgroundContentProvider provider; 45 private Comparator 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 56 public TableViewerAdapter(TableViewer viewer) { 57 this.viewer = viewer; 58 } 59 60 63 public void clear(int index) { 64 viewer.clear(index); 65 } 66 67 70 public void replace(Object element, int itemIndex) { 71 viewer.replace(element, itemIndex); 72 } 73 74 77 public void setItemCount(int total) { 78 viewer.setItemCount(total); 79 } 80 81 84 public int getItemCount() { 85 return viewer.getTable().getItemCount(); 86 } 87 88 91 public int getTopIndex() { 92 return Math.max(viewer.getTable().getTopIndex() - 1, 0); 93 } 94 95 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 109 public Control getControl() { 110 return viewer.getControl(); 111 } 112 113 } 114 115 119 public DeferredContentProvider(Comparator sortOrder) { 120 this.sortOrder = sortOrder; 121 } 122 123 126 public void dispose() { 127 setProvider(null); 128 } 129 130 133 public void inputChanged(Viewer viewer, Object oldInput, Object 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 162 public void setSortOrder(Comparator sortOrder) { 163 Assert.isNotNull(sortOrder); 164 this.sortOrder = sortOrder; 165 if (provider != null) { 166 provider.setSortOrder(sortOrder); 167 } 168 } 169 170 177 public void setFilter(IFilter toSet) { 178 this.filter = toSet; 179 if (provider != null) { 180 provider.setFilter(toSet); 181 } 182 } 183 184 191 public void setLimit(int limit) { 192 this.limit = limit; 193 if (provider != null) { 194 provider.setLimit(limit); 195 } 196 } 197 198 203 public int getLimit() { 204 return limit; 205 } 206 207 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 |