1 package com.calipso.xmleditor; 2 3 import org.exolab.castor.xml.schema.Facet; 4 import javax.swing.*; 5 import javax.swing.text.DefaultFormatterFactory ; 6 import javax.swing.text.NumberFormatter ; 7 import java.util.HashMap ; 8 import java.util.Vector ; 9 import java.util.Enumeration ; 10 import java.awt.*; 11 import java.awt.event.MouseListener ; 12 import java.awt.event.MouseEvent ; 13 import java.awt.event.MouseAdapter ; 14 import java.text.DecimalFormat ; 15 16 23 24 public class XmlEditorSubPanel extends JPanel { 25 26 private Vector cboBoxDefaults; 27 private Vector elements; 28 private Vector inputComponents; 29 30 36 public XmlEditorSubPanel(Vector keys, HashMap elements) throws XmlEditorException{ 37 this.elements = keys; 38 cboBoxDefaults = new Vector (); 39 initialize(keys, elements); 40 } 41 42 public XmlEditorSubPanel(Vector keys) { 43 this.elements = keys; 44 initialize(keys); 45 } 46 47 51 private void initialize(Vector keys) { 52 GridBagLayout bagLayout = new GridBagLayout(); 53 setLayout(bagLayout); 54 GridBagConstraints constraints = new GridBagConstraints(); 55 fillFrom(keys, bagLayout, constraints); 56 } 57 58 private void initialize(Vector keys, HashMap elements) throws XmlEditorException{ 59 GridBagLayout bagLayout = new GridBagLayout(); 60 setLayout(bagLayout); 61 GridBagConstraints constraints = new GridBagConstraints(); 62 fillFrom(keys, elements, bagLayout, constraints); 63 } 64 65 71 private void fillFrom(Vector keys, GridBagLayout bagLayout,GridBagConstraints constraints) { 72 int yPosition1 = 0; 73 int yPosition2 = 0; 74 Enumeration enumeration = keys.elements(); 75 while(enumeration.hasMoreElements()) { 76 Component component = new JLabel(enumeration.nextElement().toString()); 77 add(component); 78 constraints.weightx = 5; 79 constraints.gridx = 0; 80 constraints.gridy = yPosition1++; 81 constraints.anchor = GridBagConstraints.SOUTHWEST; 82 constraints.fill = GridBagConstraints.BOTH; 83 bagLayout.setConstraints(component, constraints); 84 85 component = new JTextField(); 86 ((JTextField)component).setAutoscrolls(true); 87 getInputComponents().add(component); 88 add(component); 89 constraints.weightx = 5; 90 constraints.gridx = 1; 91 constraints.gridy = yPosition2++; 92 constraints.anchor = GridBagConstraints.NORTHEAST; 93 constraints.fill = GridBagConstraints.BOTH; 94 bagLayout.setConstraints(component, constraints); 95 } 96 } 97 98 private void fillFrom(Vector keys, HashMap elements, GridBagLayout bagLayout,GridBagConstraints constraints) throws XmlEditorException{ 99 Enumeration enumeration = keys.elements(); 100 int yPosition1 = 0; 101 int yPosition2 = 0; 102 while(enumeration.hasMoreElements()) { 103 XmlEditorTreeNodeItemDefinition itemDefinition = (XmlEditorTreeNodeItemDefinition) elements.get(enumeration.nextElement().toString()); 104 if(itemDefinition != null) { 105 Component component = getLabel(itemDefinition); 106 add(component); 107 constraints.weightx = 5; 108 constraints.gridx = 0; 109 constraints.gridy = yPosition1++; 110 constraints.anchor = GridBagConstraints.SOUTHWEST; 111 constraints.fill = GridBagConstraints.BOTH; 112 bagLayout.setConstraints(component, constraints); 113 114 component = getInputObject(itemDefinition); 115 getInputComponents().add(component); 116 add(component); 117 constraints.weightx = 5; 118 constraints.gridx = 1; 119 constraints.gridy = yPosition2++; 120 constraints.anchor = GridBagConstraints.NORTHEAST; 121 constraints.fill = GridBagConstraints.BOTH; 122 bagLayout.setConstraints(component, constraints); 123 } 124 } 125 } 126 127 private JLabel getLabel(XmlEditorTreeNodeItemDefinition itemDefinition) { 128 JLabel label; 129 if(itemDefinition.isOptional()) { 130 label = new JLabel(" " + itemDefinition.getName() + " :"); 131 } else { 132 label = new JLabel(" " + itemDefinition.getName() + " * :" ); 133 } 134 label.setSize(new Dimension(getSize().width, HEIGHT)); 135 return label; 136 } 137 138 private Component getInputObject(XmlEditorTreeNodeItemDefinition itemDefinition) throws XmlEditorException{ 139 Component component = null; 140 int type = itemDefinition.getType(); 141 switch(type){ 142 case XmlEditorDataType.TOKENS: 143 component = new JComboBox(getItems(itemDefinition.getValue())); 144 break; 145 case XmlEditorDataType.BOOLEAN: 146 component = new JComboBox(getBooleanValues()); 147 break; 148 case XmlEditorDataType.STRING: 149 component = new JTextField(); 150 ((JTextField)component).setAutoscrolls(true); 151 break; 152 case XmlEditorDataType.INTEGER: 153 component = new JFormattedTextField(getNumberFormatter()); 154 break; 155 default: 156 throw new XmlEditorException("No existe el tipo de dato solicitado"); 157 } 158 return component; 159 } 160 161 private JFormattedTextField.AbstractFormatter getNumberFormatter() { 162 return new NumberFormatter (new DecimalFormat ("##########")); 163 } 164 165 private Vector getBooleanValues() { 166 Vector items = new Vector (); 167 items.add("FALSE"); 168 items.add("TRUE"); 169 cboBoxDefaults.add("FALSE"); 170 return items; 171 } 172 173 private Vector getItems(Object values) { 174 Vector items = new Vector (); 175 Enumeration enumeration = (Enumeration ) values; 176 for(int i = 0 ; enumeration.hasMoreElements() ; i++) { 177 Facet facet = (Facet) enumeration.nextElement(); 178 items.add(facet.getValue().toUpperCase()); 179 if(i == 0) { 180 cboBoxDefaults.add(facet.getValue().toUpperCase()); 181 } 182 } 183 return items; 184 } 185 186 189 public void eraseInputFields() { 190 Enumeration inputComponents = getInputComponents().elements(); 191 for(int i = 0 ; inputComponents.hasMoreElements() ;) { 192 Object object = inputComponents.nextElement(); 193 if(object instanceof JTextField) { 194 ((JTextField)object).setText(""); 195 } else if(object instanceof JComboBox) { 196 ((JComboBox)object).setSelectedItem(cboBoxDefaults.elementAt(i)); 197 i++; 198 } 199 } 200 validate(); 201 } 202 203 public void fillFrom(XmlEditorTreeModelNode modelNode) { 204 Vector vector = modelNode.getAttributes(); 205 Enumeration inputComponents = getInputComponents().elements(); 206 for(int i = 0 ; inputComponents.hasMoreElements() && vector.size()>i; i++) { 207 Object object = inputComponents.nextElement(); 208 if(object instanceof JTextField) { 209 ((JTextField)object).setText((String )vector.elementAt(i)); 210 } else if(object instanceof JComboBox){ 211 if(isInBox(vector.elementAt(i).toString().toUpperCase(), (JComboBox)object)){ 212 ((JComboBox)object).setSelectedItem(((String )vector.elementAt(i)).toUpperCase()); 213 }else{ 214 JOptionPane.showMessageDialog(this, "El valor " + vector.elementAt(i) + " no es valido para el atributo. Sera reemplazado", "Error", JOptionPane.ERROR_MESSAGE); 215 } 216 } 217 } 218 } 219 220 private boolean isInBox(String s, JComboBox jComboBox) { 221 for(int i=0; i<jComboBox.getItemCount(); i++){ 222 if(jComboBox.getItemAt(i).toString().equalsIgnoreCase(s)){ 223 return true; 224 } 225 } 226 return false; 227 } 228 229 public boolean isErased() { 230 boolean returnVal = true; 231 Enumeration enumeration = getInputComponents().elements(); 232 while(enumeration.hasMoreElements()) { 233 Object object = enumeration.nextElement(); 234 if(object instanceof JTextField) { 235 if(!((JTextField)object).getText().equals("")) { 236 returnVal = false; 237 break; 238 } 239 } 240 } 241 return returnVal; 242 } 243 244 public Vector getInputComponents() { 245 if(inputComponents == null) { 246 inputComponents = new Vector (); 247 } 248 return inputComponents; 249 } 250 251 public Vector getElements() { 252 return elements; 253 } 254 255 public Vector getCboBoxDefaults() { 256 return cboBoxDefaults; 257 } 258 } 259 | Popular Tags |