首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

table的列阔自动矫正。resize

2012-11-01 
table的列宽自动矫正。resize.1.在第一个类来写protected void createTableColumns(Table table) {List col

table的列宽自动矫正。resize.
1.在第一个类来写

   protected void createTableColumns(Table table) {        List columnNames = new ArrayList();        TableLayout tableLayout = new AutoResizeTableLayout(table);        for(Iterator iter = getLabelAccessors().iterator(); iter.hasNext();)        {            Accessor accessor = (Accessor)(iter.next());            String label = accessor.getOptionalString();            columnNames.add(label);            if(accessor instanceof AbstractLabelAccessor)            {                AbstractLabelAccessor labelAccessor = (AbstractLabelAccessor)accessor;                if(labelAccessor.isFixed())                {                    if(labelAccessor.getWidth() >= 0)                    {                        tableLayout.addColumnData(new ColumnPixelData(labelAccessor.getWidth()));                    }                    else                    {                        tableLayout.addColumnData(new ColumnWeightData(100/getLabelAccessors().size()));                    }                }                else                {                    if(labelAccessor.getWeight() >= 0)                    {                        if(labelAccessor.getMinWidth() >= 0)                        {                            tableLayout.addColumnData(new ColumnWeightData(labelAccessor.getWeight(), labelAccessor.getMinWidth()));                        }                        else                        {                            tableLayout.addColumnData(new ColumnWeightData(labelAccessor.getWeight()));                        }                    }                    else                    {                        // ignore MinWidth...                        tableLayout.addColumnData(new ColumnWeightData(100/getLabelAccessors().size()));                    }                }            }            else            {                if(label.length() == 0)                {                    tableLayout.addColumnData(new ColumnWeightData(NO_ABSTRACT_LABEL_ACCESSOR_EMPTY_LABEL_COLUMN_WEIGHT, true));                }                else                {                    tableLayout.addColumnData(new ColumnWeightData(NO_ABSTRACT_LABEL_ACCESSOR_NO_EMPTY_LABEL_COLUMN_WEIGHT, NO_ABSTRACT_LABEL_ACCESSOR_NO_EMPTY_LABEL_COLUMN_MINWIDTH, true));                }            }        }        for (Iterator iter = columnNames.iterator(); iter.hasNext();)        {            TableColumn column = new TableColumn(table, SWT.NONE);//            column.setMoveable(true);            column.setText((String)iter.next());        }        table.setLayout(tableLayout);        table.pack();    }



2.然后写一个TableLayout 的子类
import java.util.ArrayList;import java.util.List;import org.eclipse.jface.viewers.ColumnLayoutData;import org.eclipse.jface.viewers.ColumnPixelData;import org.eclipse.jface.viewers.ColumnWeightData;import org.eclipse.jface.viewers.TableLayout;import org.eclipse.swt.events.ControlEvent;import org.eclipse.swt.events.ControlListener;import org.eclipse.swt.widgets.Table;import org.eclipse.swt.widgets.TableColumn;/** * COMMENT - Add description of this class or interface here. *           Description should go beyond the class/interface name. *           Use the following template: * * <Short description of class (noun phrase) followed by a dot> * <More elaborate description of what kind of object this class or interface represents.> * <Give information on (special) characteristics if possible.> */public class AutoResizeTableLayout extends TableLayout implements ControlListener{    private final Table table;    private List columns = new ArrayList();    private boolean autosizing = false;    private int oldWidth;    /**     * COMMENT - Add concise description of this constructor.     *           Description should go beyond the constructor's name.     *     *     */    public AutoResizeTableLayout(final Table table) {        this.table = table;        table.addControlListener(this);    }    public void addColumnData(ColumnLayoutData data){        columns.add(data);        super.addColumnData(data);    }    /* (non-Javadoc)     * @see org.eclipse.swt.events.ControlListener#controlMoved(org.eclipse.swt.events.ControlEvent)     */    public void controlMoved(ControlEvent e) {    }    /* (non-Javadoc)     * @see org.eclipse.swt.events.ControlListener#controlResized(org.eclipse.swt.events.ControlEvent)     */    public void controlResized(ControlEvent e) {        // only react on changing width min. few pixels        // (see workaround for SWT bug getting unnecessary scroll bar)        if (Math.abs(oldWidth - table.getClientArea().width) > 5) {            if (autosizing) {                return;            }            autosizing = true;            try {                autoSizeColumns();            } finally {                autosizing = false;                oldWidth = table.getClientArea().width;            }        }    }    /**     * <Short description (short verb phrase possible) followed by a dot>     * <More elaborate description of "what" this method does. Omit the "how" unless necessary.>     *     *     */    private void autoSizeColumns() {        int width = table.getClientArea().width;        width -= 10; // workaround for SWT bug getting unnecessary scroll bar        if(width <= 1) return;        TableColumn[] tableColumns = table.getColumns();        int size = Math.min(columns.size(), tableColumns.length);        int[] widths = new int[size];        int fixedWidth = 0;        int numberOfWeightColumns = 0;        int totalWeight = 0;        //First calc space occupied by fixed columns        for (int i = 0; i < size; ++i) {            ColumnLayoutData col = (ColumnLayoutData) columns.get(i);            if(col instanceof ColumnPixelData){                int pixels = ((ColumnPixelData)col).width;                widths[i] = pixels;                fixedWidth += pixels;            }            else if(col instanceof ColumnWeightData){                ColumnWeightData cw = (ColumnWeightData)col;                ++numberOfWeightColumns;                int weight = cw.weight;                totalWeight += weight;            }            else{                throw new IllegalStateException("Unknown column layout data"); //$NON-NLS-1$            }        }        //Do we have columns that have weight?        if(numberOfWeightColumns > 0){            //Now, distribute the rest to the columns with weight            int rest = width - fixedWidth;            int totalDistributed = 0;            for (int i = 0; i < size; ++i) {                ColumnLayoutData col = (ColumnLayoutData) columns.get(i);                if(col instanceof ColumnWeightData){                    ColumnWeightData cw = (ColumnWeightData)col;                    int weight = cw.weight;                    int pixels = totalWeight == 0 ? 0 : weight * rest/totalWeight;                    if(pixels < cw.minimumWidth) pixels = cw.minimumWidth;                    totalDistributed += pixels;                    widths[i] = pixels;                }            }            //Distribute any remaining pixels to columns with weight            int diff = rest - totalDistributed;            for (int i = 0; diff > 0; ++i) {                if(i == size) i = 0;                ColumnLayoutData col = (ColumnLayoutData) columns.get(i);                if(col instanceof ColumnWeightData){                    ++widths[i];                    --diff;                }            }        }        for (int i = 0; i < size; i++) {            if(tableColumns[i].getWidth() != widths[i]) tableColumns[i].setWidth(widths[i]);        }    }    /**     * remove this from the tables listeners.     */    public void dispose() {        table.removeControlListener(this);    }}

热点排行