/** * MyJdbcGridModel.java * * Copyright 2000 Eliad Technologies, Inc. All Rights Reserved. * * This software is the proprietary information of Eliad Technologies, Inc. */ package MultiDimTable; import java.util.*; import java.beans.*; import java.sql.*; import com.eliad.model.*; import com.eliad.swing.*; public class MyJdbcGridModel extends AbstractGridModel implements RulerModelListener { private Connection connection_; private Statement statement_; private String query_; private Vector rows_; private int colCount_; private int[] colIndex_; private DirectSpanModel spanModel_; private DirectColDragController dragController_; /** Creates new MyJdbcGridModel * @param url * @param driverName * @param user * @param passwd * @throws ClassNotFoundException * @throws SQLException */ public MyJdbcGridModel(String url, String driverName) throws ClassNotFoundException, SQLException { this(url, driverName, "", ""); } public MyJdbcGridModel(String url,String driverName,String user,String passwd) throws ClassNotFoundException, SQLException { Class.forName(driverName); connection_ = DriverManager.getConnection(url, user, passwd); statement_ = connection_.createStatement(); rows_ = null; colCount_ = 0; colIndex_ = null; spanModel_ = null; } public void spanModel(DirectSpanModel spanModel) { spanModel_ = spanModel; } public void directColumnDragController(DirectColDragController dcdc) { dragController_ = dcdc; } public void setQuery(String query) { if (connection_ == null || statement_ == null) throw new IllegalStateException("There is no database or statement to execute the query."); try { query_ = query; ResultSet resultSet = statement_.executeQuery(query); ResultSetMetaData metaData = resultSet.getMetaData(); colCount_ = metaData.getColumnCount(); rows_ = new Vector(100,50); colIndex_ = new int[colCount_]; // Get all rows from the query while (resultSet.next()) { Object[] newRow = new Object[colCount_]; for (int i = 0; i < colCount_; i++) newRow[i] = resultSet.getObject(i+1); rows_.add(newRow); } resultSet.close(); // Initialize colIndex_ as direct mapping for (int i=colCount_; i-->0;) colIndex_[i] = i; } catch (SQLException ex) { System.err.println(ex); } } public String getQuery() { return query_; } public void close() throws SQLException { statement_.close(); connection_.close(); } protected void finalize() throws Throwable { try { close(); } finally { super.finalize(); } } public int getRowCount() { return rows_ == null ? 0 : rows_.size(); } public int getColumnCount() { return colCount_; } public Object getValueAt(int row,int column) { Object obj = null; try { obj = ((Object[])rows_.elementAt(row))[colIndex_[column]]; } catch (ArrayIndexOutOfBoundsException e1) { e1.printStackTrace(); } catch (IndexOutOfBoundsException e2) { e2.printStackTrace(); } catch (Exception e3) { e3.printStackTrace(); } return obj; } /** * Called whenever the ruler model changes. * @param e the event that characterizes the change. */ public void rulerModelChanged(RulerModelEvent e) { MultiDimTable.traceln("MGM.rulerModelChanged : " + e.getType()); } /** * Called whenever the ruler model structure undergoes a local change. * Implemented here to catch column or row moves, and implements them * as data swapping. * @param e the event that characterizes the change. */ public void rulerStructureChanged(RulerModelEvent e) { MultiDimTable.traceln("MGM.rulerStructureChanged : " + e.getType()); if (e.getType()==RulerModelEvent.RULER_ITEMS_MOVED) { final int c = e.getCount(), fi = e.getFromIndex(), ti = e.getToIndex(); MultiDimTable.traceln(" count = " + c + ", from = " + fi + ", to = " + ti); final int fc, fn, tc, tn; MultiDimTable.traceln(" dragController_.fromExtentCell_ = " + dragController_.fromExtentCell_); MultiDimTable.traceln(" dragController_.toExtentCell_ = " + dragController_.toExtentCell_ ); if (dragController_.fromExtentCell_ != null) { fc = dragController_.fromExtentCell_.getColumn(); fn = dragController_.fromExtentCell_.getColumnCount(); } else { fc = fi; fn = c; // c should be equal to 1 } if (dragController_.toExtentCell_ != null) { tc = dragController_.toExtentCell_.getColumn(); tn = dragController_.toExtentCell_.getColumnCount(); } else { tc = ti; tn = 1; } MultiDimTable.traceln(" fc = " + fc + ", fn = " + fn + ", tc = " + tc + ", tn = " + tn); // Move colIndex_ final int []ci = new int[fn]; MultiDimTable.traceln(" save [" + fc + ".." + (fc+fn-1) + "]"); for (int i = 0; i < fn; i++) { ci[i] = colIndex_[fc+i]; } if (fc < tc) { MultiDimTable.traceln(" move from " + (fc+fn) + " to " + fc + " len " + (tc-fc+tn-fn)); System.arraycopy(colIndex_,fc+fn,colIndex_,fc,(tc-fc+tn-fn)); final int tcR = tc+tn-fn; // Rightmost cell of span MultiDimTable.traceln(" restore [" + tcR + ".." + (tcR+fn-1) + "]"); for (int i = 0; i < fn; i++) { colIndex_[tcR+i] = ci[i]; } MultiDimTable.traceln(" fireGCC(" + fc + "," + (tc-fc+fn) + ")"); fireGridColumnsChanged(fc,tc-fc+tn); } else { MultiDimTable.traceln(" move from " + tc + " to " + (tc+fn) + " len " + (fc-tc)); System.arraycopy(colIndex_,tc,colIndex_,tc+fn,fc-tc); MultiDimTable.traceln(" restore [" + tc + ".." + (tc+fn-1) + "]"); for (int i = 0; i < fn; i++) { colIndex_[tc+i] = ci[i]; } MultiDimTable.traceln(" fireGCC(" + tc + "," + (fc-tc+fn) + ")"); fireGridColumnsChanged(tc,fc-tc+fn); } /* // Code found in GenericGridModel int fromIndex = e.getFromIndex(); int toIndex = e.getToIndex(); int count = e.getCount(); int dist = toIndex-fromIndex; int first; if (dist >= 0) { // Forward first = fromIndex; } else { // Backward dist=-dist; first = toIndex; } fireGridColumnsChanged(first,dist+count); */ } } /** * Called whenever some data in the ruler model changes. * @param e the event that characterizes the change. */ public void rulerDataChanged(RulerModelEvent e) { //MultiDimTable.traceln("MGM.rulerDataChanged : " + e.getType()); } /** * Calls to send a prenotification that ruler items are to be moved. No action has yet been taken. */ public void beforeItemsMoved(RulerModelEvent e) throws PropertyVetoException { //MultiDimTable.traceln("MGM.beforeItemsMoved : " + e.getType()); } }