* Bug 11852 fixed: now filebrowser updates (again)
[scilab.git] / scilab / modules / ui_data / src / java / org / scilab / modules / ui_data / filebrowser / ScilabTreeTableModelAdapter.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2011 - DIGITEO - Calixte DENIZET
4  *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13  *
14  */
15
16 package org.scilab.modules.ui_data.filebrowser;
17
18 import javax.swing.JTree;
19 import javax.swing.event.TreeExpansionEvent;
20 import javax.swing.event.TreeExpansionListener;
21 import javax.swing.event.TreeModelEvent;
22 import javax.swing.event.TreeModelListener;
23 import javax.swing.table.AbstractTableModel;
24 import javax.swing.tree.TreePath;
25
26 /**
27  * The tree table model abstract implementation
28  *
29  * @author Calixte DENIZET
30  */
31 @SuppressWarnings(value = {"serial"})
32 public class ScilabTreeTableModelAdapter extends AbstractTableModel {
33
34     private JTree tree;
35     private ScilabTreeTableModel treeTableModel;
36
37     /**
38      * Default constructor
39      *
40      * @param treeTableModel the table model
41      * @param tree the tree
42      */
43     public ScilabTreeTableModelAdapter(ScilabTreeTableModel treeTableModel, JTree tree) {
44         this.tree = tree;
45         this.treeTableModel = treeTableModel;
46         tree.addTreeExpansionListener(
47         new TreeExpansionListener() {
48             public void treeExpanded(TreeExpansionEvent event) {
49                 fireTableDataChanged();
50             }
51
52             public void treeCollapsed(TreeExpansionEvent event) {
53                 fireTableDataChanged();
54             }
55         });
56         tree.getModel()
57         .addTreeModelListener(
58         new TreeModelListener() {
59
60             @Override
61             public void treeStructureChanged(TreeModelEvent e) {
62                 fireTableDataChanged();
63             }
64
65             @Override
66             public void treeNodesRemoved(TreeModelEvent e) {
67                 fireTableDataChanged();
68             }
69
70             @Override
71             public void treeNodesInserted(TreeModelEvent e) {
72                 fireTableDataChanged();
73             }
74
75             @Override
76             public void treeNodesChanged(TreeModelEvent e) {
77                 fireTableDataChanged();
78             }
79         });
80     }
81
82     /** {@inheritdoc} */
83     public int getColumnCount() {
84         return treeTableModel.getColumnCount();
85     }
86
87     /** {@inheritdoc} */
88     public String getColumnName(int column) {
89         return treeTableModel.getColumnName(column);
90     }
91
92     /** {@inheritdoc} */
93     public Class getColumnClass(int column) {
94         return treeTableModel.getColumnClass(column);
95     }
96
97     /** {@inheritdoc} */
98     public int getRowCount() {
99         return tree.getRowCount();
100     }
101
102     /** {@inheritdoc} */
103     protected Object nodeForRow(int row) {
104         TreePath treePath = tree.getPathForRow(row);
105         return treePath.getLastPathComponent();
106     }
107
108     /** {@inheritdoc} */
109     public Object getValueAt(int row, int column) {
110         return treeTableModel.getValueAt(nodeForRow(row), column);
111     }
112
113     /** {@inheritdoc} */
114     public boolean isCellEditable(int row, int column) {
115         return treeTableModel.isCellEditable(nodeForRow(row), column);
116     }
117
118     /** {@inheritdoc} */
119     public void setValueAt(Object value, int row, int column) {
120         treeTableModel.setValueAt(value, nodeForRow(row), column);
121     }
122 }