001: /**
002: * In a chain of data manipulators some behaviour is common. TableMap
003: * provides most of this behavour and can be subclassed by filters
004: * that only need to override a handful of specific methods. TableMap
005: * implements TableModel by routing all requests to its model, and
006: * TableModelListener by routing all events to its listeners. Inserting
007: * a TableMap which has not been subclassed into a chain of table filters
008: * should have no effect.
009: *
010: * @author RAhul Kumar
011: * $Author: rahul_kumar $
012: * $Id: TableMap.java,v 1.4 2004/01/31 09:56:28 rahul_kumar Exp rahul $
013: * RK */package isql;
014:
015: import javax.swing.table.*;
016: import java.util.*;
017: import javax.swing.event.TableModelListener;
018: import javax.swing.event.TableModelEvent;
019:
020: public class TableMap extends AbstractTableModel implements
021: TableModelListener {
022: protected List vColumns;
023: protected List vRows;
024: Class colClass[];
025: // added 20011123
026: int colWidths[];
027: int colScales[];
028: int colPrecisions[];
029: int colTypes[];
030: String tableNames[];
031: /** milliseconds taken to execute query.
032: * RK added on 20040131 15:26:24
033: */
034: long executionTime;
035:
036: // the previous where condition added to this sql which will be
037: // removed by ask-Filter.
038: String whereCondition = null;
039:
040: public void setWidths(int[] widths) {
041: colWidths = widths;
042: }
043:
044: public void setScales(int[] Scale) {
045: colScales = Scale;
046: }
047:
048: public void setTypes(int[] Types) {
049: colTypes = Types;
050: }
051:
052: public void setPrecisions(int[] Precision) {
053: colPrecisions = Precision;
054: }
055:
056: /** TODO - this is required !!! by the autolink a-s-d facility
057: */
058: public void setTableNames(String[] TableNames) {
059: tableNames = TableNames;
060: }
061:
062: /** get col width.
063: * start with an offset of 1. pls fix someday */
064: public int getWidth(int column) {
065: if (colWidths == null)
066: return 75;
067: return colWidths[column];
068: }
069:
070: /** get col type as java.sql.Type.
071: * start with an offset of 1. pls fix someday */
072: public int getType(int column) {
073: if (colTypes == null)
074: return java.sql.Types.VARCHAR;
075: return colTypes[column + 1];
076: }
077:
078: /** TODO - this is required */
079: public String getTableName(int column) {
080: if (tableNames == null)
081: return "";
082: return tableNames[column];
083: }
084:
085: public int getScale(int column) {
086: if (colScales == null)
087: return 0;
088: return colScales[column];
089: }
090:
091: public int getPrecision(int column) {
092: if (colPrecisions == null)
093: return 0;
094: return colPrecisions[column];
095: }
096:
097: public TableMap(List vc, List vr) {
098: setModel(vc, vr);
099: }
100:
101: public void setModel(List vc, List vr) {
102: this .vColumns = vc;
103: this .vRows = vr;
104: colClass = new Class[vc.size()];
105: if (vRows != null && vRows.size() > 0) { // was bombing if no rows
106: for (int i = 0; i < colClass.length; i++) {
107: try {
108: colClass[i] = getValueAt(0, i).getClass();
109: } catch (NullPointerException exc) {
110: //System.err.println( "setModel:EXC:"+ exc.toString()); // was bombing on null columns
111: colClass[i] = java.lang.String.class;
112: } // try
113: } // for
114: }//if
115: addTableModelListener(this );
116: }
117:
118: // By default, Implement TableModel by forwarding all messages
119: // to the model.
120:
121: public Object getValueAt(int aRow, int aColumn) {
122: List row = (List) vRows.get(aRow);
123: return row.get(aColumn);
124: }
125:
126: public void setValueAt(Object aValue, int aRow, int aColumn) {
127: //model.setValueAt(aValue, aRow, aColumn);
128: }
129:
130: public int getRowCount() {
131: return (vRows == null) ? 0 : vRows.size();
132: }
133:
134: public int getColumnCount() {
135: return (vColumns == null) ? 0 : vColumns.size();
136: }
137:
138: public String getColumnName(int aColumn) {
139: return (String) vColumns.get(aColumn);
140: }
141:
142: public Class getColumnClass(int aColumn) {
143: return colClass[aColumn];
144: }
145:
146: public boolean isCellEditable(int row, int column) {
147: return false;
148: }
149:
150: //
151: // Implementation of the TableModelListener interface,
152: //
153:
154: // By default forward all events to all the listeners.
155: public void tableChanged(TableModelEvent e) {
156: fireTableChanged(e);
157: }
158:
159: public String toString(String colsep) {
160: if (colsep == null)
161: colsep = " |";
162: int colcount = getColumnCount();
163: int rowcount = getRowCount();
164: StringBuffer sb = new StringBuffer(colcount * rowcount * 5);
165: for (int i = 0; i < colcount; i++) {
166: sb.append(getColumnName(i)).append(colsep);
167: }
168: sb.append('\n');
169: for (int i = 0; i < rowcount; i++) {
170: for (int j = 0; j < colcount; j++)
171: sb.append(getValueAt(i, j)).append(colsep);
172: sb.append('\n');
173: }
174: return sb.toString();
175: }
176:
177: public String toString() {
178: return toString(" |");
179: }
180:
181: /** returns a list of column names
182: * RK added on 20031225 19:04:50
183: */
184: public List getColumns() {
185: return vColumns;
186: }
187:
188: /** returns a list of data which is a list per row.
189: * RK added on 20031225 19:05:13
190: */
191: public List getData() {
192: return vRows;
193: }
194:
195: public void setSQL(String sql) {
196: _sql = sql;
197: }
198:
199: /** get the sql statement */
200: public String getSQL() {
201: return _sql;
202: }
203:
204: /** the SQL statement relating to this map.
205: * RK added on 20031229 16:46:59
206: * added so as to be able to derive tablename while linking. Oracle
207: * is not giving the same in getTableName or rsmd.
208: */
209: protected String _sql = null;
210:
211: public void setRowCount(int lastProcessedRowCount) {
212: _rowsProcessed = lastProcessedRowCount;
213: }
214:
215: public void setStartRow(int startRow) {
216: _startRow = startRow;
217: }
218:
219: public void setEndRow(int EndRow) {
220: _endRow = EndRow;
221: }
222:
223: /*
224: public int getRowCount(){
225: return _rowsProcessed;
226: }
227: */
228: public int getStartRow() {
229: return _startRow;
230: }
231:
232: public int getEndRow() {
233: return _endRow;
234: }
235:
236: public void moreRows(boolean more) {
237: _moreRows = more;
238: }
239:
240: public boolean moreRows() {
241: return _moreRows;
242: }
243:
244: protected int _startRow;
245: protected int _endRow;
246: protected int _rowsProcessed;
247: protected boolean _moreRows;
248:
249: public long getExecutionTime() {
250: return executionTime;
251: }
252:
253: public void setExecutionTime(long executionTime) {
254: this .executionTime = executionTime;
255: }
256:
257: /**
258: * @deprecated
259: * use sort (int[], boolean) instead.
260: */
261: public void sort(int col, boolean direction) {
262: List data = this .vRows;
263: final int column = col;
264: final int dir = (direction) ? 1 : -1;
265: Comparator c = new Comparator() {
266: public int compare(Object a, Object b) {
267: Object o1 = ((List) a).get(column);
268: Object o2 = ((List) b).get(column);
269: int retval = 0;
270: if (o1 instanceof java.lang.String) {
271: String s1 = (String) o1;
272: String s2 = (String) o2;
273: retval = s1.compareTo(s2);
274: } else if (o1 instanceof java.lang.Integer) {
275: java.lang.Integer s1 = (java.lang.Integer) o1;
276: java.lang.Integer s2 = (java.lang.Integer) o2;
277: retval = s1.compareTo(s2);
278: } else if (o1 instanceof java.lang.Double) {
279: java.lang.Double s1 = (java.lang.Double) o1;
280: java.lang.Double s2 = (java.lang.Double) o2;
281: retval = s1.compareTo(s2);
282: } else if (o1 instanceof java.lang.Float) {
283: java.lang.Float s1 = (java.lang.Float) o1;
284: java.lang.Float s2 = (java.lang.Float) o2;
285: retval = s1.compareTo(s2);
286: } else if (o1 instanceof java.sql.Date) {
287: java.sql.Date s1 = (java.sql.Date) o1;
288: java.sql.Date s2 = (java.sql.Date) o2;
289: retval = s1.compareTo(s2);
290: } else if (o1 instanceof java.sql.Time) {
291: java.sql.Time s1 = (java.sql.Time) o1;
292: java.sql.Time s2 = (java.sql.Time) o2;
293: retval = s1.compareTo(s2);
294: } else if (o1 instanceof java.sql.Timestamp) {
295: java.sql.Timestamp s1 = (java.sql.Timestamp) o1;
296: java.sql.Timestamp s2 = (java.sql.Timestamp) o2;
297: retval = s1.compareTo(s2);
298: } else {
299: System.out.println(" temporary sort ");
300: String s1 = o1.toString();
301: String s2 = o2.toString();
302: retval = s1.compareTo(s2);
303: }
304: return retval * dir;
305: }
306:
307: public boolean equals(Object a) {
308: return this .equals(a);
309: }
310:
311: };
312: Collections.sort(data, c);
313: } // sort
314:
315: /** sort the given table model based on given columns in the given
316: * direction (asc or descending)
317: */
318: public void sort(int[] col, boolean direction) {
319: List data = this .vRows;
320: final int[] column = col;
321: final int dir = (direction) ? 1 : -1;
322: Comparator c = new Comparator() {
323: public int compare(Object a, Object b) {
324: int retval = 0;
325: for (int i = 0; i < column.length; i++) {
326: Object o1 = ((List) a).get(column[i]);
327: Object o2 = ((List) b).get(column[i]);
328: if (o1 instanceof java.lang.String) {
329: String s1 = (String) o1;
330: String s2 = (String) o2;
331: retval = s1.compareTo(s2);
332: } else if (o1 instanceof java.lang.Integer) {
333: java.lang.Integer s1 = (java.lang.Integer) o1;
334: java.lang.Integer s2 = (java.lang.Integer) o2;
335: retval = s1.compareTo(s2);
336: } else if (o1 instanceof java.lang.Double) {
337: java.lang.Double s1 = (java.lang.Double) o1;
338: java.lang.Double s2 = (java.lang.Double) o2;
339: retval = s1.compareTo(s2);
340: } else if (o1 instanceof java.lang.Float) {
341: java.lang.Float s1 = (java.lang.Float) o1;
342: java.lang.Float s2 = (java.lang.Float) o2;
343: retval = s1.compareTo(s2);
344: } else if (o1 instanceof java.sql.Date) {
345: java.sql.Date s1 = (java.sql.Date) o1;
346: java.sql.Date s2 = (java.sql.Date) o2;
347: retval = s1.compareTo(s2);
348: } else if (o1 instanceof java.sql.Time) {
349: java.sql.Time s1 = (java.sql.Time) o1;
350: java.sql.Time s2 = (java.sql.Time) o2;
351: retval = s1.compareTo(s2);
352: } else if (o1 instanceof java.sql.Timestamp) {
353: java.sql.Timestamp s1 = (java.sql.Timestamp) o1;
354: java.sql.Timestamp s2 = (java.sql.Timestamp) o2;
355: retval = s1.compareTo(s2);
356: } else {
357: System.out.println(" temporary sort ");
358: String s1 = o1.toString();
359: String s2 = o2.toString();
360: retval = s1.compareTo(s2);
361: }
362: if (retval == 0)
363: continue;
364: return retval * dir;
365: } // for each column
366: return 0;
367: } // compare
368:
369: public boolean equals(Object a) {
370: return this .equals(a);
371: }
372:
373: };
374: Collections.sort(data, c);
375: }
376:
377: /** sort the given table model based on given columns using the
378: * second array to decide order.
379: */
380: public void sort(int[] col, int[] direction) {
381: List data = this .vRows;
382: final int[] column = col;
383: //final int dir = (direction)?1:-1;
384: final int[] directions = direction;
385: Comparator c = new Comparator() {
386: public int compare(Object a, Object b) {
387: int retval = 0;
388: for (int i = 0; i < column.length; i++) {
389: Object o1 = ((List) a).get(column[i]);
390: Object o2 = ((List) b).get(column[i]);
391: if (o1 instanceof java.lang.String) {
392: String s1 = (String) o1;
393: String s2 = (String) o2;
394: retval = s1.compareTo(s2);
395: } else if (o1 instanceof java.lang.Integer) {
396: java.lang.Integer s1 = (java.lang.Integer) o1;
397: java.lang.Integer s2 = (java.lang.Integer) o2;
398: retval = s1.compareTo(s2);
399: } else if (o1 instanceof java.lang.Double) {
400: java.lang.Double s1 = (java.lang.Double) o1;
401: java.lang.Double s2 = (java.lang.Double) o2;
402: retval = s1.compareTo(s2);
403: } else if (o1 instanceof java.lang.Float) {
404: java.lang.Float s1 = (java.lang.Float) o1;
405: java.lang.Float s2 = (java.lang.Float) o2;
406: retval = s1.compareTo(s2);
407: } else if (o1 instanceof java.sql.Date) {
408: java.sql.Date s1 = (java.sql.Date) o1;
409: java.sql.Date s2 = (java.sql.Date) o2;
410: retval = s1.compareTo(s2);
411: } else if (o1 instanceof java.sql.Time) {
412: java.sql.Time s1 = (java.sql.Time) o1;
413: java.sql.Time s2 = (java.sql.Time) o2;
414: retval = s1.compareTo(s2);
415: } else if (o1 instanceof java.sql.Timestamp) {
416: java.sql.Timestamp s1 = (java.sql.Timestamp) o1;
417: java.sql.Timestamp s2 = (java.sql.Timestamp) o2;
418: retval = s1.compareTo(s2);
419: } else {
420: System.out.println(" temporary sort ");
421: String s1 = o1.toString();
422: String s2 = o2.toString();
423: retval = s1.compareTo(s2);
424: }
425: if (retval == 0)
426: continue;
427: retval *= directions[i];
428: return retval;
429: } // for each column
430: return 0;
431: } // compare
432:
433: public boolean equals(Object a) {
434: return this .equals(a);
435: }
436:
437: };
438: Collections.sort(data, c);
439: }
440:
441: } // class
|