root / trunk / applications / appCatalogAndGazetteerClient / src / es / gva / cit / catalog / utils / xmltreetable / XMLTreeTableModel.java @ 15558
History | View | Annotate | Download (12.9 KB)
1 |
|
---|---|
2 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
3 |
*
|
4 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
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 2
|
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
19 |
*
|
20 |
* For more information, contact:
|
21 |
*
|
22 |
* Generalitat Valenciana
|
23 |
* Conselleria d'Infraestructures i Transport
|
24 |
* Av. Blasco Ib??ez, 50
|
25 |
* 46010 VALENCIA
|
26 |
* SPAIN
|
27 |
*
|
28 |
* +34 963862235
|
29 |
* gvsig@gva.es
|
30 |
* www.gvsig.gva.es
|
31 |
*
|
32 |
* or
|
33 |
*
|
34 |
* IVER T.I. S.A
|
35 |
* Salamanca 50
|
36 |
* 46005 Valencia
|
37 |
* Spain
|
38 |
*
|
39 |
* +34 963163400
|
40 |
* dac@iver.es
|
41 |
*/
|
42 |
package es.gva.cit.catalog.utils.xmltreetable; |
43 |
import java.awt.Font; |
44 |
import java.util.Enumeration; |
45 |
import java.util.Vector; |
46 |
|
47 |
import javax.swing.JEditorPane; |
48 |
import javax.swing.JLabel; |
49 |
import javax.swing.JScrollPane; |
50 |
import javax.swing.JTree; |
51 |
import javax.swing.tree.DefaultMutableTreeNode; |
52 |
import javax.swing.tree.TreePath; |
53 |
|
54 |
import next.swing.AbstractTreeTableModel; |
55 |
import next.swing.TreeTableModel; |
56 |
|
57 |
import org.amic.util.date.Date; |
58 |
|
59 |
import es.gva.cit.catalog.metadataxml.XMLNode; |
60 |
import es.gva.cit.catalog.utils.Strings; |
61 |
|
62 |
/**
|
63 |
*
|
64 |
*
|
65 |
*
|
66 |
* @author Jorge Piera Llodra (piera_jor@gva.es)
|
67 |
*/
|
68 |
public class XMLTreeTableModel extends AbstractTreeTableModel { |
69 |
|
70 |
/**
|
71 |
*
|
72 |
*
|
73 |
*/
|
74 |
private Vector columns = null; |
75 |
|
76 |
/**
|
77 |
*
|
78 |
*
|
79 |
*/
|
80 |
private String[] allowed = null; |
81 |
|
82 |
/**
|
83 |
*
|
84 |
*
|
85 |
*/
|
86 |
private String[] forbidden = null; |
87 |
|
88 |
/**
|
89 |
*
|
90 |
*
|
91 |
*/
|
92 |
private boolean dynamic = false; |
93 |
|
94 |
/**
|
95 |
* Creates new XmlTreeTableModel
|
96 |
*
|
97 |
*
|
98 |
* @param node
|
99 |
*/
|
100 |
public XMLTreeTableModel(XMLNode node) {
|
101 |
this(node, -1, null, null); |
102 |
} |
103 |
|
104 |
/**
|
105 |
*
|
106 |
*
|
107 |
*
|
108 |
* @param node
|
109 |
* @param level
|
110 |
* @param allowed
|
111 |
* @param forbidden
|
112 |
*/
|
113 |
public XMLTreeTableModel(XMLNode node, int level, String[] allowed, String[] forbidden) { |
114 |
super(new DefaultMutableTreeNode(node)); |
115 |
this.allowed = allowed;
|
116 |
this.forbidden = forbidden;
|
117 |
processChildren((DefaultMutableTreeNode) getRoot(), level);
|
118 |
} |
119 |
|
120 |
/**
|
121 |
*
|
122 |
*
|
123 |
*/
|
124 |
public final int ALIGN_LEFT = JLabel.LEFT; |
125 |
|
126 |
/**
|
127 |
*
|
128 |
*
|
129 |
*/
|
130 |
public final int ALIGN_RIGHT = JLabel.RIGHT; |
131 |
|
132 |
/**
|
133 |
*
|
134 |
*
|
135 |
*/
|
136 |
public final int ALIGN_CENTER = JLabel.CENTER; |
137 |
|
138 |
/**
|
139 |
*
|
140 |
*
|
141 |
*
|
142 |
* @param value
|
143 |
*/
|
144 |
public void setDynamic(boolean value) { |
145 |
dynamic = value; |
146 |
} |
147 |
|
148 |
/**
|
149 |
*
|
150 |
*
|
151 |
*
|
152 |
* @return
|
153 |
* @param node
|
154 |
*/
|
155 |
public boolean isLeaf(Object node) { |
156 |
return (getChildCount(node) == 0) && !dynamic; |
157 |
} |
158 |
|
159 |
/**
|
160 |
*
|
161 |
*
|
162 |
*
|
163 |
* @return
|
164 |
* @param node
|
165 |
* @param level
|
166 |
*/
|
167 |
public boolean processChildren(DefaultMutableTreeNode node, int level) { |
168 |
if (level == 0) { |
169 |
return true; |
170 |
} |
171 |
boolean doIt = true; |
172 |
XMLNode n = (XMLNode) node.getUserObject(); |
173 |
if (allowed != null) { |
174 |
String name = n.getName();
|
175 |
doIt = false;
|
176 |
for (int i = 0; i < allowed.length; i++) |
177 |
if (allowed[i].equals(name)) {
|
178 |
doIt = true;
|
179 |
break;
|
180 |
} |
181 |
} |
182 |
if (forbidden != null) { |
183 |
String name = n.getName();
|
184 |
for (int i = 0; i < forbidden.length; i++) |
185 |
if (forbidden[i].equals(name)) {
|
186 |
doIt = false;
|
187 |
break;
|
188 |
} |
189 |
} |
190 |
if (!doIt) {
|
191 |
return false; |
192 |
} |
193 |
XMLNode[] nodes = n.getSubnodes();
|
194 |
DefaultMutableTreeNode newNode;
|
195 |
for (int i=0 ; i<nodes.length ; i++) { |
196 |
newNode = new DefaultMutableTreeNode(nodes[i]); |
197 |
if (processChildren(newNode, level - 1)) { |
198 |
node.add(newNode); |
199 |
} |
200 |
} |
201 |
return true; |
202 |
} |
203 |
|
204 |
/**
|
205 |
*
|
206 |
*
|
207 |
*
|
208 |
* @param ctt
|
209 |
*/
|
210 |
public void configureView(XMLTreeTable ctt) { |
211 |
Column col; |
212 |
javax.swing.table.TableColumnModel tcm = ctt.getColumnModel(); |
213 |
ctt.setRowHeight(20);
|
214 |
int total = 0; |
215 |
for (int i = 1; i < tcm.getColumnCount(); i++) { |
216 |
col = ((Column) columns.elementAt(i - 1));
|
217 |
tcm.getColumn(i).setPreferredWidth(50);
|
218 |
total += col.width; |
219 |
tcm.getColumn(i).setCellRenderer(new XMLTreeTableCellRenderer());
|
220 |
} |
221 |
tcm.getColumn(0).setWidth(100); |
222 |
setExpandedTree((DefaultMutableTreeNode) getRoot(), ctt.getTree());
|
223 |
} |
224 |
|
225 |
/**
|
226 |
*
|
227 |
*
|
228 |
*
|
229 |
* @return
|
230 |
* @param node
|
231 |
* @param tree
|
232 |
*/
|
233 |
public boolean setExpandedTree(DefaultMutableTreeNode node, JTree tree) { |
234 |
boolean isExpanded = false; |
235 |
Enumeration children = node.children();
|
236 |
while (children.hasMoreElements()) {
|
237 |
DefaultMutableTreeNode child = (DefaultMutableTreeNode) children.nextElement(); |
238 |
if (setExpandedTree(child, tree) && (isExpanded == false)) { |
239 |
isExpanded = true;
|
240 |
} |
241 |
} |
242 |
if (node.isLeaf() && (isValueAt(node) || isAttributeAt(node))) {
|
243 |
tree.expandPath(new TreePath(node.getPath())); |
244 |
return true; |
245 |
} |
246 |
if (isExpanded) {
|
247 |
tree.expandPath(new TreePath(node.getPath())); |
248 |
} |
249 |
return isExpanded;
|
250 |
} |
251 |
|
252 |
/**
|
253 |
*
|
254 |
*
|
255 |
*
|
256 |
* @param name
|
257 |
* @param attribute
|
258 |
* @param label
|
259 |
* @param width
|
260 |
*/
|
261 |
public void addJScrollPaneColumn(String name, String attribute, String label, int width) { |
262 |
addColumn(new Column(name, attribute, label, ALIGN_LEFT, width,
|
263 |
JScrollPane.class, null)); |
264 |
} |
265 |
|
266 |
/**
|
267 |
*
|
268 |
*
|
269 |
*
|
270 |
* @param name
|
271 |
* @param attribute
|
272 |
* @param label
|
273 |
* @param width
|
274 |
*/
|
275 |
public void addStringColumn(String name, String attribute, String label, int width) { |
276 |
addColumn(new Column(name, attribute, label, ALIGN_LEFT, width,
|
277 |
String.class, null)); |
278 |
} |
279 |
|
280 |
/**
|
281 |
*
|
282 |
*
|
283 |
*
|
284 |
* @param name
|
285 |
* @param attribute
|
286 |
* @param label
|
287 |
* @param width
|
288 |
* @param format
|
289 |
*/
|
290 |
public void addNumberColumn(String name, String attribute, String label, int width, String format) { |
291 |
addColumn(new Column(name, attribute, label, ALIGN_RIGHT, width,
|
292 |
Double.class, format));
|
293 |
} |
294 |
|
295 |
/**
|
296 |
*
|
297 |
*
|
298 |
*
|
299 |
* @param name
|
300 |
* @param attribute
|
301 |
* @param label
|
302 |
* @param width
|
303 |
* @param format
|
304 |
*/
|
305 |
public void addDateColumn(String name, String attribute, String label, int width, String format) { |
306 |
addColumn(new Column(name, attribute, label, ALIGN_LEFT, width,
|
307 |
Date.class, format));
|
308 |
} |
309 |
|
310 |
/**
|
311 |
*
|
312 |
*
|
313 |
*
|
314 |
* @param column
|
315 |
*/
|
316 |
public void addColumn(Column column) { |
317 |
if (columns == null) { |
318 |
columns = new Vector(); |
319 |
} |
320 |
columns.add(column); |
321 |
} |
322 |
|
323 |
/**
|
324 |
*
|
325 |
*
|
326 |
*
|
327 |
* @return
|
328 |
* @param parent
|
329 |
*/
|
330 |
public int getChildCount(Object parent) { |
331 |
return ((DefaultMutableTreeNode) parent).getChildCount(); |
332 |
} |
333 |
|
334 |
/**
|
335 |
*
|
336 |
*
|
337 |
*
|
338 |
* @return
|
339 |
* @param parent
|
340 |
* @param index
|
341 |
*/
|
342 |
public Object getChild(Object parent, int index) { |
343 |
return ((DefaultMutableTreeNode) parent).getChildAt(index); |
344 |
} |
345 |
|
346 |
/**
|
347 |
*
|
348 |
*
|
349 |
*
|
350 |
* @return
|
351 |
* @param column
|
352 |
*/
|
353 |
public String getColumnName(int column) { |
354 |
if (column == 0) { |
355 |
return ""; |
356 |
} |
357 |
return ((Column) columns.elementAt(column - 1)).label; |
358 |
} |
359 |
|
360 |
/**
|
361 |
*
|
362 |
*
|
363 |
*
|
364 |
* @return
|
365 |
* @param column
|
366 |
*/
|
367 |
public Class getColumnClass(int column) { |
368 |
if (column == 0) { |
369 |
return TreeTableModel.class;
|
370 |
} |
371 |
return ((Column) columns.elementAt(column - 1)).jClass; |
372 |
} |
373 |
|
374 |
/**
|
375 |
*
|
376 |
*
|
377 |
*
|
378 |
* @return
|
379 |
* @param node
|
380 |
*/
|
381 |
public boolean isValueAt(Object node) { |
382 |
DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) node; |
383 |
if (currentNode.getUserObject() instanceof XMLNode) { |
384 |
XMLNode xr = (XMLNode) currentNode.getUserObject(); |
385 |
if (xr != null) { |
386 |
if (xr.getText() != null) { |
387 |
if (!(Strings.replace(xr.getText(), " ", "").equals(""))) { |
388 |
return true; |
389 |
} |
390 |
} |
391 |
} |
392 |
} |
393 |
return false; |
394 |
} |
395 |
|
396 |
/**
|
397 |
*
|
398 |
*
|
399 |
*
|
400 |
* @return
|
401 |
* @param node
|
402 |
*/
|
403 |
public boolean isAttributeAt(Object node) { |
404 |
DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) node; |
405 |
if (currentNode.getUserObject() instanceof XMLNode) { |
406 |
XMLNode xr = (XMLNode) currentNode.getUserObject(); |
407 |
if (xr != null) { |
408 |
if (getAttribute(xr) != null) { |
409 |
if (!(Strings.replace(getAttribute(xr),
|
410 |
" ", "").equals(""))) { |
411 |
return true; |
412 |
} |
413 |
} |
414 |
} |
415 |
} |
416 |
return false; |
417 |
} |
418 |
|
419 |
/**
|
420 |
*
|
421 |
*
|
422 |
*
|
423 |
* @return
|
424 |
* @param node
|
425 |
*/
|
426 |
public String getValueAt(Object node) { |
427 |
DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) node; |
428 |
if (currentNode.getUserObject() instanceof XMLNode) { |
429 |
XMLNode xr = (XMLNode) currentNode.getUserObject(); |
430 |
|
431 |
return xr.getText();
|
432 |
} |
433 |
return null; |
434 |
} |
435 |
|
436 |
/**
|
437 |
*
|
438 |
*
|
439 |
*
|
440 |
* @return
|
441 |
* @param node
|
442 |
* @param column
|
443 |
*/
|
444 |
public Object getValueAt(Object node, int column) { |
445 |
//JScrollPane
|
446 |
JEditorPane editor = new JEditorPane(); |
447 |
editor.setEditable(false);
|
448 |
JScrollPane scroll = new JScrollPane(editor); |
449 |
scroll.setBorder(null);
|
450 |
//JLabel
|
451 |
JLabel text = new JLabel(); |
452 |
text.setFont(new Font(null, Font.PLAIN, 12)); |
453 |
DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) node; |
454 |
if (currentNode.getUserObject() instanceof XMLNode) { |
455 |
XMLNode xr = (XMLNode) currentNode.getUserObject(); |
456 |
if (column == 0) { |
457 |
return xr.toString();
|
458 |
} |
459 |
Column col = (Column) columns.elementAt(column - 1);
|
460 |
String att = col.attribute;
|
461 |
try {
|
462 |
if (att.equals("tag-name")) { |
463 |
if (xr.getName().trim().length() > 40) { |
464 |
editor.setText(xr.getName()); |
465 |
editor.setCaretPosition(0);
|
466 |
return scroll;
|
467 |
} |
468 |
text.setText(xr.getName()); |
469 |
|
470 |
return text;
|
471 |
} else if (att.equals("tag-value")) { |
472 |
if (xr.getText().trim().length() > 40) { |
473 |
editor.setText(xr.getText()); |
474 |
editor.setCaretPosition(0);
|
475 |
return scroll;
|
476 |
} |
477 |
text.setText(xr.getText()); |
478 |
return text;
|
479 |
} else {
|
480 |
if (getAttribute(xr).trim().length() > 40) { |
481 |
editor.setText(getAttribute(xr)); |
482 |
editor.setCaretPosition(0);
|
483 |
return scroll;
|
484 |
} |
485 |
text.setText(getAttribute(xr)); |
486 |
return text;
|
487 |
} |
488 |
} catch (Exception ex) { |
489 |
} |
490 |
} |
491 |
return null; |
492 |
} |
493 |
|
494 |
/**
|
495 |
*
|
496 |
*
|
497 |
*
|
498 |
* @return
|
499 |
* @param node
|
500 |
*/
|
501 |
public String getAttribute(XMLNode node) { |
502 |
String attributes = ""; |
503 |
Vector vAttributes = node.getAttributeNames();
|
504 |
|
505 |
for (int i = 0; i < vAttributes.size(); i++){ |
506 |
String att = (String)vAttributes.get(i); |
507 |
String val = node.getAttribute(att);
|
508 |
attributes = attributes + |
509 |
att + "=" + val + " "; |
510 |
} |
511 |
return attributes;
|
512 |
} |
513 |
|
514 |
/**
|
515 |
*
|
516 |
*
|
517 |
*
|
518 |
* @return
|
519 |
*/
|
520 |
public int getColumnCount() { |
521 |
if (columns == null) { |
522 |
return 1; |
523 |
} |
524 |
return columns.size() + 1; |
525 |
} |
526 |
/**
|
527 |
*
|
528 |
*
|
529 |
*/
|
530 |
public class Column { |
531 |
|
532 |
/**
|
533 |
*
|
534 |
*
|
535 |
*/
|
536 |
int align;
|
537 |
|
538 |
/**
|
539 |
*
|
540 |
*
|
541 |
*/
|
542 |
int width;
|
543 |
|
544 |
/**
|
545 |
*
|
546 |
*
|
547 |
*/
|
548 |
Class jClass;
|
549 |
|
550 |
/**
|
551 |
*
|
552 |
*
|
553 |
*/
|
554 |
String name;
|
555 |
|
556 |
/**
|
557 |
*
|
558 |
*
|
559 |
*/
|
560 |
String attribute;
|
561 |
|
562 |
/**
|
563 |
*
|
564 |
*
|
565 |
*/
|
566 |
String label;
|
567 |
|
568 |
/**
|
569 |
*
|
570 |
*
|
571 |
*/
|
572 |
String format;
|
573 |
|
574 |
/**
|
575 |
*
|
576 |
*
|
577 |
*
|
578 |
* @param name
|
579 |
* @param attribute
|
580 |
* @param label
|
581 |
* @param align
|
582 |
* @param width
|
583 |
* @param jClass
|
584 |
* @param format
|
585 |
*/
|
586 |
Column(String name, String attribute, String label, int align, int width, Class jClass, String format) { |
587 |
this.name = name;
|
588 |
this.attribute = attribute;
|
589 |
this.label = label;
|
590 |
this.align = align;
|
591 |
this.width = width;
|
592 |
this.jClass = jClass;
|
593 |
this.format = format;
|
594 |
} |
595 |
} |
596 |
} |