01: package org.swingml.treetablebrowser.ext;
02:
03: import java.awt.*;
04: import java.util.*;
05: import java.util.List;
06:
07: import javax.swing.*;
08:
09: import org.swingml.*;
10: import org.swingml.view.*;
11: import org.swingml.view.Renderer;
12: import org.swingml.model.TableColumnModel;
13:
14: public class TreeTableBrowserRenderer extends RendererUtil implements
15: Renderer {
16:
17: private void applyColumnOrder(TreeTableComponent component,
18: TreeTableBrowserModel model) {
19: List columns = model.getTreeTableColumns();
20: for (int index = 0; index < columns.size(); index++) {
21: TreeTableBrowserColumnModel columnModel = (TreeTableBrowserColumnModel) columns
22: .get(index);
23: String columnName = columnModel.getText();
24: int currentIndex = component.getColumnModel()
25: .getColumnIndex(columnName);
26: if (columnModel.getColumnOrder() >= 0) {
27: component.getColumnModel().moveColumn(currentIndex,
28: columnModel.getColumnOrder());
29: } else if (currentIndex != index) {
30: component.getColumnModel().moveColumn(currentIndex,
31: columns.size() - 1);
32: }
33: }
34: }
35:
36: private void applyColumnWidths(TreeTableComponent component,
37: TreeTableBrowserModel model) {
38: List columns = model.getTreeTableColumns();
39: for (int i = 0; i < columns.size(); i++) {
40: component.setColumnWidth(i,
41: ((TreeTableBrowserColumnModel) columns.get(i))
42: .getWidth());
43: }
44: }
45:
46: public Container render(AbstractSwingMLModel aModel,
47: Container aParent) {
48: TreeTableBrowserModel treeTableModel = (TreeTableBrowserModel) aModel;
49: TreeTableComponent treeTable = new TreeTableComponent(
50: treeTableModel);
51: new TreeTableBrowserCellLabelDecorator(treeTable);
52: new TreeTableBrowserCellIconDecorator(treeTable);
53: treeTableModel.sort();
54: // apply widths only after the refresh because it creates new columns
55: // and the widths will be lost on the recreation if set before this
56: // point.
57: applyColumnWidths(treeTable, treeTableModel);
58: applyColumnOrder(treeTable, treeTableModel);
59: treeTable.setName(treeTableModel.getName());
60: String theOrientation = treeTableModel.getOrientation();
61: JScrollPane sp = new JScrollPane(treeTable);
62: if (treeTableModel.getWidth() > 0
63: && treeTableModel.getHeight() > 0) {
64: Dimension d = new Dimension(treeTableModel.getWidth(),
65: treeTableModel.getHeight());
66: sp.setPreferredSize(d);
67: }
68: sp.getViewport().setBackground(Color.WHITE);
69: if (theOrientation != null) {
70: aParent.add(sp, theOrientation);
71: } else {
72: aParent.add(sp);
73: }
74: treeTable.browser.refresh();
75: for (Iterator i = treeTableModel.getExpanded().iterator(); i
76: .hasNext();) {
77: treeTable.expand((String) i.next());
78: }
79: if (treeTableModel.getExpandToNodeName() != null) {
80: treeTable.expandTo(treeTableModel.getExpandToNodeName());
81: }
82:
83: return treeTable;
84: }
85: }
|