public static int getColumnIndex(Table table, Point pointCursor) { // searching current column index int currentColumnIndex = -1; TableColumn[] columns = table.getColumns(); for (int i = 0, width = 0; i < columns.length; i++) { TableColumn column = columns[i]; int widthColumn = column.getWidth(); if (pointCursor.x >= width && pointCursor.x <= width + widthColumn && ((!WindowSystem.isGTK() && pointCursor.y > table.getHeaderHeight() && pointCursor.y < table.getHeaderHeight() + table.getItemCount() * table.getItemHeight()) || (WindowSystem.isGTK() && pointCursor.y > 0 && pointCursor.y < table.getItemCount() * table.getItemHeight() + table.getItemHeight()))) { currentColumnIndex = i; break; } width += widthColumn; } return currentColumnIndex; }
public int getColumnIndex(Point pointCursor) { // searching current column index int currentColumnIndex = -1; TableColumn[] columns = table.getColumns(); int minY = 0; int maxY = table.getHeaderHeight() + (table.getItemCount() * (table.getItemHeight() + table.getBorderWidth() + (WindowSystem.isGTK() ? 2 : 0))); for (int i = 0, width = 0; i < columns.length; i++) { TableColumn column = columns[i]; int widthColumn = column.getWidth(); if (pointCursor.x >= width && pointCursor.x <= width + widthColumn && pointCursor.y > minY && pointCursor.y < maxY) { currentColumnIndex = i; break; } width += widthColumn; } return currentColumnIndex; }
private void changeColumnLayoutData(final TableColumn currentTableColumn, Rectangle bounds) { Integer columnIndex = getColumnIndex(currentTableColumn); ColumnLayoutData columnLayoutData = columnsLayoutData.get(columnIndex); if (columnLayoutData instanceof ColumnPixelData) { ColumnPixelData columnPixelData = (ColumnPixelData) columnLayoutData; columnPixelData.width = currentTableColumn.getWidth(); // System.out.println("columnPixelData.width="+columnPixelData.width); } else if (columnLayoutData instanceof ColumnWeightData) { ColumnWeightData columnWeightData = (ColumnWeightData) columnLayoutData; Table table = currentTableColumn.getParent(); TableColumn[] columns = table.getColumns(); // int totalWidthWithoutWeight = 0; int totalWidthWithWeight = 0; int totalWeight = 0; for (int i = 0; i < columns.length; i++) { ColumnLayoutData data = columnsLayoutData.get(i); if (data instanceof ColumnWeightData) { int weight = ((ColumnWeightData) data).weight; totalWeight += weight; totalWidthWithWeight += columns[i].getWidth(); } else { if (WindowSystem.isGTK()) { totalWidthWithWeight += columns[i].getWidth(); } } } float coef = (float) currentTableColumn.getWidth() * (float) totalWeight / ((float) totalWidthWithWeight); columnWeightData.weight = Math.round(coef); } }
/** * DOC amaumont Comment method "resizeControl". * * @param e */ private synchronized void controlResizedExecute(ControlEvent e) { final TableColumn currentTableColumn = (TableColumn) e.widget; if (!WindowSystem.isGTK() && !columnsResizingByLayout && (fillHorizontal || continuousLayout)) { // System.out.println("controlResizedExecute"); if (continuousLayout && !fillHorizontal) { // asyncThreadingForManualColumnResizingFalse.interrupt(); // if (!fillHorizontal) { // manualResizing = false; // } } if (!manualResizing) { manualResizing = true; final Table table = currentTableColumn.getParent(); Rectangle bounds = table.getClientArea(); // System.out.println("currentTableColumn.getWidth()=" + currentTableColumn.getWidth()); // System.out.println("columnsResizingByLayout=" + columnsResizingByLayout); // System.out.println("currentTableColumn.hashCode()=" + currentTableColumn.hashCode()); if (table.getHorizontalBar().getSelection() == 0) { if (!WindowSystem.isGTK()) { changeColumnLayoutData(currentTableColumn, bounds); } lastDisplayedWidth = bounds.width + widthAdjustValue; // System.out.println("lastWidth="+lastDisplayedWidth); // System.out.println("referenceWidth="+referenceWidth); referenceWidth = computeCurrentTableWidth(); TableColumn[] tableColumns = table.getColumns(); if (fillHorizontal && tableColumns.length - 1 >= 0) { int widthAll = referenceWidth; int indexLastColumn = tableColumns.length - 1; TableColumn lastTableColumn = tableColumns[indexLastColumn]; TableViewerCreatorColumnNotModifiable tableViewerCreatorColumn = (TableViewerCreatorColumnNotModifiable) tableViewerCreator.getColumns().get(indexLastColumn); ColumnLayoutData columnLayoutData = columnsLayoutData.get(indexLastColumn); int minimumWidth = 0; if (columnLayoutData instanceof ColumnWeightData) { minimumWidth = ((ColumnWeightData) columnLayoutData).minimumWidth; } else if (columnLayoutData instanceof ColumnPixelData) { minimumWidth = ((ColumnPixelData) columnLayoutData).width; } int widthLastColumn = lastTableColumn.getWidth(); int newColumnWidth = lastDisplayedWidth - (widthAll - widthLastColumn); if (newColumnWidth > minimumWidth) { if (referenceWidth - widthLastColumn < lastDisplayedWidth) { if (newColumnWidth > 0) { // System.out.println("change"); lastTableColumn.setWidth(newColumnWidth); changeColumnLayoutData(lastTableColumn, bounds); } } else { int width = tableViewerCreatorColumn.getWidth(); // System.out.println("weight=" + weight); // System.out.println("width=" + width); if (columnLayoutData instanceof ColumnWeightData) { lastTableColumn.setWidth(width); changeColumnLayoutData(lastTableColumn, bounds); } } } referenceWidth = computeCurrentTableWidth() + widthAdjustValue; // System.out.println("referenceWidth=" + referenceWidth); } } if (continuousLayout && !fillHorizontal) { // asyncThreadingForManualColumnResizingFalse.start(); Runnable runable = new Runnable() { public void run() { try { synchronized (this) { wait(500); manualResizing = false; } } catch (InterruptedException e) { manualResizing = false; } } }; new Thread(runable).start(); } } if (fillHorizontal) { manualResizing = false; } tableViewerCreator.redrawTableEditorControls(); } }
private void layout(final Composite c) { // System.out.println("Layout" + System.currentTimeMillis()); final Table table = (Table) c; Rectangle bounds = table.getBounds(); Rectangle clientArea = table.getClientArea(); if (WindowSystem.isGTK()) { bounds = table.getClientArea(); } else { bounds = table.getBounds(); } int displayedWidth = 0; int heightFilledByRows = table.getItemCount() * table.getItemHeight() + table.getHeaderHeight(); if (firstTime) { displayedWidth = bounds.width - 2 * c.getBorderWidth() + widthAdjustValue; if (bounds.height < heightFilledByRows) { displayedWidth += -table.getVerticalBar().getSize().x; } } else { if (WindowSystem.isGTK()) { heightFilledByRows -= table.getHeaderHeight(); } if (fillHorizontal) { if (bounds.height < heightFilledByRows) { displayedWidth = clientArea.width + widthAdjustValue; } else { displayedWidth = bounds.width + widthAdjustValue; } referenceWidth = displayedWidth; lastDisplayedWidth = displayedWidth; } else { int newVisibleWidth = bounds.width + widthAdjustValue; if (bounds.height < heightFilledByRows) { newVisibleWidth += -table.getVerticalBar().getSize().x; } displayedWidth = referenceWidth - 2 * c.getBorderWidth() - (lastDisplayedWidth - newVisibleWidth); } } if (firstTime) { referenceWidth = displayedWidth; lastDisplayedWidth = displayedWidth; } // 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 (displayedWidth <= 1) { return; } Item[] tableColumns = getColumns(c); int size = Math.min(columnsLayoutData.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) columnsLayoutData.get(i); if (col instanceof ColumnPixelData) { ColumnPixelData cpd = (ColumnPixelData) col; int pixels = cpd.width; if (cpd.addTrim) { pixels += COLUMN_TRIM; } 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 = displayedWidth - fixedWidth; int totalDistributed = 0; for (int i = 0; i < size; ++i) { ColumnLayoutData col = (ColumnLayoutData) columnsLayoutData.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) columnsLayoutData.get(i); if (col instanceof ColumnWeightData) { ++widths[i]; --diff; } } } columnsResizingByLayout = true; final boolean previousVisible = table.getVisible(); if (previousVisible) { table.setVisible(false); } for (int i = 0; i < size; i++) { setWidth(tableColumns[i], widths[i]); } table.setVisible(previousVisible); columnsResizingByLayout = false; firstTime = false; }