home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 10.5 KB | 388 lines |
- /*
- * DbDataSource.java 1.0 12 Jan 1997
- *
- * Copyright (c) 1996 Krumel & Associates, Inc. All Rights Reserved.
- *
- * This software is provided as is. Krumel & Associates shall not be liable
- * for any damages suffered by licensee as a result of using, modifying or
- * distributing this software or its derivatives.
- */
- package symantec.itools.db.awt;
-
- import java.awt.Point;
- import java.awt.Image;
-
- public class DbDataSource implements DataSource {
- Matrix rowCache = new Matrix();
-
- Grid view;
- DbDataStore store;
- DbDataUpdater updater;
- MetaTable meta;
- boolean caching = false;
- Data currData;
- int currDataRow, currDataCol;
-
- public DbDataSource(Grid g, DbDataStore s, DbDataUpdater u, MetaTable m) {
- view = g;
- setupSource(s, u, m);
- }
-
- public Grid getView() {
- return view;
- }
-
- public void setupSource(DbDataStore s, DbDataUpdater u, MetaTable m) {
- store = s;
- store.setDbDataSource(this);
- updater = u;
- meta = m;
-
- if (meta != null) {
- meta.setDbDataSource(this);
- }
-
- //cache if store doesn't
- caching = !store.supportsCaching();
- }
-
- public void setGrid(Grid v) {
- view = v;
- }
-
- public void fetchMode(boolean manual) {
- store.fetchMode(manual);
- }
-
- public void setDefaultData(Data defaultValue) {}
-
- public void setDefaultData() {}
-
- public boolean supportsMeta() {
- return meta != null;
- }
-
- public Matrix getCache() { return rowCache; }
-
- public int lastCachedRow() {
- return rowCache.rows() - 1;
- }
-
- public MetaTable getMetaTable() { return meta; }
-
- public void setupGrid(Grid v) throws TypeNotSupported {
- if (meta == null) {
- throw new TypeNotSupported("MetaTable not set");
- }
-
- meta.setupGrid(view);
- }
-
- public void commitData() throws TypeNotSupported {
- if (currData != null && currData.changed()) {
- setData(currDataRow, currDataCol-1, currData);
- currData.commit();
- currData = null;
- currDataRow = -1;
- }
- }
-
- public void setCurrentRow(int row) throws TypeNotSupported {
- store.setCurrentRow(row);
- }
-
- public Data readData(int row, int col) throws DataNotAvailable {
- col++;
-
- if (currDataRow == row && currDataCol == col) {
- return currData;
- }
-
- //if caching and it is cached then return it
- if (caching && rowCache.contains(row, col)) {
- return (Data)rowCache.elementAt(row, col);
- }
-
- //if not then go get it and then return it
- //cache if store isn't caching for us
- Data d = store.getData(row, col);
-
- if (caching) {
- rowCache.addElement(row, col, d);
- markClean(row);
- }
-
- return d;
- }
-
- //call this method when going to be doing edits
- //call commitData() when done with it (usually when lose focus)
- public Data getData(Coordinate coords) throws DataNotAvailable {
- return getData(coords.row, coords.col);
- }
-
- //call this method when going to be doing edits
- //call commitData() when done with it (usually when lose focus)
- public Data getData(int row, int col) throws DataNotAvailable {
- col++; //we maintain state information in col = 0 so it is all 1 based
-
- if (currDataRow == row && currDataCol == col) {
- return currData;
- }
- currData = readData(row, col-1);
- currDataRow = row;
- currDataCol = col;
-
- return currData;
- }
-
- public void addResultSetRow(int row, Data data[]) {
- //row should already be zero relative by DbDataStore
- //store has obtained a new row of information so store in matrix
- //and mark row as clean
-
- //remember - row state stored in col 0
- for (int col=1; col<=data.length; col++) {
- rowCache.updateElement(row, col, data[col-1]);
- }
-
- markClean(row);
- }
-
- //It is assumed that data has been verified already by data object, but...
- public void setData(int row, int col, Data data) throws TypeNotSupported {
- col++; //we maintain state information in col = 0
- //if cached then mark row for update and change data
- if (caching) {
- rowCache.updateElement(row, col, data);
- markModified(row);
- return;
- }
-
- //not caching so tell source to update
- store.update(row, col, data);
- }
-
- public void setData(Coordinate coord, Data data) throws TypeNotSupported {
- setData(coord.row, coord.col, data);
- }
-
- public String getText(Coordinate coords) throws DataNotAvailable {
- return getData(coords).toString();
- }
-
- public void undeleteRow(int row) throws TypeNotSupported {
- if (caching) {
- //for now just assume it was previously changed
- //need to add ability to remember previous state through XOR
- markModified(row);
- }
-
- updater.undeleteRow(row);
- }
-
- public void deleteRow(int row) throws TypeNotSupported {
- if (caching) {
- markDeleted(row);
- }
-
- updater.deleteRow(row);
- }
-
- public void insertRow(int row) throws TypeNotSupported {
- updater.insertRow(row);
- }
-
- public int appendRow() throws TypeNotSupported {
- return updater.appendRow();
- }
-
- public boolean supports(Coordinate coords, int type) {
- if (type == Data.STRING) return true;
-
- return false;
- }
-
- public Image getImage(Coordinate coords) throws DataNotAvailable {
- return getData(coords).toImage();
- }
-
- public boolean handleEvent(java.awt.Event e) {
- switch(e.id) {
- case view.UNDO_CELL_EVENT:
- rollback();
- break;
- case view.LOST_FOCUS:
- break;
- }
- return false;
- }
-
- public void handleException(int row, int col, Exception ex) {
- view.handleException(row, col, ex);
- }
-
- public int rowState(int row) {
- return store.rowState(row);
- }
-
- public void clear() {
- rowCache.removeAllElements();
- currData = null;
- currDataRow = -1;
- store.clear();
- }
-
- public void refresh() {
- //clear cache, clear view, and re-issue query
- rowCache.removeAllElements();
- currData = null;
- currDataRow = -1;
- store.refresh();
- }
-
- public void undoRow(int row) throws TypeNotSupported {
- store.undoRow(row);
- }
-
- public void save() throws TypeNotSupported {
- //iterate cache and update all non-clean rows
- updater.save();
- }
-
- public boolean isDataEditable(int row, int col) {
- if (meta != null) {
- try {
- return meta.isDataEditable(row, col+1);
- } catch(DataNotAvailable ex) {
- return false;
- }
- }
-
- return true;
- }
-
- protected void markModified(int row) {
- RowState state;
-
- if (rowCache.contains(row, 0)) {
- state = (RowState)rowCache.elementAt(row, 0);
- state.markModified();
- } else {
- //it is really new, not modified
- state = new RowState();
- rowCache.addElement(row, 0, state);
- state.markNew();
- }
- }
-
- protected void markNew(int row) {
- RowState state;
-
- if (rowCache.contains(row, 0)) {
- state = (RowState)rowCache.elementAt(row, 0);
- } else {
- state = new RowState();
- rowCache.addElement(row, 0, state);
- }
-
- state.markNew();
- }
-
- protected void markDeleted(int row) {
- //if something is being deleted it should already be there so
- //let's assume it is there.
- RowState state = (RowState)rowCache.elementAt(row, 0);
- state.markDeleted();
- }
-
- protected void markClean(int row) {
- RowState state;
-
- if (rowCache.contains(row, 0)) {
- state = (RowState)rowCache.elementAt(row, 0);
- } else {
- state = new RowState();
- rowCache.addElement(row, 0, state);
- }
-
- state.markClean();
- }
-
- public int validDataRowRange(int top, int bottom) throws DataNotAvailable {
- //subtract one to make 0 relative
- return store.validDataRowRange(top, bottom)-1;
- }
-
- public int rows() {
- return store.rowsRetrieved();
- }
-
- public int fetchAllRows() {
- return store.fetchAllRows();
- }
-
- //Here are some methods needed to support DefaultData
- public int type(int row, int col) {
- return Data.STRING;
- }
-
- public void rollback(int row, int col) { rollback(); }
-
- public void rollbackCurrentData() {
- rollback();
- }
-
- public void rollback() {
- currData = null;
- currDataRow = -1;
- }
-
- public void commit(int row, int col) {}
- public boolean isMasked(int row, int col) {
- return false;
- }
-
- public String getMask(int row, int col) throws TypeNotSupported {
- throw new TypeNotSupported("stubbed");
- }
-
- public boolean supportsChoice(int row, int col) {
- return false;
- }
-
- public Data[] getChoices(int row, int col) throws TypeNotSupported {
- throw new TypeNotSupported("stubbed");
- }
-
- public void setText(int row, int col, String t) {}
-
- //pos is space where to be inserted (0 = first char)
- public void insertChar(int row, int col, int pos, char c) {}
-
- public void setText(int row, int col, char c) {}
-
- public void appendChar(int row, int col, char c) {}
-
- public void clearText(int row, int col) {}
-
- public void deleteChar(int row, int col, int pos) {}
-
- public String subString(int row, int col, int spos, int epos) {
- return "stubbed";
- }
-
- public void setImage(int row, int col, Image i) {}
-
- public String toString(int row, int col) {
- return "stubbed";
- }
-
- public Image toImage(int row, int col) {
- return null;
- }
-
- public Object getSynchronizationObject() {
- return store.getSynchronizationObject();
- }
- }