TreeTableBrowserRenderer.java :  » XML-UI » SwingML » org » swingml » treetablebrowser » ext » Java Open Source

Java Open Source » XML UI » SwingML 
SwingML » org » swingml » treetablebrowser » ext » TreeTableBrowserRenderer.java
package org.swingml.treetablebrowser.ext;

import java.awt.*;
import java.util.*;
import java.util.List;

import javax.swing.*;

import org.swingml.*;
import org.swingml.view.*;
import org.swingml.view.Renderer;
import org.swingml.model.TableColumnModel;


public class TreeTableBrowserRenderer extends RendererUtil implements Renderer {

    private void applyColumnOrder (TreeTableComponent component, TreeTableBrowserModel model) {
        List columns = model.getTreeTableColumns();
        for (int index = 0; index < columns.size(); index++) {
            TreeTableBrowserColumnModel columnModel = (TreeTableBrowserColumnModel) columns.get(index);
            String columnName = columnModel.getText();
            int currentIndex = component.getColumnModel().getColumnIndex(columnName);
            if (columnModel.getColumnOrder() >= 0) {
                component.getColumnModel().moveColumn(currentIndex, columnModel.getColumnOrder());
            } else if (currentIndex != index){
                component.getColumnModel().moveColumn(currentIndex, columns.size() - 1);
            }
        }
    }

    private void applyColumnWidths (TreeTableComponent component, TreeTableBrowserModel model) {
        List columns = model.getTreeTableColumns();
        for (int i = 0; i < columns.size(); i++) {
            component.setColumnWidth(i, ((TreeTableBrowserColumnModel) columns.get(i)).getWidth());
        }
    }

    public Container render (AbstractSwingMLModel aModel, Container aParent) {
        TreeTableBrowserModel treeTableModel = (TreeTableBrowserModel) aModel;
        TreeTableComponent treeTable = new TreeTableComponent(treeTableModel);
        new TreeTableBrowserCellLabelDecorator(treeTable);
        new TreeTableBrowserCellIconDecorator(treeTable);
        treeTableModel.sort();
        // apply widths only after the refresh because it creates new columns
        // and the widths will be lost on the recreation if set before this
        // point.
        applyColumnWidths(treeTable, treeTableModel);
        applyColumnOrder(treeTable, treeTableModel);
        treeTable.setName(treeTableModel.getName());
        String theOrientation = treeTableModel.getOrientation();
        JScrollPane sp = new JScrollPane(treeTable);
        if (treeTableModel.getWidth() > 0 && treeTableModel.getHeight() > 0) {
            Dimension d = new Dimension(treeTableModel.getWidth(), treeTableModel.getHeight());
            sp.setPreferredSize(d);
        }
        sp.getViewport().setBackground(Color.WHITE);
        if (theOrientation != null) {
            aParent.add(sp, theOrientation);
        } else {
            aParent.add(sp);
        }
        treeTable.browser.refresh();
        for (Iterator i = treeTableModel.getExpanded().iterator(); i.hasNext();) {
            treeTable.expand((String) i.next());
        }
        if (treeTableModel.getExpandToNodeName() != null) {
            treeTable.expandTo(treeTableModel.getExpandToNodeName());
        }
        
        return treeTable;
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.