KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > rcp > layout > WeightedTableLayout


1 package com.nightlabs.rcp.layout;
2
3 import org.eclipse.jface.viewers.TableLayout;
4 import org.eclipse.swt.widgets.Composite;
5 import org.eclipse.swt.widgets.ScrollBar;
6 import org.eclipse.swt.widgets.Table;
7 import org.eclipse.swt.widgets.TableColumn;
8
9 /**
10  *
11  * @author Nicklas Shiffler
12  *
13  */

14
15 public class WeightedTableLayout extends TableLayout
16 {
17   private int[] weights;
18   
19   public WeightedTableLayout(int[] weights)
20   {
21     if (weights != null)
22       this.weights = weights;
23     else
24       this.weights = new int[0];
25   }
26   
27   public void layout(Composite c, boolean flush)
28   {
29     Table table = (Table)c;
30     int columnCount = table.getColumnCount();
31     
32
33     int width = table.getBounds().width;
34     ScrollBar sb = table.getVerticalBar();
35     if(sb.isEnabled() && sb.isVisible())
36       width -= sb.getSize().x;
37     
38     int[] _weights = new int[columnCount];
39     int totalWeight = 0;
40     
41     for(int i = 0; i < columnCount; i++)
42     {
43       if (i >= _weights.length)
44       {
45         _weights[i] = 1;
46         totalWeight++;
47       }
48       else
49       {
50         _weights[i] = weights[i];
51         totalWeight += weights[i];
52       }
53     }
54     
55     for(int i = 0; i < columnCount; i++)
56     {
57       TableColumn tc = table.getColumn(i);
58       tc.setWidth((width * _weights[i]) / totalWeight);
59     }
60   }
61 }
62
63
Popular Tags