001: package com.calipso.xmleditor;
002:
003: import org.exolab.castor.xml.schema.Facet;
004: import javax.swing.*;
005: import javax.swing.text.DefaultFormatterFactory;
006: import javax.swing.text.NumberFormatter;
007: import java.util.HashMap;
008: import java.util.Vector;
009: import java.util.Enumeration;
010: import java.awt.*;
011: import java.awt.event.MouseListener;
012: import java.awt.event.MouseEvent;
013: import java.awt.event.MouseAdapter;
014: import java.text.DecimalFormat;
015:
016: /**
017: *
018: * User: soliveri
019: * Date: 02-oct-2003
020: * Time: 14:03:08
021: *
022: */
023:
024: public class XmlEditorSubPanel extends JPanel {
025:
026: private Vector cboBoxDefaults;
027: private Vector elements;
028: private Vector inputComponents;
029:
030: /**
031: * Crea un nuevo SubPanel con las claves y elementos pasados por parametros.
032: * @param keys
033: * @param elements
034: * @throws XmlEditorException
035: */
036: public XmlEditorSubPanel(Vector keys, HashMap elements)
037: throws XmlEditorException {
038: this .elements = keys;
039: cboBoxDefaults = new Vector();
040: initialize(keys, elements);
041: }
042:
043: public XmlEditorSubPanel(Vector keys) {
044: this .elements = keys;
045: initialize(keys);
046: }
047:
048: /**
049: * Inicializa el panel, con las claves dadas.
050: * @param keys
051: */
052: private void initialize(Vector keys) {
053: GridBagLayout bagLayout = new GridBagLayout();
054: setLayout(bagLayout);
055: GridBagConstraints constraints = new GridBagConstraints();
056: fillFrom(keys, bagLayout, constraints);
057: }
058:
059: private void initialize(Vector keys, HashMap elements)
060: throws XmlEditorException {
061: GridBagLayout bagLayout = new GridBagLayout();
062: setLayout(bagLayout);
063: GridBagConstraints constraints = new GridBagConstraints();
064: fillFrom(keys, elements, bagLayout, constraints);
065: }
066:
067: /**
068: * Completa el panel (GridBagLayout) con los valores que tiene creando los labels y valores en base a sus atributos.
069: * @param keys
070: * @param bagLayout
071: * @param constraints
072: */
073: private void fillFrom(Vector keys, GridBagLayout bagLayout,
074: GridBagConstraints constraints) {
075: int yPosition1 = 0;
076: int yPosition2 = 0;
077: Enumeration enumeration = keys.elements();
078: while (enumeration.hasMoreElements()) {
079: Component component = new JLabel(enumeration.nextElement()
080: .toString());
081: add(component);
082: constraints.weightx = 5;
083: constraints.gridx = 0;
084: constraints.gridy = yPosition1++;
085: constraints.anchor = GridBagConstraints.SOUTHWEST;
086: constraints.fill = GridBagConstraints.BOTH;
087: bagLayout.setConstraints(component, constraints);
088:
089: component = new JTextField();
090: ((JTextField) component).setAutoscrolls(true);
091: getInputComponents().add(component);
092: add(component);
093: constraints.weightx = 5;
094: constraints.gridx = 1;
095: constraints.gridy = yPosition2++;
096: constraints.anchor = GridBagConstraints.NORTHEAST;
097: constraints.fill = GridBagConstraints.BOTH;
098: bagLayout.setConstraints(component, constraints);
099: }
100: }
101:
102: private void fillFrom(Vector keys, HashMap elements,
103: GridBagLayout bagLayout, GridBagConstraints constraints)
104: throws XmlEditorException {
105: Enumeration enumeration = keys.elements();
106: int yPosition1 = 0;
107: int yPosition2 = 0;
108: while (enumeration.hasMoreElements()) {
109: XmlEditorTreeNodeItemDefinition itemDefinition = (XmlEditorTreeNodeItemDefinition) elements
110: .get(enumeration.nextElement().toString());
111: if (itemDefinition != null) {
112: Component component = getLabel(itemDefinition);
113: add(component);
114: constraints.weightx = 5;
115: constraints.gridx = 0;
116: constraints.gridy = yPosition1++;
117: constraints.anchor = GridBagConstraints.SOUTHWEST;
118: constraints.fill = GridBagConstraints.BOTH;
119: bagLayout.setConstraints(component, constraints);
120:
121: component = getInputObject(itemDefinition);
122: getInputComponents().add(component);
123: add(component);
124: constraints.weightx = 5;
125: constraints.gridx = 1;
126: constraints.gridy = yPosition2++;
127: constraints.anchor = GridBagConstraints.NORTHEAST;
128: constraints.fill = GridBagConstraints.BOTH;
129: bagLayout.setConstraints(component, constraints);
130: }
131: }
132: }
133:
134: private JLabel getLabel(
135: XmlEditorTreeNodeItemDefinition itemDefinition) {
136: JLabel label;
137: if (itemDefinition.isOptional()) {
138: label = new JLabel(" " + itemDefinition.getName() + " :");
139: } else {
140: label = new JLabel(" " + itemDefinition.getName()
141: + " * :");
142: }
143: label.setSize(new Dimension(getSize().width, HEIGHT));
144: return label;
145: }
146:
147: private Component getInputObject(
148: XmlEditorTreeNodeItemDefinition itemDefinition)
149: throws XmlEditorException {
150: Component component = null;
151: int type = itemDefinition.getType();
152: switch (type) {
153: case XmlEditorDataType.TOKENS:
154: component = new JComboBox(getItems(itemDefinition
155: .getValue()));
156: break;
157: case XmlEditorDataType.BOOLEAN:
158: component = new JComboBox(getBooleanValues());
159: break;
160: case XmlEditorDataType.STRING:
161: component = new JTextField();
162: ((JTextField) component).setAutoscrolls(true);
163: break;
164: case XmlEditorDataType.INTEGER:
165: component = new JFormattedTextField(getNumberFormatter());
166: break;
167: default:
168: throw new XmlEditorException(
169: "No existe el tipo de dato solicitado");
170: }
171: return component;
172: }
173:
174: private JFormattedTextField.AbstractFormatter getNumberFormatter() {
175: return new NumberFormatter(new DecimalFormat("##########"));
176: }
177:
178: private Vector getBooleanValues() {
179: Vector items = new Vector();
180: items.add("FALSE");
181: items.add("TRUE");
182: cboBoxDefaults.add("FALSE");
183: return items;
184: }
185:
186: private Vector getItems(Object values) {
187: Vector items = new Vector();
188: Enumeration enumeration = (Enumeration) values;
189: for (int i = 0; enumeration.hasMoreElements(); i++) {
190: Facet facet = (Facet) enumeration.nextElement();
191: items.add(facet.getValue().toUpperCase());
192: if (i == 0) {
193: cboBoxDefaults.add(facet.getValue().toUpperCase());
194: }
195: }
196: return items;
197: }
198:
199: /**
200: * Borra todos los campos de un panel
201: */
202: public void eraseInputFields() {
203: Enumeration inputComponents = getInputComponents().elements();
204: for (int i = 0; inputComponents.hasMoreElements();) {
205: Object object = inputComponents.nextElement();
206: if (object instanceof JTextField) {
207: ((JTextField) object).setText("");
208: } else if (object instanceof JComboBox) {
209: ((JComboBox) object).setSelectedItem(cboBoxDefaults
210: .elementAt(i));
211: i++;
212: }
213: }
214: validate();
215: }
216:
217: public void fillFrom(XmlEditorTreeModelNode modelNode) {
218: Vector vector = modelNode.getAttributes();
219: Enumeration inputComponents = getInputComponents().elements();
220: for (int i = 0; inputComponents.hasMoreElements()
221: && vector.size() > i; i++) {
222: Object object = inputComponents.nextElement();
223: if (object instanceof JTextField) {
224: ((JTextField) object).setText((String) vector
225: .elementAt(i));
226: } else if (object instanceof JComboBox) {
227: if (isInBox(vector.elementAt(i).toString()
228: .toUpperCase(), (JComboBox) object)) {
229: ((JComboBox) object)
230: .setSelectedItem(((String) vector
231: .elementAt(i)).toUpperCase());
232: } else {
233: JOptionPane
234: .showMessageDialog(
235: this ,
236: "El valor "
237: + vector.elementAt(i)
238: + " no es valido para el atributo. Sera reemplazado",
239: "Error", JOptionPane.ERROR_MESSAGE);
240: }
241: }
242: }
243: }
244:
245: private boolean isInBox(String s, JComboBox jComboBox) {
246: for (int i = 0; i < jComboBox.getItemCount(); i++) {
247: if (jComboBox.getItemAt(i).toString().equalsIgnoreCase(s)) {
248: return true;
249: }
250: }
251: return false;
252: }
253:
254: public boolean isErased() {
255: boolean returnVal = true;
256: Enumeration enumeration = getInputComponents().elements();
257: while (enumeration.hasMoreElements()) {
258: Object object = enumeration.nextElement();
259: if (object instanceof JTextField) {
260: if (!((JTextField) object).getText().equals("")) {
261: returnVal = false;
262: break;
263: }
264: }
265: }
266: return returnVal;
267: }
268:
269: public Vector getInputComponents() {
270: if (inputComponents == null) {
271: inputComponents = new Vector();
272: }
273: return inputComponents;
274: }
275:
276: public Vector getElements() {
277: return elements;
278: }
279:
280: public Vector getCboBoxDefaults() {
281: return cboBoxDefaults;
282: }
283: }
|