📄 tableviewer.java
字号:
// earlier associate // e.g. if (a, b) is replaced by (b, a), the disassociate of b to // item 1 could undo // the associate of b to item 0. Object[] children = getSortedChildren(getRoot()); TableItem[] items = getTable().getItems(); int min = Math.min(children.length, items.length); for (int i = 0; i < min; ++i) { TableItem item = items[i]; // if the element is unchanged, update its label if appropriate if (equals(children[i], item.getData())) { if (updateLabels) { updateItem(item, children[i]); } else { // associate the new element, even if equal to the old // one, // to remove stale references (see bug 31314) associate(children[i], item); } } else { // updateItem does an associate(...), which can mess up // the associations if the order of elements has changed. // E.g. (a, b) -> (b, a) first replaces a->0 with b->0, then // replaces b->1 with a->1, but this actually removes b->0. // So, if the object associated with this item has changed, // just disassociate it for now, and update it below. item.setText(""); //$NON-NLS-1$ item.setImage(new Image[Math.max(1,table.getColumnCount())]);//Clear all images disassociate(item); } } // dispose of all items beyond the end of the current elements if (min < items.length) { for (int i = items.length; --i >= min;) { disassociate(items[i]); } table.remove(min, items.length - 1); } // Workaround for 1GDGN4Q: ITPUI:WIN2000 - TableViewer icons get // scrunched if (table.getItemCount() == 0) { table.removeAll(); } // Update items which were removed above for (int i = 0; i < min; ++i) { TableItem item = items[i]; if (item.getData() == null) { updateItem(item, children[i]); } } // add any remaining elements for (int i = min; i < children.length; ++i) { createItem(children[i],i); } } /** * Removes the given elements from this table viewer. * * @param elements * the elements to remove */ private void internalRemove(final Object[] elements) { Object input = getInput(); for (int i = 0; i < elements.length; ++i) { if (equals(elements[i], input)) { setInput(null); return; } } // use remove(int[]) rather than repeated TableItem.dispose() calls // to allow SWT to optimize multiple removals int[] indices = new int[elements.length]; int count = 0; for (int i = 0; i < elements.length; ++i) { Widget w = findItem(elements[i]); if (w instanceof TableItem) { TableItem item = (TableItem) w; disassociate(item); indices[count++] = table.indexOf(item); } } if (count < indices.length) { System.arraycopy(indices, 0, indices = new int[count], 0, count); } table.remove(indices); // Workaround for 1GDGN4Q: ITPUI:WIN2000 - TableViewer icons get // scrunched if (table.getItemCount() == 0) { table.removeAll(); } } /** * Returns whether there is an active cell editor. * * @return <code>true</code> if there is an active cell editor, and * <code>false</code> otherwise */ public boolean isCellEditorActive() { return tableViewerImpl.isCellEditorActive(); } /** * Removes the given elements from this table viewer. The selection is * updated if required. * <p> * This method should be called (by the content provider) when elements have * been removed from the model, in order to cause the viewer to accurately * reflect the model. This method only affects the viewer, not the model. * </p> * * @param elements * the elements to remove */ public void remove(final Object[] elements) { assertElementsNotNull(elements); if (elements.length == 0) { return; } preservingSelection(new Runnable() { public void run() { internalRemove(elements); } }); } /** * Removes the given element from this table viewer. The selection is * updated if necessary. * <p> * This method should be called (by the content provider) when a single * element has been removed from the model, in order to cause the viewer to * accurately reflect the model. This method only affects the viewer, not * the model. Note that there is another method for efficiently processing * the simultaneous removal of multiple elements. * </p> * <strong>NOTE:</strong> removing an object from a virtual * table will decrement the itemCount. * * @param element * the element */ public void remove(Object element) { remove(new Object[] { element }); } /* * (non-Javadoc) * @see org.eclipse.jface.viewers.StructuredViewer#reveal(java.lang.Object) */ public void reveal(Object element) { Assert.isNotNull(element); Widget w = findItem(element); if (w instanceof TableItem) { getTable().showItem((TableItem) w); } } /** * Sets the cell editors of this table viewer. * * @param editors * the list of cell editors */ public void setCellEditors(CellEditor[] editors) { tableViewerImpl.setCellEditors(editors); } /** * Sets the cell modifier of this table viewer. * * @param modifier * the cell modifier */ public void setCellModifier(ICellModifier modifier) { tableViewerImpl.setCellModifier(modifier); } /** * Sets the column properties of this table viewer. The properties must * correspond with the columns of the table control. They are used to * identify the column in a cell modifier. * * @param columnProperties * the list of column properties */ public void setColumnProperties(String[] columnProperties) { tableViewerImpl.setColumnProperties(columnProperties); } /** * The table viewer implementation of this <code>Viewer</code> framework * method ensures that the given label provider is an instance of either * <code>ITableLabelProvider</code> or <code>ILabelProvider</code>. * <p> * If the label provider is an {@link ITableLabelProvider}, then it * provides a separate label text and image for each column. Implementers of * <code>ITableLabelProvider</code> may also implement * {@link ITableColorProvider} and/or {@link ITableFontProvider} to provide * colors and/or fonts. * </p> * <p> * If the label provider is an <code>ILabelProvider</code>, then it * provides only the label text and image for the first column, and any * remaining columns are blank. Implementers of <code>ILabelProvider</code> * may also implement {@link IColorProvider} and/or {@link IFontProvider} to * provide colors and/or fonts. * </p> */ public void setLabelProvider(IBaseLabelProvider labelProvider) { Assert.isTrue(labelProvider instanceof ITableLabelProvider || labelProvider instanceof ILabelProvider); super.setLabelProvider(labelProvider); if(labelProvider instanceof ITableFontProvider || labelProvider instanceof ITableColorProvider) { tableColorAndFont = new TableColorAndFontCollector(labelProvider); } else { tableColorAndFont = new TableColorAndFontNoOp(); } } /** * <p> * Sets a new selection for this viewer and optionally makes it visible. * The TableViewer implmentation of this method is ineffecient for the * ILazyContentProvider as lookup is done by indices rather than elements * and may require population of the entire table in worse case. * </p> * <p> * Use Table#setSelection(int[] indices) and Table#showSelection() if * you wish to set selection more effeciently when using a ILazyContentProvider. * </p> * * @param selection the new selection * @param reveal <code>true</code> if the selection is to be made * visible, and <code>false</code> otherwise * @see Table#setSelection(int[]) * @see Table#showSelection() */ public void setSelection(ISelection selection, boolean reveal) { super.setSelection(selection, reveal); } /* * (non-Javadoc) * @see org.eclipse.jface.viewers.StructuredViewer#setSelectionToWidget(java.util.List, boolean) */ protected void setSelectionToWidget(List list, boolean reveal) { if (list == null) { table.deselectAll(); return; } if(virtualManager != null){ virtualSetSelectionToWidget(list, reveal); return; } int size = list.size(); TableItem[] items = new TableItem[size]; int count = 0; for (int i = 0; i < size; ++i) { Object o = list.get(i); Widget w = findItem(o); if (w instanceof TableItem) { TableItem item = (TableItem) w; items[count++] = item; } } if (count < size) { System.arraycopy(items, 0, items = new TableItem[count], 0, count); } table.setSelection(items); if (reveal) { table.showSelection(); } } /** * Set the selection on a virtual table * @param list The elements to set * @param reveal Whether or not reveal the first item. */ private void virtualSetSelectionToWidget(List list, boolean reveal) { int size = list.size(); int[] indices = new int[list.size()]; TableItem firstItem = null; int count = 0; HashSet virtualElements = new HashSet(); for (int i = 0; i < size; ++i) { Object o = list.get(i); Widget w = findItem(o); if (w instanceof TableItem) { TableItem item = (TableItem) w; indices[count++] = getTable().indexOf(item); if (firstItem == null) { firstItem = item; } } else { virtualElements.add(o); } } if(getContentProvider() instanceof ILazyContentProvider){ ILazyContentProvider provider = (ILazyContentProvider) getContentProvider(); //Now go through it again until all is done or we are no longer virtual //This may create all items so it is not a good //idea in general. //Use #setSelection (int [] indices,boolean reveal) instead for (int i = 0; virtualElements.size() > 0 && i < getTable().getItemCount(); i++) { provider.updateElement(i); TableItem item = getTable().getItem(i); if(virtualElements.contains(item.getData())){ indices[count++] = i; virtualElements.remove(item.getData()); if (firstItem == null) { firstItem = item; } } } } else{ if(count != list.size()){//As this is expensive skip it if all have been found //If it is not lazy we can use the cache for (int i = 0; i < virtualManager.cachedElements.length; i++) { Object element = virtualManager.cachedElements[i]; if(virtualElements.contains(element)){ TableItem item = getTable().getItem(i); item.getText();//Be sure to fire the update indices[count++] = i; virtualElements.remove(element); if (firstItem == null) { firstItem = item; } } } } } if (count < size) { System.arraycopy(indices, 0, indices = new int[count], 0, count); } table.setSelection(indices); if (reveal && firstItem != null) { table.showItem(firstItem); } } /** * Set the item count of the receiver. * @param count the new table size. * * @since 3.1 */ public void setItemCount(int count){ getTable().setItemCount(count); getTable().redraw(); } /** * Replace the entries starting at index with elements. * This method assumes all of these values are correct * and will not call the content provider to verify. * <strong>Note that this method will create a TableItem * for all of the elements provided</strong>. * @param element * @param index * @see ILazyContentProvider * * @since 3.1 */ public void replace(Object element, int index){ TableItem item = getTable().getItem(index); refreshItem(item, element); } /** * Clear the table item at the specified index * @param index the index of the table item to be cleared * * @since 3.1 */ public void clear(int index) { TableItem item = getTable().getItem(index); if (item.getData() != null) { disassociate(item); } table.clear(index); } /* (non-Javadoc) * @see org.eclipse.jface.viewers.StructuredViewer#getRawChildren(java.lang.Object) */ protected Object[] getRawChildren(Object parent) { Assert.isTrue(!(getContentProvider() instanceof ILazyContentProvider),"Cannot get raw children with an ILazyContentProvider");//$NON-NLS-1$ return super.getRawChildren(parent); } /* (non-Javadoc) * @see org.eclipse.jface.viewers.StructuredViewer#assertContentProviderType(org.eclipse.jface.viewers.IContentProvider) */ protected void assertContentProviderType(IContentProvider provider) { Assert.isTrue(provider instanceof IStructuredContentProvider || provider instanceof ILazyContentProvider); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -