1 11 package org.eclipse.update.internal.ui.wizards; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.swt.SWT; 17 import org.eclipse.swt.events.ControlAdapter; 18 import org.eclipse.swt.events.ControlEvent; 19 import org.eclipse.swt.graphics.Point; 20 import org.eclipse.swt.graphics.Rectangle; 21 import org.eclipse.swt.widgets.Composite; 22 import org.eclipse.swt.widgets.Table; 23 import org.eclipse.swt.widgets.TableColumn; 24 import org.eclipse.core.runtime.Assert; 25 import org.eclipse.jface.viewers.ColumnLayoutData; 26 import org.eclipse.jface.viewers.ColumnPixelData; 27 import org.eclipse.jface.viewers.ColumnWeightData; 28 29 34 public class TableLayoutComposite extends Composite { 35 36 private List columns= new ArrayList (); 37 38 45 private static int COLUMN_TRIM = "carbon".equals(SWT.getPlatform()) ? 24 : 3; 47 50 public TableLayoutComposite(Composite parent, int style) { 51 super(parent, style); 52 addControlListener(new ControlAdapter() { 53 public void controlResized(ControlEvent e) { 54 Rectangle area= getClientArea(); 55 Table table= (Table)getChildren()[0]; 56 Point preferredSize= computeTableSize(table); 57 int width= area.width - 2 * table.getBorderWidth(); 58 if (preferredSize.y > area.height) { 59 Point vBarSize = table.getVerticalBar().getSize(); 62 width -= vBarSize.x; 63 } 64 layoutTable(table, width, area, table.getSize().x < area.width); 65 } 66 }); 67 } 68 69 74 public void addColumnData(ColumnLayoutData data) { 75 columns.add(data); 76 } 77 78 80 private Point computeTableSize(Table table) { 81 Point result= table.computeSize(SWT.DEFAULT, SWT.DEFAULT); 82 83 int width= 0; 84 int size= columns.size(); 85 for (int i= 0; i < size; ++i) { 86 ColumnLayoutData layoutData= (ColumnLayoutData) columns.get(i); 87 if (layoutData instanceof ColumnPixelData) { 88 ColumnPixelData col= (ColumnPixelData) layoutData; 89 width += col.width; 90 if (col.addTrim) { 91 width += COLUMN_TRIM; 92 } 93 } else if (layoutData instanceof ColumnWeightData) { 94 ColumnWeightData col= (ColumnWeightData) layoutData; 95 width += col.minimumWidth; 96 } else { 97 Assert.isTrue(false, "Unknown column layout data"); } 99 } 100 if (width > result.x) 101 result.x= width; 102 return result; 103 } 104 105 private void layoutTable(Table table, int width, Rectangle area, boolean increase) { 106 if (width <= 1) 111 return; 112 113 TableColumn[] tableColumns= table.getColumns(); 114 int size= Math.min(columns.size(), tableColumns.length); 115 int[] widths= new int[size]; 116 int fixedWidth= 0; 117 int numberOfWeightColumns= 0; 118 int totalWeight= 0; 119 120 for (int i= 0; i < size; i++) { 122 ColumnLayoutData col= (ColumnLayoutData) columns.get(i); 123 if (col instanceof ColumnPixelData) { 124 ColumnPixelData cpd = (ColumnPixelData) col; 125 int pixels = cpd.width; 126 if (cpd.addTrim) { 127 pixels += COLUMN_TRIM; 128 } 129 widths[i]= pixels; 130 fixedWidth += pixels; 131 } else if (col instanceof ColumnWeightData) { 132 ColumnWeightData cw= (ColumnWeightData) col; 133 numberOfWeightColumns++; 134 int weight= cw.weight; 137 fixedWidth+=cw.minimumWidth; 138 totalWeight += weight; 139 } else { 140 Assert.isTrue(false, "Unknown column layout data"); } 142 } 143 144 if (numberOfWeightColumns > 0) { 146 int rest= width - fixedWidth; 148 int totalDistributed= 0; 149 for (int i= 0; i < size; ++i) { 150 ColumnLayoutData col= (ColumnLayoutData) columns.get(i); 151 if (col instanceof ColumnWeightData) { 152 ColumnWeightData cw= (ColumnWeightData) col; 153 int weight= cw.weight; 156 int sparePixels= totalWeight == 0 ? 0 : weight * rest / totalWeight; 157 int newWidth = cw.minimumWidth; 158 if (sparePixels >0 ) 159 newWidth += sparePixels; 160 totalDistributed += newWidth; 161 widths[i]= newWidth; 162 } 163 } 164 165 int diff= rest - totalDistributed; 167 for (int i= 0; diff > 0; ++i) { 168 if (i == size) 169 i= 0; 170 ColumnLayoutData col= (ColumnLayoutData) columns.get(i); 171 if (col instanceof ColumnWeightData) { 172 ++widths[i]; 173 --diff; 174 } 175 } 176 } 177 178 if (increase) { 179 table.setSize(area.width, area.height); 180 } 181 for (int i= 0; i < size; i++) { 182 tableColumns[i].setWidth(widths[i]); 183 } 184 if (!increase) { 185 table.setSize(area.width, area.height); 186 } 187 } 188 } 189 | Popular Tags |