001: package com.calipso.xmleditor;
002:
003: import javax.swing.tree.DefaultMutableTreeNode;
004: import javax.swing.*;
005: import java.util.Vector;
006: import java.util.Enumeration;
007:
008: /**
009: *
010: * User: soliveri
011: * Date: 01-oct-2003
012: * Time: 17:13:07
013: *
014: */
015:
016: public class XmlEditorTreeModelNode extends DefaultMutableTreeNode {
017:
018: private Vector attributes;
019: private Vector attributeNames;
020:
021: public XmlEditorTreeModelNode(String name) {
022: super (name);
023: }
024:
025: /**
026: * Agrega los atributos a un Nodo en base a los datos de un panel.
027: * @param subPanel
028: */
029: public void addAttributesFrom(XmlEditorSubPanel subPanel) {
030: attributeNames = subPanel.getElements();
031: Enumeration enumeration = subPanel.getInputComponents()
032: .elements();
033: for (int i = 0; enumeration.hasMoreElements(); i++) {
034: Object object = enumeration.nextElement();
035: if (object instanceof JTextField) {
036: getAttributes().add(((JTextField) object).getText());
037: } else {
038: JComboBox comboBox = (JComboBox) object;
039: getAttributes().add(
040: comboBox.getSelectedItem().toString());
041: }
042: }
043: }
044:
045: /**
046: * Actualiza un nodo en base los valores de un SubPanel
047: * @param subPanel
048: */
049: public void updateAttributesFrom(XmlEditorSubPanel subPanel) {
050: Enumeration enumeration = subPanel.getInputComponents()
051: .elements();
052: for (int i = 0; enumeration.hasMoreElements(); i++) {
053: Object object = enumeration.nextElement();
054: if (getAttributes().size() > i) {
055: getAttributes().remove(i);
056: if (object instanceof JTextField) {
057: getAttributes().add(i,
058: ((JTextField) object).getText());
059: } else if (object instanceof JComboBox) {
060: JComboBox comboBox = (JComboBox) object;
061: Object[] objects = comboBox.getSelectedObjects();
062: getAttributes().add(i, objects[0]);
063: }
064: }
065: }
066: }
067:
068: public void addAttributesFrom(Vector attrNames, Vector attributes) {
069: this .attributeNames = attrNames;
070: this .attributes = attributes;
071: }
072:
073: public Vector getAttributeNames() {
074: return attributeNames;
075: }
076:
077: public Vector getAttributes() {
078: if (attributes == null) {
079: attributes = new Vector();
080: }
081: return attributes;
082: }
083:
084: public String getId() {
085: Vector attributeNames = getAttributeNames();
086: if (attributeNames != null && !attributeNames.isEmpty()) {
087: int pos = attributeNames.indexOf("Id");
088: if (pos >= 0) {
089: return getAttributes().elementAt(pos).toString();
090: } else if (attributeNames.indexOf("Name") >= 0) {
091: return getAttributes().elementAt(
092: attributeNames.indexOf("Name")).toString();
093: } else {
094: return getAttributes().elementAt(0).toString();
095: }
096: }
097: return null;
098: }
099:
100: public String getAttribute(String attributeName) {
101: int index = getAttributeNames().indexOf(attributeName);
102: if (index >= 0) {
103: return getAttributes().elementAt(index).toString();
104: } else {
105: return "";
106: }
107: }
108:
109: }
|