diff options
author | Grant Gayed <ggayed> | 2005-03-23 22:26:27 +0000 |
---|---|---|
committer | Grant Gayed <ggayed> | 2005-03-23 22:26:27 +0000 |
commit | 67f57a1503dff18128d85f6be9ce1151c584d527 (patch) | |
tree | b0885af85722bef912ddfc3ee8670ce167a78fbb | |
parent | 179e1e8feb58af613b2d9e2d8a1b0f959cbc3d62 (diff) | |
download | eclipse.platform.swt-67f57a1503dff18128d85f6be9ce1151c584d527.tar.gz eclipse.platform.swt-67f57a1503dff18128d85f6be9ce1151c584d527.tar.xz eclipse.platform.swt-67f57a1503dff18128d85f6be9ce1151c584d527.zip |
reorderable columns
3 files changed, 238 insertions, 72 deletions
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/emulated/treetable/org/eclipse/swt/widgets/Table.java b/bundles/org.eclipse.swt/Eclipse SWT/emulated/treetable/org/eclipse/swt/widgets/Table.java index 7dfd175db9..f2caa0aa26 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/emulated/treetable/org/eclipse/swt/widgets/Table.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/emulated/treetable/org/eclipse/swt/widgets/Table.java @@ -42,6 +42,7 @@ import org.eclipse.swt.internal.*; public class Table extends Composite { Canvas header; TableColumn[] columns = new TableColumn [0]; + TableColumn[] orderedColumns; TableItem[] items = new TableItem [0]; TableItem[] selectedItems = new TableItem [0]; TableItem focusItem, anchorItem, lastClickedItem; @@ -320,6 +321,7 @@ public void clearAll () { * -1 if the x lies to the right of the last column. */ int computeColumnIntersect (int x, int startColumn) { + TableColumn[] columns = getOrderedColumns (); if (columns.length - 1 < startColumn) return -1; int rightX = columns [startColumn].getX (); for (int i = startColumn; i < columns.length; i++) { @@ -340,6 +342,7 @@ public Point computeSize (int wHint, int hHint, boolean changed) { width = Math.max (width, itemBounds.x + itemBounds.width); } } else { + TableColumn[] columns = getOrderedColumns (); TableColumn lastColumn = columns [columns.length - 1]; width = lastColumn.getX () + lastColumn.width; } @@ -359,6 +362,23 @@ void createItem (TableColumn column, int index) { System.arraycopy (columns, index, newColumns, index + 1, columns.length - index); columns = newColumns; + if (orderedColumns != null) { + int insertIndex = 0; + if (index > 0) { + insertIndex = columns [index - 1].getOrderIndex () + 1; + } + TableColumn[] newOrderedColumns = new TableColumn [orderedColumns.length + 1]; + System.arraycopy (orderedColumns, 0, newOrderedColumns, 0, insertIndex); + newOrderedColumns [insertIndex] = column; + System.arraycopy ( + orderedColumns, + insertIndex, + newOrderedColumns, + insertIndex + 1, + orderedColumns.length - insertIndex); + orderedColumns = newOrderedColumns; + } + /* allow all items to update their internal structures accordingly */ for (int i = 0; i < items.length; i++) { items [i].addColumn (column); @@ -495,14 +515,37 @@ public void deselectAll () { } } void destroyItem (TableColumn column) { - int numColumns = columns.length; int index = column.getIndex (); + int orderedIndex = column.getOrderIndex (); TableColumn[] newColumns = new TableColumn [columns.length - 1]; System.arraycopy (columns, 0, newColumns, 0, index); System.arraycopy (columns, index + 1, newColumns, index, newColumns.length - index); columns = newColumns; + if (orderedColumns != null) { + if (columns.length < 2) { + orderedColumns = null; + } else { + int removeIndex = column.getOrderIndex (); + TableColumn[] newOrderedColumns = new TableColumn [orderedColumns.length - 1]; + System.arraycopy (orderedColumns, 0, newOrderedColumns, 0, removeIndex); + System.arraycopy ( + orderedColumns, + removeIndex + 1, + newOrderedColumns, + removeIndex, + newOrderedColumns.length - removeIndex); + orderedColumns = newOrderedColumns; + } + } + + /* ensure that column 0 always has left-alignment */ + if (index == 0 && columns.length > 0) { + columns [0].style |= SWT.LEFT; + columns [0].style &= ~(SWT.CENTER | SWT.RIGHT); + } + /* allow all items to update their internal structures accordingly */ for (int i = 0; i < items.length; i++) { items [i].removeColumn (column, index); @@ -526,10 +569,11 @@ void destroyItem (TableColumn column) { redraw (); } } - for (int i = index; i < columns.length; i++) { - Event event = new Event (); - event.widget = columns [i]; - columns [i].sendEvent (SWT.Move, event); + TableColumn[] columns = getOrderedColumns (); + for (int i = orderedIndex; i < columns.length; i++) { + if (!columns [i].isDisposed ()) { + columns [i].sendEvent (SWT.Move); + } } } /* @@ -537,6 +581,8 @@ void destroyItem (TableColumn column) { * item being destroyed. */ void destroyItem (TableItem item) { + if (item == focusItem) reassignFocus (); + int index = item.index; Rectangle bounds = item.getBounds (); int rightX = bounds.x + bounds.width; @@ -577,7 +623,6 @@ void destroyItem (TableItem item) { newSelectedItems.length - selectionIndex); selectedItems = newSelectedItems; } - if (item == focusItem) reassignFocus (); if (item == anchorItem) anchorItem = null; } int getCellPadding () { @@ -673,8 +718,17 @@ public int getColumnCount () { */ public int[] getColumnOrder () { checkWidget (); - // TODO - return null; + int[] result = new int [columns.length]; + if (orderedColumns != null) { + for (int i = 0; i < result.length; i++) { + result [i] = orderedColumns [i].getIndex (); + } + } else { + for (int i = 0; i < columns.length; i++) { + result [i] = i; + } + } + return result; } /** * Returns an array of <code>TableColumn</code>s which are the @@ -883,6 +937,10 @@ public boolean getLinesVisible () { checkWidget (); return linesVisible; } +TableColumn[] getOrderedColumns () { + if (orderedColumns != null) return orderedColumns; + return columns; +} /** * Returns an array of <code>TableItem</code>s that are currently * selected in the receiver. An empty array indicates that no @@ -1049,6 +1107,7 @@ void handleEvents (Event event) { } void headerOnMouseDown (Event event) { if (event.button != 1) return; + TableColumn[] columns = getOrderedColumns (); for (int i = 0; i < columns.length; i++) { TableColumn column = columns [i]; int x = column.getX () + column.width; @@ -1122,6 +1181,7 @@ void headerOnMouseUp (Event event) { resizeColumn = null; } void headerOnPaint (Event event) { + TableColumn[] columns = getOrderedColumns (); int numColumns = columns.length; GC gc = event.gc; Rectangle clipping = gc.getClipping (); @@ -1539,7 +1599,7 @@ void onDispose () { } topIndex = 0; items = selectedItems = null; - columns = null; + columns = orderedColumns = null; focusItem = anchorItem = lastClickedItem = null; header = null; resizeColumn = null; @@ -2111,6 +2171,7 @@ void onPageUp (int stateMask) { postEvent (SWT.Selection, newEvent); } void onPaint (Event event) { + TableColumn[] columns = getOrderedColumns (); GC gc = event.gc; Rectangle clipping = gc.getClipping (); int numColumns = columns.length; @@ -2141,7 +2202,6 @@ void onPaint (Event event) { int rightX = clipping.x + clipping.width; int headerHeight = getHeaderHeight (); int y = (clipping.y - headerHeight) / itemHeight * itemHeight + headerHeight; - if (y == headerHeight) y += itemHeight; /* do not paint line at very top */ while (y <= bottomY) { gc.drawLine (clipping.x, y, rightX, y); y += itemHeight; @@ -2174,15 +2234,17 @@ void onPaint (Event event) { if (isFocusControl ()) { if (focusItem == item) { Rectangle focusBounds = item.getFocusBounds (); - gc.setClipping (focusBounds); - int[] oldLineDash = gc.getLineDash (); - if (item.isSelected ()) { - gc.setLineDash (new int[] {2, 2}); - } else { - gc.setLineDash (new int[] {1, 1}); + if (focusBounds.width > 0) { + gc.setClipping (focusBounds); + int[] oldLineDash = gc.getLineDash (); + if (item.isSelected ()) { + gc.setLineDash (new int[] {2, 2}); + } else { + gc.setLineDash (new int[] {1, 1}); + } + gc.drawFocus (focusBounds.x, focusBounds.y, focusBounds.width, focusBounds.height); + gc.setLineDash (oldLineDash); } - gc.drawFocus (focusBounds.x, focusBounds.y, focusBounds.width, focusBounds.height); - gc.setLineDash (oldLineDash); } } } @@ -2333,14 +2395,15 @@ void redrawItems (int startIndex, int endIndex, boolean focusBoundsOnly) { int height = (endIndex - startIndex + 1) * itemHeight; if (focusBoundsOnly) { if (columns.length > 0) { - int rightX = 0; + TableColumn lastColumn; if ((style & SWT.FULL_SELECTION) != 0) { - TableColumn lastColumn = columns [columns.length - 1]; - rightX = lastColumn.getX () + lastColumn.width; + TableColumn[] orderedColumns = getOrderedColumns (); + lastColumn = orderedColumns [orderedColumns.length - 1]; } else { - rightX = columns [0].width - horizontalOffset; + lastColumn = columns [0]; } - if (rightX <= 0) return; /* first column not visible */ + int rightX = lastColumn.getX () + lastColumn.getWidth (); + if (rightX <= 0) return; /* focus column(s) not visible */ } endIndex = Math.min (endIndex, items.length - 1); for (int i = startIndex; i <= endIndex; i++) { @@ -2651,7 +2714,40 @@ void selectItem (TableItem item, boolean addToSelection) { public void setColumnOrder (int [] order) { checkWidget (); if (order == null) error (SWT.ERROR_NULL_ARGUMENT); - // TODO + if (columns.length == 0) { + if (order.length != 0) error (SWT.ERROR_INVALID_ARGUMENT); + return; + } + if (order.length != columns.length) error (SWT.ERROR_INVALID_ARGUMENT); + boolean reorder = false; + boolean [] seen = new boolean [columns.length]; + int[] oldOrder = getColumnOrder (); + for (int i = 0; i < order.length; i++) { + int index = order [i]; + if (index < 0 || index >= columns.length) error (SWT.ERROR_INVALID_RANGE); + if (seen [index]) error (SWT.ERROR_INVALID_ARGUMENT); + seen [index] = true; + if (index != oldOrder [i]) reorder = true; + } + if (!reorder) return; + + int[] oldX = new int [columns.length]; + for (int i = 0; i < columns.length; i++) { + oldX [i] = columns [i].getX (); + } + orderedColumns = new TableColumn [order.length]; + for (int i = 0; i < order.length; i++) { + orderedColumns [i] = columns [order [i]]; + } + for (int i = 0; i < orderedColumns.length; i++) { + TableColumn column = orderedColumns [i]; + if (!column.isDisposed () && column.getX () != oldX [column.getIndex ()]) { + column.sendEvent (SWT.Move); + } + } + + redraw (); + if (header.isVisible ()) header.redraw (); } void setFocusItem (TableItem item, boolean redrawOldFocus) { if (item == focusItem) return; @@ -2991,9 +3087,10 @@ public void setSelection (int [] indices) { public void setTopIndex (int index) { checkWidget (); if (!(0 <= index && index < items.length)) return; - if (index == topIndex) return; int visibleItemCount = (getClientArea ().height - getHeaderHeight ()) / itemHeight; if (items.length <= visibleItemCount) return; + index = Math.min (index, items.length - visibleItemCount); + if (index == topIndex) return; update (); int change = topIndex - index; @@ -3031,20 +3128,21 @@ public void showColumn (TableColumn column) { int x = column.getX (); int rightX = x + column.width; Rectangle bounds = getClientArea (); - int boundsRight = bounds.x + bounds.width; - if (bounds.x <= x && rightX <= boundsRight) return; /* column is fully visible */ + if (0 <= x && rightX <= bounds.width) return; /* column is fully visible */ int absX = 0; /* the X of the column irrespective of the horizontal scroll */ - for (int i = 0; i < column.getIndex (); i++) { + TableColumn[] columns = getOrderedColumns (); + for (int i = 0; i < column.getOrderIndex (); i++) { absX += columns [i].width; } if (x < bounds.x) { /* column is to left of viewport */ horizontalOffset = absX; } else { - horizontalOffset = boundsRight - absX; + horizontalOffset = absX + column.width - bounds.width; } getHorizontalBar ().setSelection (horizontalOffset); redraw (); + if (header.isVisible ()) header.redraw (); } /** * Shows the item. If the item is already showing in the receiver, @@ -3154,13 +3252,12 @@ void updateColumnWidth (TableColumn column, int width) { header.redraw (x, 0, bounds.width - x, getHeaderHeight (), false); } - Event event = new Event (); - event.widget = column; - column.sendEvent (SWT.Resize, event); - for (int i = column.getIndex () + 1; i < columns.length; i++) { - event = new Event (); - event.widget = columns [i]; - columns [i].sendEvent (SWT.Move, event); + column.sendEvent (SWT.Resize); + TableColumn[] columns = getOrderedColumns (); + for (int i = column.getOrderIndex () + 1; i < columns.length; i++) { + if (!columns [i].isDisposed ()) { + columns [i].sendEvent (SWT.Move); + } } } /* diff --git a/bundles/org.eclipse.swt/Eclipse SWT/emulated/treetable/org/eclipse/swt/widgets/TableColumn.java b/bundles/org.eclipse.swt/Eclipse SWT/emulated/treetable/org/eclipse/swt/widgets/TableColumn.java index 1d55489c26..4828028ff8 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/emulated/treetable/org/eclipse/swt/widgets/TableColumn.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/emulated/treetable/org/eclipse/swt/widgets/TableColumn.java @@ -218,10 +218,26 @@ public void dispose () { if (isDisposed ()) return; Rectangle parentBounds = parent.getClientArea (); int x = getX (); + int index = getIndex (); + int orderIndex = getOrderIndex (); Table parent = this.parent; dispose (true); + int width = parentBounds.width - x; parent.redraw (x, 0, width, parentBounds.height, false); + /* + * If column 0 was disposed and if the parent has style CHECK then + * the new column 0 will change, so explicitly redraw it if it appears to + * the left of the disposed column in the column order. + */ + if ((parent.style & SWT.CHECK) != 0 && index == 0) { + if (parent.columns.length > 0) { + TableColumn newColumn0 = parent.columns [0]; + if (newColumn0.getOrderIndex () < orderIndex) { + parent.redraw (newColumn0.getX (), 0, newColumn0.width, parentBounds.height, false); + } + } + } if (parent.getHeaderVisible ()) { parent.header.redraw (x, 0, width, parent.getHeaderHeight (), false); } @@ -295,6 +311,14 @@ public boolean getMoveable () { checkWidget (); return moveable; } +int getOrderIndex () { + TableColumn[] orderedColumns = parent.orderedColumns; + if (orderedColumns == null) return getIndex (); + for (int i = 0; i < orderedColumns.length; i++) { + if (orderedColumns [i] == this) return i; + } + return -1; +} /** * Returns the receiver's parent, which must be a <code>Table</code>. * @@ -347,10 +371,11 @@ public int getWidth () { return width; } int getX () { - int index = getIndex (); + TableColumn[] columns = parent.getOrderedColumns (); + int index = getOrderIndex (); int result = -parent.horizontalOffset; for (int i = 0; i < index; i++) { - result += parent.columns [i].width; + result += columns [i].width; } return result; } @@ -370,11 +395,11 @@ public void pack () { TableItem[] availableItems = parent.items; if (availableItems.length == 0) return; int index = getIndex (); - int width = getPreferredWidth (); + int newWidth = getPreferredWidth (); for (int i = 0; i < availableItems.length; i++) { - width = Math.max (width, availableItems [i].getPreferredWidth (index)); + newWidth = Math.max (newWidth, availableItems [i].getPreferredWidth (index)); } - parent.updateColumnWidth (this, width); + if (newWidth != width) parent.updateColumnWidth (this, newWidth); } void paint (GC gc) { int padding = parent.getHeaderPadding (); diff --git a/bundles/org.eclipse.swt/Eclipse SWT/emulated/treetable/org/eclipse/swt/widgets/TableItem.java b/bundles/org.eclipse.swt/Eclipse SWT/emulated/treetable/org/eclipse/swt/widgets/TableItem.java index 4b62439bc4..b039a6ac68 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/emulated/treetable/org/eclipse/swt/widgets/TableItem.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/emulated/treetable/org/eclipse/swt/widgets/TableItem.java @@ -192,15 +192,16 @@ void addColumn (TableColumn column) { } if (index == 0 && columnCount > 1) { - /* - * The new second column now has more space available to it than it did while it - * was the first column since it no longer has to show hierarchy decorations, so - * recompute its displayText. + /* + * The new second column may have more width available to it than it did when it was + * the first column if checkboxes are being shown, so recompute its displayText if needed. */ - GC gc = new GC (parent); - gc.setFont (getFont (1)); - computeDisplayText (1, gc); - gc.dispose (); + if ((parent.style & SWT.CHECK) != 0) { + GC gc = new GC (parent); + gc.setFont (getFont (1)); + computeDisplayText (1, gc); + gc.dispose (); + } } } static Table checkNull (Table table) { @@ -220,6 +221,8 @@ void clear () { font = null; cellFonts = null; cached = false; + text = ""; + image = null; int columnCount = parent.columns.length; if (columnCount > 0) { @@ -234,13 +237,22 @@ void clear () { void computeDisplayText (int columnIndex, GC gc) { int columnCount = parent.columns.length; if (columnCount == 0) return; - + TableColumn column = parent.columns [columnIndex]; int availableWidth = column.width - 2 * parent.getCellPadding () - 2 * MARGIN_TEXT; - Image image = getImage (columnIndex); - if (image != null) { - availableWidth -= image.getBounds ().width; - availableWidth -= Table.MARGIN_IMAGE; + if (columnIndex == 0) { + availableWidth -= parent.col0ImageWidth; + if (parent.col0ImageWidth > 0) availableWidth -= Table.MARGIN_IMAGE; + if ((parent.style & SWT.CHECK) != 0) { + availableWidth -= parent.checkboxBounds.width; + availableWidth -= Table.MARGIN_IMAGE; + } + } else { + Image image = getImage (columnIndex); + if (image != null) { + availableWidth -= image.getBounds ().width; + availableWidth -= Table.MARGIN_IMAGE; + } } String text = getText (columnIndex); @@ -366,8 +378,7 @@ public Color getBackground (int columnIndex) { if (cellBackgrounds == null || cellBackgrounds [columnIndex] == null) return getBackground (); return cellBackgrounds [columnIndex]; } -Rectangle getBounds () { - // TODO is this method needed? +public Rectangle getBounds () { checkWidget (); int textPaintWidth = textWidths [0] + 2 * MARGIN_TEXT; return new Rectangle (getTextX (0), parent.getItemY (this), textPaintWidth, parent.itemHeight - 1); @@ -441,7 +452,11 @@ Rectangle getCellBounds (int columnIndex) { Rectangle getCheckboxBounds () { if ((parent.getStyle () & SWT.CHECK) == 0) return null; Rectangle result = parent.checkboxBounds; - result.x = parent.getCellPadding () - parent.horizontalOffset; + if (parent.columns.length == 0) { + result.x = parent.getCellPadding () - parent.horizontalOffset; + } else { + result.x = parent.columns [0].getX () + parent.getCellPadding (); + } result.y = parent.getItemY (this) + (parent.itemHeight - result.height) / 2; return result; } @@ -513,7 +528,18 @@ String getDisplayText (int columnIndex) { * Returns the bounds that should be used for drawing a focus rectangle on the receiver */ Rectangle getFocusBounds () { - int x = getTextX (0); + int x = 0; + int[] columnOrder = parent.getColumnOrder (); + if ((parent.style & SWT.FULL_SELECTION) != 0) { + int col0index = columnOrder.length == 0 ? 0 : columnOrder [0]; + if (col0index == 0) { + x = getTextX (0); + } else { + x = 0; + } + } else { + x = getTextX (0); + } int width; TableColumn[] columns = parent.columns; if (columns.length == 0) { @@ -521,7 +547,7 @@ Rectangle getFocusBounds () { } else { TableColumn column; if ((parent.style & SWT.FULL_SELECTION) != 0) { - column = columns [columns.length - 1]; + column = columns [columnOrder [columnOrder.length - 1]]; } else { column = columns [0]; } @@ -638,7 +664,19 @@ public boolean getGrayed () { * Returns the bounds representing the clickable region that should select the receiver. */ Rectangle getHitBounds () { - int contentX = getContentX (0); + int[] columnOrder = parent.getColumnOrder (); + int contentX = 0; + if ((parent.style & SWT.FULL_SELECTION) != 0) { + int col0index = columnOrder.length == 0 ? 0 : columnOrder [0]; + if (col0index == 0) { + contentX = getContentX (0); + } else { + contentX = 0; + } + } else { + contentX = getContentX (0); + } + int width = 0; TableColumn[] columns = parent.columns; if (columns.length == 0) { @@ -650,7 +688,7 @@ Rectangle getHitBounds () { */ TableColumn column; if ((parent.style & SWT.FULL_SELECTION) != 0) { - column = columns [columns.length - 1]; + column = columns [columnOrder [columnOrder.length - 1]]; } else { column = columns [0]; } @@ -855,7 +893,8 @@ void paint (GC gc, TableColumn column, boolean paintCellContent) { if (!background.equals (parent.getBackground ())) { Color oldBackground = gc.getBackground (); gc.setBackground (background); - if (columnIndex == 0) { + TableColumn[] orderedColumns = parent.orderedColumns; + if (columnIndex == 0 && (column == null || column.getOrderIndex () == 0)) { Rectangle focusBounds = getFocusBounds (); int fillWidth = 0; if (column == null) { @@ -884,13 +923,17 @@ void paint (GC gc, TableColumn column, boolean paintCellContent) { if (parent.columns.length < 2 || (parent.style & SWT.FULL_SELECTION) == 0) { fillWidth -= 2; /* space for right bound of focus rect */ } - gc.fillRectangle (focusBounds.x + 1, focusBounds.y + 1, fillWidth, focusBounds.height - 2); + if (fillWidth > 0) { + gc.fillRectangle (focusBounds.x + 1, focusBounds.y + 1, fillWidth, focusBounds.height - 2); + } } else { int fillWidth = column.width; if (columnIndex == parent.columns.length - 1) { fillWidth -= 2; /* space for right bound of focus rect */ } - gc.fillRectangle (column.getX (), y + 2, fillWidth, itemHeight - 3); + if (fillWidth > 0) { + gc.fillRectangle (column.getX (), y + 2, fillWidth, itemHeight - 3); + } } gc.setBackground (oldBackground); } @@ -1058,14 +1101,15 @@ void removeColumn (TableColumn column, int index) { image = images [0]; images [0] = null; /* - * The new first column will not have as much width available to it as it did when it was - * the second column since it now has to show hierarchy decorations as well, so recompute - * its displayText. + * The new first column may not have as much width available to it as it did when it was + * the second column if checkboxes are being shown, so recompute its displayText if needed. */ - GC gc = new GC (parent); - gc.setFont (getFont (0)); - computeDisplayText (0, gc); - gc.dispose (); + if ((parent.style & SWT.CHECK) != 0) { + GC gc = new GC (parent); + gc.setFont (getFont (0)); + computeDisplayText (0, gc); + gc.dispose (); + } } if (columnCount < 2) { texts = null; @@ -1452,7 +1496,7 @@ public void setImage (int columnIndex, Image value) { } gc.dispose (); parent.redraw ( - 0, 0, + columns [0].getX (), 0, columns [0].width, parent.getClientArea ().height, true); |