at.spardat.xma.mdl.table.TableLayoutManager Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* s IT Solutions AT Spardat GmbH - initial API and implementation
*******************************************************************************/
/*
* @(#) $Id: TableLayoutManager.java 5024 2010-02-10 16:19:12Z gub $
*
*
*
*
*
*/
package at.spardat.xma.mdl.table;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
/**
* TableLayoutManager implements a more sophisticated Layout for Table columns than just column widths.
* For each column the follwing can be defined:
*
* - The absolut width in pixel.
*
- The width as percentage of the width of the whole table.
*
- The minimum width in pixel.
*
- The maximum width in pixel.
*
* Only the absolut width or a percentage may be specified for each individual column.
* Everytime, the table is resized, this information is used to calculate new widths of the columns.
* If the end user resizes a comlumn header via the GUI, the absolute width or percentage is
* ajusted automatically. If the end user violates the minimum or maximum, the respective limit is
* ajusted, too. Only the minLimit is enforced to prevent the user from completly removing a column.
*
* A default width for columns without absolut width or percentage definition may be defined.
* Default minimum and maximun width for columns without individal limits may be defined, too.
*
* One column may be specified to strech over the rest of the table width. Its absolute width
* or percentage is ignored. Only its limits are respected.
*/
public class TableLayoutManager implements ControlListener {
/**
* Use no information from the table.
*/
public static final int NONE = 0;
/**
* Calculate the percentage of the column based on its current width and the current width of the table.
*/
public static final int PERCENT = 1;
/**
* Determine that the calculation of percentages should always yield a sum of 100 percent.
*/
public static final int SCALED = 1<<4;
/**
* Calculate the percentage of the column based on its current width and the sum of all column widths.
* This always yields a sum of 100 percent.
* SCALEDPERCENT = PERCENT | SCALED
*/
public static final int SCALEDPERCENT = PERCENT | SCALED;
/**
* Use the currend width of the table as it's minimum width.
*/
public static final int MIN = 1<<1;
/**
* Use the currend width of the table as it's maximum width.
*/
public static final int MAX = 1<<2;
/**
* Use the currend width of the table as it's absolut width.
*/
public static final int ABSOLUT = 1<<3;
int[] min,max,absolut;
float[] percent;
int minLimit=10;
Table table;
int tableWidth;
int strech = -1;
int strechWidth;
int defaultMinWidth=minLimit;
int defaultMaxWidth=Short.MAX_VALUE;
int defaultAbsolutWidth=40;
boolean layouting=false;
/**
* Attaches the TableLayoutManager to an SWT-Table. From this time on, the column widths are controled by
* the TableLayoutManager.
* @param table to manage
* @param take defines which layout information should be calculated from the current width of the columns.
* possible values: {@link #PERCENT}, {@link #SCALEDPERCENT}, {@link #MIN}, {@link #MAX}, {@link #ABSOLUT}. These value may be ored
* together to take more information.
*/
public void manageTable(Table table,int take) {
if(this.table!=null) { // remove from old table if any
this.table.removeControlListener(this);
for(int i=0,size=this.table.getColumnCount();i -1 means remove the percentage information.
*/
public void setPercent(int column,float percent) {
this.percent[column]=percent;
this.absolut[column]=-1;
}
/**
* Get the minimum width of the given column in pixel.
* @param column index of the column in the SWT-Table.
*/
public int getMinWidth(int column) {
return min[column];
}
/**
* Set the minimum width of the given column in pixel.
* @param column index of the column in the SWT-Table.
*/
public void setMinWidth(int column, int min) {
// if(min=0) percent[i]*=factor;
}
}
/**
* Return the lower limit for all column widths. If the end user ties to resize any
* column to a smaler width, this column width will be set to minLimit.
*/
public int getMinLimit() {
return minLimit;
}
/**
* Set the lower limit for all column widths. If the end user ties to resize any
* column to a smaler width, this column width will be set to minLimit.
*/
public void setMinLimit(int minLimit) {
this.minLimit = minLimit;
if(defaultMinWidthca.height) {
//// //
// ScrollBar bar = table.getVerticalBar();
// if(bar!=null) width -= bar.getSize().x;
//// }
// return width;
return table.getClientArea().width;
}
/**
* Recalculate the width of all columns according to the layout information of this TableLayoutManager.
* The SWT-columns are resized to the new widths. Same as {@link #layout(boolean) layout(true)}.
*/
public void layout() {
layout(true);
}
/**
* Recalculate the width of all columns according to the layout information of this TableLayoutManager.
* The SWT-columns are resized to the new widths.
* @param force if false recalculate the column widths if the table width changed since the last calculation.
* if true recalculate in any case.
*/
public void layout(boolean force) {
int newTableWidth = getTableClientWidth(table);
if(force||tableWidth!=newTableWidth) {
layouting=true;
if(percent.length!=table.getColumnCount()) throw new IllegalStateException();
tableWidth=newTableWidth;
int restWidth = tableWidth;
for(int i=0;i=0&&strechstrechWidth) strechWidth=min[strech];
if(max[strech]=0) width = (int)(tableWidth*percent[i]/100);
else width = absolut[i];
if(min[i]>width) width=min[i];
if(max[i]width) width=minLimit;
int oldWidth=calcWidth(index);
if(width!=oldWidth) {
if(percent[index]>=0) percent[index]=(float)width/tableWidth*100;
else absolut[index]=width;
if(min[index]>width) min[index]=width;
if(max[index]=0) percent[i]*=factor;
}
layout(true);
}
/**
* Listener method called by SWT every time the table is moved. Does nothing.
*/
public void controlMoved(ControlEvent e) {}
/**
* Listener method called by SWT every time the table or a column is resized.
* If the table is resized calls {#link #layout(boolean)}.
* If a column is resized calls {#link #columnResized}.
*/
public void controlResized(ControlEvent e) {
if(layouting) return; // ignore self generated events
if(e.getSource()==table) {
table.getDisplay().asyncExec(new Runnable() {
public void run() {
if(!table.isDisposed()) {
layout(false); // if it is executed inside the event method, a horizontal scrollbar apears on shrink
}
}
});
} else if(e.getSource() instanceof TableColumn) {
final TableColumn column = (TableColumn) e.getSource();
table.getDisplay().asyncExec(new Runnable() {
public void run() {
if(!column.isDisposed()) {
// if it is executed inside the event method,
// the horizontal scrollbar does not appear or disappear correctly
columnResized(column);
}
}
});
}
}
/**
* Resizes the given column to zero and scales up all columns with
* percentage widths to fill up the gap.
* @param index of the column to collapse
* @since 2.1.1
*/
public void collapseColumn(int column) {
int oldWidth=calcWidth(column);
int columnSum=0;
for(int i=0;i1) { // only growing is allowed
for(int i=0;i=0) percent[i]*=factor;
}
}
layout();
}
}