1 11 package org.eclipse.jdt.internal.ui.util; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.core.runtime.Assert; 17 18 import org.eclipse.swt.SWT; 19 import org.eclipse.swt.events.ControlAdapter; 20 import org.eclipse.swt.events.ControlEvent; 21 import org.eclipse.swt.graphics.Point; 22 import org.eclipse.swt.graphics.Rectangle; 23 import org.eclipse.swt.widgets.Composite; 24 import org.eclipse.swt.widgets.Table; 25 import org.eclipse.swt.widgets.TableColumn; 26 27 import org.eclipse.jface.viewers.ColumnLayoutData; 28 import org.eclipse.jface.viewers.ColumnPixelData; 29 import org.eclipse.jface.viewers.ColumnWeightData; 30 31 36 public class TableLayoutComposite extends Composite { 37 38 45 private static int COLUMN_TRIM = "carbon".equals(SWT.getPlatform()) ? 24 : 3; 47 private List columns= new ArrayList (); 48 49 52 public TableLayoutComposite(Composite parent, int style) { 53 super(parent, style); 54 addControlListener(new ControlAdapter() { 55 public void controlResized(ControlEvent e) { 56 Rectangle area= getClientArea(); 57 Table table= (Table)getChildren()[0]; 58 Point preferredSize= computeTableSize(table); 59 int width= area.width - 2 * table.getBorderWidth(); 60 if (preferredSize.y > area.height) { 61 Point vBarSize = table.getVerticalBar().getSize(); 64 width -= vBarSize.x; 65 } 66 layoutTable(table, width, area, table.getSize().x < area.width); 67 } 68 }); 69 } 70 71 76 public void addColumnData(ColumnLayoutData data) { 77 columns.add(data); 78 } 79 80 82 private Point computeTableSize(Table table) { 83 Point result= table.computeSize(SWT.DEFAULT, SWT.DEFAULT); 84 85 int width= 0; 86 int size= columns.size(); 87 for (int i= 0; i < size; ++i) { 88 ColumnLayoutData layoutData= (ColumnLayoutData) columns.get(i); 89 if (layoutData instanceof ColumnPixelData) { 90 ColumnPixelData col= (ColumnPixelData) layoutData; 91 width += col.width; 92 if (col.addTrim) { 93 width += COLUMN_TRIM; 94 } 95 } else if (layoutData instanceof ColumnWeightData) { 96 ColumnWeightData col= (ColumnWeightData) layoutData; 97 width += col.minimumWidth; 98 } else { 99 Assert.isTrue(false, "Unknown column layout data"); } 101 } 102 if (width > result.x) 103 result.x= width; 104 return result; 105 } 106 107 private void layoutTable(Table table, int width, Rectangle area, boolean increase) { 108 if (width <= 1) 113 return; 114 115 TableColumn[] tableColumns= table.getColumns(); 116 int size= Math.min(columns.size(), tableColumns.length); 117 int[] widths= new int[size]; 118 int fixedWidth= 0; 119 int numberOfWeightColumns= 0; 120 int totalWeight= 0; 121 122 for (int i= 0; i < size; i++) { 124 ColumnLayoutData col= (ColumnLayoutData) columns.get(i); 125 if (col instanceof ColumnPixelData) { 126 ColumnPixelData cpd= (ColumnPixelData) col; 127 int pixels= cpd.width; 128 if (cpd.addTrim) { 129 pixels += COLUMN_TRIM; 130 } 131 widths[i]= pixels; 132 fixedWidth += pixels; 133 } else if (col instanceof ColumnWeightData) { 134 ColumnWeightData cw= (ColumnWeightData) col; 135 numberOfWeightColumns++; 136 int weight= cw.weight; 139 totalWeight += weight; 140 } else { 141 Assert.isTrue(false, "Unknown column layout data"); } 143 } 144 145 if (numberOfWeightColumns > 0) { 147 int rest= width - fixedWidth; 149 int totalDistributed= 0; 150 for (int i= 0; i < size; ++i) { 151 ColumnLayoutData col= (ColumnLayoutData) columns.get(i); 152 if (col instanceof ColumnWeightData) { 153 ColumnWeightData cw= (ColumnWeightData) col; 154 int weight= cw.weight; 157 int pixels= totalWeight == 0 ? 0 : weight * rest / totalWeight; 158 if (pixels < cw.minimumWidth) 159 pixels= cw.minimumWidth; 160 totalDistributed += pixels; 161 widths[i]= pixels; 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 |