package org.mwc.asset.SimulationController.table;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;

public class ColumnsResizer
{

	private final Table myTable;

	private final int myFixedColumnsWidth;

	private Iterable<ColumnSizeData> mySizeDatas;

	private int myTableWidth;

	private int myUserTableWidth;

	private int myUserTotalWidth;

	public ColumnsResizer(Table table, int fixedColumnsWidth,
			final boolean fitOnResize)
	{
		myTable = table;
		myFixedColumnsWidth = fixedColumnsWidth;
		myTable.addListener(SWT.Resize, new Listener()
		{

			public void handleEvent(Event event)
			{
				if (mySizeDatas != null)
				{
					doResize(true, fitOnResize);
				}
			}
		});
	}

	private void doResize(boolean checkWidth, boolean fitTable)
	{
		if (checkWidth && myTableWidth == myTable.getClientArea().width)
		{
			return;
		}

		boolean updateWeights = false;
		for (ColumnSizeData sizeData : mySizeDatas)
		{
			if (sizeData.isWidthChanged())
			{
				updateWeights = true;
				break;
			}
		}
		if (updateWeights)
		{
			myUserTableWidth = myTableWidth;
			myUserTotalWidth = myFixedColumnsWidth;
			for (ColumnSizeData sizeData : mySizeDatas)
			{
				sizeData.updateWeight();
				myUserTotalWidth += sizeData.getTableColumn().getWidth();
			}
		}

		myTableWidth = myTable.getClientArea().width;

		int totalWidth;
		if (fitTable)
		{
			totalWidth = myTableWidth;
			myUserTableWidth = myTableWidth;
			myUserTotalWidth = totalWidth;
		}
		else
		{
			totalWidth = myUserTotalWidth * myTableWidth / myUserTableWidth;
		}
		totalWidth -= myFixedColumnsWidth;

		int totalWeight = 0;
		for (ColumnSizeData sizeData : mySizeDatas)
		{
			totalWeight += sizeData.getWeight();
		}
		for (ColumnSizeData sizeData : mySizeDatas)
		{
			sizeData.setWidth(totalWidth * sizeData.getWeight() / totalWeight);
		}
	}

	public void fitTableWidth()
	{
		doResize(false, true);
	}

	public void setSizeDatas(Iterable<ColumnSizeData> sizeDatas)
	{
		mySizeDatas = sizeDatas;
	}
}

@@ -1419,16 +1419,18 @@
 %0A%09%09%7D%0D%0A%0D%0A
+%0D%0A
 %09%09myTabl
@@ -1630,24 +1630,112 @@
 %09else%0D%0A%09%09%7B%0D%0A
+%09%09%09// just check we haven't been collapsed%0D%0A%09%09%09if (myUserTableWidth == 0)%0D%0A%09%09%09%09return;%0D%0A
 %09%09%09totalWidt

handle resize if table is collapsed