# HG changeset patch # User Frank Benoit # Date 1207247882 -7200 # Node ID 5802cda3813d3b0185746ac7545d6c972a9ceea1 # Parent 51d06982c55004c5a026cf11eb3b1f1ec6d0894a TreeColumnLayout diff -r 51d06982c550 -r 5802cda3813d dwtx/jface/layout/TreeColumnLayout.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dwtx/jface/layout/TreeColumnLayout.d Thu Apr 03 20:38:02 2008 +0200 @@ -0,0 +1,123 @@ +/******************************************************************************* + * Copyright (c) 2007 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Tom Schindl - initial API and implementation + * - fix for bug 178280, 183999, 184609 + * IBM Corporation - API refactoring and general maintenance + * Port to the D programming language: + * Frank Benoit + *******************************************************************************/ + +module dwtx.jface.layout.TreeColumnLayout; + +import dwtx.jface.layout.AbstractColumnLayout; + +import dwt.events.TreeEvent; +import dwt.events.TreeListener; +import dwt.widgets.Composite; +import dwt.widgets.Layout; +import dwt.widgets.Scrollable; +import dwt.widgets.Tree; +import dwt.widgets.TreeColumn; +import dwt.widgets.Widget; +import dwtx.jface.viewers.ColumnLayoutData; +import dwtx.jface.viewers.ColumnPixelData; + +import dwt.dwthelper.utils; +import dwt.dwthelper.Runnable; + +/** + * The TreeColumnLayout is the {@link Layout} used to maintain {@link TreeColumn} sizes in a + * {@link Tree}. + * + *

+ * You can only add the {@link Layout} to a container whose only + * child is the {@link Tree} control you want the {@link Layout} applied to. + * Don't assign the layout directly the {@link Tree} + *

+ * + * @since 3.3 + */ +public class TreeColumnLayout : AbstractColumnLayout { + private bool addListener = true; + + private static class TreeLayoutListener : TreeListener { + + public void treeCollapsed(TreeEvent e) { + update(cast(Tree) e.widget); + } + + public void treeExpanded(TreeEvent e) { + update(cast(Tree) e.widget); + } + + private void update(Tree tree) { + tree.getDisplay().asyncExec(new class Runnable { + Tree tree_; + this(){ + tree_=tree; + } + public void run() { + tree_.update(); + tree_.getParent().layout(); + } + + }); + } + + } + + private static const TreeLayoutListener listener; + + static this(){ + listener = new TreeLayoutListener(); + } + + protected void layout(Composite composite, bool flushCache) { + super.layout(composite, flushCache); + if( addListener ) { + addListener=false; + (cast(Tree)getControl(composite)).addTreeListener(listener); + } + } + + /* (non-Javadoc) + * @see dwtx.jface.layout.AbstractColumnLayout#getColumnCount(dwt.widgets.Scrollable) + */ + int getColumnCount(Scrollable tree) { + return (cast(Tree) tree).getColumnCount(); + } + + /* (non-Javadoc) + * @see dwtx.jface.layout.AbstractColumnLayout#setColumnWidths(dwt.widgets.Scrollable, int[]) + */ + void setColumnWidths(Scrollable tree, int[] widths) { + TreeColumn[] columns = (cast(Tree) tree).getColumns(); + for (int i = 0; i < widths.length; i++) { + columns[i].setWidth(widths[i]); + } + } + + /* (non-Javadoc) + * @see dwtx.jface.layout.AbstractColumnLayout#getLayoutData(dwt.widgets.Scrollable, int) + */ + ColumnLayoutData getLayoutData(Scrollable tableTree, int columnIndex) { + TreeColumn column = (cast(Tree) tableTree).getColumn(columnIndex); + return cast(ColumnLayoutData) column.getData(LAYOUT_DATA); + } + + void updateColumnData(Widget column) { + TreeColumn tColumn = cast(TreeColumn) column; + Tree t = tColumn.getParent(); + + if( ! IS_GTK || t.getColumn(t.getColumnCount()-1) !is tColumn ){ + tColumn.setData(LAYOUT_DATA,new ColumnPixelData(tColumn.getWidth())); + layout(t.getParent(), true); + } + } +}