1 package com.calipso.xmleditor; 2 3 import javax.swing.tree.DefaultMutableTreeNode ; 4 import javax.swing.*; 5 import java.util.Vector ; 6 import java.util.Enumeration ; 7 8 15 16 public class XmlEditorTreeModelNode extends DefaultMutableTreeNode { 17 18 private Vector attributes; 19 private Vector attributeNames; 20 21 public XmlEditorTreeModelNode(String name) { 22 super(name); 23 } 24 25 29 public void addAttributesFrom(XmlEditorSubPanel subPanel) { 30 attributeNames = subPanel.getElements(); 31 Enumeration enumeration = subPanel.getInputComponents().elements(); 32 for(int i = 0 ; enumeration.hasMoreElements() ; i++) { 33 Object 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 47 public void updateAttributesFrom(XmlEditorSubPanel subPanel) { 48 Enumeration enumeration = subPanel.getInputComponents().elements(); 49 for(int i = 0 ; enumeration.hasMoreElements() ; i++) { 50 Object 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 [] objects =comboBox.getSelectedObjects(); 58 getAttributes().add(i, objects [0]); 59 } 60 } 61 } 62 } 63 64 public void addAttributesFrom(Vector attrNames, Vector attributes) { 65 this.attributeNames = attrNames; 66 this.attributes = attributes; 67 } 68 69 public Vector getAttributeNames() { 70 return attributeNames; 71 } 72 73 public Vector getAttributes() { 74 if(attributes == null) { 75 attributes = new Vector (); 76 } 77 return attributes; 78 } 79 80 public String getId() { 81 Vector 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 getAttribute(String 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 |