Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 51697 Details for
Bug 93611
[Viewers] Please refactor TableLayout to behave like standard layout
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Snippet to test
Snippet013TableLayout.java (text/plain), 8.73 KB, created by
Thomas Schindl
on 2006-10-10 10:58:53 EDT
(
hide
)
Description:
Snippet to test
Filename:
MIME Type:
Creator:
Thomas Schindl
Created:
2006-10-10 10:58:53 EDT
Size:
8.73 KB
patch
obsolete
>/******************************************************************************* > * Copyright (c) 2006 Tom Schindl 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: > * Tom Schindl - initial API and implementation > *******************************************************************************/ > >package org.eclipse.jface.snippets.viewers; > >import java.util.ArrayList; > >import org.eclipse.core.runtime.Assert; >import org.eclipse.jface.viewers.ColumnLayoutData; >import org.eclipse.jface.viewers.ColumnPixelData; >import org.eclipse.jface.viewers.ColumnWeightData; >import org.eclipse.jface.viewers.IStructuredContentProvider; >import org.eclipse.jface.viewers.ITableLabelProvider; >import org.eclipse.jface.viewers.LabelProvider; >import org.eclipse.jface.viewers.TableViewer; >import org.eclipse.jface.viewers.Viewer; >import org.eclipse.swt.SWT; >import org.eclipse.swt.events.ControlAdapter; >import org.eclipse.swt.events.ControlEvent; >import org.eclipse.swt.graphics.Image; >import org.eclipse.swt.graphics.Point; >import org.eclipse.swt.graphics.Rectangle; >import org.eclipse.swt.layout.FillLayout; >import org.eclipse.swt.widgets.Display; >import org.eclipse.swt.widgets.Shell; >import org.eclipse.swt.widgets.Table; >import org.eclipse.swt.widgets.TableColumn; > >/** > * A simple TableViewer to demonstrate usage > * > * @author Tom Schindl <tom.schindl@bestsolution.at> > * > */ >public class Snippet013TableLayout { > /** > * The TableResizeAdapter is a ControlAdapter used to set the size of a > * table. It is defined on a Composite which is the parent of a table. The > * Composites only child is the table. > */ > private class TableResizeAdapter extends ControlAdapter { > > private Table table; > > /** > * Create a new instance of the receiver with the table to create > * specified > */ > public TableResizeAdapter(Table tableChild) { > table = tableChild; > } > > /** > * The list of column layout data (element type: > * <code>ColumnLayoutData</code>). > */ > private ArrayList columns = new ArrayList(); > > /** > * Adds a new column of data. > * > * @param data > * the column layout data > */ > public void addColumnData(ColumnLayoutData data) { > columns.add(data); > } > > /** > * Sent when the size (width, height) of a control changes. The default > * behavior is to do nothing. > * > * @param e > * an event containing information about the resize > */ > public void controlResized(ControlEvent e) { > Rectangle area = table.getParent().getClientArea(); > > int width = area.width - 2 * table.getBorderWidth(); > Point preferredSize = table.computeSize(SWT.DEFAULT,SWT.DEFAULT); > if (preferredSize.y > area.height) { > // Subtract the scrollbar width from the total column width > // if a vertical scrollbar will be required > Point vBarSize = table.getVerticalBar().getSize(); > width -= vBarSize.x; > } > > // XXX: Layout is being called with an invalid value the first time > // it is being called on Linux. This method resets the > // Layout to null so we make sure we run it only when > // the value is OK. > 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++; > // first time, use the weight specified by the column data, > // otherwise use the actual width as the weight > // int weight = firstTime ? cw.weight : > tableColumns[i].getWidth(); > int weight = cw.weight; > totalWeight += weight; > } else { > Assert.isTrue(false, "Unknown column layout data"); //$NON-NLS-1$ > } > } > > // Do we have columns that have a 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; > // calculate weight as above > // int weight = firstTime ? cw.weight : > tableColumns[i].getWidth(); > 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; > } > } > } > > Point oldSize = table.getSize(); > > if (!(oldSize.x > area.width)) { > table.setSize(area.width, area.height); > } > > table.setRedraw(false); > > for (int i = 0; i < size; i++) { > tableColumns[i].setWidth(widths[i]); > } > > table.setRedraw(true); > > if (oldSize.x > area.width) { > table.setSize(area.width, area.height); > } > } > > } > > private class MyContentProvider implements IStructuredContentProvider { > > /* > * (non-Javadoc) > * > * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object) > */ > public Object[] getElements(Object inputElement) { > return (MyModel[]) inputElement; > } > > /* > * (non-Javadoc) > * > * @see org.eclipse.jface.viewers.IContentProvider#dispose() > */ > public void dispose() { > > } > > /* > * (non-Javadoc) > * > * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, > * java.lang.Object, java.lang.Object) > */ > public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { > > } > > } > > private class MyLabelProvider extends LabelProvider implements > ITableLabelProvider { > > public Image getColumnImage(Object element, int columnIndex) { > return null; > } > > public String getColumnText(Object element, int columnIndex) { > return columnIndex + " - " + element; > } > > } > > public class MyModel { > public int counter; > > public MyModel(int counter) { > this.counter = counter; > } > > public String toString() { > return "Item " + this.counter; > } > } > > public Snippet013TableLayout(Shell shell) { > final TableViewer v = new TableViewer(new Table(shell, SWT.BORDER) { > > @Override > protected void checkSubclass() { > > } > > @Override > public Point computeSize(int wHint, int hHint, boolean changed) { > System.err.println("COMPUTING SIZE"); > return super.computeSize(wHint, hHint, changed); > } > > @Override > public void setSize(Point size) { > System.err.println("SETTING SIZE"); > super.setSize(size); > } > > }); > v.setLabelProvider(new MyLabelProvider()); > v.setContentProvider(new MyContentProvider()); > v.getTable().setHeaderVisible(true); > > TableColumn column = new TableColumn(v.getTable(), SWT.NONE); > column.setText("Column 1"); > > column = new TableColumn(v.getTable(), SWT.NONE); > column.setText("Column 2"); > > TableResizeAdapter ad = new TableResizeAdapter(v.getTable()); > ColumnLayoutData data = new ColumnWeightData(50, 100); > ad.addColumnData(data); > > data = new ColumnWeightData(50, 100); > ad.addColumnData(data); > shell.addControlListener(ad); > > MyModel[] model = createModel(); > v.setInput(model); > v.getTable().setLinesVisible(true); > } > > private MyModel[] createModel() { > MyModel[] elements = new MyModel[10]; > > for (int i = 0; i < 10; i++) { > elements[i] = new MyModel(i); > } > > return elements; > } > > /** > * @param args > */ > public static void main(String[] args) { > Display display = new Display(); > Shell shell = new Shell(display); > shell.setLayout(new FillLayout()); > new Snippet013TableLayout(shell); > shell.open(); > > while (!shell.isDisposed()) { > if (!display.readAndDispatch()) > display.sleep(); > } > > display.dispose(); > > } > >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 93611
:
51696
| 51697 |
51698