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.table;
022:
023: import com.salmonllc.sql.DataStoreBuffer;
024: import com.salmonllc.sql.DataStoreException;
025: import com.salmonllc.sql.DataStoreEvaluator;
026: import com.salmonllc.sql.DataStoreExpression;
027: import com.salmonllc.swing.STable;
028:
029: import javax.swing.*;
030: import javax.swing.table.DefaultTableCellRenderer;
031: import java.awt.*;
032: import java.util.Vector;
033:
034: /**
035: * A cell renderer that displays an icon and/or tooltip for a table column if a boolean expression is true for the row.
036: */
037: public class ExpressionCellRenderer extends DefaultTableCellRenderer {
038: private DataStoreBuffer _ds;
039: private Vector _eval;
040: private DataStoreEvaluator _mainEval;
041: protected STable _tab;
042: private static String _colExp = "$col$";
043: private String _colName = null;
044:
045: /**
046: * Creates a <code>SLabel</code> instance with
047: * no image and with an empty string for the title.
048: * The label is centered vertically
049: * in its display area.
050: * The label's contents, once set, will be displayed on the leading edge
051: * of the label's display area.
052: */
053: public ExpressionCellRenderer(STable tab) {
054: super ();
055: _ds = ((DataStoreTableModel) tab.getModel()).getDataStore();
056: _eval = new Vector();
057: _tab = tab;
058: }
059:
060: /**
061: * Sets the expression that will be computed to display this cell
062: */
063: public void setExpression(DataStoreEvaluator exp) {
064: _mainEval = exp;
065: }
066:
067: /**
068: * Sets the expression that will be computed to display this cell
069: */
070: public void setExpression(DataStoreExpression exp)
071: throws DataStoreException {
072: setExpression(new DataStoreEvaluator(_ds, exp));
073: }
074:
075: /**
076: * Sets the expression that will be computed to display this cell
077: */
078: public void setExpression(String exp) throws DataStoreException {
079: if (_ds.getColumnIndex(exp) == -1)
080: setExpression(new DataStoreEvaluator(_ds, exp));
081: else
082: _colName = exp;
083: }
084:
085: /**
086: * Adds the validations for a column in the datastore to this renderer
087: */
088: public void addColumnValidations(String column, Icon icon) {
089: Object o[] = new Object[3];
090: o[0] = column;
091: o[1] = _colExp;
092: o[2] = icon;
093: _eval.add(o);
094: }
095:
096: /**
097: * Displays the icon and message expression for each cell where the expression evaluates to false
098: * @throws DataStoreException
099: */
100: public void addValidateExpression(DataStoreEvaluator exp,
101: String message, Icon icon) throws DataStoreException {
102: Object o[] = new Object[3];
103: o[0] = exp;
104: o[1] = message;
105: o[2] = icon;
106: _eval.add(o);
107: }
108:
109: /**
110: * Displays the icon and message for each cell where the expression evaluates to false
111: * @throws DataStoreException
112: */
113: public void addValidateExpression(String exp, String message,
114: Icon icon) throws DataStoreException {
115: Object o[] = new Object[3];
116: o[0] = new DataStoreEvaluator(_ds, exp);
117: o[1] = message;
118: o[2] = icon;
119: _eval.add(o);
120: }
121:
122: /**
123: * Displays the icon and message expression for each cell where the expression evaluates to false
124: * @throws DataStoreException
125: */
126: public void addValidateExpression(DataStoreExpression exp,
127: String message, Icon icon) throws DataStoreException {
128: Object o[] = new Object[3];
129: o[0] = new DataStoreEvaluator(_ds, exp);
130: o[1] = message;
131: o[2] = icon;
132: _eval.add(o);
133: }
134:
135: public Component getRenderer() {
136: return getTableCellRendererComponent(_tab, null, false, false,
137: 0, 0);
138: }
139:
140: public Component getTableCellRendererComponent(JTable table,
141: Object value, boolean isSelected, boolean hasFocus,
142: int row, int column) {
143: Component comp = super .getTableCellRendererComponent(table,
144: value, isSelected, hasFocus, row, column);
145: if (comp instanceof JLabel) {
146: try {
147: if (_mainEval != null) {
148: String val = _mainEval.evaluateRowFormat(row);
149: ((JLabel) comp).setText(val);
150: }
151: Icon icon = null;
152: String toolTip = null;
153:
154: for (int i = 0; i < _eval.size(); i++) {
155: Object o[] = (Object[]) _eval.elementAt(i);
156: if (o[1] == _colExp) {
157: int colIndex = _ds
158: .getColumnIndex((String) o[0]);
159: if (colIndex != -1) {
160: DataStoreException ex[] = _ds
161: .validateColumn(row, colIndex, null);
162: if (ex.length > 0) {
163: icon = (Icon) o[2];
164: toolTip = ex[0].getMessage();
165: break;
166: }
167: }
168: } else if (o[0] != null) {
169: Object ret = ((DataStoreEvaluator) o[0])
170: .evaluateRow(row);
171: if ((ret instanceof Boolean)
172: && !((Boolean) ret).booleanValue()) {
173: icon = (Icon) o[2];
174: toolTip = (String) o[1];
175: break;
176: }
177: }
178: }
179:
180: ((JLabel) comp).setIcon(icon);
181: ((JLabel) comp).setToolTipText(toolTip);
182: if (!isSelected) {
183: ((JLabel) comp).setBackground(_tab
184: .getBackground(row));
185: ((JLabel) comp).setForeground(_tab
186: .getForeground(row));
187: } else {
188: ((JLabel) comp).setBackground(_tab
189: .getSelectionBackground(row));
190: ((JLabel) comp).setForeground(_tab
191: .getSelectionForeground(row));
192: }
193: } catch (DataStoreException ex) {
194: }
195: }
196: int col = _tab.getColumnModel().getColumn(column)
197: .getModelIndex();
198: if (hasFocus && table.getModel().isCellEditable(row, col))
199: table.editCellAt(row, column);
200: return comp;
201: }
202:
203: /**
204: * Returns the DataStoreEvaluator for this cell
205: */
206: public DataStoreEvaluator getEvaluator() {
207: return _mainEval;
208: }
209:
210: /**
211: * If this renderer is bound to a particular column in a datastore then this returns the name of it
212: * @return
213: */
214: public String getColName() {
215: return _colName;
216: }
217:
218: }
|