001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: package com.salmonllc.ideTools;
021:
022: /////////////////////////
023: //$Archive: /SOFIA/SourceCode/com/salmonllc/ideTools/ValidationDialog.java $
024: //$Author: Dan $
025: //$Revision: 5 $
026: //$Modtime: 12/17/03 4:51p $
027: /////////////////////////
028: import java.awt.Container;
029: import java.awt.Dimension;
030: import java.awt.Frame;
031: import java.awt.Toolkit;
032: import java.awt.event.ActionEvent;
033: import java.awt.event.ActionListener;
034: import java.util.ArrayList;
035:
036: import javax.swing.Box;
037: import javax.swing.BoxLayout;
038: import javax.swing.JButton;
039: import javax.swing.JComponent;
040: import javax.swing.JDialog;
041: import javax.swing.JLabel;
042: import javax.swing.JPanel;
043: import javax.swing.border.EmptyBorder;
044:
045: import com.salmonllc.sql.DataDictionary;
046: import com.salmonllc.sql.DataStoreBuffer;
047: import com.salmonllc.sql.DataStoreException;
048: import com.salmonllc.sql.DataStoreExpression;
049: import com.salmonllc.sql.ModelChangedEvent;
050: import com.salmonllc.sql.ModelChangedListener;
051: import com.salmonllc.sql.ValidationRule;
052: import com.salmonllc.swing.SComboBox;
053: import com.salmonllc.swing.SComponentHelper;
054: import com.salmonllc.swing.SOption;
055: import com.salmonllc.swing.STable;
056: import com.salmonllc.swing.STextField;
057:
058: public class ValidationDialog extends JDialog implements
059: ActionListener, ModelChangedListener {
060:
061: ValidationRuleDefinition[] _rules;
062: STable _rulesTable;
063: DataStoreBuffer _rulesDs;
064: JButton _ok, _cancel, _addRule, _deleteRule;
065: Box _expressionBox, _minValBox, _maxValBox, _lookupTableBox,
066: _lookupDescBox, _lookupExpBox, _lookupBucketBox;
067: JPanel _detailGrid;
068:
069: DataDictionary _dd;
070: boolean _clickedCancel = true;
071: private String _ruleDesc[] = { "Expression", "JavaScript",
072: "Lookup", "Range", "Regular Expression", "Required",
073: "Type Check" };
074: private int _ruleType[] = { ValidationRule.TYPE_EXPRESSION,
075: ValidationRule.TYPE_JAVASCRIPT, ValidationRule.TYPE_LOOKUP,
076: ValidationRule.TYPE_RANGE,
077: ValidationRule.TYPE_REGULAR_EXPRESSION,
078: ValidationRule.TYPE_REQUIRED,
079: ValidationRule.TYPE_TYPE_CHECK };
080:
081: public static final String COL_RULE_TYPE = "RuleType";
082: public static final String COL_COL_NAME = "ColumnName";
083: public static final String COL_ERROR_MESSAGE = "ErrorMessage";
084: public static final String COL_RULE_EXP = "RuleExpression";
085: public static final String COL_MIN_VAL = "MinValue";
086: public static final String COL_MAX_VAL = "MaxValue";
087: public static final String COL_LOOKUP_TABLE = "LookupTable";
088: public static final String COL_LOOKUP_EXPRESSION = "LookupExpression";
089: public static final String COL_LOOKUP_DESC_COL = "LookupDescCol";
090: public static final String COL_LOOKUP_DESC_BUCKET = "LookupDescBucket";
091:
092: private class RuleResolver implements DataStoreExpression {
093: public Object evaluateExpression(DataStoreBuffer dsBuf, int row)
094: throws DataStoreException {
095: int i = dsBuf.getInt(row, COL_RULE_TYPE);
096: for (int j = 0; j < _ruleType.length; j++) {
097: if (_ruleType[j] == i)
098: return _ruleDesc[j];
099: }
100: return null;
101: }
102: }
103:
104: public ValidationDialog(Frame owner,
105: ValidationRuleDefinition[] def, String colNames[],
106: DataDictionary dd, int buttonHeight) {
107: super (owner, "Validations", true);
108: _dd = dd;
109: int width = 700;
110: int height = 400;
111: Dimension frameBounds = Toolkit.getDefaultToolkit()
112: .getScreenSize();
113: int x = (frameBounds.width - width) / 2;
114: int y = (frameBounds.height - height) / 2;
115:
116: setBounds(x, y, width, height);
117: setResizable(false);
118: setModal(true);
119:
120: _rules = def;
121:
122: //build a datastore to hold the rules
123: _rulesDs = new DataStoreBuffer();
124: _rulesDs.addBucket(COL_RULE_TYPE, DataStoreBuffer.DATATYPE_INT);
125: _rulesDs.addBucket(COL_COL_NAME,
126: DataStoreBuffer.DATATYPE_STRING);
127: _rulesDs.addBucket(COL_ERROR_MESSAGE,
128: DataStoreBuffer.DATATYPE_STRING);
129: _rulesDs.addBucket(COL_RULE_EXP,
130: DataStoreBuffer.DATATYPE_STRING);
131: _rulesDs
132: .addBucket(COL_MIN_VAL, DataStoreBuffer.DATATYPE_STRING);
133: _rulesDs
134: .addBucket(COL_MAX_VAL, DataStoreBuffer.DATATYPE_STRING);
135: _rulesDs.addBucket(COL_LOOKUP_TABLE,
136: DataStoreBuffer.DATATYPE_STRING);
137: _rulesDs.addBucket(COL_LOOKUP_EXPRESSION,
138: DataStoreBuffer.DATATYPE_STRING);
139: _rulesDs.addBucket(COL_LOOKUP_DESC_COL,
140: DataStoreBuffer.DATATYPE_STRING);
141: _rulesDs.addBucket(COL_LOOKUP_DESC_BUCKET,
142: DataStoreBuffer.DATATYPE_STRING);
143:
144: _rulesDs.insertRow();
145:
146: //main box
147: JPanel main = new JPanel();
148: main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
149: main.setBorder(new EmptyBorder(10, 10, 10, 10));
150:
151: //row1, column headings
152: JPanel box = new JPanel();
153: box.setLayout(new BoxLayout(box, BoxLayout.X_AXIS));
154: box.add(DialogComponentFactory.makeLabel(
155: "Validations in DataStore:", 345));
156: box.add(Box.createHorizontalGlue());
157: box.add(DialogComponentFactory.makeLabel(
158: "Selected Validation:", 218));
159: box.add(_addRule = DialogComponentFactory.makeButton("Add", 55,
160: buttonHeight));
161: box.add(_deleteRule = DialogComponentFactory.makeButton("Del",
162: 55, buttonHeight));
163:
164: main.add(box);
165:
166: //row2, screens
167: box = new JPanel();
168: box.setLayout(new BoxLayout(box, BoxLayout.X_AXIS));
169: _rulesTable = new STable();
170: _rulesTable.setDataStore(_rulesDs);
171: try {
172: _rulesTable.addDisplayColumn(new RuleResolver(), "Type");
173: _rulesTable.addDisplayColumn(COL_COL_NAME, "Column Name");
174: _rulesTable.addDisplayColumn(COL_ERROR_MESSAGE,
175: "Error Message");
176: } catch (DataStoreException e) {
177: e.printStackTrace();
178: }
179: box.add(DialogComponentFactory.makeScrollPane(345, 240,
180: _rulesTable));
181: box.add(Box.createRigidArea(new Dimension(5, 0)));
182:
183: //detail screen
184: JPanel box2 = new JPanel();
185: box2.setLayout(new BoxLayout(box2, BoxLayout.Y_AXIS));
186: box.add(box2);
187: box2.add(Box.createRigidArea(new Dimension(0, 10)));
188: _detailGrid = box2;
189:
190: //rule types
191: SComboBox ruleTypesEdit = new SComboBox();
192: ruleTypesEdit.setColumn(_rulesDs, COL_RULE_TYPE);
193: for (int i = 0; i < _ruleDesc.length; i++)
194: ruleTypesEdit.addItem(new SOption(new Integer(_ruleType[i])
195: .toString(), _ruleDesc[i]));
196: addFieldToGrid("Rule Type", ruleTypesEdit, box2);
197:
198: //column list
199: SComboBox ruleColumnEdit = new SComboBox();
200: ruleColumnEdit.setColumn(_rulesDs, COL_COL_NAME);
201:
202: for (int i = 0; i < colNames.length; i++)
203: ruleColumnEdit.addItem(colNames[i]);
204: addFieldToGrid("Column", ruleColumnEdit, box2);
205:
206: //error message
207: STextField ruleErrorEdit = new STextField();
208: ruleErrorEdit.setColumn(_rulesDs, COL_ERROR_MESSAGE);
209: addFieldToGrid("Error Message", ruleErrorEdit, box2);
210:
211: //expression
212: STextField ruleExp = new STextField();
213: ruleExp.setColumn(_rulesDs, COL_RULE_EXP);
214: _expressionBox = addFieldToGrid("Expression", ruleExp, box2);
215:
216: //minimum value and maximum value
217: STextField minValExp = new STextField();
218: minValExp.setColumn(_rulesDs, COL_MIN_VAL);
219: _minValBox = addFieldToGrid("Min Value", minValExp, box2);
220: STextField maxValExp = new STextField();
221: maxValExp.setColumn(_rulesDs, COL_MAX_VAL);
222: _maxValBox = addFieldToGrid("Max Value", maxValExp, box2);
223:
224: //lookup fields
225: STextField lookupTable = new STextField();
226: lookupTable.setColumn(_rulesDs, COL_LOOKUP_TABLE);
227: _lookupTableBox = addFieldToGrid("Lookup Table", lookupTable,
228: box2);
229:
230: STextField lookupWhereExpression = new STextField();
231: lookupWhereExpression
232: .setColumn(_rulesDs, COL_LOOKUP_EXPRESSION);
233: _lookupExpBox = addFieldToGrid("Where Exp",
234: lookupWhereExpression, box2);
235:
236: STextField lookupDescCol = new STextField();
237: lookupDescCol.setColumn(_rulesDs, COL_LOOKUP_DESC_COL);
238: _lookupDescBox = addFieldToGrid("Desc Source Col",
239: lookupDescCol, box2);
240:
241: SComboBox lookupDescBucket = new SComboBox();
242: lookupDescBucket.setColumn(_rulesDs, COL_LOOKUP_DESC_BUCKET);
243: _lookupBucketBox = addFieldToGrid("Desc Dest Bucket",
244: lookupDescBucket, box2);
245:
246: lookupDescBucket.addItem(new SOption(null, ""));
247: for (int i = 0; i < colNames.length; i++)
248: lookupDescBucket.addItem(colNames[i]);
249:
250: box2.add(Box.createVerticalGlue());
251:
252: main.add(box);
253: main.add(Box.createRigidArea(new Dimension(0, 5)));
254:
255: //row 3
256: _ok = new JButton("OK");
257: _ok.addActionListener(this );
258: _cancel = new JButton("Cancel");
259: _cancel.addActionListener(this );
260: JPanel buttonBar = new JPanel();
261: buttonBar.setLayout(new BoxLayout(buttonBar, BoxLayout.X_AXIS));
262: buttonBar.add(_ok = new JButton("OK"));
263: buttonBar.add(_cancel = new JButton("Cancel"));
264: main.add(buttonBar);
265:
266: //populate rule ds
267: _rulesDs.reset();
268: for (int i = 0; i < def.length; i++) {
269: _rulesDs.insertRow();
270: try {
271: _rulesDs.setInt(COL_RULE_TYPE, def[i].rule
272: .getRuleType());
273: _rulesDs.setString(COL_COL_NAME, def[i].columnName);
274: _rulesDs.setString(COL_ERROR_MESSAGE, def[i].rule
275: .getErrorMessage());
276: _rulesDs.setString(COL_RULE_EXP, def[i].rule
277: .getExpression());
278: Object o = def[i].rule.getMinValue();
279: _rulesDs.setString(COL_MIN_VAL, o == null ? null : o
280: .toString());
281: o = def[i].rule.getMaxValue();
282: _rulesDs.setString(COL_MAX_VAL, o == null ? null : o
283: .toString());
284: _rulesDs.setString(COL_LOOKUP_TABLE, def[i].rule
285: .getLookupExpression());
286: _rulesDs.setString(COL_LOOKUP_DESC_COL, def[i].rule
287: .getDesciptionColumn());
288: _rulesDs.setString(COL_LOOKUP_DESC_BUCKET, def[i].rule
289: .getDescriptionBucket());
290: _rulesDs.setString(COL_LOOKUP_EXPRESSION, def[i].rule
291: .getLookupExpression());
292:
293: } catch (DataStoreException e1) {
294: e1.printStackTrace();
295: }
296: }
297: _rulesDs.resetStatus();
298:
299: //Add Listeners
300: _ok.addActionListener(this );
301: _cancel.addActionListener(this );
302: _addRule.addActionListener(this );
303: _deleteRule.addActionListener(this );
304:
305: //Add it to the screen
306: Container cp = getContentPane();
307: cp.setLayout(new BoxLayout(cp, BoxLayout.Y_AXIS));
308: cp.add(main);
309: cp.add(Box.createVerticalGlue());
310: cp.add(buttonBar);
311: cp.add(Box.createRigidArea(new Dimension(0, 20)));
312: enableDisableButtons();
313: _rulesDs.addModelChangedListener(this );
314: if (_rulesDs.getRowCount() == 0)
315: _rulesDs.insertRow();
316: else
317: _rulesDs.gotoFirst();
318: modelChanged(new ModelChangedEvent(_rulesDs, 1));
319: setVisible(true);
320: }
321:
322: public void actionPerformed(ActionEvent e) {
323: if (e.getSource() == _cancel) {
324: _clickedCancel = true;
325: setVisible(false);
326: } else if (e.getSource() == _ok) {
327: _clickedCancel = false;
328: setVisible(false);
329: } else if (e.getSource() == _addRule) {
330: _rulesDs.insertRow();
331: } else if (e.getSource() == _deleteRule) {
332: _rulesDs.deleteRow();
333: }
334:
335: }
336:
337: private void enableDisableButtons() {
338: _deleteRule.setEnabled(_rulesDs.getRowCount() > 0);
339: }
340:
341: private Box addFieldToGrid(String label, JComponent comp,
342: JPanel mainGrid) {
343: Dimension capSize = new Dimension(100, 25);
344: Dimension fieldSize = new Dimension(225, 25);
345:
346: JLabel l = DialogComponentFactory.makeLabel(label + ":");
347: l.setMaximumSize(capSize);
348: l.setMinimumSize(capSize);
349:
350: Box b = Box.createHorizontalBox();
351: b.add(l);
352: comp.setMaximumSize(fieldSize);
353: comp.setMinimumSize(fieldSize);
354: b.add(comp);
355:
356: Box b2 = Box.createVerticalBox();
357: b2.add(b);
358: b2.add(Box.createRigidArea(new Dimension(0, 5)));
359: mainGrid.add(b2);
360: return b2;
361:
362: }
363:
364: public void modelChanged(ModelChangedEvent evt) {
365: boolean enabled = _rulesDs.getRowCount() > 0;
366: SComponentHelper.setAllComponentsEnabled(_detailGrid, enabled);
367: if (_rulesDs.getRow() == -1
368: || _rulesDs.getRow() >= _rulesDs.getRowCount())
369: return;
370: int type;
371: try {
372: type = _rulesDs.getInt(COL_RULE_TYPE);
373: _expressionBox
374: .setVisible(type == ValidationRule.TYPE_EXPRESSION
375: || type == ValidationRule.TYPE_REGULAR_EXPRESSION
376: || type == ValidationRule.TYPE_JAVASCRIPT);
377: _maxValBox.setVisible(type == ValidationRule.TYPE_RANGE);
378: _minValBox.setVisible(type == ValidationRule.TYPE_RANGE);
379: _lookupBucketBox
380: .setVisible(type == ValidationRule.TYPE_LOOKUP);
381: _lookupDescBox
382: .setVisible(type == ValidationRule.TYPE_LOOKUP);
383: _lookupExpBox
384: .setVisible(type == ValidationRule.TYPE_LOOKUP);
385: _lookupTableBox
386: .setVisible(type == ValidationRule.TYPE_LOOKUP);
387:
388: } catch (DataStoreException e) {
389: e.printStackTrace();
390: }
391: enableDisableButtons();
392: }
393:
394: public ValidationRuleDefinition[] getValidationRules() {
395: ArrayList list = new ArrayList();
396: for (int i = 0; i < _rulesDs.getRowCount(); i++) {
397: try {
398: if (_rulesDs.getAny(i, COL_RULE_TYPE) != null
399: && _rulesDs.getAny(i, COL_COL_NAME) != null) {
400: int type = _rulesDs.getInt(i, COL_RULE_TYPE);
401: String colName = _rulesDs
402: .getString(i, COL_COL_NAME);
403: String errorMessage = _rulesDs.getString(i,
404: COL_ERROR_MESSAGE);
405: ValidationRule rule = null;
406: if (type == ValidationRule.TYPE_EXPRESSION
407: || type == ValidationRule.TYPE_REGULAR_EXPRESSION
408: || type == ValidationRule.TYPE_JAVASCRIPT)
409: rule = new ValidationRule(_rulesDs.getString(i,
410: COL_RULE_EXP), errorMessage, type);
411: else if (type == ValidationRule.TYPE_TYPE_CHECK
412: || type == ValidationRule.TYPE_REQUIRED)
413: rule = new ValidationRule(errorMessage, type);
414: else if (type == ValidationRule.TYPE_RANGE)
415: rule = new ValidationRule(_rulesDs.getString(i,
416: COL_MIN_VAL), _rulesDs.getString(i,
417: COL_MAX_VAL), errorMessage);
418: else if (type == ValidationRule.TYPE_LOOKUP)
419: rule = new ValidationRule(_rulesDs.getString(i,
420: COL_LOOKUP_TABLE), _rulesDs.getString(
421: i, COL_LOOKUP_EXPRESSION), _rulesDs
422: .getString(i, COL_LOOKUP_DESC_COL),
423: _rulesDs.getString(i,
424: COL_LOOKUP_DESC_BUCKET),
425: errorMessage);
426:
427: if (rule != null) {
428: ValidationRuleDefinition def = new ValidationRuleDefinition();
429: def.columnName = colName;
430: def.rule = rule;
431: list.add(def);
432: }
433: }
434: } catch (DataStoreException e) {
435: e.printStackTrace();
436: }
437: }
438: ValidationRuleDefinition[] ret = new ValidationRuleDefinition[list
439: .size()];
440: list.toArray(ret);
441: return ret;
442: }
443:
444: /**
445: * @return whether or not the user clicked cancel to close the dialog
446: */
447: public boolean isClickedCancel() {
448: return _clickedCancel;
449: }
450:
451: }
|