001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
010: * - fix for bug 178280, 183999, 184609
011: * IBM Corporation - API refactoring and general maintenance
012: *******************************************************************************/package org.eclipse.jface.layout;
013:
014: import org.eclipse.jface.viewers.ColumnLayoutData;
015: import org.eclipse.jface.viewers.ColumnPixelData;
016: import org.eclipse.swt.events.TreeEvent;
017: import org.eclipse.swt.events.TreeListener;
018: import org.eclipse.swt.widgets.Composite;
019: import org.eclipse.swt.widgets.Layout;
020: import org.eclipse.swt.widgets.Scrollable;
021: import org.eclipse.swt.widgets.Tree;
022: import org.eclipse.swt.widgets.TreeColumn;
023: import org.eclipse.swt.widgets.Widget;
024:
025: /**
026: * The TreeColumnLayout is the {@link Layout} used to maintain {@link TreeColumn} sizes in a
027: * {@link Tree}.
028: *
029: * <p>
030: * <b>You can only add the {@link Layout} to a container whose <i>only</i>
031: * child is the {@link Tree} control you want the {@link Layout} applied to.
032: * Don't assign the layout directly the {@link Tree}</b>
033: * </p>
034: *
035: * @since 3.3
036: */
037: public class TreeColumnLayout extends AbstractColumnLayout {
038: private boolean addListener = true;
039:
040: private static class TreeLayoutListener implements TreeListener {
041:
042: public void treeCollapsed(TreeEvent e) {
043: update((Tree) e.widget);
044: }
045:
046: public void treeExpanded(TreeEvent e) {
047: update((Tree) e.widget);
048: }
049:
050: private void update(final Tree tree) {
051: tree.getDisplay().asyncExec(new Runnable() {
052:
053: public void run() {
054: tree.update();
055: tree.getParent().layout();
056: }
057:
058: });
059: }
060:
061: }
062:
063: private static final TreeLayoutListener listener = new TreeLayoutListener();
064:
065: protected void layout(Composite composite, boolean flushCache) {
066: super .layout(composite, flushCache);
067: if (addListener) {
068: addListener = false;
069: ((Tree) getControl(composite)).addTreeListener(listener);
070: }
071: }
072:
073: /* (non-Javadoc)
074: * @see org.eclipse.jface.layout.AbstractColumnLayout#getColumnCount(org.eclipse.swt.widgets.Scrollable)
075: */
076: int getColumnCount(Scrollable tree) {
077: return ((Tree) tree).getColumnCount();
078: }
079:
080: /* (non-Javadoc)
081: * @see org.eclipse.jface.layout.AbstractColumnLayout#setColumnWidths(org.eclipse.swt.widgets.Scrollable, int[])
082: */
083: void setColumnWidths(Scrollable tree, int[] widths) {
084: TreeColumn[] columns = ((Tree) tree).getColumns();
085: for (int i = 0; i < widths.length; i++) {
086: columns[i].setWidth(widths[i]);
087: }
088: }
089:
090: /* (non-Javadoc)
091: * @see org.eclipse.jface.layout.AbstractColumnLayout#getLayoutData(org.eclipse.swt.widgets.Scrollable, int)
092: */
093: ColumnLayoutData getLayoutData(Scrollable tableTree, int columnIndex) {
094: TreeColumn column = ((Tree) tableTree).getColumn(columnIndex);
095: return (ColumnLayoutData) column.getData(LAYOUT_DATA);
096: }
097:
098: void updateColumnData(Widget column) {
099: TreeColumn tColumn = (TreeColumn) column;
100: Tree t = tColumn.getParent();
101:
102: if (!IS_GTK || t.getColumn(t.getColumnCount() - 1) != tColumn) {
103: tColumn.setData(LAYOUT_DATA, new ColumnPixelData(tColumn
104: .getWidth()));
105: layout(t.getParent(), true);
106: }
107: }
108: }
|