KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > layout > TreeColumnLayout


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
10  * - fix for bug 178280, 183999, 184609
11  * IBM Corporation - API refactoring and general maintenance
12  *******************************************************************************/

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 /**
29  * The TreeColumnLayout is the {@link Layout} used to maintain {@link TreeColumn} sizes in a
30  * {@link Tree}.
31  *
32  * <p>
33  * <b>You can only add the {@link Layout} to a container whose <i>only</i>
34  * child is the {@link Tree} control you want the {@link Layout} applied to.
35  * Don't assign the layout directly the {@link Tree}</b>
36  * </p>
37  *
38  * @since 3.3
39  */

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 JavaDoc() {
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     /* (non-Javadoc)
77      * @see org.eclipse.jface.layout.AbstractColumnLayout#getColumnCount(org.eclipse.swt.widgets.Scrollable)
78      */

79     int getColumnCount(Scrollable tree) {
80         return ((Tree) tree).getColumnCount();
81     }
82
83     /* (non-Javadoc)
84      * @see org.eclipse.jface.layout.AbstractColumnLayout#setColumnWidths(org.eclipse.swt.widgets.Scrollable, int[])
85      */

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     /* (non-Javadoc)
94      * @see org.eclipse.jface.layout.AbstractColumnLayout#getLayoutData(org.eclipse.swt.widgets.Scrollable, int)
95      */

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