KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.calipso.xmleditor;
2
3 import org.exolab.castor.xml.schema.Facet;
4 import javax.swing.*;
5 import javax.swing.text.DefaultFormatterFactory JavaDoc;
6 import javax.swing.text.NumberFormatter JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.Vector JavaDoc;
9 import java.util.Enumeration JavaDoc;
10 import java.awt.*;
11 import java.awt.event.MouseListener JavaDoc;
12 import java.awt.event.MouseEvent JavaDoc;
13 import java.awt.event.MouseAdapter JavaDoc;
14 import java.text.DecimalFormat JavaDoc;
15
16 /**
17  *
18  * User: soliveri
19  * Date: 02-oct-2003
20  * Time: 14:03:08
21  *
22  */

23
24 public class XmlEditorSubPanel extends JPanel {
25
26   private Vector JavaDoc cboBoxDefaults;
27   private Vector JavaDoc elements;
28   private Vector JavaDoc inputComponents;
29
30   /**
31    * Crea un nuevo SubPanel con las claves y elementos pasados por parametros.
32    * @param keys
33    * @param elements
34    * @throws XmlEditorException
35    */

36   public XmlEditorSubPanel(Vector JavaDoc keys, HashMap JavaDoc elements) throws XmlEditorException{
37     this.elements = keys;
38     cboBoxDefaults = new Vector JavaDoc();
39     initialize(keys, elements);
40   }
41
42   public XmlEditorSubPanel(Vector JavaDoc keys) {
43     this.elements = keys;
44     initialize(keys);
45   }
46
47   /**
48    * Inicializa el panel, con las claves dadas.
49     * @param keys
50    */

51   private void initialize(Vector JavaDoc 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 JavaDoc keys, HashMap JavaDoc 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   /**
66    * Completa el panel (GridBagLayout) con los valores que tiene creando los labels y valores en base a sus atributos.
67    * @param keys
68    * @param bagLayout
69    * @param constraints
70    */

71   private void fillFrom(Vector JavaDoc keys, GridBagLayout bagLayout,GridBagConstraints constraints) {
72     int yPosition1 = 0;
73     int yPosition2 = 0;
74     Enumeration JavaDoc 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 JavaDoc keys, HashMap JavaDoc elements, GridBagLayout bagLayout,GridBagConstraints constraints) throws XmlEditorException{
99     Enumeration JavaDoc 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 JavaDoc(new DecimalFormat JavaDoc("##########"));
163   }
164
165   private Vector JavaDoc getBooleanValues() {
166     Vector JavaDoc items = new Vector JavaDoc();
167     items.add("FALSE");
168     items.add("TRUE");
169     cboBoxDefaults.add("FALSE");
170     return items;
171   }
172
173   private Vector JavaDoc getItems(Object JavaDoc values) {
174     Vector JavaDoc items = new Vector JavaDoc();
175     Enumeration JavaDoc enumeration = (Enumeration JavaDoc) 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   /**
187    * Borra todos los campos de un panel
188    */

189   public void eraseInputFields() {
190     Enumeration JavaDoc inputComponents = getInputComponents().elements();
191     for(int i = 0 ; inputComponents.hasMoreElements() ;) {
192       Object JavaDoc 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 JavaDoc vector = modelNode.getAttributes();
205     Enumeration JavaDoc inputComponents = getInputComponents().elements();
206     for(int i = 0 ; inputComponents.hasMoreElements() && vector.size()>i; i++) {
207       Object JavaDoc object = inputComponents.nextElement();
208       if(object instanceof JTextField) {
209         ((JTextField)object).setText((String JavaDoc)vector.elementAt(i));
210       } else if(object instanceof JComboBox){
211         if(isInBox(vector.elementAt(i).toString().toUpperCase(), (JComboBox)object)){
212           ((JComboBox)object).setSelectedItem(((String JavaDoc)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 JavaDoc 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 JavaDoc enumeration = getInputComponents().elements();
232     while(enumeration.hasMoreElements()) {
233       Object JavaDoc 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 JavaDoc getInputComponents() {
245     if(inputComponents == null) {
246       inputComponents = new Vector JavaDoc();
247     }
248     return inputComponents;
249   }
250
251   public Vector JavaDoc getElements() {
252     return elements;
253   }
254
255   public Vector JavaDoc getCboBoxDefaults() {
256     return cboBoxDefaults;
257   }
258 }
259
Popular Tags