home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 9.3 KB | 330 lines |
- /*
- * DefaultDataSource.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 DefaultDataSource implements DataSource {
- Matrix data = new Matrix();
- Grid view;
-
- boolean useDefault = false;
- boolean autoNumber = false;
- int first;
- Data defaultValue;
-
- public DefaultDataSource(Grid v) {
- this(v, false, null);
- }
-
- public DefaultDataSource(Grid v, boolean useDefaults) {
- this(v, useDefaults, null);
- }
-
- public DefaultDataSource(Grid v, boolean useDefaults, Data defaultData) {
- setGrid(v);
- this.useDefault = useDefaults;
- if (useDefaults) {
- defaultValue = (defaultData != null) ?defaultData :new DefaultData(this);
- }
- }
-
- public int rows() {
- return data.rows();
- }
-
- public int fetchAllRows() {
- return data.rows();
- }
-
- public void fetchMode(boolean manual) {}
-
- public Grid getView() { return view; }
-
- public int validDataRowRange(int top, int bottom) throws DataNotAvailable {
- int rows = data.rows();
-
- if (rows <= top) {
- throw new DataNotAvailable("Requested top=" + top + " but only " +
- rows + " available");
- }
-
- return Math.min(rows-1, bottom);
- }
-
- public void setCurrentRow(int row) throws TypeNotSupported {}
-
- public void doAutoNumbering(boolean an) {
- doAutoNumbering(an, first);
- }
-
- public void doAutoNumbering(boolean an, int n) {
- autoNumber = an;
- first = n;
- if (!useDefault && an == true) {
- useDefault = true;
- defaultValue = new ImageStringData(this);
- }
- }
-
- public void commitData() throws TypeNotSupported {}
-
- public void setDefaultData() {
- if (defaultValue == null) {
- setDefaultData(new DefaultData(this));
- }
- }
-
- public void setDefaultData(Data d) {
- useDefault = true;
- autoNumber = false;
- defaultValue = d;
- }
-
- public Data getDefaultData() {
- if (defaultValue == null) {
- throw new NullPointerException("Default data value not set");
- }
-
- return defaultValue;
- }
-
- public void setGrid(Grid v) {
- view = v;
- }
-
- public boolean supportsMeta() {
- return false;
- }
-
- public void setupGrid(Grid v) throws TypeNotSupported {
- throw new TypeNotSupported("DefaultDataSource does not support meta information");
- }
-
- public Data readData(int row, int col) throws DataNotAvailable {
- return getData(row, col);
- }
-
- public Data getData(Coordinate coords) throws DataNotAvailable {
- return getData(coords.row, coords.col);
- }
-
- public Data getData(int r, int c) throws DataNotAvailable {
- try {
- return (Data)data.elementAt(r, c);
- } catch(NullPointerException e) {
- if (autoNumber) {
- int state = view.rowState(r+1);
- defaultValue.setText(Integer.toString(r + first));
- if (state == DELETED_ROW) {
- defaultValue.appendChar('*');
- }
- if (view.relation != null) {
- DbDataSource source = (DbDataSource)view.dataSource;
- DbaDataStore store = (DbaDataStore)source.store;
- String text = defaultValue.toString();
- if (store.isCurrentRow(r+1)) {
- defaultValue.setText('<' + text + '>');
- }
- }
- return defaultValue;
- } else if (useDefault) {
- if (defaultValue instanceof DefaultData) {
- ((DefaultData)defaultValue).setRowAndCol(r, c);
- }
- return defaultValue;
- } else {
- throw e;
- }
- }
- }
-
- public void setData(Coordinate coords, Data d) throws TypeNotSupported {
- setData(coords.row, coords.col, d);
- }
-
- public void setData(int r, int c, Data d) throws TypeNotSupported {
- data.updateElement(r, c, d);
-
- }
-
- public String getText(Coordinate coords) throws DataNotAvailable {
- return getData(coords.row, coords.col).toString();
- }
-
- public boolean supports(Coordinate coords, int type) {
- return type == Data.STRING ||
- type == Data.IMAGE ||
- type == Data.IMAGE_STRING;
- }
-
- public Image getImage(Coordinate coords) throws DataNotAvailable {
- Data d = (Data)data.elementAt(coords.row, coords.col);
- return d.toImage();
- }
-
- public boolean handleEvent(java.awt.Event e) {
- if (e.arg instanceof TableCell) {
- TableCell cell = (TableCell)e.arg;
- Data data;
- try {
- data = cell.getData(); //this is kind of silly and circular
- } catch(DataNotAvailable ex) { return false; }
-
- switch(e.id) {
- case view.UNDO_CELL_EVENT:
- data.rollback();
- break;
- case view.LOST_FOCUS:
- try {
- data.commit();
- } catch(TypeNotSupported ex) {
-
- }
- break;
- }
- }
-
- return false;
- }
-
- //zero based
- public void handleException(int row, int col, Exception ex) {
- view.handleException(row, col, ex);
- }
-
- public void undeleteRow(int row) throws TypeNotSupported {
- throw new TypeNotSupported("Undelete not supported in DefaultDataSource");
- }
-
- public void deleteRow(int row) throws TypeNotSupported {
- data.removeRow(row);
- }
-
- public void insertRow(int row) throws TypeNotSupported {
- data.insertRow(row);
- }
-
- public int appendRow() throws TypeNotSupported {
- data.insertRow(data.rows());
-
- return data.rows()-1;
- }
-
- public int rowState(int row) { return DataSource.NEW_ROW; }
-
- public void clear() {
- data.removeAllElements();
- }
-
- public void refresh() {}
-
- public void save() {}
-
- public void undoRow(int row) throws TypeNotSupported {}
-
- //returns Data object for row and col. If not found then makes a new one
- //and inserts it.
- Data fetchData(int row, int col) {
- if (data.contains(row, col)) {
- return (Data)data.elementAt(row, col);
- } else {
- //need to create a Data
- Data d = new ImageStringData(this); //need to add column default support here
- data.updateElement(row, col, d);
- return d;
- }
- }
-
- public boolean isDataEditable(int row, int col) { return true; }
-
- public int type(int row, int col) {
- return Data.STRING;
- }
-
- public void rollbackCurrentData() {}
-
- public void rollback(int row, int col) {}
-
- public void commit(int row, int col) {}
-
- public boolean supportsChoice(int row, int col) {
- return false;
- }
-
- public Data[] getChoices(int row, int col) throws TypeNotSupported {
- throw new TypeNotSupported();
- }
-
- public void setText(int row, int col, String t) {
- fetchData(row, col).setText(t);
- }
-
- //pos is space where to be inserted (0 = first char)
- public void insertChar(int row, int col, int pos, char c) {
- fetchData(row, col).insertChar(pos, c);
- }
-
- public void setText(int row, int col, char c) {
- fetchData(row, col).setText(c);
- }
-
- public void appendChar(int row, int col, char c) {
- fetchData(row, col).appendChar(c);
- }
-
- public void clearText(int row, int col) {
- if (data.contains(row, col)) {
- ((Data)data.elementAt(row, col)).clearText();
- }
- }
-
- public void deleteChar(int row, int col, int pos) {
- if (data.contains(row, col)) {
- ((Data)data.elementAt(row, col)).deleteChar(pos);
- }
- }
-
- public String subString(int row, int col, int spos, int epos) {
- if (data.contains(row, col)) {
- return ((Data)data.elementAt(row, col)).subString(spos, epos);
- }
-
- throw new StringIndexOutOfBoundsException();
- }
-
- public void setImage(int row, int col, Image i) {
- fetchData(row, col).setImage(i);
- }
-
- public String toString(int row, int col) {
- if (data.contains(row, col)) {
- return ((Data)data.elementAt(row, col)).toString();
- } else {
- return "";
- }
- }
-
- public Image toImage(int row, int col) {
- if (data.contains(row, col)) {
- return ((Data)data.elementAt(row, col)).toImage();
- } else {
- return null;
- }
- }
-
- public Object getSynchronizationObject() {
- return data;
- }
- }
-
-