001: /*
002: The contents of this file are subject to the Common Public Attribution License
003: Version 1.0 (the "License"); you may not use this file except in compliance with
004: the License. You may obtain a copy of the License at
005: http://www.projity.com/license . The License is based on the Mozilla Public
006: License Version 1.1 but Sections 14 and 15 have been added to cover use of
007: software over a computer network and provide for limited attribution for the
008: Original Developer. In addition, Exhibit A has been modified to be consistent
009: with Exhibit B.
010:
011: Software distributed under the License is distributed on an "AS IS" basis,
012: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
013: specific language governing rights and limitations under the License. The
014: Original Code is OpenProj. The Original Developer is the Initial Developer and
015: is Projity, Inc. All portions of the code written by Projity are Copyright (c)
016: 2006, 2007. All Rights Reserved. Contributors Projity, Inc.
017:
018: Alternatively, the contents of this file may be used under the terms of the
019: Projity End-User License Agreeement (the Projity License), in which case the
020: provisions of the Projity License are applicable instead of those above. If you
021: wish to allow use of your version of this file only under the terms of the
022: Projity License and not to allow others to use your version of this file under
023: the CPAL, indicate your decision by deleting the provisions above and replace
024: them with the notice and other provisions required by the Projity License. If
025: you do not delete the provisions above, a recipient may use your version of this
026: file under either the CPAL or the Projity License.
027:
028: [NOTE: The text of this license may differ slightly from the text of the notices
029: in Exhibits A and B of the license at http://www.projity.com/license. You should
030: use the latest text at http://www.projity.com/license for your modifications.
031: You may not remove this license text from the source files.]
032:
033: Attribution Information: Attribution Copyright Notice: Copyright © 2006, 2007
034: Projity, Inc. Attribution Phrase (not exceeding 10 words): Powered by OpenProj,
035: an open source solution from Projity. Attribution URL: http://www.projity.com
036: Graphic Image as provided in the Covered Code as file: openproj_logo.png with
037: alternatives listed on http://www.projity.com/logo
038:
039: Display of Attribution Information is required in Larger Works which are defined
040: in the CPAL as a work which combines Covered Code or portions thereof with code
041: not governed by the terms of the CPAL. However, in addition to the other notice
042: obligations, all copies of the Covered Code in Executable and Source Code form
043: distributed must, as a form of attribution of the original author, include on
044: each user interface screen the "OpenProj" logo visible to all users. The
045: OpenProj logo should be located horizontally aligned with the menu bar and left
046: justified on the top left of the screen adjacent to the File menu. The logo
047: must be at least 100 x 25 pixels. When users click on the "OpenProj" logo it
048: must direct them back to http://www.projity.com.
049: */
050: package com.projity.dialog.util;
051:
052: import java.awt.Color;
053: import java.awt.Component;
054: import java.awt.event.ActionEvent;
055: import java.awt.event.ActionListener;
056: import java.text.ParseException;
057:
058: import javax.swing.InputVerifier;
059: import javax.swing.JComponent;
060: import javax.swing.JSpinner;
061: import javax.swing.JTextField;
062: import javax.swing.undo.UndoableEditSupport;
063:
064: import com.projity.dialog.FieldDialog;
065: import com.projity.field.Field;
066: import com.projity.field.FieldContext;
067: import com.projity.field.FieldParseException;
068: import com.projity.field.ObjectRef;
069: import com.projity.field.Select;
070: import com.projity.options.EditOption;
071: import com.projity.undo.FieldEdit;
072: import com.projity.util.Alert;
073: import com.projity.util.DateTime;
074:
075: /**
076: *
077: */
078: public class FieldVerifier extends InputVerifier {
079: protected FieldContext context = null;
080: protected Field field;
081: protected ObjectRef objectRef = null;
082: protected Object source;
083: protected Object value;
084: protected Exception exception = null;
085: protected boolean updating = false;
086: boolean testing = false;
087:
088: // private UndoableEditSupport undoableEditSupport;
089: /**
090: * @param value TODO
091: *
092: */
093: public FieldVerifier(Field field, ObjectRef objectRef, Object value/*,UndoableEditSupport undoableEditSupport*/) {
094: super ();
095: this .field = field;
096: this .objectRef = objectRef;
097: setValue(value);
098: this .source = this ;
099: // this.undoableEditSupport=undoableEditSupport;
100: }
101:
102: /**
103: * Get the top level component. For dates and spinners, the verification is triggered on a grandchild. We need the grandparent
104: * @param component
105: * @return
106: */
107: static JComponent valueHoldingComponent(JComponent component) {
108: Object p = component.getParent();
109: // for spinners and dates, need to go up to grandparent to get the control which holds the value
110: if (p != null && p instanceof LookupField)
111: p = ((LookupField) p).getDisplay();
112: else if (p != null && p instanceof Component)
113: p = ((Component) p).getParent();
114: if (p instanceof JSpinner || p instanceof ExtDateField)
115: component = (JComponent) p;
116: return component;
117: }
118:
119: /* (non-Javadoc)
120: * @see javax.swing.InputVerifier#verify(javax.swing.JComponent)
121: */
122: public boolean verify(JComponent component) {
123: if (updating) {
124: return true;
125: }
126:
127: FieldDialog parentFieldDialog = ComponentFactory
128: .getParentFieldDialog(component);
129: if (parentFieldDialog != null)
130: parentFieldDialog.setDirtyComponent(null);
131:
132: JComponent c = component;
133: component = valueHoldingComponent(component);
134:
135: component.setForeground(Color.BLACK);
136: c.setForeground(Color.BLACK);
137:
138: Object newValue = ComponentFactory.getValueFromComponent(
139: component, field);
140: //System.out.println("new value " + newValue + " " + (newValue != null ?newValue.getClass():""));
141:
142: // avoid validating unchanged controls
143: if (newValue == value
144: || (newValue != null && newValue.equals(value))) { //unchanged
145: if (component instanceof JSpinner
146: || component instanceof ExtDateField) { // if a spinner, check for modified text
147: String text = ((JTextField) c).getText();
148: try {
149: if (!(component instanceof ExtDateField)
150: || text.trim().length() > 0)
151: newValue = field.getFormat().parseObject(text);
152: else {
153: ((JTextField) c).setText(""); // put in empty text
154: newValue = null;
155: }
156: } catch (ParseException e1) {
157: exception = new FieldParseException(field
158: .syntaxErrorForField());
159: component.setForeground(Color.RED);
160: c.setForeground(Color.RED);
161: if (parentFieldDialog != null)
162: parentFieldDialog.setDirtyComponent(c);
163:
164: return false;
165: }
166: } else {
167: return true;
168: }
169: }
170: if (newValue != null && value != null
171: && newValue.toString().equals(value.toString()))
172: return true;
173:
174: exception = null;
175: try {
176: if (field.hasOptions()) {
177: if (newValue == null)
178: newValue = Select.EMPTY;
179:
180: field.setText(objectRef, newValue.toString(), context);
181: } else {
182: if (field.isDate()) {
183: if (newValue != null && newValue instanceof String) {
184: try {
185: newValue = EditOption.getInstance()
186: .getDateFormat().parseObject(
187: (String) newValue);
188: } catch (ParseException e) {
189: }
190: }
191: if (newValue == null
192: || newValue.toString().trim().equals("")) // empty text on date is a null date
193: newValue = DateTime.getZeroDate();
194: }
195: if (newValue != value) {
196: Object oldValue = field
197: .getValue(objectRef, context);
198: field
199: .setValue(objectRef, source, newValue,
200: context);
201: UndoableEditSupport undoableEditSupport = objectRef
202: .getDataFactory().getUndoController()
203: .getEditSupport();
204: if (undoableEditSupport != null) {
205: undoableEditSupport.postEdit(new FieldEdit(
206: field, objectRef, value, oldValue,
207: this , context));
208: }
209: }
210:
211: }
212: } catch (FieldParseException e) {
213: exception = e;
214: component.setForeground(Color.RED);
215: c.setForeground(Color.RED);
216: if (parentFieldDialog != null)
217: parentFieldDialog.setDirtyComponent(c);
218: return false;
219: }
220: setValue(newValue); // set to new value for next time
221: return true;
222: }
223:
224: /* (non-Javadoc)
225: * @see javax.swing.InputVerifier#shouldYieldFocus(javax.swing.JComponent)
226: */
227: public boolean shouldYieldFocus(JComponent arg0) {
228: if (testing) // sempaphore to protect infinite focus loop when popping up error dialog. Does not need to be synchronized since the verifier is not shared
229: return true;
230: testing = true;
231: boolean result = super .shouldYieldFocus(arg0);
232: if (result == false)
233: Alert.error(exception.getMessage(), arg0);
234: testing = false;
235: return result;
236: }
237:
238: /**
239: * @param value The value to set.
240: */
241: void setValue(Object value) {
242: this .value = value;
243: }
244:
245: /** A generic listener class that will validate on an event */
246: public static class VerifierListener implements ActionListener {
247: public void actionPerformed(ActionEvent e) {
248: JComponent c = (JComponent) e.getSource();
249: InputVerifier v = c.getInputVerifier();
250: if (v != null) // on init, it is null
251: v.verify(c);
252: }
253: }
254:
255: public final void setUpdating(boolean doNotVerify) {
256: this .updating = doNotVerify;
257: }
258:
259: final Object getValue() {
260: return value;
261: }
262:
263: /**
264: * @return
265: */
266: final boolean isUpdating() {
267: return updating;
268: }
269:
270: public final Exception getException() {
271: return exception;
272: }
273: }
|