📄 tableviewer.java
字号:
/******************************************************************************* * Copyright (c) 2000, 2006 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/package org.eclipse.jface.viewers;import java.util.ArrayList;import java.util.HashSet;import java.util.List;import org.eclipse.jface.util.Assert;import org.eclipse.swt.SWT;import org.eclipse.swt.custom.TableEditor;import org.eclipse.swt.events.MouseAdapter;import org.eclipse.swt.events.MouseEvent;import org.eclipse.swt.graphics.Image;import org.eclipse.swt.graphics.Rectangle;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Control;import org.eclipse.swt.widgets.Event;import org.eclipse.swt.widgets.Item;import org.eclipse.swt.widgets.Listener;import org.eclipse.swt.widgets.Table;import org.eclipse.swt.widgets.TableItem;import org.eclipse.swt.widgets.Widget;/** * A concrete viewer based on a SWT <code>Table</code> control. * <p> * This class is not intended to be subclassed outside the viewer framework. It * is designed to be instantiated with a pre-existing SWT table control and * configured with a domain-specific content provider, table label provider, * element filter (optional), and element sorter (optional). * </p> * <p> * Label providers for table viewers must implement either the * <code>ITableLabelProvider</code> or the <code>ILabelProvider</code> * interface (see <code>TableViewer.setLabelProvider</code> for more details). * </p> * <p> * As of 3.1 the TableViewer now supports the SWT.VIRTUAL flag. If the * underlying table is SWT.VIRTUAL, the content provider may implement * {@link ILazyContentProvider} instead of {@link IStructuredContentProvider}. * Note that in this case, the viewer does not support sorting or filtering. * Also note that in this case, the Widget based APIs may return null if the * element is not specified or not created yet. * </p> * <p> * Users of SWT.VIRTUAL should also avoid using getItems() from the Table within * the TreeViewer as this does not necessarily generate a callback for the * TreeViewer to populate the items. It also has the side effect of creating all * of the items thereby eliminating the performance improvements of SWT.VIRTUAL. * </p> * * @see SWT#VIRTUAL * @see #doFindItem(Object) * @see #internalRefresh(Object, boolean) */public class TableViewer extends StructuredViewer { private class VirtualManager{ /** * The currently invisible elements as provided * by the content provider or by addition. * This will not be populated by an ILazyStructuredContentProvider * as an ILazyStructuredContentProvider is only queried * on the virtual callabck. */ private Object[] cachedElements = new Object[0]; /** * Create a new instance of the receiver. * */ public VirtualManager(){ addTableListener(); } /** * Add the listener for SetData on the table */ private void addTableListener() { table.addListener(SWT.SetData,new Listener(){ /* (non-Javadoc) * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event) */ public void handleEvent(Event event) { TableItem item = (TableItem) event.item; final int index = table.indexOf(item); Object element = resolveElement(index); if(element == null){ //Didn't find it so make a request //Keep looking if it is not in the cache. IContentProvider contentProvider = getContentProvider(); //If we are building lazily then request lookup now if(contentProvider instanceof ILazyContentProvider){ ((ILazyContentProvider) contentProvider). updateElement(index); return; } } associate(element,item); updateItem(item,element); } }); } /** * Get the element at index.Resolve it lazily if this * is available. * @param index * @return Object or <code>null</code> if it could * not be found */ protected Object resolveElement(int index) { Object element = null; if(index < cachedElements.length) { element = cachedElements[index]; } return element; } /** * A non visible item has been added. * @param element * @param index */ public void notVisibleAdded(Object element, int index) { int requiredCount = index + 1; if(requiredCount > getTable().getItemCount()){ getTable().setItemCount(requiredCount); Object[] newCache = new Object[requiredCount]; System.arraycopy(cachedElements, 0, newCache, 0, cachedElements.length); cachedElements = newCache; } cachedElements[index] = element; } } private VirtualManager virtualManager; /** * TableColorAndFontNoOp is an optimization for tables without * color and font support. * @see ITableColorProvider * @see ITableFontProvider */ private class TableColorAndFontNoOp{ /** * Create a new instance of the receiver. * */ TableColorAndFontNoOp(){ } /** * Set the fonts and colors for the tableItem if there is a color * and font provider available. * @param tableItem The item to update. * @param element The element being represented * @param column The column index */ public void setFontsAndColors(TableItem tableItem, Object element, int column){ } } /** * TableColorAndFontCollector is an helper class for color and font * support for tables that support the ITableFontProvider and * the ITableColorProvider. * @see ITableColorProvider * @see ITableFontProvider */ private class TableColorAndFontCollector extends TableColorAndFontNoOp{ ITableFontProvider fontProvider = null; ITableColorProvider colorProvider = null; /** * Create an instance of the receiver. Set the color and font * providers if provider can be cast to the correct type. * @param provider IBaseLabelProvider */ public TableColorAndFontCollector(IBaseLabelProvider provider){ if(provider instanceof ITableFontProvider) { fontProvider = (ITableFontProvider) provider; } if(provider instanceof ITableColorProvider) { colorProvider = (ITableColorProvider) provider; } } /** * Set the fonts and colors for the tableItem if there is a color * and font provider available. * @param tableItem The item to update. * @param element The element being represented * @param column The column index */ public void setFontsAndColors(TableItem tableItem, Object element, int column){ if (colorProvider != null) { tableItem.setBackground(column, colorProvider.getBackground(element, column)); tableItem.setForeground(column, colorProvider.getForeground(element, column)); } if(fontProvider != null) { tableItem.setFont(column,fontProvider.getFont(element,column)); } } } /** * Internal table viewer implementation. */ private TableEditorImpl tableViewerImpl; /** * This viewer's table control. */ private Table table; /** * This viewer's table editor. */ private TableEditor tableEditor; /** * The color and font collector for the cells. */ private TableColorAndFontNoOp tableColorAndFont = new TableColorAndFontNoOp(); /** * Creates a table viewer on a newly-created table control under the given * parent. The table control is created using the SWT style bits * <code>MULTI, H_SCROLL, V_SCROLL,</code> and <code>BORDER</code>. The * viewer has no input, no content provider, a default label provider, no * sorter, and no filters. The table has no columns. * * @param parent * the parent control */ public TableViewer(Composite parent) { this(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER); } /** * Creates a table viewer on a newly-created table control under the given * parent. The table control is created using the given style bits. The * viewer has no input, no content provider, a default label provider, no * sorter, and no filters. The table has no columns. * * @param parent * the parent control * @param style * SWT style bits */ public TableViewer(Composite parent, int style) { this(new Table(parent, style)); } /** * Creates a table viewer on the given table control. The viewer has no * input, no content provider, a default label provider, no sorter, and no * filters. * * @param table * the table control */ public TableViewer(Table table) { this.table = table; hookControl(table); tableEditor = new TableEditor(table); initTableViewerImpl(); initializeVirtualManager(table.getStyle()); } /** * Initialize the virtual manager to manage the virtual state * if the table is VIRTUAL. If not use the default no-op * version. * @param style */ private void initializeVirtualManager(int style) { if((style & SWT.VIRTUAL) == 0) { return; } virtualManager = new VirtualManager(); } /** * Adds the given elements to this table viewer. If this viewer does not * have a sorter, the elements are added at the end in the order given; * otherwise the elements are inserted at appropriate positions. * <p> * This method should be called (by the content provider) when elements have * been added to 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 add */ public void add(Object[] elements) { assertElementsNotNull(elements); Object[] filtered = filter(elements); for (int i = 0; i < filtered.length; i++) { Object element = filtered[i]; int index = indexForElement(element); createItem(element, index); } } /** * Create a new TableItem at index if required. * @param element * @param index * * @since 3.1 */ private void createItem(Object element, int index) { if(virtualManager == null) { updateItem(new TableItem(getTable(), SWT.NONE, index), element); } else{ virtualManager.notVisibleAdded(element,index); } } /** * Adds the given element to this table viewer. If this viewer does not have * a sorter, the element is added at the end; otherwise the element is * inserted at the appropriate position. * <p> * This method should be called (by the content provider) when a single * element has been added to 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 addition of multiple elements. * </p> * * @param element * the element to add */ public void add(Object element) { add(new Object[] { element }); } /** * Cancels a currently active cell editor. All changes already done in the * cell editor are lost. */ public void cancelEditing() { tableViewerImpl.cancelEditing(); } /* * (non-Javadoc) * @see org.eclipse.jface.viewers.StructuredViewer#doFindInputItem(java.lang.Object) */ protected Widget doFindInputItem(Object element) { if (equals(element, getRoot())) { return getTable(); } return null; } /* * (non-Javadoc) * @see org.eclipse.jface.viewers.StructuredViewer#doFindItem(java.lang.Object) */ protected Widget doFindItem(Object element) { TableItem[] children = table.getItems(); for (int i = 0; i < children.length; i++) { TableItem item = children[i]; Object data = item.getData(); if (data != null && equals(data, element)) { return item; } } return null; } /* * (non-Javadoc) * @see org.eclipse.jface.viewers.StructuredViewer#doUpdateItem(org.eclipse.swt.widgets.Widget, java.lang.Object, boolean) */ protected void doUpdateItem(Widget widget, Object element, boolean fullMap) { if (widget instanceof TableItem) { final TableItem item = (TableItem) widget; // remember element we are showing if (fullMap) { associate(element, item); } else { Object data = item.getData(); if (data != null) { unmapElement(data, item); } item.setData(element); mapElement(element, item); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -