1 11 package org.eclipse.jface.viewers; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.core.runtime.Assert; 17 import org.eclipse.jface.layout.TableColumnLayout; 18 import org.eclipse.swt.SWT; 19 import org.eclipse.swt.graphics.Point; 20 import org.eclipse.swt.widgets.Composite; 21 import org.eclipse.swt.widgets.Item; 22 import org.eclipse.swt.widgets.Layout; 23 import org.eclipse.swt.widgets.Table; 24 import org.eclipse.swt.widgets.TableColumn; 25 import org.eclipse.swt.widgets.Tree; 26 import org.eclipse.swt.widgets.TreeColumn; 27 28 34 public class TableLayout extends Layout { 35 36 43 private static int COLUMN_TRIM = "carbon".equals(SWT.getPlatform()) ? 24 : 3; 45 49 private List columns = new ArrayList (); 50 51 54 private boolean firstTime = true; 55 56 59 public TableLayout() { 60 } 61 62 68 public void addColumnData(ColumnLayoutData data) { 69 columns.add(data); 70 } 71 72 75 public Point computeSize(Composite c, int wHint, int hHint, boolean flush) { 76 if (wHint != SWT.DEFAULT && hHint != SWT.DEFAULT) { 77 return new Point(wHint, hHint); 78 } 79 80 Table table = (Table) c; 81 table.setLayout(null); 83 Point result = table.computeSize(wHint, hHint, flush); 85 table.setLayout(this); 86 87 int width = 0; 88 int size = columns.size(); 89 for (int i = 0; i < size; ++i) { 90 ColumnLayoutData layoutData = (ColumnLayoutData) columns.get(i); 91 if (layoutData instanceof ColumnPixelData) { 92 ColumnPixelData col = (ColumnPixelData) layoutData; 93 width += col.width; 94 if (col.addTrim) { 95 width += COLUMN_TRIM; 96 } 97 } else if (layoutData instanceof ColumnWeightData) { 98 ColumnWeightData col = (ColumnWeightData) layoutData; 99 width += col.minimumWidth; 100 } else { 101 Assert.isTrue(false, "Unknown column layout data"); } 103 } 104 if (width > result.x) { 105 result.x = width; 106 } 107 return result; 108 } 109 110 113 public void layout(Composite c, boolean flush) { 114 if (!firstTime) { 121 return; 122 } 123 124 int width = c.getClientArea().width; 125 126 if (width <= 1) { 131 return; 132 } 133 134 Item[] tableColumns = getColumns(c); 135 int size = Math.min(columns.size(), tableColumns.length); 136 int[] widths = new int[size]; 137 int fixedWidth = 0; 138 int numberOfWeightColumns = 0; 139 int totalWeight = 0; 140 141 for (int i = 0; i < size; i++) { 143 ColumnLayoutData col = (ColumnLayoutData) columns.get(i); 144 if (col instanceof ColumnPixelData) { 145 ColumnPixelData cpd = (ColumnPixelData) col; 146 int pixels = cpd.width; 147 if (cpd.addTrim) { 148 pixels += COLUMN_TRIM; 149 } 150 widths[i] = pixels; 151 fixedWidth += pixels; 152 } else if (col instanceof ColumnWeightData) { 153 ColumnWeightData cw = (ColumnWeightData) col; 154 numberOfWeightColumns++; 155 int weight = cw.weight; 160 totalWeight += weight; 161 } else { 162 Assert.isTrue(false, "Unknown column layout data"); } 164 } 165 166 if (numberOfWeightColumns > 0) { 168 int rest = width - fixedWidth; 170 int totalDistributed = 0; 171 for (int i = 0; i < size; ++i) { 172 ColumnLayoutData col = (ColumnLayoutData) columns.get(i); 173 if (col instanceof ColumnWeightData) { 174 ColumnWeightData cw = (ColumnWeightData) col; 175 int weight = cw.weight; 179 int pixels = totalWeight == 0 ? 0 : weight * rest 180 / totalWeight; 181 if (pixels < cw.minimumWidth) { 182 pixels = cw.minimumWidth; 183 } 184 totalDistributed += pixels; 185 widths[i] = pixels; 186 } 187 } 188 189 int diff = rest - totalDistributed; 191 for (int i = 0; diff > 0; ++i) { 192 if (i == size) { 193 i = 0; 194 } 195 ColumnLayoutData col = (ColumnLayoutData) columns.get(i); 196 if (col instanceof ColumnWeightData) { 197 ++widths[i]; 198 --diff; 199 } 200 } 201 } 202 203 firstTime = false; 204 205 for (int i = 0; i < size; i++) { 206 setWidth(tableColumns[i], widths[i]); 207 } 208 } 209 210 216 private void setWidth(Item item, int width) { 217 if (item instanceof TreeColumn) { 218 ((TreeColumn) item).setWidth(width); 219 } else { 220 ((TableColumn) item).setWidth(width); 221 } 222 223 } 224 225 231 private Item[] getColumns(Composite composite) { 232 if (composite instanceof Tree) { 233 return ((Tree) composite).getColumns(); 234 } 235 return ((Table) composite).getColumns(); 236 } 237 } 238 | Popular Tags |