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