1 13 14 package org.eclipse.jface.layout; 15 16 17 import org.eclipse.jface.viewers.ColumnLayoutData; 18 import org.eclipse.jface.viewers.ColumnPixelData; 19 import org.eclipse.swt.events.TreeEvent; 20 import org.eclipse.swt.events.TreeListener; 21 import org.eclipse.swt.widgets.Composite; 22 import org.eclipse.swt.widgets.Layout; 23 import org.eclipse.swt.widgets.Scrollable; 24 import org.eclipse.swt.widgets.Tree; 25 import org.eclipse.swt.widgets.TreeColumn; 26 import org.eclipse.swt.widgets.Widget; 27 28 40 public class TreeColumnLayout extends AbstractColumnLayout { 41 private boolean addListener = true; 42 43 private static class TreeLayoutListener implements TreeListener { 44 45 public void treeCollapsed(TreeEvent e) { 46 update((Tree) e.widget); 47 } 48 49 public void treeExpanded(TreeEvent e) { 50 update((Tree) e.widget); 51 } 52 53 private void update(final Tree tree) { 54 tree.getDisplay().asyncExec(new Runnable () { 55 56 public void run() { 57 tree.update(); 58 tree.getParent().layout(); 59 } 60 61 }); 62 } 63 64 } 65 66 private static final TreeLayoutListener listener = new TreeLayoutListener(); 67 68 protected void layout(Composite composite, boolean flushCache) { 69 super.layout(composite, flushCache); 70 if( addListener ) { 71 addListener=false; 72 ((Tree)getControl(composite)).addTreeListener(listener); 73 } 74 } 75 76 79 int getColumnCount(Scrollable tree) { 80 return ((Tree) tree).getColumnCount(); 81 } 82 83 86 void setColumnWidths(Scrollable tree, int[] widths) { 87 TreeColumn[] columns = ((Tree) tree).getColumns(); 88 for (int i = 0; i < widths.length; i++) { 89 columns[i].setWidth(widths[i]); 90 } 91 } 92 93 96 ColumnLayoutData getLayoutData(Scrollable tableTree, int columnIndex) { 97 TreeColumn column = ((Tree) tableTree).getColumn(columnIndex); 98 return (ColumnLayoutData) column.getData(LAYOUT_DATA); 99 } 100 101 void updateColumnData(Widget column) { 102 TreeColumn tColumn = (TreeColumn) column; 103 Tree t = tColumn.getParent(); 104 105 if( ! IS_GTK || t.getColumn(t.getColumnCount()-1) != tColumn ){ 106 tColumn.setData(LAYOUT_DATA,new ColumnPixelData(tColumn.getWidth())); 107 layout(t.getParent(), true); 108 } 109 } 110 } 111 | Popular Tags |