1 14 15 package org.eclipse.jface.viewers; 16 17 18 import org.eclipse.core.runtime.Assert; 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.graphics.Point; 21 import org.eclipse.swt.widgets.Composite; 22 import org.eclipse.swt.widgets.Control; 23 import org.eclipse.swt.widgets.Item; 24 import org.eclipse.swt.widgets.Table; 25 import org.eclipse.swt.widgets.TableItem; 26 import org.eclipse.swt.widgets.Widget; 27 28 60 public class TableViewer extends AbstractTableViewer { 61 64 private Table table; 65 66 69 private TableViewerRow cachedRow; 70 71 81 public TableViewer(Composite parent) { 82 this(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER); 83 } 84 85 96 public TableViewer(Composite parent, int style) { 97 this(new Table(parent, style)); 98 } 99 100 108 public TableViewer(Table table) { 109 this.table = table; 110 hookControl(table); 111 } 112 113 public Control getControl() { 114 return table; 115 } 116 117 122 public Table getTable() { 123 return table; 124 } 125 126 protected ColumnViewerEditor createViewerEditor() { 127 return new TableViewerEditor(this,null,new ColumnViewerEditorActivationStrategy(this),ColumnViewerEditor.DEFAULT); 128 } 129 130 150 public void setSelection(ISelection selection, boolean reveal) { 151 super.setSelection(selection, reveal); 152 } 153 154 protected ViewerRow getViewerRowFromItem(Widget item) { 155 if( cachedRow == null ) { 156 cachedRow = new TableViewerRow((TableItem) item); 157 } else { 158 cachedRow.setItem((TableItem) item); 159 } 160 161 return cachedRow; 162 } 163 164 172 protected ViewerRow internalCreateNewRowPart(int style, int rowIndex) { 173 TableItem item; 174 175 if (rowIndex >= 0) { 176 item = new TableItem(table, style, rowIndex); 177 } else { 178 item = new TableItem(table, style); 179 } 180 181 return getViewerRowFromItem(item); 182 } 183 184 protected Item getItemAt(Point p) { 185 TableItem[] selection = table.getSelection(); 186 187 if( selection.length == 1 ) { 188 int columnCount = table.getColumnCount(); 189 190 for( int i = 0; i < columnCount; i++ ) { 191 if( selection[0].getBounds(i).contains(p) ) { 192 return selection[0]; 193 } 194 } 195 } 196 197 return table.getItem(p); 198 } 199 200 202 protected int doGetItemCount() { 203 return table.getItemCount(); 204 } 205 206 protected int doIndexOf(Item item) { 207 return table.indexOf((TableItem)item); 208 } 209 210 protected void doSetItemCount(int count) { 211 table.setItemCount(count); 212 } 213 214 protected Item[] doGetItems() { 215 return table.getItems(); 216 } 217 218 protected int doGetColumnCount() { 219 return table.getColumnCount(); 220 } 221 222 protected Widget doGetColumn(int index) { 223 return table.getColumn(index); 224 } 225 226 protected Item doGetItem(int index) { 227 return table.getItem(index); 228 } 229 230 protected Item[] doGetSelection() { 231 return table.getSelection(); 232 } 233 234 protected int[] doGetSelectionIndices() { 235 return table.getSelectionIndices(); 236 } 237 238 protected void doClearAll() { 239 table.clearAll(); 240 } 241 242 protected void doResetItem(Item item) { 243 TableItem tableItem = (TableItem) item; 244 int columnCount = Math.max(1, table.getColumnCount()); 245 for (int i = 0; i < columnCount; i++) { 246 tableItem.setText(i, ""); if (tableItem.getImage(i) != null) { 248 tableItem.setImage(i, null); 249 } 250 } 251 } 252 253 protected void doRemove(int start, int end) { 254 table.remove(start, end); 255 } 256 257 protected void doRemoveAll() { 258 table.removeAll(); 259 } 260 261 protected void doRemove(int[] indices) { 262 table.remove(indices); 263 } 264 265 protected void doShowItem(Item item) { 266 table.showItem((TableItem)item); 267 } 268 269 protected void doDeselectAll() { 270 table.deselectAll(); 271 } 272 273 protected void doSetSelection(Item[] items) { 274 Assert.isNotNull(items, "Items-Array can not be null"); 276 TableItem[] t = new TableItem[items.length]; 277 System.arraycopy(items, 0, t, 0, t.length); 278 279 table.setSelection(t); 280 } 281 282 protected void doShowSelection() { 283 table.showSelection(); 284 } 285 286 protected void doSetSelection(int[] indices) { 287 table.setSelection(indices); 288 } 289 290 protected void doClear(int index) { 291 table.clear(index); 292 } 293 294 protected void doSelect(int[] indices) { 295 table.select(indices); 296 } 297 298 326 public void refresh(final Object element, final boolean updateLabels, 327 boolean reveal) { 328 if (isBusy()) 329 return; 330 331 if( isCellEditorActive() ) { 332 cancelEditing(); 333 } 334 335 preservingSelection(new Runnable () { 336 public void run() { 337 internalRefresh(element, updateLabels); 338 } 339 }, reveal); 340 } 341 342 366 public void refresh(boolean updateLabels, boolean reveal) { 367 refresh(getRoot(), updateLabels, reveal); 368 } 369 } 370 | Popular Tags |