KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > xmleditor > XmlEditorTreeModelNode


1 package com.calipso.xmleditor;
2
3 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
4 import javax.swing.*;
5 import java.util.Vector JavaDoc;
6 import java.util.Enumeration JavaDoc;
7
8 /**
9  *
10  * User: soliveri
11  * Date: 01-oct-2003
12  * Time: 17:13:07
13  *
14  */

15
16 public class XmlEditorTreeModelNode extends DefaultMutableTreeNode JavaDoc {
17
18   private Vector JavaDoc attributes;
19   private Vector JavaDoc attributeNames;
20
21   public XmlEditorTreeModelNode(String JavaDoc name) {
22     super(name);
23   }
24
25   /**
26    * Agrega los atributos a un Nodo en base a los datos de un panel.
27    * @param subPanel
28    */

29   public void addAttributesFrom(XmlEditorSubPanel subPanel) {
30     attributeNames = subPanel.getElements();
31     Enumeration JavaDoc enumeration = subPanel.getInputComponents().elements();
32     for(int i = 0 ; enumeration.hasMoreElements() ; i++) {
33       Object JavaDoc object = enumeration.nextElement();
34       if(object instanceof JTextField) {
35         getAttributes().add(((JTextField)object).getText());
36       } else {
37         JComboBox comboBox = (JComboBox) object;
38         getAttributes().add(comboBox.getSelectedItem().toString());
39       }
40     }
41   }
42
43   /**
44    * Actualiza un nodo en base los valores de un SubPanel
45    * @param subPanel
46    */

47   public void updateAttributesFrom(XmlEditorSubPanel subPanel) {
48     Enumeration JavaDoc enumeration = subPanel.getInputComponents().elements();
49     for(int i = 0 ; enumeration.hasMoreElements() ; i++) {
50       Object JavaDoc object = enumeration.nextElement();
51       if(getAttributes().size()>i){
52         getAttributes().remove(i);
53         if(object instanceof JTextField) {
54           getAttributes().add(i, ((JTextField)object).getText());
55         } else if(object instanceof JComboBox) {
56           JComboBox comboBox = (JComboBox) object;
57           Object JavaDoc [] objects =comboBox.getSelectedObjects();
58           getAttributes().add(i, objects [0]);
59         }
60       }
61     }
62   }
63
64   public void addAttributesFrom(Vector JavaDoc attrNames, Vector JavaDoc attributes) {
65     this.attributeNames = attrNames;
66     this.attributes = attributes;
67   }
68
69   public Vector JavaDoc getAttributeNames() {
70     return attributeNames;
71   }
72
73   public Vector JavaDoc getAttributes() {
74     if(attributes == null) {
75       attributes = new Vector JavaDoc();
76     }
77     return attributes;
78   }
79
80   public String JavaDoc getId() {
81     Vector JavaDoc attributeNames = getAttributeNames();
82     if(attributeNames!=null && !attributeNames.isEmpty()){
83       int pos = attributeNames.indexOf("Id");
84       if(pos >= 0){
85         return getAttributes().elementAt(pos).toString();
86       }else if(attributeNames.indexOf("Name")>=0){
87         return getAttributes().elementAt(attributeNames.indexOf("Name")).toString();
88       }else{
89         return getAttributes().elementAt(0).toString();
90       }
91     }
92     return null;
93   }
94
95   public String JavaDoc getAttribute(String JavaDoc attributeName) {
96     int index = getAttributeNames().indexOf(attributeName);
97     if(index>=0){
98       return getAttributes().elementAt(index).toString();
99     }else{
100       return "";
101     }
102   }
103
104 }
105
Popular Tags