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:
021: package com.salmonllc.swing;
022:
023: import com.salmonllc.sql.*;
024: import com.salmonllc.swing.events.ValueChangedEvent;
025:
026: import javax.swing.*;
027: import java.util.Vector;
028:
029: /**
030: * SOFIA implementation of a Swing Label. This label allows binding to a DataStore column for the value and binding to DataStore validations to display error icons.
031: */
032: public class SLabel extends JLabel implements ModelChangedListener,
033: SComponent {
034: private DataStoreEvaluator _eval;
035: private Vector _exp;
036: private static String _colExp = "$col$";
037: private DataStoreBuffer _valDs;
038: private Icon _errorIcon;
039: private String _errorMsg;
040:
041: /**
042: * Creates a <code>SLabel</code> instance with
043: * no image and with an empty string for the title.
044: * The label is centered vertically
045: * in its display area.
046: * The label's contents, once set, will be displayed on the leading edge
047: * of the label's display area.
048: */
049: public SLabel() {
050: }
051:
052: /**
053: * Creates a <code>SLabel</code> instance with the specified image.
054: * The label is centered vertically and horizontally
055: * in its display area.
056: *
057: * @param image The image to be displayed by the label.
058: */
059: public SLabel(Icon image) {
060: super (image);
061: if (image instanceof SIcon)
062: ((SIcon) image).setParent(this );
063: }
064:
065: /**
066: * Creates a <code>SLabel</code> instance with the specified
067: * image and horizontal alignment.
068: * The label is centered vertically in its display area.
069: *
070: * @param image The image to be displayed by the label.
071: * @param horizontalAlignment One of the following constants
072: * defined in <code>SwingConstants</code>:
073: * <code>LEFT</code>,
074: * <code>CENTER</code>,
075: * <code>RIGHT</code>,
076: * <code>LEADING</code> or
077: * <code>TRAILING</code>.
078: */
079: public SLabel(Icon image, int horizontalAlignment) {
080: super (image, horizontalAlignment);
081: if (image instanceof SIcon)
082: ((SIcon) image).setParent(this );
083:
084: }
085:
086: /**
087: * Creates a <code>SLabel</code> instance with the specified text.
088: * The label is aligned against the leading edge of its display area,
089: * and centered vertically.
090: *
091: * @param text The text to be displayed by the label.
092: */
093: public SLabel(String text) {
094: super (text);
095: }
096:
097: /**
098: * Creates a <code>SLabel</code> instance with the specified
099: * text and horizontal alignment.
100: * The label is centered vertically in its display area.
101: *
102: * @param text The text to be displayed by the label.
103: * @param horizontalAlignment One of the following constants
104: * defined in <code>SwingConstants</code>:
105: * <code>LEFT</code>,
106: * <code>CENTER</code>,
107: * <code>RIGHT</code>,
108: * <code>LEADING</code> or
109: * <code>TRAILING</code>.
110: */
111: public SLabel(String text, int horizontalAlignment) {
112: super (text, horizontalAlignment);
113: }
114:
115: /**
116: * Creates a <code>SLabel</code> instance with the specified
117: * text, image, and horizontal alignment.
118: * The label is centered vertically in its display area.
119: * The text is on the trailing edge of the image.
120: *
121: * @param text The text to be displayed by the label.
122: * @param icon The image to be displayed by the label.
123: * @param horizontalAlignment One of the following constants
124: * defined in <code>SwingConstants</code>:
125: * <code>LEFT</code>,
126: * <code>CENTER</code>,
127: * <code>RIGHT</code>,
128: * <code>LEADING</code> or
129: * <code>TRAILING</code>.
130: */
131: public SLabel(String text, Icon icon, int horizontalAlignment) {
132: super (text, icon, horizontalAlignment);
133: if (icon instanceof SIcon)
134: ((SIcon) icon).setParent(this );
135:
136: }
137:
138: /**
139: * Binds the component to a DataStore expression
140: * @param buf The DataStore to bind to
141: * @param expression The DataStore expression to compute
142: * @throws DataStoreException if the expression is invalid
143: */
144: public void setExpression(DataStoreBuffer buf, String expression)
145: throws DataStoreException {
146: _eval = new DataStoreEvaluator(buf, expression);
147: buf.addModelChangedListener(this );
148: evalExpression();
149: }
150:
151: /**
152: * Binds the component to a DataStore expression
153: * @param buf The DataStore to bind to
154: * @param expression The DataStore expression to compute
155: * @throws DataStoreException if the expression is invalid
156: */
157: public void setExpression(DataStoreBuffer buf,
158: DataStoreExpression expression) throws DataStoreException {
159: _eval = new DataStoreEvaluator(buf, expression);
160: buf.addModelChangedListener(this );
161: evalExpression();
162: }
163:
164: /**
165: * Part of the model changed listener interface, don't call this method directly
166: */
167: public void modelChanged(ModelChangedEvent evt) {
168: evalExpression();
169: }
170:
171: private void evalExpression() {
172: try {
173: if (_eval != null)
174: setText(_eval.evaluateRowFormat());
175: } catch (DataStoreException ex) {
176: setText("");
177: ex.printStackTrace();
178: }
179:
180: try {
181: if (_exp != null) {
182: Icon icon = null;
183: String toolTip = null;
184:
185: for (int i = 0; i < _exp.size(); i++) {
186: Object o[] = (Object[]) _exp.elementAt(i);
187: if (o[1] == _colExp) {
188: int colIndex = _valDs
189: .getColumnIndex((String) o[0]);
190: if (colIndex != -1) {
191: DataStoreException ex[] = _valDs
192: .validateColumn(_valDs.getRow(),
193: colIndex, null);
194: if (ex.length > 0) {
195: icon = (Icon) o[2];
196: toolTip = ex[0].getMessage();
197: break;
198: }
199: }
200: } else if (o[0] != null) {
201: Object ret = ((DataStoreEvaluator) o[0])
202: .evaluateRow();
203: if ((ret instanceof Boolean)
204: && !((Boolean) ret).booleanValue()) {
205: icon = (Icon) o[2];
206: toolTip = (String) o[1];
207: break;
208: }
209: }
210: }
211: _errorIcon = icon;
212: _errorMsg = toolTip;
213: setIcon(icon);
214: setToolTipText(toolTip);
215: }
216: } catch (DataStoreException ex) {
217: ex.printStackTrace();
218: }
219: }
220:
221: /**
222: * Returns the value of the data in the component
223: */
224: public String getValue() {
225: try {
226: if (_eval != null)
227: return (_eval.evaluateRowFormat());
228: else
229: return getText();
230: } catch (DataStoreException ex) {
231: ex.printStackTrace();
232: return null;
233: }
234: }
235:
236: /**
237: * This method is used by the framework and should not be called directly
238: */
239: public ValueChangedEvent generateValueChangedEvent() {
240: return null;
241: }
242:
243: /**
244: * This method is used by the framework and should not be called directly
245: */
246: public SComponentHelper getHelper() {
247: return null;
248: }
249:
250: /**
251: * Displays the icon and message expression for each cell where the expression evaluates to false
252: * @throws DataStoreException
253: */
254: public void addValidateExpression(DataStoreEvaluator exp,
255: String message, Icon icon) throws DataStoreException {
256: Object o[] = new Object[3];
257: o[0] = exp;
258: o[1] = message;
259: o[2] = icon;
260: if (_exp == null)
261: _exp = new Vector();
262: _exp.add(o);
263: exp.getDataStore().addModelChangedListener(this );
264: }
265:
266: /**
267: * Displays the icon and message for each cell where the expression evaluates to false
268: * @throws DataStoreException
269: */
270: public void addValidationExpression(DataStoreBuffer ds, String exp,
271: String message, Icon icon) throws DataStoreException {
272: DataStoreEvaluator eval = new DataStoreEvaluator(ds, exp);
273: this .addValidateExpression(eval, message, icon);
274: }
275:
276: /**
277: * Displays the icon and message expression for each cell where the expression evaluates to false
278: * @throws DataStoreException
279: */
280: public void addValidateExpression(DataStoreBuffer ds,
281: DataStoreExpression exp, String message, Icon icon)
282: throws DataStoreException {
283: DataStoreEvaluator eval = new DataStoreEvaluator(ds, exp);
284: this .addValidateExpression(eval, message, icon);
285: }
286:
287: /**
288: * Uses the rules embedded in the datastore for a particular column to indicate whether or not to display an icon and and error message tooltip
289: */
290: public void addValidateColumn(DataStoreBuffer dsb,
291: String validateColumn, Icon icon) {
292: Object o[] = new Object[3];
293: o[0] = validateColumn;
294: o[1] = _colExp;
295: o[2] = icon;
296: if (_exp == null)
297: _exp = new Vector();
298: _exp.add(o);
299: _valDs = dsb;
300: dsb.addModelChangedListener(this );
301: }
302:
303: /**
304: * Returns the Icon for the last error or null if there isn't any.
305: * @return Icon
306: */
307: public Icon getErrorIcon() {
308: return _errorIcon;
309: }
310:
311: /**
312: * Returns the errorMessage for the last error or null if there isn't any.
313: * @return String
314: */
315: public String getErrorMessage() {
316: return _errorMsg;
317: }
318:
319: }
|