1 /*******************************************************************************
 
   2  * Copyright (c) 2000, 2003 IBM Corporation and others.
 
   3  * All rights reserved. This program and the accompanying materials 
 
   4  * are made available under the terms of the Common Public License v1.0
 
   5  * which accompanies this distribution, and is available at
 
   6  * http://www.eclipse.org/legal/cpl-v10.html
 
   9  *     IBM Corporation - initial API and implementation
 
  10  *******************************************************************************/
 
  11 package net.sourceforge.phpdt.internal.ui.util;
 
  13 import java.util.ArrayList;
 
  14 import java.util.List;
 
  16 import org.eclipse.jface.util.Assert;
 
  17 import org.eclipse.jface.viewers.ColumnLayoutData;
 
  18 import org.eclipse.jface.viewers.ColumnPixelData;
 
  19 import org.eclipse.jface.viewers.ColumnWeightData;
 
  20 import org.eclipse.swt.SWT;
 
  21 import org.eclipse.swt.events.ControlAdapter;
 
  22 import org.eclipse.swt.events.ControlEvent;
 
  23 import org.eclipse.swt.graphics.Point;
 
  24 import org.eclipse.swt.graphics.Rectangle;
 
  25 import org.eclipse.swt.widgets.Composite;
 
  26 import org.eclipse.swt.widgets.Table;
 
  27 import org.eclipse.swt.widgets.TableColumn;
 
  30  * A special composite to layout columns inside a table. The composite is needed since we have
 
  31  * to layout the columns "before" the actual table gets layouted. Hence we can't use a normal
 
  34 public class TableLayoutComposite extends Composite {
 
  36         private List columns= new ArrayList();
 
  39          * Creates a new <code>TableLayoutComposite</code>.
 
  41         public TableLayoutComposite(Composite parent, int style) {
 
  43         addControlListener(new ControlAdapter() {
 
  44             public void controlResized(ControlEvent e) {
 
  45                 Rectangle area= getClientArea();
 
  46                 Table table= (Table)getChildren()[0];
 
  47                 Point preferredSize= computeTableSize(table);
 
  48                 int width= area.width - 2 * table.getBorderWidth();
 
  49                 if (preferredSize.y > area.height) {
 
  50                     // Subtract the scrollbar width from the total column width
 
  51                     // if a vertical scrollbar will be required
 
  52                     Point vBarSize = table.getVerticalBar().getSize();
 
  55                 layoutTable(table, width, area, table.getSize().x < area.width);
 
  61          * Adds a new column of data to this table layout.
 
  63          * @param data the column layout data
 
  65         public void addColumnData(ColumnLayoutData data) {
 
  69         //---- Helpers -------------------------------------------------------------------------------------
 
  71         private Point computeTableSize(Table table) {
 
  72                 Point result= table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
 
  75                 int size= columns.size();
 
  76                 for (int i= 0; i < size; ++i) {
 
  77                         ColumnLayoutData layoutData= (ColumnLayoutData) columns.get(i);
 
  78                         if (layoutData instanceof ColumnPixelData) {
 
  79                                 ColumnPixelData col= (ColumnPixelData) layoutData;
 
  81                         } else if (layoutData instanceof ColumnWeightData) {
 
  82                                 ColumnWeightData col= (ColumnWeightData) layoutData;
 
  83                                 width += col.minimumWidth;
 
  85                                 Assert.isTrue(false, "Unknown column layout data"); //$NON-NLS-1$
 
  93         private void layoutTable(Table table, int width, Rectangle area, boolean increase) {
 
  94                 // XXX: Layout is being called with an invalid value the first time
 
  95                 // it is being called on Linux. This method resets the
 
  96                 // Layout to null so we make sure we run it only when
 
 101                 TableColumn[] tableColumns= table.getColumns();
 
 102                 int size= Math.min(columns.size(), tableColumns.length);
 
 103                 int[] widths= new int[size];
 
 105                 int numberOfWeightColumns= 0;
 
 108                 // First calc space occupied by fixed columns
 
 109                 for (int i= 0; i < size; i++) {
 
 110                         ColumnLayoutData col= (ColumnLayoutData) columns.get(i);
 
 111                         if (col instanceof ColumnPixelData) {
 
 112                                 int pixels= ((ColumnPixelData) col).width;
 
 114                                 fixedWidth += pixels;
 
 115                         } else if (col instanceof ColumnWeightData) {
 
 116                                 ColumnWeightData cw= (ColumnWeightData) col;
 
 117                                 numberOfWeightColumns++;
 
 118                                 // first time, use the weight specified by the column data, otherwise use the actual width as the weight
 
 119                                 // int weight = firstTime ? cw.weight : tableColumns[i].getWidth();
 
 120                                 int weight= cw.weight;
 
 121                                 totalWeight += weight;
 
 123                                 Assert.isTrue(false, "Unknown column layout data"); //$NON-NLS-1$
 
 127                 // Do we have columns that have a weight
 
 128                 if (numberOfWeightColumns > 0) {
 
 129                         // Now distribute the rest to the columns with weight.
 
 130                         int rest= width - fixedWidth;
 
 131                         int totalDistributed= 0;
 
 132                         for (int i= 0; i < size; ++i) {
 
 133                                 ColumnLayoutData col= (ColumnLayoutData) columns.get(i);
 
 134                                 if (col instanceof ColumnWeightData) {
 
 135                                         ColumnWeightData cw= (ColumnWeightData) col;
 
 136                                         // calculate weight as above
 
 137                                         // int weight = firstTime ? cw.weight : tableColumns[i].getWidth();
 
 138                                         int weight= cw.weight;
 
 139                                         int pixels= totalWeight == 0 ? 0 : weight * rest / totalWeight;
 
 140                                         if (pixels < cw.minimumWidth)
 
 141                                                 pixels= cw.minimumWidth;
 
 142                                         totalDistributed += pixels;
 
 147                         // Distribute any remaining pixels to columns with weight.
 
 148                         int diff= rest - totalDistributed;
 
 149                         for (int i= 0; diff > 0; ++i) {
 
 152                                 ColumnLayoutData col= (ColumnLayoutData) columns.get(i);
 
 153                                 if (col instanceof ColumnWeightData) {
 
 161                         table.setSize(area.width, area.height);
 
 163                 for (int i= 0; i < size; i++) {
 
 164                         tableColumns[i].setWidth(widths[i]);
 
 167                         table.setSize(area.width, area.height);