Skip to content

Commit

Permalink
Pooled cells.
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanSweet committed May 28, 2013
1 parent e1f6615 commit 89648ba
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 24 deletions.
Expand Up @@ -35,6 +35,15 @@ class AndroidToolkit extends Toolkit<View, Table, TableLayout> {
static final HashMap<String, Integer> drawableToID = new HashMap();
static Paint paint;

public Cell obtainCell (TableLayout layout) {
Cell cell = new Cell();
cell.setLayout(layout);
return cell;
}

public void freeCell (Cell cell) {
}

public void setWidget (TableLayout layout, Cell cell, View widget) {
super.setWidget(layout, cell, widget);
layout.otherChildren.remove(widget);
Expand Down
@@ -1,24 +1,32 @@

package com.esotericsoftware.tablelayout.swing;

import com.esotericsoftware.tablelayout.BaseTableLayout.Debug;
import com.esotericsoftware.tablelayout.Cell;
import com.esotericsoftware.tablelayout.Toolkit;

import java.awt.Component;
import java.awt.Container;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

import com.esotericsoftware.tablelayout.BaseTableLayout.Debug;
import com.esotericsoftware.tablelayout.Toolkit;

class SwingToolkit extends Toolkit<Component, Table, TableLayout> {
static Timer timer;
static ArrayList<TableLayout> debugLayouts = new ArrayList(0);

public Cell obtainCell (TableLayout layout) {
Cell cell = new Cell();
cell.setLayout(layout);
return cell;
}

public void freeCell (Cell cell) {
}

public void addChild (Component parent, Component child) {
if (parent instanceof JScrollPane)
((JScrollPane)parent).setViewportView(child);
Expand Down
Expand Up @@ -2,13 +2,23 @@
package com.esotericsoftware.tablelayout.twl;

import com.esotericsoftware.tablelayout.BaseTableLayout.Debug;
import com.esotericsoftware.tablelayout.Cell;
import com.esotericsoftware.tablelayout.Toolkit;

import de.matthiasmann.twl.Widget;

public class TwlToolkit extends Toolkit<Widget, Table, TableLayout> {
static public final TwlToolkit instance = new TwlToolkit();

public Cell obtainCell (TableLayout layout) {
Cell cell = new Cell();
cell.setLayout(layout);
return cell;
}

public void freeCell (Cell cell) {
}

public void addChild (Widget parent, Widget child) {
parent.add(child);
}
Expand Down
Expand Up @@ -84,7 +84,7 @@ public void invalidate () {

/** Adds a new cell to the table with the specified widget. */
public Cell<C> add (C widget) {
Cell cell = new Cell(this);
Cell cell = toolkit.obtainCell((L)this);
cell.widget = widget;

if (cells.size() > 0) {
Expand Down Expand Up @@ -127,7 +127,7 @@ public Cell<C> add (C widget) {
* for all cells in the new row. */
public Cell row () {
if (cells.size() > 0) endRow();
rowDefaults = new Cell(this);
rowDefaults = toolkit.obtainCell((L)this);
return rowDefaults;
}

Expand All @@ -149,7 +149,7 @@ private void endRow () {
public Cell columnDefaults (int column) {
Cell cell = columnDefaults.size() > column ? columnDefaults.get(column) : null;
if (cell == null) {
cell = new Cell(this);
cell = toolkit.obtainCell((L)this);
if (column >= columnDefaults.size()) {
for (int i = columnDefaults.size(); i < column; i++)
columnDefaults.add(null);
Expand Down Expand Up @@ -591,7 +591,7 @@ private void computeSize () {
if (prefWidth < minWidth) prefWidth = minWidth;
if (maxWidth > 0 && prefWidth > maxWidth) prefWidth = maxWidth;

float spannedMinWidth = 0, spannedPrefWidth = 0;
float spannedMinWidth = -(c.computedPadLeft + c.computedPadRight), spannedPrefWidth = spannedMinWidth;
for (int column = c.column, nn = column + c.colspan; column < nn; column++) {
spannedMinWidth += columnMinWidth[column];
spannedPrefWidth += columnPrefWidth[column];
Expand Down Expand Up @@ -681,25 +681,19 @@ public void layout (float layoutX, float layoutY, float layoutWidth, float layou
float hpadding = w(padLeft) + w(padRight);
float vpadding = h(padTop) + h(padBottom);

// totalMinWidth/totalMinHeight are needed because tableMinWidth/tableMinHeight could be based on this.width or this.height.
float totalMinWidth = 0, totalMinHeight = 0;
float totalExpandWidth = 0, totalExpandHeight = 0;
for (int i = 0; i < columns; i++) {
totalMinWidth += columnMinWidth[i];
for (int i = 0; i < columns; i++)
totalExpandWidth += expandWidth[i];
}
for (int i = 0; i < rows; i++) {
totalMinHeight += rowMinHeight[i];
for (int i = 0; i < rows; i++)
totalExpandHeight += expandHeight[i];
}

// Size columns and rows between min and pref size using (preferred - min) size to weight distribution of extra space.
float[] columnWeightedWidth;
float totalGrowWidth = tablePrefWidth - totalMinWidth;
float totalGrowWidth = tablePrefWidth - tableMinWidth;
if (totalGrowWidth == 0)
columnWeightedWidth = columnMinWidth;
else {
float extraWidth = Math.min(totalGrowWidth, Math.max(0, layoutWidth - totalMinWidth));
float extraWidth = Math.min(totalGrowWidth, Math.max(0, layoutWidth - tableMinWidth));
columnWeightedWidth = this.columnWeightedWidth = ensureSize(this.columnWeightedWidth, columns);
for (int i = 0; i < columns; i++) {
float growWidth = columnPrefWidth[i] - columnMinWidth[i];
Expand All @@ -709,12 +703,12 @@ public void layout (float layoutX, float layoutY, float layoutWidth, float layou
}

float[] rowWeightedHeight;
float totalGrowHeight = tablePrefHeight - totalMinHeight;
float totalGrowHeight = tablePrefHeight - tableMinHeight;
if (totalGrowHeight == 0)
rowWeightedHeight = rowMinHeight;
else {
rowWeightedHeight = this.rowWeightedHeight = ensureSize(this.rowWeightedHeight, rows);
float extraHeight = Math.min(totalGrowHeight, Math.max(0, layoutHeight - totalMinHeight));
float extraHeight = Math.min(totalGrowHeight, Math.max(0, layoutHeight - tableMinHeight));
for (int i = 0; i < rows; i++) {
float growHeight = rowPrefHeight[i] - rowMinHeight[i];
float growRatio = growHeight / (float)totalGrowHeight;
Expand Down
10 changes: 7 additions & 3 deletions tablelayout/src/com/esotericsoftware/tablelayout/Cell.java
Expand Up @@ -50,13 +50,16 @@ public class Cell<C> {
float widgetX, widgetY;
float widgetWidth, widgetHeight;

private final BaseTableLayout layout;
private BaseTableLayout layout;
boolean endRow;
int column, row;
int cellAboveIndex = -1;
float computedPadTop, computedPadLeft, computedPadBottom, computedPadRight;

Cell (BaseTableLayout layout) {
public Cell () {
}

public void setLayout (BaseTableLayout layout) {
this.layout = layout;
}

Expand Down Expand Up @@ -907,7 +910,8 @@ public BaseTableLayout getLayout () {
}

static Cell defaults (BaseTableLayout layout) {
Cell defaults = new Cell(layout);
Cell defaults = new Cell();
defaults.layout = layout;
defaults.minWidth = Value.minWidth;
defaults.minHeight = Value.minHeight;
defaults.prefWidth = Value.prefWidth;
Expand Down
4 changes: 4 additions & 0 deletions tablelayout/src/com/esotericsoftware/tablelayout/Toolkit.java
Expand Up @@ -34,6 +34,10 @@
public abstract class Toolkit<C, T extends C, L extends BaseTableLayout> {
static public Toolkit instance;

abstract public Cell obtainCell (L layout);

abstract public void freeCell (Cell cell);

abstract public void addChild (C parent, C child);

abstract public void removeChild (C parent, C child);
Expand Down

0 comments on commit 89648ba

Please sign in to comment.