public static void maxTableColumnsWidth(Table table) { table.setRedraw(false); try { int columnCount = table.getColumnCount(); if (columnCount > 0) { int totalWidth = 0; final TableColumn[] columns = table.getColumns(); for (TableColumn tc : columns) { tc.pack(); totalWidth += tc.getWidth(); } final Rectangle clientArea = table.getClientArea(); if (totalWidth < clientArea.width) { int extraSpace = clientArea.width - totalWidth; extraSpace /= columnCount; for (TableColumn tc : columns) { tc.setWidth(tc.getWidth() + extraSpace); } } } } finally { table.setRedraw(true); } }
public static void packColumns(Table table, boolean fit) { table.setRedraw(false); try { int totalWidth = 0; final TableColumn[] columns = table.getColumns(); for (TableColumn column : columns) { column.pack(); totalWidth += column.getWidth(); } final Rectangle clientArea = table.getClientArea(); if (clientArea.width > 0 && totalWidth > clientArea.width) { for (TableColumn column : columns) { int colWidth = column.getWidth(); if (colWidth > totalWidth / 3) { // If some columns is too big (more than 33% of total width) // Then shrink it to 30% column.setWidth(totalWidth / 3); totalWidth -= colWidth; totalWidth += column.getWidth(); } } int extraSpace = totalWidth - clientArea.width; for (TableColumn tc : columns) { double ratio = (double) tc.getWidth() / totalWidth; int newWidth = (int) (tc.getWidth() - extraSpace * ratio); tc.setWidth(newWidth); } } if (fit && totalWidth < clientArea.width) { int sbWidth = 0; if (table.getVerticalBar() != null) { sbWidth = table.getVerticalBar().getSize().x; } if (columns.length > 0) { float extraSpace = (clientArea.width - totalWidth - sbWidth) / columns.length; for (TableColumn tc : columns) { tc.setWidth((int) (tc.getWidth() + extraSpace)); } } } } finally { table.setRedraw(true); } }
@Override public void createPartControl(Composite parent) { final IActionBars actionBars = getViewSite().getActionBars(); IToolBarManager toolbar = actionBars.getToolBarManager(); mSymbolEntryText = new TextContributionItem(""); // $NON-NLS-1$ toolbar.add(mSymbolEntryText); toolbar.add(new AddSymbolAction(mSymbolEntryText, this)); final Table table = new Table(parent, SWT.MULTI | SWT.FULL_SELECTION | SWT.V_SCROLL | SWT.BORDER); table.setHeaderVisible(true); mViewer = new TableViewer(table); GridDataFactory.defaultsFor(table).applyTo(table); final MarketDataItemComparator comparator = new MarketDataItemComparator(); mViewer.setComparator(comparator); SelectionListener listener = new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { // determine new sort column and direction TableColumn sortColumn = table.getSortColumn(); TableColumn currentColumn = (TableColumn) e.widget; final int index = table.indexOf(currentColumn); int dir = table.getSortDirection(); if (sortColumn == currentColumn) { dir = dir == SWT.UP ? SWT.DOWN : SWT.UP; } else { table.setSortColumn(currentColumn); dir = SWT.UP; } table.setSortDirection(dir); comparator.setSort(dir == SWT.UP ? 1 : -1); comparator.setIndex(index); mViewer.refresh(); } }; // create columns, using FIXFieldLocalizer to preserve backwards // compatibility TableViewerColumn symbolColumn = new TableViewerColumn( mViewer, createColumn( table, FIXFieldLocalizer.getLocalizedFIXFieldName(Symbol.class.getSimpleName()), SWT.LEFT, listener)); symbolColumn.setEditingSupport(new SymbolEditingSupport()); createColumn( table, FIXFieldLocalizer.getLocalizedFIXFieldName(LastPx.class.getSimpleName()), SWT.RIGHT, listener); createColumn( table, FIXFieldLocalizer.getLocalizedFIXFieldName(LastQty.class.getSimpleName()), SWT.RIGHT, listener); createColumn( table, FIXFieldLocalizer.getLocalizedFIXFieldName(BidSize.class.getSimpleName()), SWT.RIGHT, listener); createColumn( table, FIXFieldLocalizer.getLocalizedFIXFieldName(BidPx.class.getSimpleName()), SWT.RIGHT, listener); createColumn( table, FIXFieldLocalizer.getLocalizedFIXFieldName(OfferPx.class.getSimpleName()), SWT.RIGHT, listener); createColumn( table, FIXFieldLocalizer.getLocalizedFIXFieldName(OfferSize.class.getSimpleName()), SWT.RIGHT, listener); // restore table state if it exists if (mViewState != null) { ColumnState.restore(table, mViewState); for (TableColumn column : table.getColumns()) { if (column.getWidth() == 0) { column.setResizable(false); } } } registerContextMenu(); getSite().setSelectionProvider(mViewer); ObservableListContentProvider content = new ObservableListContentProvider(); mViewer.setContentProvider(content); IObservableSet domain = content.getKnownElements(); IObservableMap[] maps = new IObservableMap[] { BeansObservables.observeMap(domain, MarketDataViewItem.class, "symbol"), // $NON-NLS-1$ createCompositeMap( domain, "latestTick", MDPackage.Literals.MD_LATEST_TICK__PRICE), // $NON-NLS-1$ createCompositeMap( domain, "latestTick", MDPackage.Literals.MD_LATEST_TICK__SIZE), // $NON-NLS-1$ createCompositeMap( domain, "topOfBook", MDPackage.Literals.MD_TOP_OF_BOOK__BID_SIZE), // $NON-NLS-1$ createCompositeMap( domain, "topOfBook", MDPackage.Literals.MD_TOP_OF_BOOK__BID_PRICE), // $NON-NLS-1$ createCompositeMap( domain, "topOfBook", MDPackage.Literals.MD_TOP_OF_BOOK__ASK_PRICE), // $NON-NLS-1$ createCompositeMap( domain, "topOfBook", MDPackage.Literals.MD_TOP_OF_BOOK__ASK_SIZE) // $NON-NLS-1$ }; mViewer.setLabelProvider(new ObservableMapLabelProvider(maps)); mViewer.setUseHashlookup(true); mItems = WritableList.withElementType(MarketDataViewItem.class); mViewer.setInput(mItems); }