001: package net.xoetrope.builder.editor.dialog;
002:
003: import java.io.BufferedReader;
004:
005: import java.awt.FlowLayout;
006: import java.awt.event.ActionEvent;
007: import java.awt.event.ActionListener;
008: import java.awt.event.ItemEvent;
009: import java.awt.event.ItemListener;
010: import javax.swing.DefaultListModel;
011: import javax.swing.JButton;
012: import javax.swing.JCheckBox;
013: import javax.swing.JComboBox;
014: import javax.swing.JDialog;
015: import javax.swing.JLabel;
016: import javax.swing.JList;
017: import javax.swing.JPanel;
018: import javax.swing.JTextField;
019: import javax.swing.event.ListSelectionEvent;
020: import javax.swing.event.ListSelectionListener;
021:
022: import net.xoetrope.builder.editor.XEditorProject;
023: import net.xoetrope.xml.XmlElement;
024: import net.xoetrope.xml.XmlSource;
025: import net.xoetrope.xui.XResourceManager;
026: import net.xoetrope.debug.DebugLogger;
027:
028: /**
029: * <p>Description: </p>
030: * <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003</p>
031: * $Revision: 1.6 $
032: * License: see license.txt
033: */
034: public class ValidationDlg extends JDialog implements
035: ListSelectionListener, ItemListener, ActionListener {
036: JList lstValidations;
037: JComboBox cmbValidationTypes;
038: JButton btnNew, btnSave, btnCancel;
039: JTextField txtMin, txtMax, txtValidationName;
040: JLabel lblMin, lblMax, lblValidationName;
041: JCheckBox chkMandatory;
042: JPanel pnlMinMaxControls;
043: // JPanel pnlParams;
044: JPanel pnlButtons;
045: XmlElement nodValidations;
046:
047: boolean bRet = false;
048:
049: public final static String VALIDATION_MANDATORY = "XMandatoryValidator";
050: public final static String VALIDATION_MINMAX = "XMinMaxValidator";
051:
052: private XEditorProject currentProject;
053:
054: public ValidationDlg(XEditorProject project) {
055: setModal(true);
056:
057: currentProject = project;
058:
059: getContentPane().setLayout(null);
060: setSize(300, 400);
061: setupDlg();
062: }
063:
064: private void setupDlg() {
065: pnlMinMaxControls = new JPanel(null);
066: pnlMinMaxControls.setBounds(1, 200, 298, 50);
067: getContentPane().add(pnlMinMaxControls);
068:
069: // pnlParams = new JPanel( new GridLayout() );
070: // pnlParams.setBounds( 1, 200, 298, 100 );
071: // getContentPane().add( pnlParams );
072: pnlButtons = new JPanel(new FlowLayout());
073: btnNew = new JButton("New");
074: pnlButtons.add(btnNew);
075: btnNew.addActionListener(this );
076: btnSave = new JButton("Save");
077: btnSave.addActionListener(this );
078: pnlButtons.add(btnSave);
079: btnCancel = new JButton("Cancel");
080: btnCancel.addActionListener(this );
081: pnlButtons.add(btnCancel);
082: pnlButtons.setBounds(1, 270, 298, 29);
083: getContentPane().add(pnlButtons);
084:
085: lstValidations = new JList();
086: lstValidations.setBounds(1, 1, 298, 150);
087: lstValidations.setSelectionMode(lstValidations
088: .getSelectionModel().SINGLE_SELECTION);
089: lstValidations.addListSelectionListener(this );
090: getContentPane().add(lstValidations);
091: cmbValidationTypes = new JComboBox();
092: cmbValidationTypes.addItemListener(this );
093: cmbValidationTypes.addItem(VALIDATION_MANDATORY);
094: cmbValidationTypes.addItem(VALIDATION_MINMAX);
095: cmbValidationTypes.setBounds(10, 155, 180, 20);
096: getContentPane().add(cmbValidationTypes);
097:
098: lblValidationName = new JLabel("Validation name :");
099: lblValidationName.setBounds(10, 175, 100, 20);
100: getContentPane().add(lblValidationName);
101:
102: txtValidationName = new JTextField();
103: txtValidationName.setBounds(115, 175, 70, 20);
104: getContentPane().add(txtValidationName);
105:
106: lblMin = new JLabel("Min");
107: lblMin.setBounds(5, 5, 40, 20);
108: pnlMinMaxControls.add(lblMin);
109: txtMin = new JTextField();
110: txtMin.setBounds(50, 5, 30, 20);
111: pnlMinMaxControls.add(txtMin);
112:
113: lblMax = new JLabel("Max");
114: lblMax.setBounds(85, 5, 40, 20);
115: pnlMinMaxControls.add(lblMax);
116: txtMax = new JTextField();
117: txtMax.setBounds(130, 5, 30, 20);
118: pnlMinMaxControls.add(txtMax);
119:
120: chkMandatory = new JCheckBox("Mandatory");
121: chkMandatory.setBounds(10, 25, 100, 20);
122: pnlMinMaxControls.add(chkMandatory);
123:
124: populateList();
125: }
126:
127: private void openValidationsDoc() {
128: BufferedReader r = null;
129: try {
130: r = XResourceManager.getBufferedReader("validations.xml",
131: null);
132: nodValidations = XmlSource.read(r);
133: } catch (Exception ex) {
134: ex.printStackTrace();
135: }
136: }
137:
138: private void populateList() {
139: lstValidations.removeAll();
140: DefaultListModel mdl = new DefaultListModel();
141: String[] validations = currentProject.getValidations();
142: for (int i = 0; i < validations.length; i++) {
143: mdl.addElement(validations[i]);
144: }
145: lstValidations.setModel(mdl);
146: // lstValidations.removeAll();
147: // DefaultListModel mdl = new DefaultListModel();
148: // Vector validations = nodValidations.getChildren();
149: // for ( int i=0; i<validations.size(); i++ ){
150: // XmlElement eleValid = (XmlElement)validations.elementAt(i);
151: // mdl.addElement( eleValid.getAttribute("name") );
152: // }
153: // lstValidations.setModel( mdl );
154: }
155:
156: public void valueChanged(ListSelectionEvent evt) {
157: XmlElement ele = currentProject
158: .getValidation((String) lstValidations
159: .getSelectedValue());
160: DebugLogger.logWarning(ele.getAttribute("type"));
161: cmbValidationTypes.setSelectedItem(ele.getAttribute("type"));
162: }
163:
164: public void itemStateChanged(ItemEvent evt) {
165: if (cmbValidationTypes.getSelectedItem() == null) {
166: pnlMinMaxControls.setVisible(false);
167: } else if (((String) cmbValidationTypes.getSelectedItem())
168: .compareTo(this .VALIDATION_MINMAX) == 0) {
169: pnlMinMaxControls.setVisible(true);
170: } else {
171: pnlMinMaxControls.setVisible(false);
172: }
173:
174: // for ( int comp=pnlParams.getComponentCount(); comp>0; comp-- ){
175: // pnlParams.remove( pnlParams.getComponent(comp) );
176: // }
177: // Vector validations = nodValidations.getChildren();
178: // for ( int i=0; i<validations.size(); i++ ){
179: // XmlElement eleValid = (XmlElement)validations.elementAt(i);
180: // String validationName = eleValid.getAttribute("name");
181: // if ( validationName.compareTo((String)cmbValidationTypes.getSelectedItem())==0 ){
182: // if ( eleValid.elementAt(0)!=null ){
183: // Vector eleFields = eleValid.elementAt( 0 ).getChildren();
184: // for ( int iFields=0; iFields<eleFields.size(); iFields++ ){
185: // XmlElement eleField = (XmlElement)eleFields.elementAt(iFields);
186: // String strType = eleField.getAttribute("type");
187: // String strPrompt = eleField.getAttribute("prompt");
188: // String strName = eleField.getAttribute("name");
189: // if ( strType.compareTo("boolean")==0 ){
190: // JCheckBox checkbox = new JCheckBox( strPrompt );
191: // checkbox.setName( strName );
192: // pnlParams.add( checkbox );
193: // } else {
194: // JLabel label = new JLabel(strPrompt);
195: // JTextField textfield = new JTextField();
196: // pnlParams.add( label );
197: // pnlParams.add( textfield );
198: // }
199: // }
200: // }
201: // }
202: // }
203: }
204:
205: public void changeValidation(String validationName) {
206:
207: }
208:
209: public String addValidation(String newValidation) {
210: txtValidationName.setEnabled(true);
211: txtValidationName.setText(newValidation);
212: btnNew.setEnabled(false);
213: btnSave.setEnabled(true);
214: btnCancel.setEnabled(true);
215: lstValidations.setEnabled(false);
216: lstValidations.setSelectedIndex(-1);
217: show();
218: if (bRet)
219: return txtValidationName.getText();
220: else
221: return null;
222: }
223:
224: public void actionPerformed(ActionEvent evt) {
225: if (evt.getSource().equals(btnSave)) {
226: bRet = true;
227: hide();
228: } else if (evt.getSource().equals(btnCancel)) {
229: bRet = false;
230: hide();
231: }
232: }
233: }
|