Hi Dany
Thanks a lot for your solution!!!
It works fine... ...but ;)
1. Because I must show the hole text even if the tree itself is smaller I had
to set tableTree.setAutoResizeMode(ULCTableTree.AUTO_RESIZE_OFF).
2. I added a TreeExpansionListener to adjust the column if a node is expanded.
So far it's perfect.
But now I can not initialise the column correctly. Because I had to turn off
AUTO-RESIZE the column is show at the beginning with its default width...
I tried UlcUtilities.invokeLater() and UITableTree.postInitializeState() but
the column width is even reduced.
Then I tried a IRoundTripListener and there I could not remove the listener
when tableTree.isUploaded() was true because the width is as well reduced...
...the RoundTripListener is called twice and then the width is ok. ?-/
When do I know that the UI is completely uploaded during startup?
Thanks a lot!!
Regards
Alex
Here my snippet :
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import com.ulcjava.base.application.AbstractAction;
import com.ulcjava.base.application.AbstractApplication;
import com.ulcjava.base.application.ApplicationContext;
import com.ulcjava.base.application.ULCBorderLayoutPane;
import com.ulcjava.base.application.ULCButton;
import com.ulcjava.base.application.ULCFrame;
import com.ulcjava.base.application.ULCListSelectionModel;
import com.ulcjava.base.application.ULCProxy;
import com.ulcjava.base.application.ULCScrollPane;
import com.ulcjava.base.application.ULCTableTree;
import com.ulcjava.base.application.event.ActionEvent;
import com.ulcjava.base.application.event.IRoundTripListener;
import com.ulcjava.base.application.event.ITreeExpansionListener;
import com.ulcjava.base.application.event.RoundTripEvent;
import com.ulcjava.base.application.event.TreeExpansionEvent;
import com.ulcjava.base.application.tabletree.AbstractTableTreeModel;
import com.ulcjava.base.application.util.Dimension;
import com.ulcjava.base.client.UIProxy;
import com.ulcjava.base.client.UITableTree;
import com.ulcjava.base.client.tabletree.TableTreeTree;
import com.ulcjava.base.development.DevelopmentRunner;
public class MyExpandTableTreeColumns extends AbstractApplication {
public void start() {
final ULCTableTree tableTree = new MyULCTableTree();
tableTree.setModel(new SnippetTableTreeModel());
tableTree.getColumnModel().getSelectionModel().setSelectionMode(ULCListSelectionModel.SINGLE_SELECTION);
tableTree.setRowSelectionAllowed(false);
tableTree.setColumnSelectionAllowed(true);
tableTree.setAutoResizeMode(ULCTableTree.AUTO_RESIZE_OFF);
tableTree.addTreeExpansionListener(new ITreeExpansionListener(){
public void treeCollapsed(TreeExpansionEvent arg0) { /* noop */
}
public void treeExpanded(TreeExpansionEvent arg0) {
if(-1 == tableTree.getSelectedColumn()){
tableTree.setColumnSelectionInterval(0,0);
}
ULCSnippetHelper.getInstance().expandTableTreeColumn(tableTree,
tableTree.getSelectedColumn());
}
});
ApplicationContext.addRoundTripListener(new IRoundTripListener(){
public void roundTripDidStart(RoundTripEvent arg0) { /*
noop */ }
public void roundTripWillEnd(RoundTripEvent arg0) {
if(tableTree.isUploaded()){
if(-1 == tableTree.getSelectedColumn()){
tableTree.setColumnSelectionInterval(0,0);
}
ULCSnippetHelper.getInstance().expandTableTreeColumn(tableTree,
tableTree.getSelectedColumn());
System.out.println("roundTripWillEnd...");
// ApplicationContext.removeRoundTripListener(this);
}
}
});
ULCFrame frame = new ULCFrame("Snippet");
frame.setDefaultCloseOperation(ULCFrame.TERMINATE_ON_CLOSE);
frame.getContentPane().add(new ULCScrollPane(tableTree));
frame.getContentPane().add(
new ULCButton(new ExpandSelectedColumnAction(tableTree)),
ULCBorderLayoutPane.SOUTH);
frame.pack();
frame.setSize(new Dimension(200,300));
frame.setVisible(true);
//init columnwidth
// UlcUtilities.invokeLater(new Runnable(){
// public void run() {
// tableTree.setColumnSelectionInterval(0,0);
//
ULCSnippetHelper.getInstance().expandTableTreeColumn(tableTree,
// tableTree.getSelectedColumn());
// }
// });
}
public static void main(String[] args) {
DevelopmentRunner.setApplicationClass(MyExpandTableTreeColumns.class);
DevelopmentRunner.main(args);
}
public static class MyULCTableTree extends ULCTableTree {
protected String typeString() {
return MyUITableTree.class.getName();
}
}
public static class MyUITableTree extends UITableTree {
public void postInitializeState(){
super.postInitializeState();
// UISnippetHelper.expandTableTreeColumn(this, 0);
}
}
public static class ExpandSelectedColumnAction extends AbstractAction {
private ULCTableTree fTableTree;
public ExpandSelectedColumnAction(ULCTableTree tableTree) {
fTableTree = tableTree;
setEnabler(fTableTree.getColumnModel().getSelectionModel());
putValue(NAME, "Expand Selected Column");
}
public void actionPerformed(ActionEvent event) {
ULCSnippetHelper.getInstance().expandTableTreeColumn(fTableTree,
fTableTree.getSelectedColumn());
}
}
public static class ULCSnippetHelper extends ULCProxy {
private static final String INSTANCE_KEY = ULCSnippetHelper.class
.getName()
+ ".INSTANCE";
private ULCSnippetHelper() {
upload();
markUncollectable();
}
public static ULCSnippetHelper getInstance() {
ULCSnippetHelper result = (ULCSnippetHelper) ApplicationContext
.getAttribute(INSTANCE_KEY);
if (result == null) {
result = new ULCSnippetHelper();
ApplicationContext.setAttribute(INSTANCE_KEY, result);
}
return result;
}
public void expandTableTreeColumn(ULCTableTree tableTree, int column) {
invokeUI("expandTableTreeColumn", new Object[] { tableTree,
new Integer(column) });
}
protected String typeString() {
return UISnippetHelper.class.getName();
}
}
public static class UISnippetHelper extends UIProxy {
public static void expandTableTreeColumn(UITableTree tableTree,
int resizingColumnIndex) {
final JTable basicTable =
tableTree.getBasicTableTree().getBasicTable();
TableColumn resizingColumn = basicTable.getColumnModel().getColumn(
resizingColumnIndex);
// set the table column's width as if the user is resizing a table
column by mouse
// -> BaiscTableHeaderUI.MouseInputHandler and JTable.doLayout()
basicTable.getTableHeader().setResizingColumn(resizingColumn);
resizingColumn.setWidth(toPreferredWidth(tableTree,
resizingColumnIndex));
SwingUtilities.invokeLater(new Runnable() {
public void run() {
basicTable.getTableHeader().setResizingColumn(null);
}
});
}
private static int toPreferredWidth(UITableTree tableTree,
int columnIndex) {
TableTreeTree basicTree = (TableTreeTree) tableTree
.getBasicTableTree().getBasicTree();
JTable basicTable = tableTree.getBasicTableTree().getBasicTable();
TableColumn column =
basicTable.getColumnModel().getColumn(columnIndex);
int result = column.getMinWidth();
for (int i = 0; i < basicTable.getRowCount(); i++) {
TableCellRenderer renderer = getCellRenderer(basicTable, i,
columnIndex);
Component component = basicTable.prepareRenderer(renderer, i,
columnIndex);
int preferredWidth = component.getPreferredSize().width;
// consider tree handle and icon for the tree column
if (isTreeColumn(tableTree, columnIndex)) {
preferredWidth += basicTree.getTableCellOffset(i);
}
result = Math.max(result, preferredWidth);
}
result += basicTable.getColumnModel().getColumnMargin();
return Math.min(result, column.getMaxWidth());
}
private static boolean isTreeColumn(UITableTree tableTree,
int columnIndex) {
JTable basicTable = tableTree.getBasicTableTree().getBasicTable();
return basicTable.convertColumnIndexToModel(columnIndex) ==
tableTree
.getBasicTableTree().getModel().getTreeColumn();
}
// we can't use TableTreeTable's getCellRenderer method as this one is
overwritten
// -> copy & pasted code from JTable.getCellRenderer()
private static TableCellRenderer getCellRenderer(JTable table,
int row,
int column) {
TableColumn tableColumn = table.getColumnModel().getColumn(column);
TableCellRenderer result = tableColumn.getCellRenderer();
if (result == null) {
result = table.getDefaultRenderer(table.getColumnClass(column));
}
return result;
}
}
public static class SnippetTableTreeModel extends AbstractTableTreeModel {
public boolean isLeaf(Object node) {
return false;
}
public Object getValueAt(Object node, int column) {
return node.toString() + ":" + column;
}
public Object getRoot() {
return "root";
}
public int getIndexOfChild(Object parent, Object child) {
String childValue = child.toString();
int index = childValue.lastIndexOf('.');
return Integer.parseInt(childValue.substring(index + 1));
}
public int getColumnCount() {
return 3;
}
public int getChildCount(Object parent) {
return 4;
}
public Object getChild(Object parent, int index) {
return parent.toString() + "." + index;
}
}
}
-----Ursprüngliche Nachricht-----
Von: Daniel Grob [mailto:[EMAIL PROTECTED]
Gesendet: Dienstag, 19. September 2006 22:39
An: [email protected]; Stucki Alexander, Bedag
Betreff: Re: [ULC-developer] How to expand the columns width of ULCTableTree?
Hi Alex,
This was a hard one for two reasons:
1. In Swing it is difficult to programatically set the width of a column
(?!?)
2. ULC's tree column handling is implemented in a special way
However, you can find a solution at the end of my mail.
Regards Dany
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import com.ulcjava.base.application.AbstractAction;
import com.ulcjava.base.application.AbstractApplication;
import com.ulcjava.base.application.ApplicationContext;
import com.ulcjava.base.application.ULCBorderLayoutPane;
import com.ulcjava.base.application.ULCButton;
import com.ulcjava.base.application.ULCFrame;
import com.ulcjava.base.application.ULCListSelectionModel;
import com.ulcjava.base.application.ULCProxy;
import com.ulcjava.base.application.ULCScrollPane;
import com.ulcjava.base.application.ULCTableTree;
import com.ulcjava.base.application.event.ActionEvent;
import com.ulcjava.base.application.tabletree.AbstractTableTreeModel;
import com.ulcjava.base.client.UIProxy;
import com.ulcjava.base.client.UITableTree;
import com.ulcjava.base.client.tabletree.TableTreeTree;
import com.ulcjava.base.development.DevelopmentRunner;
public class ExpandTableTreeColumns extends AbstractApplication {
public void start() {
ULCTableTree tableTree = new ULCTableTree(new SnippetTableTreeModel());
tableTree.getColumnModel().getSelectionModel().setSelectionMode(
ULCListSelectionModel.SINGLE_SELECTION);
tableTree.setRowSelectionAllowed(false);
tableTree.setColumnSelectionAllowed(true);
ULCFrame frame = new ULCFrame("Snippet");
frame.setDefaultCloseOperation(ULCFrame.TERMINATE_ON_CLOSE);
frame.getContentPane().add(new ULCScrollPane(tableTree));
frame.getContentPane().add(
new ULCButton(new ExpandSelectedColumnAction(tableTree)),
ULCBorderLayoutPane.SOUTH);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
DevelopmentRunner.setApplicationClass(ExpandTableTreeColumns.class);
DevelopmentRunner.main(args);
}
public static class ExpandSelectedColumnAction extends AbstractAction {
private ULCTableTree fTableTree;
public ExpandSelectedColumnAction(ULCTableTree tableTree) {
fTableTree = tableTree;
setEnabler(fTableTree.getColumnModel().getSelectionModel());
putValue(NAME, "Expand Selected Column");
}
public void actionPerformed(ActionEvent event) {
ULCSnippetHelper.getInstance().expandTableTreeColumn(fTableTree,
fTableTree.getSelectedColumn());
}
}
public static class ULCSnippetHelper extends ULCProxy {
private static final String INSTANCE_KEY = ULCSnippetHelper.class
.getName()
+ ".INSTANCE";
private ULCSnippetHelper() {
upload();
markUncollectable();
}
public static ULCSnippetHelper getInstance() {
ULCSnippetHelper result = (ULCSnippetHelper) ApplicationContext
.getAttribute(INSTANCE_KEY);
if (result == null) {
result = new ULCSnippetHelper();
ApplicationContext.setAttribute(INSTANCE_KEY, result);
}
return result;
}
public void expandTableTreeColumn(ULCTableTree tableTree, int
column) {
invokeUI("expandTableTreeColumn", new Object[] { tableTree,
new Integer(column) });
}
protected String typeString() {
return UISnippetHelper.class.getName();
}
}
public static class UISnippetHelper extends UIProxy {
public void expandTableTreeColumn(UITableTree tableTree,
int resizingColumnIndex) {
final JTable basicTable = tableTree.getBasicTableTree()
.getBasicTable();
TableColumn resizingColumn = basicTable.getColumnModel().getColumn(
resizingColumnIndex);
// set the table column's width as if the user is resizing a table
column by mouse
// -> BaiscTableHeaderUI.MouseInputHandler and JTable.doLayout()
basicTable.getTableHeader().setResizingColumn(resizingColumn);
resizingColumn.setWidth(toPreferredWidth(tableTree,
resizingColumnIndex));
SwingUtilities.invokeLater(new Runnable() {
public void run() {
basicTable.getTableHeader().setResizingColumn(null);
}
});
}
private static int toPreferredWidth(UITableTree tableTree,
int columnIndex) {
TableTreeTree basicTree = (TableTreeTree) tableTree
.getBasicTableTree().getBasicTree();
JTable basicTable =
tableTree.getBasicTableTree().getBasicTable();
TableColumn column = basicTable.getColumnModel().getColumn(
columnIndex);
int result = column.getMinWidth();
for (int i = 0; i < basicTable.getRowCount(); i++) {
TableCellRenderer renderer = getCellRenderer(basicTable, i,
columnIndex);
Component component =
basicTable.prepareRenderer(renderer, i,
columnIndex);
int preferredWidth = component.getPreferredSize().width;
// consider tree handle and icon for the tree column
if (isTreeColumn(tableTree, columnIndex)) {
preferredWidth += basicTree.getTableCellOffset(i);
}
result = Math.max(result, preferredWidth);
}
result += basicTable.getColumnModel().getColumnMargin();
return Math.min(result, column.getMaxWidth());
}
private static boolean isTreeColumn(UITableTree tableTree,
int columnIndex) {
JTable basicTable =
tableTree.getBasicTableTree().getBasicTable();
return basicTable.convertColumnIndexToModel(columnIndex) ==
tableTree
.getBasicTableTree().getModel().getTreeColumn();
}
// we can't use TableTreeTable's getCellRenderer method as this one is
overwritten
// -> copy & pasted code from JTable.getCellRenderer()
private static TableCellRenderer getCellRenderer(JTable table, int row,
int column) {
TableColumn tableColumn =
table.getColumnModel().getColumn(column);
TableCellRenderer result = tableColumn.getCellRenderer();
if (result == null) {
result =
table.getDefaultRenderer(table.getColumnClass(column));
}
return result;
}
}
public static class SnippetTableTreeModel extends AbstractTableTreeModel {
public boolean isLeaf(Object node) {
return false;
}
public Object getValueAt(Object node, int column) {
return node.toString() + ":" + column;
}
public Object getRoot() {
return "root";
}
public int getIndexOfChild(Object parent, Object child) {
String childValue = child.toString();
int index = childValue.lastIndexOf('.');
return Integer.parseInt(childValue.substring(index + 1));
}
public int getColumnCount() {
return 3;
}
public int getChildCount(Object parent) {
return 4;
}
public Object getChild(Object parent, int index) {
return parent.toString() + "." + index;
}
}
}
>
> Hi All
>
> I want to expand the column width of my ULCTableTree according the
> values of the column cells.
> The tree is in a ScrollPane and then I tried to figure out the
> PreferredSize of every cell.
> I have found an example doing this in a JTable by asking the
> CellRendererComponent for its PreferredSize.
>
> I tried it on server and on client side without success (missing
> cellRendererComponent, fix preferredSize of the column, etc).
>
> Does anybody know an example for this problem?
> How do I get the best width for a cell value without calculating it
> myself?
>
> TIA!
> Alex
>
> Alex Stucki
> Bedag Informatik AG
> Software-Entwicklung
> Gutenbergstrasse 1
> 3011 Bern
> Telefon: +41 (0)31 633 21 21 (direkt 633 25 35)
> E-Mail: _mailto:[EMAIL PROTECTED]
> _www.bedag.ch_ <file://www.bedag.ch>
>
>
_______________________________________________
ULC-developer mailing list
[email protected]
http://lists.canoo.com/mailman/listinfo/ulc-developer