001    /* JAPI - (Yet another (hopefully) useful) Java API
002     *
003     * Copyright (C) 2004-2006 Christian Hujer
004     *
005     * This program is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU General Public License as
007     * published by the Free Software Foundation; either version 2 of the
008     * License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful, but
011     * WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013     * General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program; if not, write to the Free Software
017     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018     * 02111-1307, USA.
019     */
020    
021    package net.sf.japi.swing.treetable;
022    
023    import javax.swing.JTree;
024    import javax.swing.event.TreeExpansionEvent;
025    import javax.swing.event.TreeExpansionListener;
026    import javax.swing.table.AbstractTableModel;
027    import javax.swing.tree.TreePath;
028    
029    /** Wraps a TreeTableModel in a TableModel.
030     * @author <a href="mailto:chris@riedquat.de">Christian Hujer</a>
031     */
032    public class TreeTableModelTableModelAdapter extends AbstractTableModel {
033    
034        /** The underlying JTree. */
035        private JTree tree;
036    
037        /** The underlying TreeTableModel. */
038        private TreeTableModel treeTableModel;
039    
040        /** Create a TreeTableModelAdapter.
041         * @param treeTableModel TreeTableModel to adapt
042         * @param tree JTree to adapt
043         */
044        public TreeTableModelTableModelAdapter(final TreeTableModel treeTableModel, final JTree tree) {
045            this.tree = tree;
046            this.treeTableModel = treeTableModel;
047    
048            tree.addTreeExpansionListener(new MyTreeExpansionListener());
049        }
050    
051        /** {@inheritDoc} */
052        public int getRowCount() {
053            return tree.getRowCount();
054        }
055    
056        /** {@inheritDoc} */
057        public int getColumnCount() {
058            return treeTableModel.getColumnCount();
059        }
060    
061        /** {@inheritDoc} */
062        public Object getValueAt(final int rowIndex, final int columnIndex) {
063            return treeTableModel.getValueAt(nodeForRow(rowIndex), columnIndex);
064        }
065    
066        /** {@inheritDoc} */
067        @Override public Class<?> getColumnClass(final int columnIndex) {
068            return treeTableModel.getColumnClass(columnIndex);
069        }
070    
071        /** {@inheritDoc} */
072        @Override public String getColumnName(final int column) {
073            return treeTableModel.getColumnName(column);
074        }
075    
076        /** {@inheritDoc} */
077        @Override public boolean isCellEditable(final int rowIndex, final int columnIndex) {
078            return treeTableModel.isCellEditable(nodeForRow(rowIndex), columnIndex);
079        }
080    
081        protected Object nodeForRow(final int row) {
082            final TreePath treePath = tree.getPathForRow(row);
083            return treePath.getLastPathComponent();
084        }
085    
086        /** {@inheritDoc} */
087        @Override public void setValueAt(final Object aValue, final int rowIndex, final int columnIndex) {
088            treeTableModel.setValueAt(aValue, nodeForRow(rowIndex), columnIndex);
089        }
090    
091        /** TreeExpansionListener for handling TreeExpansion events.
092         * @author <a href="mailto:chris@riedquat.de">Christian Hujer</a>
093         */
094        private class MyTreeExpansionListener implements TreeExpansionListener {
095    
096            /** {@inheritDoc} */
097            public void treeExpanded(final TreeExpansionEvent event) {
098                fireTableDataChanged();
099            }
100    
101            /** {@inheritDoc} */
102            public void treeCollapsed(final TreeExpansionEvent event) {
103                fireTableDataChanged();
104            }
105    
106        } // class MyTreeExpansionListener
107    
108    } // class TreeTableModelAdapter