You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2009/08/27 16:22:27 UTC
svn commit: r808426 - in
/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra:
TerraTableViewHeaderSkin.java TerraTableViewSkin.java
Author: gbrown
Date: Thu Aug 27 14:22:27 2009
New Revision: 808426
URL: http://svn.apache.org/viewvc?rev=808426&view=rev
Log:
Optimize column width calculations in TableView and TableViewHeader skins.
Modified:
incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTableViewHeaderSkin.java
incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTableViewSkin.java
Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTableViewHeaderSkin.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTableViewHeaderSkin.java?rev=808426&r1=808425&r2=808426&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTableViewHeaderSkin.java (original)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTableViewHeaderSkin.java Thu Aug 27 14:22:27 2009
@@ -24,6 +24,7 @@
import java.awt.RenderingHints;
import java.awt.geom.GeneralPath;
+import org.apache.pivot.collections.ArrayList;
import org.apache.pivot.collections.Sequence;
import org.apache.pivot.wtk.Bounds;
import org.apache.pivot.wtk.Component;
@@ -105,11 +106,12 @@
private boolean columnsResizable;
private boolean includeTrailingVerticalGridLine;
- // Derived colors
private Color bevelColor;
private Color pressedBevelColor;
private Color disabledBevelColor;
+ private ArrayList<Integer> columnWidths = null;
+
private int pressedHeaderIndex = -1;
private int resizeHeaderIndex = -1;
@@ -221,7 +223,18 @@
}
public void layout() {
- // No-op
+ TableViewHeader tableViewHeader = (TableViewHeader)getComponent();
+ TableView tableView = tableViewHeader.getTableView();
+
+ TableView.ColumnSequence columns = tableView.getColumns();
+ int n = columns.getLength();
+
+ columnWidths = new ArrayList<Integer>(n);
+
+ for (int i = 0; i < n; i++) {
+ Bounds columnBounds = tableView.getColumnBounds(i);
+ columnWidths.add(columnBounds.width);
+ }
}
public void paint(Graphics2D graphics) {
@@ -259,9 +272,6 @@
TableView.ColumnSequence columns = tableView.getColumns();
TableViewHeader.DataRenderer dataRenderer = tableViewHeader.getDataRenderer();
- Sequence<Integer> columnWidths = TerraTableViewSkin.getColumnWidths(tableView,
- getWidth());
-
int cellX = 0;
for (int columnIndex = 0, columnCount = columns.getLength();
columnIndex < columnCount; columnIndex++) {
@@ -338,9 +348,6 @@
TableView tableView = tableViewHeader.getTableView();
if (tableView != null) {
- Sequence<Integer> columnWidths = TerraTableViewSkin.getColumnWidths(tableView,
- getWidth());
-
int i = 0;
int n = columnWidths.getLength();
int columnX = 0;
@@ -365,9 +372,6 @@
TableView tableView = tableViewHeader.getTableView();
if (tableView != null) {
- Sequence<Integer> columnWidths = TerraTableViewSkin.getColumnWidths(tableView,
- getWidth());
-
if (index < 0
|| index >= columnWidths.getLength()) {
throw new IndexOutOfBoundsException();
Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTableViewSkin.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTableViewSkin.java?rev=808426&r1=808425&r2=808426&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTableViewSkin.java (original)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTableViewSkin.java Thu Aug 27 14:22:27 2009
@@ -71,6 +71,8 @@
private boolean showHighlight;
private boolean includeTrailingVerticalGridLine;
+ private ArrayList<Integer> columnWidths = null;
+
private int highlightedIndex = -1;
private int editIndex = -1;
@@ -156,7 +158,58 @@
}
public void layout() {
- // No-op
+ // Recalculate column widths
+ TableView tableView = (TableView)getComponent();
+
+ int width = getWidth();
+
+ int fixedWidth = 0;
+ int relativeWidth = 0;
+
+ TableView.ColumnSequence columns = tableView.getColumns();
+ int n = columns.getLength();
+
+ columnWidths = new ArrayList<Integer>(n);
+
+ for (int i = 0; i < n; i++) {
+ TableView.Column column = columns.get(i);
+
+ if (column.isRelative()) {
+ columnWidths.add(0);
+ relativeWidth += column.getWidth();
+ } else {
+ int columnWidth = column.getWidth();
+
+ if (columnWidth == -1) {
+ // Calculate the maximum cell width
+ columnWidth = 0;
+
+ TableView.CellRenderer cellRenderer = column.getCellRenderer();
+ List<?> tableData = tableView.getTableData();
+
+ for (Object rowData : tableData) {
+ cellRenderer.render(rowData, tableView, column, false, false, false);
+ columnWidth = Math.max(cellRenderer.getPreferredWidth(-1), columnWidth);
+ }
+ }
+
+ columnWidths.add(columnWidth);
+ fixedWidth += columnWidth;
+ }
+ }
+
+ fixedWidth += n - 1;
+ int variableWidth = Math.max(width - fixedWidth, 0);
+
+ for (int i = 0; i < n; i++) {
+ TableView.Column column = columns.get(i);
+
+ if (column.isRelative()) {
+ int columnWidth = (int)Math.round((double)(column.getWidth()
+ * variableWidth) / (double)relativeWidth);
+ columnWidths.update(i ,columnWidth);
+ }
+ }
}
@SuppressWarnings("unchecked")
@@ -169,7 +222,6 @@
int height = getHeight();
int rowHeight = getRowHeight();
- Sequence<Integer> columnWidths = getColumnWidths();
// Paint the background
graphics.setPaint(backgroundColor);
@@ -327,83 +379,6 @@
return rowHeight;
}
- /**
- * Returns the column widths for this table.
- *
- * @return
- * The widths of all columns based on the current overall width.
- */
- public Sequence<Integer> getColumnWidths() {
- return getColumnWidths((TableView)getComponent(), getWidth());
- }
-
- /**
- * Returns the column widths, determined by applying relative size values
- * to the available width.
- *
- * @param columns
- * The columns whose widths are to be determined.
- *
- * @param width
- * The total available width for the columns.
- *
- * @return
- * The widths of all columns based on the current overall width.
- */
- public static Sequence<Integer> getColumnWidths(TableView tableView, int width) {
- // TODO Cache these values and recalculate only when size changes?
-
- int fixedWidth = 0;
- int relativeWidth = 0;
-
- TableView.ColumnSequence columns = tableView.getColumns();
- int n = columns.getLength();
-
- ArrayList<Integer> columnWidths = new ArrayList<Integer>(n);
-
- for (int i = 0; i < n; i++) {
- TableView.Column column = columns.get(i);
-
- if (column.isRelative()) {
- columnWidths.add(0);
- relativeWidth += column.getWidth();
- } else {
- int columnWidth = column.getWidth();
-
- if (columnWidth == -1) {
- // Calculate the maximum cell width
- columnWidth = 0;
-
- TableView.CellRenderer cellRenderer = column.getCellRenderer();
- List<?> tableData = tableView.getTableData();
-
- for (Object rowData : tableData) {
- cellRenderer.render(rowData, tableView, column, false, false, false);
- columnWidth = Math.max(cellRenderer.getPreferredWidth(-1), columnWidth);
- }
- }
-
- columnWidths.add(columnWidth);
- fixedWidth += columnWidth;
- }
- }
-
- fixedWidth += n - 1;
- int variableWidth = Math.max(width - fixedWidth, 0);
-
- for (int i = 0; i < n; i++) {
- TableView.Column column = columns.get(i);
-
- if (column.isRelative()) {
- int columnWidth = (int)Math.round((double)(column.getWidth()
- * variableWidth) / (double)relativeWidth);
- columnWidths.update(i ,columnWidth);
- }
- }
-
- return columnWidths;
- }
-
// Table view skin methods
@SuppressWarnings("unchecked")
public int getRowAt(int y) {
@@ -428,8 +403,6 @@
throw new IllegalArgumentException("x is negative");
}
- Sequence<Integer> columnWidths = getColumnWidths();
-
int i = 0;
int n = columnWidths.getLength();
int columnX = 0;
@@ -454,7 +427,6 @@
}
public Bounds getColumnBounds(int columnIndex) {
- Sequence<Integer> columnWidths = getColumnWidths();
int columnCount = columnWidths.getLength();
if (columnIndex < 0
@@ -475,7 +447,6 @@
public Bounds getCellBounds(int rowIndex, int columnIndex) {
TableView tableView = (TableView)getComponent();
List<Object> tableData = (List<Object>)tableView.getTableData();
- Sequence<Integer> columnWidths = getColumnWidths();
if (rowIndex < 0
|| rowIndex >= tableData.getLength()) {
@@ -1084,7 +1055,9 @@
}
public void columnSortDirectionChanged(TableView.Column column, SortDirection previousSortDirection) {
- // TODO Repaint; paint a "selection" color for the sorted column
+ TableView tableView = column.getTableView();
+ int columnIndex = tableView.getColumns().indexOf(column);
+ repaintComponent(getColumnBounds(columnIndex));
}
public void columnFilterChanged(TableView.Column column, Object previousFilter) {