svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / swing / treeTable / TreeTable.java @ 40561
History | View | Annotate | Download (7.43 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.gui.beans.swing.treeTable; |
25 |
|
26 |
import java.awt.Component; |
27 |
import java.awt.Dimension; |
28 |
|
29 |
import javax.swing.Icon; |
30 |
import javax.swing.JTable; |
31 |
import javax.swing.table.TableCellEditor; |
32 |
import javax.swing.tree.DefaultTreeCellRenderer; |
33 |
import javax.swing.tree.DefaultTreeSelectionModel; |
34 |
import javax.swing.tree.TreeCellEditor; |
35 |
import javax.swing.tree.TreeCellRenderer; |
36 |
import javax.swing.tree.TreeModel; |
37 |
|
38 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
39 |
*
|
40 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
41 |
*
|
42 |
* This program is free software; you can redistribute it and/or
|
43 |
* modify it under the terms of the GNU General Public License
|
44 |
* as published by the Free Software Foundation; either version 2
|
45 |
* of the License, or (at your option) any later version.
|
46 |
*
|
47 |
* This program is distributed in the hope that it will be useful,
|
48 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
49 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
50 |
* GNU General Public License for more details.
|
51 |
*
|
52 |
* You should have received a copy of the GNU General Public License
|
53 |
* along with this program; if not, write to the Free Software
|
54 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
55 |
*
|
56 |
* For more information, contact:
|
57 |
*
|
58 |
* Generalitat Valenciana
|
59 |
* Conselleria d'Infraestructures i Transport
|
60 |
* Av. Blasco Ib??ez, 50
|
61 |
* 46010 VALENCIA
|
62 |
* SPAIN
|
63 |
*
|
64 |
* +34 963862235
|
65 |
* gvsig@gva.es
|
66 |
* www.gvsig.gva.es
|
67 |
*
|
68 |
* or
|
69 |
*
|
70 |
* IVER T.I. S.A
|
71 |
* Salamanca 50
|
72 |
* 46005 Valencia
|
73 |
* Spain
|
74 |
*
|
75 |
* +34 963163400
|
76 |
* dac@iver.es
|
77 |
*/
|
78 |
/* CVS MESSAGES:
|
79 |
*
|
80 |
* $Id: TreeTable.java 13655 2007-09-12 16:28:55Z bsanchez $
|
81 |
* $Log$
|
82 |
* Revision 1.2 2007-09-12 16:28:23 bsanchez
|
83 |
* *** empty log message ***
|
84 |
*
|
85 |
* Revision 1.1 2007/08/20 08:34:46 evercher
|
86 |
* He fusionado LibUI con LibUIComponents
|
87 |
*
|
88 |
* Revision 1.4 2007/01/17 17:18:29 jorpiell
|
89 |
* A?adido un m?todo para expandir todo el ?rbol
|
90 |
*
|
91 |
* Revision 1.3 2006/10/25 14:45:52 jorpiell
|
92 |
* A?adidos los renderers al arbol
|
93 |
*
|
94 |
* Revision 1.2 2006/10/23 11:13:55 jorpiell
|
95 |
* A?adida la posibilidad de recargar el ?rbol tantas veces como queramos
|
96 |
*
|
97 |
* Revision 1.1 2006/10/23 08:18:39 jorpiell
|
98 |
* A?adida la clase treetable
|
99 |
*
|
100 |
*
|
101 |
*/
|
102 |
/**
|
103 |
* To create a new tree table it is necessary to create a new
|
104 |
* treetablemodel. See the ExampleModel.java.
|
105 |
* @author Jorge Piera Llodr? (piera_jor@gva.es)
|
106 |
*/
|
107 |
|
108 |
public class TreeTable extends JTable { |
109 |
private static final long serialVersionUID = 6630322246955414756L; |
110 |
protected TreeTableCellRenderer tree;
|
111 |
|
112 |
public TreeTable(TreeTableModel treeTableModel) {
|
113 |
super();
|
114 |
setModel(treeTableModel); |
115 |
} |
116 |
|
117 |
public TreeTable(TreeTableModel treeTableModel,TreeCellRenderer cellRenderer,TreeCellEditor cellEditor) { |
118 |
super();
|
119 |
setModel(treeTableModel,cellRenderer,cellEditor); |
120 |
} |
121 |
|
122 |
public void setModel(TreeTableModel treeTableModel,TreeCellRenderer cellRenderer,TreeCellEditor cellEditor){ |
123 |
tree = new TreeTableCellRenderer(treeTableModel,this,cellRenderer,cellEditor); |
124 |
super.setModel(new TreeTableModelAdapter(treeTableModel, tree)); |
125 |
setModel(); |
126 |
} |
127 |
|
128 |
public void setModel(TreeTableModel treeTableModel){ |
129 |
tree = new TreeTableCellRenderer(treeTableModel,this); |
130 |
super.setModel(new TreeTableModelAdapter(treeTableModel, tree)); |
131 |
setModel(); |
132 |
} |
133 |
|
134 |
private void setModel(){ |
135 |
// Force the JTable and JTree to share their row selection models.
|
136 |
tree.setSelectionModel(new DefaultTreeSelectionModel() { |
137 |
private static final long serialVersionUID = 2719965083562067462L; |
138 |
// Extend the implementation of the constructor, as if:
|
139 |
/* public this() */ {
|
140 |
setSelectionModel(listSelectionModel); |
141 |
} |
142 |
}); |
143 |
// Make the tree and table row heights the same.
|
144 |
tree.setRowHeight(getRowHeight()); |
145 |
|
146 |
// Install the tree editor renderer and editor.
|
147 |
setDefaultRenderer(TreeTableModel.class, tree); |
148 |
setDefaultEditor(TreeTableModel.class, new TreeTableCellEditor());
|
149 |
|
150 |
setShowGrid(false);
|
151 |
setIntercellSpacing(new Dimension(0, 0)); |
152 |
} |
153 |
|
154 |
/* Workaround for BasicTableUI anomaly. Make sure the UI never tries to
|
155 |
* paint the editor. The UI currently uses different techniques to
|
156 |
* paint the renderers and editors and overriding setBounds() below
|
157 |
* is not the right thing to do for an editor. Returning -1 for the
|
158 |
* editing row in this case, ensures the editor is never painted.
|
159 |
*/
|
160 |
public int getEditingRow() { |
161 |
return (getColumnClass(editingColumn) == TreeTableModel.class) ? -1 : editingRow; |
162 |
} |
163 |
|
164 |
|
165 |
public class TreeTableCellEditor extends AbstractCellEditor implements TableCellEditor { |
166 |
public Component getTableCellEditorComponent(JTable table, Object value, |
167 |
boolean isSelected, int r, int c) { |
168 |
return tree;
|
169 |
} |
170 |
} |
171 |
|
172 |
/**
|
173 |
* Deletes all the tree icons
|
174 |
*
|
175 |
*/
|
176 |
public void deleteIcons(){ |
177 |
setLeafIcon(null);
|
178 |
setClosedIcon(null);
|
179 |
setOpenIcon(null);
|
180 |
} |
181 |
|
182 |
/**
|
183 |
* Sets the root visible
|
184 |
*/
|
185 |
public void setRootVisible(boolean rootVisible){ |
186 |
tree.setRootVisible(rootVisible); |
187 |
} |
188 |
|
189 |
/**
|
190 |
* Sets the leaftIcon
|
191 |
* @param newIcon
|
192 |
*/
|
193 |
public void setLeafIcon(Icon newIcon){ |
194 |
TreeTableCellRenderer renderer = (TreeTableCellRenderer)getTree(); |
195 |
DefaultTreeCellRenderer treeRenderer = (DefaultTreeCellRenderer)renderer.getCellRenderer(); |
196 |
treeRenderer.setLeafIcon(newIcon); |
197 |
} |
198 |
|
199 |
/**
|
200 |
* Sets the open icon
|
201 |
* @param newIcon
|
202 |
*/
|
203 |
public void setOpenIcon(Icon newIcon){ |
204 |
TreeTableCellRenderer renderer = (TreeTableCellRenderer)getTree(); |
205 |
DefaultTreeCellRenderer treeRenderer = (DefaultTreeCellRenderer)renderer.getCellRenderer(); |
206 |
treeRenderer.setOpenIcon(newIcon); |
207 |
} |
208 |
|
209 |
/**
|
210 |
* Sets the close icon
|
211 |
* @param newIcon
|
212 |
*/
|
213 |
public void setClosedIcon(Icon newIcon){ |
214 |
TreeTableCellRenderer renderer = (TreeTableCellRenderer)getTree(); |
215 |
DefaultTreeCellRenderer treeRenderer = (DefaultTreeCellRenderer)renderer.getCellRenderer(); |
216 |
treeRenderer.setClosedIcon(newIcon); |
217 |
} |
218 |
|
219 |
public Component getTree() { |
220 |
return tree;
|
221 |
} |
222 |
|
223 |
public int expandJTreeNode (Object node, int row){ |
224 |
TreeModel model = tree.getModel();
|
225 |
if (node != null && !model.isLeaf(node)){ |
226 |
tree.expandRow(row); |
227 |
for (int index = 0; row + 1 < tree.getRowCount() && index < model.getChildCount(node); index++){ |
228 |
row++; |
229 |
Object child = model.getChild(node, index);
|
230 |
if (child == null) |
231 |
break;
|
232 |
javax.swing.tree.TreePath path; |
233 |
while ((path = tree.getPathForRow(row)) != null && path.getLastPathComponent() != child){ |
234 |
tree.expandRow(row); |
235 |
row++; |
236 |
} |
237 |
if (path == null) |
238 |
break;
|
239 |
row = expandJTreeNode(child, row); |
240 |
} |
241 |
} |
242 |
return row;
|
243 |
} |
244 |
|
245 |
} |
246 |
|