public class DataTypeByte extends BaseDataTypeComponent implements IDataTypeComponent { /* whether nulls are allowed or not */ private boolean _isNullable; /* whether number is signed or unsigned */ private boolean _isSigned; /* the number of decimal digits allowed in the number */ private int _scale; /* table of which we are part (needed for creating popup dialog) */ private JTable _table; /* The JTextComponent that is being used for editing */ private IRestorableTextComponent _textComponent; /* The CellRenderer used for this data type */ //??? For now, use the same renderer as everyone else. //?? //?? IN FUTURE: change this to use a new instance of renederer //?? for this data type. private DefaultColumnRenderer _renderer = DefaultColumnRenderer.getInstance(); /** * Constructor - save the data needed by this data type. */ public DataTypeByte(JTable table, ColumnDisplayDefinition colDef) { _table = table; _colDef = colDef; _isNullable = colDef.isNullable(); _isSigned = colDef.isSigned(); _scale = colDef.getScale(); } /** * Return the name of the java class used to hold this data type. */ public String getClassName() { return "java.lang.Byte"; } /** * Determine if two objects of this data type contain the same value. * Neither of the objects is null */ public boolean areEqual(Object obj1, Object obj2) { return ((Byte)obj1).equals(obj2); } /* * First we have the methods for in-cell and Text-table operations */ /** * Render a value into text for this DataType. */ public String renderObject(Object value) { return (String)_renderer.renderObject(value); } /** * This Data Type can be edited in a table cell. */ public boolean isEditableInCell(Object originalValue) { return true; } /** * See if a value in a column has been limited in some way and * needs to be re-read before being used for editing. * For read-only tables this may actually return true since we want * to be able to view the entire contents of the cell even if it was not * completely loaded during the initial table setup. */ public boolean needToReRead(Object originalValue) { // this DataType does not limit the data read during the initial load of the table, // so there is no need to re-read the complete data later return false; } /** * Return a JTextField usable in a CellEditor. */ public JTextField getJTextField() { _textComponent = new RestorableJTextField(); // special handling of operations while editing this data type ((RestorableJTextField)_textComponent).addKeyListener(new KeyTextHandler()); // // handle mouse events for double-click creation of popup dialog. // This happens only in the JTextField, not the JTextArea, so we can // make this an inner class within this method rather than a separate // inner class as is done with the KeyTextHandler class. // ((RestorableJTextField)_textComponent).addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent evt) { if (evt.getClickCount() == 2) { MouseEvent tableEvt = SwingUtilities.convertMouseEvent( (RestorableJTextField)DataTypeByte.this._textComponent, evt, DataTypeByte.this._table); CellDataPopup.showDialog(DataTypeByte.this._table, DataTypeByte.this._colDef, tableEvt, true); } } }); // end of mouse listener return (JTextField)_textComponent; } /** * Implement the interface for validating and converting to internal object. * Null is a valid successful return, so errors are indicated only by * existance or not of a message in the messageBuffer. */ public Object validateAndConvert(String value, Object originalValue, StringBuffer messageBuffer) { // handle null, which is shown as the special string "<null>" if (value.equals("<null>") || value.equals("")) return null; // Do the conversion into the object in a safe manner try { Object obj = new Byte(value); return obj; } catch (Exception e) { messageBuffer.append(e.toString()+"\n"); //?? do we need the message also, or is it automatically part of the toString()? //messageBuffer.append(e.getMessage()); return null; } } /** * If true, this tells the PopupEditableIOPanel to use the * binary editing panel rather than a pure text panel. * The binary editing panel assumes the data is an array of bytes, * converts it into text form, allows the user to change how that * data is displayed (e.g. Hex, Decimal, etc.), and converts * the data back from text to bytes when the user editing is completed. * If this returns false, this DataType class must * convert the internal data into a text string that * can be displayed (and edited, if allowed) in a TextField * or TextArea, and must handle all * user key strokes related to editing of that data. */ public boolean useBinaryEditingPanel() { return false; } /* * Now the functions for the Popup-related operations. */ /** * Returns true if data type may be edited in the popup, * false if not. */ public boolean isEditableInPopup(Object originalValue) { return true; } /* * Return a JTextArea usable in the CellPopupDialog * and fill in the value. */ public JTextArea getJTextArea(Object value) { _textComponent = new RestorableJTextArea(); // value is a simple string representation of the data, // the same one used in Text and in-cell operations. ((RestorableJTextArea)_textComponent).setText(renderObject(value)); // special handling of operations while editing this data type ((RestorableJTextArea)_textComponent).addKeyListener(new KeyTextHandler()); return (RestorableJTextArea)_textComponent; } /** * Validating and converting in Popup is identical to cell-related operation. */ public Object validateAndConvertInPopup(String value, Object originalValue, StringBuffer messageBuffer) { return validateAndConvert(value, originalValue, messageBuffer); } /* * The following is used in both cell and popup operations. */ /* * Internal class for handling key events during editing * of both JTextField and JTextArea. */ private class KeyTextHandler extends BaseKeyTextHandler { public void keyTyped(KeyEvent e) { char c = e.getKeyChar(); // as a coding convenience, create a reference to the text component // that is typecast to JTextComponent. this is not essential, as we // could typecast every reference, but this makes the code cleaner JTextComponent _theComponent = (JTextComponent)DataTypeByte.this._textComponent; String text = _theComponent.getText(); // look for illegal chars if ( ! DataTypeByte.this._isSigned && c == '-') { // cannot use '-' when unsigned _beepHelper.beep(_theComponent); e.consume(); } // tabs and newlines get put into the text before this check, // so remove them // This only applies to Popup editing since these chars are // not passed to this level by the in-cell editor. if (c == KeyEvent.VK_TAB || c == KeyEvent.VK_ENTER) { // remove all instances of the offending char int index = text.indexOf(c); if (index != -1) { if (index == text.length() -1) { text = text.substring(0, text.length()-1); // truncate string } else { text = text.substring(0, index) + text.substring(index+1); } ((IRestorableTextComponent)_theComponent).updateText( text); _beepHelper.beep(_theComponent); } e.consume(); } if ( ! ( Character.isDigit(c) || (c == '-') || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE) ) ) { _beepHelper.beep(_theComponent); e.consume(); } // check for max size reached (only works when DB provides non-zero scale info if (DataTypeByte.this._scale > 0 && text.length() == DataTypeByte.this._scale && c != KeyEvent.VK_BACK_SPACE && c != KeyEvent.VK_DELETE) { // max size reached e.consume(); _beepHelper.beep(_theComponent); } // handle cases of null // The processing is different when nulls are allowed and when they are not. // if ( DataTypeByte.this._isNullable) { // user enters something when field is null if (text.equals("<null>")) { if ((c==KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE)) { // delete when null => original value DataTypeByte.this._textComponent.restoreText(); e.consume(); } else { // non-delete when null => clear field and add text DataTypeByte.this._textComponent.updateText(""); // fall through to normal processing of this key stroke } } else { // check for user deletes last thing in field if ((c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE)) { if (text.length() <= 1 ) { // about to delete last thing in field, so replace with null DataTypeByte.this._textComponent.updateText("<null>"); e.consume(); } } } } else { // field is not nullable // handleNotNullableField(text, c, e, _textComponent); } } } /* * DataBase-related functions */ /** * On input from the DB, read the data from the ResultSet into the appropriate * type of object to be stored in the table cell. */ public Object readResultSet(ResultSet rs, int index, boolean limitDataRead) throws java.sql.SQLException { byte data = rs.getByte(index); if (rs.wasNull()) { return null; } else { return Byte.valueOf(data); } } /** * When updating the database, generate a string form of this object value * that can be used in the WHERE clause to match the value in the database. * A return value of null means that this column cannot be used in the WHERE * clause, while a return of "null" (or "is null", etc) means that the column * can be used in the WHERE clause and the value is actually a null value. * This function must also include the column label so that its output * is of the form: * "columnName = value" * or * "columnName is null" * or whatever is appropriate for this column in the database. */ public String getWhereClauseValue(Object value, ISQLDatabaseMetaData md) { if (value == null || value.toString() == null || value.toString().length() == 0) return _colDef.getLabel() + " IS NULL"; else return _colDef.getLabel() + "=" + value.toString(); } /** * When updating the database, insert the appropriate datatype into the * prepared statment at the given variable position. */ public void setPreparedStatementValue(PreparedStatement pstmt, Object value, int position) throws java.sql.SQLException { if (value == null) { pstmt.setNull(position, _colDef.getSqlType()); } else { pstmt.setInt(position, ((Byte)value).intValue()); } } /** * Get a default value for the table used to input data for a new row * to be inserted into the DB. */ public Object getDefaultValue(String dbDefaultValue) { if (dbDefaultValue != null) { // try to use the DB default value StringBuffer mbuf = new StringBuffer(); Object newObject = validateAndConvert(dbDefaultValue, null, mbuf); // if there was a problem with converting, then just fall through // and continue as if there was no default given in the DB. // Otherwise, use the converted object if (mbuf.length() == 0) return newObject; } // no default in DB. If nullable, use null. if (_isNullable) return null; // field is not nullable, so create a reasonable default value return Byte.valueOf((byte)0); } /* * File IO related functions */ /** * Say whether or not object can be exported to and imported from * a file. We put both export and import together in one test * on the assumption that all conversions can be done both ways. */ public boolean canDoFileIO() { return true; } /** * Read a file and construct a valid object from its contents. * Errors are returned by throwing an IOException containing the * cause of the problem as its message. * <P> * DataType is responsible for validating that the imported * data can be converted to an object, and then must return * a text string that can be used in the Popup window text area. * This object-to-text conversion is the same as is done by * the DataType object internally in the getJTextArea() method. * * <P> * File is assumed to be and ASCII string of digits * representing a value of this data type. */ public String importObject(FileInputStream inStream) throws IOException { InputStreamReader inReader = new InputStreamReader(inStream); int fileSize = inStream.available(); char charBuf[] = new char[fileSize]; int count = inReader.read(charBuf, 0, fileSize); if (count != fileSize) throw new IOException( "Could read only "+ count + " chars from a total file size of " + fileSize + ". Import failed."); // convert file text into a string // Special case: some systems tack a newline at the end of // the text read. Assume that if last char is a newline that // we want everything else in the line. String fileText; if (charBuf[count-1] == KeyEvent.VK_ENTER) fileText = new String(charBuf, 0, count-1); else fileText = new String(charBuf); // test that the string is valid by converting it into an // object of this data type StringBuffer messageBuffer = new StringBuffer(); validateAndConvertInPopup(fileText, null, messageBuffer); if (messageBuffer.length() > 0) { // convert number conversion issue into IO issue for consistancy throw new IOException( "Text does not represent data of type "+getClassName()+ ". Text was:\n"+fileText); } // return the text from the file since it does // represent a valid data value return fileText; } /** * Construct an appropriate external representation of the object * and write it to a file. * Errors are returned by throwing an IOException containing the * cause of the problem as its message. * <P> * DataType is responsible for validating that the given text * text from a Popup JTextArea can be converted to an object. * This text-to-object conversion is the same as validateAndConvertInPopup, * which may be used internally by the object to do the validation. * <P> * The DataType object must flush and close the output stream before returning. * Typically it will create another object (e.g. an OutputWriter), and * that is the object that must be flushed and closed. * * <P> * File is assumed to be and ASCII string of digits * representing a value of this data type. */ public void exportObject(FileOutputStream outStream, String text) throws IOException { OutputStreamWriter outWriter = new OutputStreamWriter(outStream); // check that the text is a valid representation StringBuffer messageBuffer = new StringBuffer(); validateAndConvertInPopup(text, null, messageBuffer); if (messageBuffer.length() > 0) { // there was an error in the conversion throw new IOException(new String(messageBuffer)); } // just send the text to the output file outWriter.write(text); outWriter.flush(); outWriter.close();
public class DataTypeShort extends BaseDataTypeComponent implements IDataTypeComponent { /* whether nulls are allowed or not */ private boolean _isNullable; /* whether number is signed or unsigned */ private boolean _isSigned; /* the number of decimal digits allowed in the number */ private int _scale; /* table of which we are part (needed for creating popup dialog) */ private JTable _table; /* The JTextComponent that is being used for editing */ private IRestorableTextComponent _textComponent; /* The CellRenderer used for this data type */ //??? For now, use the same renderer as everyone else. //?? //?? IN FUTURE: change this to use a new instance of renederer //?? for this data type. private DefaultColumnRenderer _renderer = DefaultColumnRenderer.getInstance(); /** * Constructor - save the data needed by this data type. */ public DataTypeShort(JTable table, ColumnDisplayDefinition colDef) { _table = table; _colDef = colDef; _isNullable = colDef.isNullable(); _isSigned = colDef.isSigned(); _scale = colDef.getScale(); } /** * Return the name of the java class used to hold this data type. */ public String getClassName() { return "java.lang.Short"; } /** * Determine if two objects of this data type contain the same value. * Neither of the objects is null */ public boolean areEqual(Object obj1, Object obj2){ return (obj1).equals(obj2); } /* * First we have the methods for in-cell and Text-table operations */ /** * Render a value into text for this DataType. */ public String renderObject(Object value) { return (String)_renderer.renderObject(value); } /** * This Data Type can be edited in a table cell. */ public boolean isEditableInCell(Object originalValue) { return true; } /** * See if a value in a column has been limited in some way and * needs to be re-read before being used for editing. * For read-only tables this may actually return true since we want * to be able to view the entire contents of the cell even if it was not * completely loaded during the initial table setup. */ public boolean needToReRead(Object originalValue) { // this DataType does not limit the data read during the initial load of the table, // so there is no need to re-read the complete data later return false; } /** * Return a JTextField usable in a CellEditor. */ public JTextField getJTextField() { _textComponent = new RestorableJTextField(); // special handling of operations while editing this data type ((RestorableJTextField)_textComponent).addKeyListener(new KeyTextHandler()); // // handle mouse events for double-click creation of popup dialog. // This happens only in the JTextField, not the JTextArea, so we can // make this an inner class within this method rather than a separate // inner class as is done with the KeyTextHandler class. // ((RestorableJTextField)_textComponent).addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent evt) { if (evt.getClickCount() == 2) { MouseEvent tableEvt = SwingUtilities.convertMouseEvent( (RestorableJTextField)DataTypeShort.this._textComponent, evt, DataTypeShort.this._table); CellDataPopup.showDialog(DataTypeShort.this._table, DataTypeShort.this._colDef, tableEvt, true); } } }); // end of mouse listener return (JTextField)_textComponent; } /** * Implement the interface for validating and converting to internal object. * Null is a valid successful return, so errors are indicated only by * existance or not of a message in the messageBuffer. */ public Object validateAndConvert(String value, Object originalValue, StringBuffer messageBuffer) { // handle null, which is shown as the special string "<null>" if (value.equals("<null>") || value.equals("")) return null; // Do the conversion into the object in a safe manner try { Object obj = new Short(value); return obj; } catch (Exception e) { messageBuffer.append(e.toString()+"\n"); //?? do we need the message also, or is it automatically part of the toString()? //messageBuffer.append(e.getMessage()); return null; } } /** * If true, this tells the PopupEditableIOPanel to use the * binary editing panel rather than a pure text panel. * The binary editing panel assumes the data is an array of bytes, * converts it into text form, allows the user to change how that * data is displayed (e.g. Hex, Decimal, etc.), and converts * the data back from text to bytes when the user editing is completed. * If this returns false, this DataType class must * convert the internal data into a text string that * can be displayed (and edited, if allowed) in a TextField * or TextArea, and must handle all * user key strokes related to editing of that data. */ public boolean useBinaryEditingPanel() { return false; } /* * Now the functions for the Popup-related operations. */ /** * Returns true if data type may be edited in the popup, * false if not. */ public boolean isEditableInPopup(Object originalValue) { return true; } /* * Return a JTextArea usable in the CellPopupDialog * and fill in the value. */ public JTextArea getJTextArea(Object value) { _textComponent = new RestorableJTextArea(); // value is a simple string representation of the data, // the same one used in Text and in-cell operations. ((RestorableJTextArea)_textComponent).setText(renderObject(value)); // special handling of operations while editing this data type ((RestorableJTextArea)_textComponent).addKeyListener(new KeyTextHandler()); return (RestorableJTextArea)_textComponent; } /** * Validating and converting in Popup is identical to cell-related operation. */ public Object validateAndConvertInPopup(String value, Object originalValue, StringBuffer messageBuffer) { return validateAndConvert(value, originalValue, messageBuffer); } /* * The following is used in both cell and popup operations. */ /* * Internal class for handling key events during editing * of both JTextField and JTextArea. */ private class KeyTextHandler extends BaseKeyTextHandler { public void keyTyped(KeyEvent e) { char c = e.getKeyChar(); // as a coding convenience, create a reference to the text component // that is typecast to JTextComponent. this is not essential, as we // could typecast every reference, but this makes the code cleaner JTextComponent _theComponent = (JTextComponent)DataTypeShort.this._textComponent; String text = _theComponent.getText(); // look for illegal chars if ( ! DataTypeShort.this._isSigned && c == '-') { // cannot use '-' when unsigned _beepHelper.beep(_theComponent); e.consume(); } // tabs and newlines get put into the text before this check, // so remove them // This only applies to Popup editing since these chars are // not passed to this level by the in-cell editor. if (c == KeyEvent.VK_TAB || c == KeyEvent.VK_ENTER) { // remove all instances of the offending char int index = text.indexOf(c); if (index != -1) { if (index == text.length() -1) { text = text.substring(0, text.length()-1); // truncate string } else { text = text.substring(0, index) + text.substring(index+1); } ((IRestorableTextComponent)_theComponent).updateText( text); _beepHelper.beep(_theComponent); } e.consume(); } if ( ! ( Character.isDigit(c) || (c == '-') || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE) ) ) { _beepHelper.beep(_theComponent); e.consume(); } // check for max size reached (only works when DB provides non-zero scale info if (DataTypeShort.this._scale > 0 && text.length() == DataTypeShort.this._scale && c != KeyEvent.VK_BACK_SPACE && c != KeyEvent.VK_DELETE) { // max size reached e.consume(); _beepHelper.beep(_theComponent); } // handle cases of null // The processing is different when nulls are allowed and when they are not. // if ( DataTypeShort.this._isNullable) { // user enters something when field is null if (text.equals("<null>")) { if ((c==KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE)) { // delete when null => original value DataTypeShort.this._textComponent.restoreText(); e.consume(); } else { // non-delete when null => clear field and add text DataTypeShort.this._textComponent.updateText(""); // fall through to normal processing of this key stroke } } else { // check for user deletes last thing in field if ((c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE)) { if (text.length() <= 1 ) { // about to delete last thing in field, so replace with null DataTypeShort.this._textComponent.updateText("<null>"); e.consume(); } } } } else { // field is not nullable // handleNotNullableField(text, c, e, _textComponent); } } } /* * DataBase-related functions */ /** * On input from the DB, read the data from the ResultSet into the appropriate * type of object to be stored in the table cell. */ public Object readResultSet(ResultSet rs, int index, boolean limitDataRead) throws java.sql.SQLException { short data = rs.getShort(index); if (rs.wasNull()) { return null; } else { return Short.valueOf(data); } } /** * When updating the database, generate a string form of this object value * that can be used in the WHERE clause to match the value in the database. * A return value of null means that this column cannot be used in the WHERE * clause, while a return of "null" (or "is null", etc) means that the column * can be used in the WHERE clause and the value is actually a null value. * This function must also include the column label so that its output * is of the form: * "columnName = value" * or * "columnName is null" * or whatever is appropriate for this column in the database. */ public String getWhereClauseValue(Object value, ISQLDatabaseMetaData md) { if (value == null || value.toString() == null || value.toString().length() == 0) return _colDef.getLabel() + " IS NULL"; else return _colDef.getLabel() + "=" + value.toString(); } /** * When updating the database, insert the appropriate datatype into the * prepared statment at the given variable position. */ public void setPreparedStatementValue(PreparedStatement pstmt, Object value, int position) throws java.sql.SQLException { if (value == null) { pstmt.setNull(position, _colDef.getSqlType()); } else { pstmt.setInt(position, ((Short)value).intValue()); } } /** * Get a default value for the table used to input data for a new row * to be inserted into the DB. */ public Object getDefaultValue(String dbDefaultValue) { if (dbDefaultValue != null) { // try to use the DB default value StringBuffer mbuf = new StringBuffer(); Object newObject = validateAndConvert(dbDefaultValue, null, mbuf); // if there was a problem with converting, then just fall through // and continue as if there was no default given in the DB. // Otherwise, use the converted object if (mbuf.length() == 0) return newObject; } // no default in DB. If nullable, use null. if (_isNullable) return null; // field is not nullable, so create a reasonable default value return Short.valueOf((short)0); } /* * File IO related functions */ /** * Say whether or not object can be exported to and imported from * a file. We put both export and import together in one test * on the assumption that all conversions can be done both ways. */ public boolean canDoFileIO() { return true; } /** * Read a file and construct a valid object from its contents. * Errors are returned by throwing an IOException containing the * cause of the problem as its message. * <P> * DataType is responsible for validating that the imported * data can be converted to an object, and then must return * a text string that can be used in the Popup window text area. * This object-to-text conversion is the same as is done by * the DataType object internally in the getJTextArea() method. * * <P> * File is assumed to be and ASCII string of digits * representing a value of this data type. */ public String importObject(FileInputStream inStream) throws IOException { InputStreamReader inReader = new InputStreamReader(inStream); int fileSize = inStream.available(); char charBuf[] = new char[fileSize]; int count = inReader.read(charBuf, 0, fileSize); if (count != fileSize) throw new IOException( "Could read only "+ count + " chars from a total file size of " + fileSize + ". Import failed."); // convert file text into a string // Special case: some systems tack a newline at the end of // the text read. Assume that if last char is a newline that // we want everything else in the line. String fileText; if (charBuf[count-1] == KeyEvent.VK_ENTER) fileText = new String(charBuf, 0, count-1); else fileText = new String(charBuf); // test that the string is valid by converting it into an // object of this data type StringBuffer messageBuffer = new StringBuffer(); validateAndConvertInPopup(fileText, null, messageBuffer); if (messageBuffer.length() > 0) { // convert number conversion issue into IO issue for consistancy throw new IOException( "Text does not represent data of type "+getClassName()+ ". Text was:\n"+fileText); } // return the text from the file since it does // represent a valid data value return fileText; } /** * Construct an appropriate external representation of the object * and write it to a file. * Errors are returned by throwing an IOException containing the * cause of the problem as its message. * <P> * DataType is responsible for validating that the given text * text from a Popup JTextArea can be converted to an object. * This text-to-object conversion is the same as validateAndConvertInPopup, * which may be used internally by the object to do the validation. * <P> * The DataType object must flush and close the output stream before returning. * Typically it will create another object (e.g. an OutputWriter), and * that is the object that must be flushed and closed. * * <P> * File is assumed to be and ASCII string of digits * representing a value of this data type. */ public void exportObject(FileOutputStream outStream, String text) throws IOException { OutputStreamWriter outWriter = new OutputStreamWriter(outStream); // check that the text is a valid representation StringBuffer messageBuffer = new StringBuffer(); validateAndConvertInPopup(text, null, messageBuffer); if (messageBuffer.length() > 0) { // there was an error in the conversion throw new IOException(new String(messageBuffer)); } // just send the text to the output file outWriter.write(text); outWriter.flush(); outWriter.close();
Clone fragments detected by clone detection tool
File path: /sql12/fw/src/net/sourceforge/squirrel_sql/fw/datasetviewer/cellcomponent/DataTypeByte.java File path: /sql12/fw/src/net/sourceforge/squirrel_sql/fw/datasetviewer/cellcomponent/DataTypeShort.java
Method name: Method name:
Number of AST nodes: 0 Number of AST nodes: 0
1
public class DataTypeByte extends BaseDataTypeComponent
1
public class DataTypeShort extends BaseDataTypeComponent
2
	implements IDataTypeComponent
2
	implements IDataTypeComponent
3
{
3
{
4
	/* whether nulls are allowed or not */
4
	/* whether nulls are allowed or not */
5
	private boolean _isNullable;
5
	private boolean _isNullable;
6
	/* whether number is signed or unsigned */
6
	/* whether number is signed or unsigned */
7
	private boolean _isSigned;
7
	private boolean _isSigned;
8
	/* the number of decimal digits allowed in the number */
8
	/* the number of decimal digits allowed in the number */
9
	private int _scale;
9
	private int _scale;
10
	/* table of which we are part (needed for creating popup dialog) */
10
	/* table of which we are part (needed for creating popup dialog) */
11
	private JTable _table;
11
	private JTable _table;
12
	
12
	
13
	/* The JTextComponent that is being used for editing */
13
	/* The JTextComponent that is being used for editing */
14
	private IRestorableTextComponent _textComponent;
14
	private IRestorableTextComponent _textComponent;
15
	
15
	
16
	/* The CellRenderer used for this data type */
16
	/* The CellRenderer used for this data type */
17
	//??? For now, use the same renderer as everyone else.
17
	//??? For now, use the same renderer as everyone else.
18
	//??
18
	//??
19
	//?? IN FUTURE: change this to use a new instance of renederer
19
	//?? IN FUTURE: change this to use a new instance of renederer
20
	//?? for this data type.
20
	//?? for this data type.
21
	private DefaultColumnRenderer _renderer = DefaultColumnRenderer.getInstance();
21
	private DefaultColumnRenderer _renderer = DefaultColumnRenderer.getInstance();
22
	/**
22
	/**
23
	 * Constructor - save the data needed by this data type.
23
	 * Constructor - save the data needed by this data type.
24
	 */
24
	 */
25
	public DataTypeByte(JTable table, ColumnDisplayDefinition colDef) {
25
	public DataTypeShort(JTable table, ColumnDisplayDefinition colDef) {
26
		_table = table;
26
		_table = table;
27
		_colDef = colDef;
27
		_colDef = colDef;
28
		_isNullable = colDef.isNullable();
28
		_isNullable = colDef.isNullable();
29
		_isSigned = colDef.isSigned();
29
		_isSigned = colDef.isSigned();
30
		_scale = colDef.getScale();
30
		_scale = colDef.getScale();
31
	}
31
	}
32
	
32
	
33
	/**
33
	/**
34
	 * Return the name of the java class used to hold this data type.
34
	 * Return the name of the java class used to hold this data type.
35
	 */
35
	 */
36
	public String getClassName() {
36
	public String getClassName() {
37
		return "java.lang.Byte";
37
		return "java.lang.Short";
38
	}
38
	}
39
	/**
39
	/**
40
	 * Determine if two objects of this data type contain the same value.
40
	 * Determine if two objects of this data type contain the same value.
41
	 * Neither of the objects is null
41
	 * Neither of the objects is null
42
	 */
42
	 */
43
	public boolean areEqual(Object obj1, Object obj2) {
43
	public boolean areEqual(Object obj1, Object obj2){
44
		return ((Byte)obj1).equals(obj2);
44
		return (obj1).equals(obj2);
45
	}
45
	}
46
	/*
46
	/*
47
	 * First we have the methods for in-cell and Text-table operations
47
	 * First we have the methods for in-cell and Text-table operations
48
	 */
48
	 */
49
	 
49
	 
50
	/**
50
	/**
51
	 * Render a value into text for this DataType.
51
	 * Render a value into text for this DataType.
52
	 */
52
	 */
53
	public String renderObject(Object value) {
53
	public String renderObject(Object value) {
54
		return (String)_renderer.renderObject(value);
54
		return (String)_renderer.renderObject(value);
55
	}
55
	}
56
	
56
	
57
	/**
57
	/**
58
	 * This Data Type can be edited in a table cell.
58
	 * This Data Type can be edited in a table cell.
59
	 */
59
	 */
60
	public boolean isEditableInCell(Object originalValue) {
60
	public boolean isEditableInCell(Object originalValue) {
61
		return true;	
61
		return true;	
62
	}
62
	}
63
	/**
63
	/**
64
	 * See if a value in a column has been limited in some way and
64
	 * See if a value in a column has been limited in some way and
65
	 * needs to be re-read before being used for editing.
65
	 * needs to be re-read before being used for editing.
66
	 * For read-only tables this may actually return true since we want
66
	 * For read-only tables this may actually return true since we want
67
	 * to be able to view the entire contents of the cell even if it was not
67
	 * to be able to view the entire contents of the cell even if it was not
68
	 * completely loaded during the initial table setup.
68
	 * completely loaded during the initial table setup.
69
	 */
69
	 */
70
	public boolean needToReRead(Object originalValue) {
70
	public boolean needToReRead(Object originalValue) {
71
		// this DataType does not limit the data read during the initial load of the table,
71
		// this DataType does not limit the data read during the initial load of the table,
72
		// so there is no need to re-read the complete data later
72
		// so there is no need to re-read the complete data later
73
		return false;
73
		return false;
74
	}
74
	}
75
	
75
	
76
	/**
76
	/**
77
	 * Return a JTextField usable in a CellEditor.
77
	 * Return a JTextField usable in a CellEditor.
78
	 */
78
	 */
79
	public JTextField getJTextField() {
79
	public JTextField getJTextField() {
80
		_textComponent = new RestorableJTextField();
80
		_textComponent = new RestorableJTextField();
81
		
81
		
82
		// special handling of operations while editing this data type
82
		// special handling of operations while editing this data type
83
		((RestorableJTextField)_textComponent).addKeyListener(new KeyTextHandler());
83
		((RestorableJTextField)_textComponent).addKeyListener(new KeyTextHandler());
84
				
84
				
85
		//
85
		//
86
		// handle mouse events for double-click creation of popup dialog.
86
		// handle mouse events for double-click creation of popup dialog.
87
		// This happens only in the JTextField, not the JTextArea, so we can
87
		// This happens only in the JTextField, not the JTextArea, so we can
88
		// make this an inner class within this method rather than a separate
88
		// make this an inner class within this method rather than a separate
89
		// inner class as is done with the KeyTextHandler class.
89
		// inner class as is done with the KeyTextHandler class.
90
		//
90
		//
91
		((RestorableJTextField)_textComponent).addMouseListener(new MouseAdapter()
91
		((RestorableJTextField)_textComponent).addMouseListener(new MouseAdapter()
92
		{
92
		{
93
			public void mousePressed(MouseEvent evt)
93
			public void mousePressed(MouseEvent evt)
94
			{
94
			{
95
				if (evt.getClickCount() == 2)
95
				if (evt.getClickCount() == 2)
96
				{
96
				{
97
					MouseEvent tableEvt = SwingUtilities.convertMouseEvent(
97
					MouseEvent tableEvt = SwingUtilities.convertMouseEvent(
98
						(RestorableJTextField)DataTypeByte.this._textComponent,
98
						(RestorableJTextField)DataTypeShort.this._textComponent,
99
						evt, DataTypeByte.this._table);
99
						evt, DataTypeShort.this._table);
100
					CellDataPopup.showDialog(DataTypeByte.this._table,
100
					CellDataPopup.showDialog(DataTypeShort.this._table,
101
						DataTypeByte.this._colDef, tableEvt, true);
101
						DataTypeShort.this._colDef, tableEvt, true);
102
				}
102
				}
103
			}
103
			}
104
		});	// end of mouse listener
104
		});	// end of mouse listener
105
		return (JTextField)_textComponent;
105
		return (JTextField)_textComponent;
106
	}
106
	}
107
	/**
107
	/**
108
	 * Implement the interface for validating and converting to internal object.
108
	 * Implement the interface for validating and converting to internal object.
109
	 * Null is a valid successful return, so errors are indicated only by
109
	 * Null is a valid successful return, so errors are indicated only by
110
	 * existance or not of a message in the messageBuffer.
110
	 * existance or not of a message in the messageBuffer.
111
	 */
111
	 */
112
	public Object validateAndConvert(String value, Object originalValue, StringBuffer messageBuffer) {
112
	public Object validateAndConvert(String value, Object originalValue, StringBuffer messageBuffer) {
113
		// handle null, which is shown as the special string "<null>"
113
		// handle null, which is shown as the special string "<null>"
114
		if (value.equals("<null>") || value.equals(""))
114
		if (value.equals("<null>") || value.equals(""))
115
			return null;
115
			return null;
116
		// Do the conversion into the object in a safe manner
116
		// Do the conversion into the object in a safe manner
117
		try {
117
		try {
118
			Object obj = new Byte(value);
118
			Object obj = new Short(value);
119
			return obj;
119
			return obj;
120
		}
120
		}
121
		catch (Exception e) {
121
		catch (Exception e) {
122
			messageBuffer.append(e.toString()+"\n");
122
			messageBuffer.append(e.toString()+"\n");
123
			//?? do we need the message also, or is it automatically part of the toString()?
123
			//?? do we need the message also, or is it automatically part of the toString()?
124
			//messageBuffer.append(e.getMessage());
124
			//messageBuffer.append(e.getMessage());
125
			return null;
125
			return null;
126
		}
126
		}
127
	}
127
	}
128
	/**
128
	/**
129
	 * If true, this tells the PopupEditableIOPanel to use the
129
	 * If true, this tells the PopupEditableIOPanel to use the
130
	 * binary editing panel rather than a pure text panel.
130
	 * binary editing panel rather than a pure text panel.
131
	 * The binary editing panel assumes the data is an array of bytes,
131
	 * The binary editing panel assumes the data is an array of bytes,
132
	 * converts it into text form, allows the user to change how that
132
	 * converts it into text form, allows the user to change how that
133
	 * data is displayed (e.g. Hex, Decimal, etc.), and converts
133
	 * data is displayed (e.g. Hex, Decimal, etc.), and converts
134
	 * the data back from text to bytes when the user editing is completed.
134
	 * the data back from text to bytes when the user editing is completed.
135
	 * If this returns false, this DataType class must
135
	 * If this returns false, this DataType class must
136
	 * convert the internal data into a text string that
136
	 * convert the internal data into a text string that
137
	 * can be displayed (and edited, if allowed) in a TextField
137
	 * can be displayed (and edited, if allowed) in a TextField
138
	 * or TextArea, and must handle all
138
	 * or TextArea, and must handle all
139
	 * user key strokes related to editing of that data.
139
	 * user key strokes related to editing of that data.
140
	 */
140
	 */
141
	public boolean useBinaryEditingPanel() {
141
	public boolean useBinaryEditingPanel() {
142
		return false;
142
		return false;
143
	}
143
	}
144
	 
144
	 
145
	/*
145
	/*
146
	 * Now the functions for the Popup-related operations.
146
	 * Now the functions for the Popup-related operations.
147
	 */
147
	 */
148
	
148
	
149
	/**
149
	/**
150
	 * Returns true if data type may be edited in the popup,
150
	 * Returns true if data type may be edited in the popup,
151
	 * false if not.
151
	 * false if not.
152
	 */
152
	 */
153
	public boolean isEditableInPopup(Object originalValue) {
153
	public boolean isEditableInPopup(Object originalValue) {
154
		return true;
154
		return true;
155
	}
155
	}
156
	/*
156
	/*
157
	 * Return a JTextArea usable in the CellPopupDialog
157
	 * Return a JTextArea usable in the CellPopupDialog
158
	 * and fill in the value.
158
	 * and fill in the value.
159
	 */
159
	 */
160
	 public JTextArea getJTextArea(Object value) {
160
	 public JTextArea getJTextArea(Object value) {
161
		_textComponent = new RestorableJTextArea();
161
		_textComponent = new RestorableJTextArea();
162
		
162
		
163
		// value is a simple string representation of the data,
163
		// value is a simple string representation of the data,
164
		// the same one used in Text and in-cell operations.
164
		// the same one used in Text and in-cell operations.
165
		((RestorableJTextArea)_textComponent).setText(renderObject(value));
165
		((RestorableJTextArea)_textComponent).setText(renderObject(value));
166
		
166
		
167
		// special handling of operations while editing this data type
167
		// special handling of operations while editing this data type
168
		((RestorableJTextArea)_textComponent).addKeyListener(new KeyTextHandler());
168
		((RestorableJTextArea)_textComponent).addKeyListener(new KeyTextHandler());
169
		
169
		
170
		return (RestorableJTextArea)_textComponent;
170
		return (RestorableJTextArea)_textComponent;
171
	 }
171
	 }
172
	/**
172
	/**
173
	 * Validating and converting in Popup is identical to cell-related operation.
173
	 * Validating and converting in Popup is identical to cell-related operation.
174
	 */
174
	 */
175
	public Object validateAndConvertInPopup(String value, Object originalValue, StringBuffer messageBuffer) {
175
	public Object validateAndConvertInPopup(String value, Object originalValue, StringBuffer messageBuffer) {
176
		return validateAndConvert(value, originalValue, messageBuffer);
176
		return validateAndConvert(value, originalValue, messageBuffer);
177
	}
177
	}
178
	/*
178
	/*
179
	 * The following is used in both cell and popup operations.
179
	 * The following is used in both cell and popup operations.
180
	 */	
180
	 */	
181
	
181
	
182
	/*
182
	/*
183
	 * Internal class for handling key events during editing
183
	 * Internal class for handling key events during editing
184
	 * of both JTextField and JTextArea.
184
	 * of both JTextField and JTextArea.
185
	 */
185
	 */
186
	 private class KeyTextHandler extends BaseKeyTextHandler {
186
	 private class KeyTextHandler extends BaseKeyTextHandler {
187
	 	public void keyTyped(KeyEvent e) {
187
	 	public void keyTyped(KeyEvent e) {
188
				char c = e.getKeyChar();
188
				char c = e.getKeyChar();
189
				
189
				
190
				// as a coding convenience, create a reference to the text component
190
				// as a coding convenience, create a reference to the text component
191
				// that is typecast to JTextComponent.  this is not essential, as we
191
				// that is typecast to JTextComponent.  this is not essential, as we
192
				// could typecast every reference, but this makes the code cleaner
192
				// could typecast every reference, but this makes the code cleaner
193
				JTextComponent _theComponent = (JTextComponent)DataTypeByte.this._textComponent;
193
				JTextComponent _theComponent = (JTextComponent)DataTypeShort.this._textComponent;
194
				String text = _theComponent.getText();
194
				String text = _theComponent.getText();
195
	
195
	
196
				// look for illegal chars
196
				// look for illegal chars
197
				if ( ! DataTypeByte.this._isSigned && c == '-') {
197
				if ( ! DataTypeShort.this._isSigned && c == '-') {
198
					// cannot use '-' when unsigned
198
					// cannot use '-' when unsigned
199
					_beepHelper.beep(_theComponent);
199
					_beepHelper.beep(_theComponent);
200
					e.consume();
200
					e.consume();
201
				}
201
				}
202
												
202
												
203
				// tabs and newlines get put into the text before this check,
203
				// tabs and newlines get put into the text before this check,
204
				// so remove them
204
				// so remove them
205
				// This only applies to Popup editing since these chars are
205
				// This only applies to Popup editing since these chars are
206
				// not passed to this level by the in-cell editor.
206
				// not passed to this level by the in-cell editor.
207
				if (c == KeyEvent.VK_TAB || c == KeyEvent.VK_ENTER) {
207
				if (c == KeyEvent.VK_TAB || c == KeyEvent.VK_ENTER) {
208
					// remove all instances of the offending char
208
					// remove all instances of the offending char
209
					int index = text.indexOf(c);
209
					int index = text.indexOf(c);
210
					if (index != -1) {
210
					if (index != -1) {
211
						if (index == text.length() -1) {
211
						if (index == text.length() -1) {
212
							text = text.substring(0, text.length()-1);	// truncate string
212
							text = text.substring(0, text.length()-1);	// truncate string
213
						}
213
						}
214
						else {
214
						else {
215
							text = text.substring(0, index) + text.substring(index+1);
215
							text = text.substring(0, index) + text.substring(index+1);
216
						}
216
						}
217
						((IRestorableTextComponent)_theComponent).updateText( text);
217
						((IRestorableTextComponent)_theComponent).updateText( text);
218
						_beepHelper.beep(_theComponent);
218
						_beepHelper.beep(_theComponent);
219
					}
219
					}
220
					e.consume();
220
					e.consume();
221
				}
221
				}
222
				if ( ! ( Character.isDigit(c) ||
222
				if ( ! ( Character.isDigit(c) ||
223
					(c == '-') ||
223
					(c == '-') ||
224
					(c == KeyEvent.VK_BACK_SPACE) ||
224
					(c == KeyEvent.VK_BACK_SPACE) ||
225
					(c == KeyEvent.VK_DELETE) ) ) {
225
					(c == KeyEvent.VK_DELETE) ) ) {
226
					_beepHelper.beep(_theComponent);
226
					_beepHelper.beep(_theComponent);
227
					e.consume();
227
					e.consume();
228
				}
228
				}
229
				// check for max size reached (only works when DB provides non-zero scale info
229
				// check for max size reached (only works when DB provides non-zero scale info
230
				if (DataTypeByte.this._scale > 0 &&
230
				if (DataTypeShort.this._scale > 0 &&
231
					text.length() == DataTypeByte.this._scale &&
231
					text.length() == DataTypeShort.this._scale &&
232
					c != KeyEvent.VK_BACK_SPACE &&
232
					c != KeyEvent.VK_BACK_SPACE &&
233
					c != KeyEvent.VK_DELETE) {
233
					c != KeyEvent.VK_DELETE) {
234
					// max size reached
234
					// max size reached
235
					e.consume();
235
					e.consume();
236
					_beepHelper.beep(_theComponent);
236
					_beepHelper.beep(_theComponent);
237
				}
237
				}
238
				// handle cases of null
238
				// handle cases of null
239
				// The processing is different when nulls are allowed and when they are not.
239
				// The processing is different when nulls are allowed and when they are not.
240
				//
240
				//
241
				if ( DataTypeByte.this._isNullable) {
241
				if ( DataTypeShort.this._isNullable) {
242
					// user enters something when field is null
242
					// user enters something when field is null
243
					if (text.equals("<null>")) {
243
					if (text.equals("<null>")) {
244
						if ((c==KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE)) {
244
						if ((c==KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE)) {
245
							// delete when null => original value
245
							// delete when null => original value
246
							DataTypeByte.this._textComponent.restoreText();
246
							DataTypeShort.this._textComponent.restoreText();
247
							e.consume();
247
							e.consume();
248
						}
248
						}
249
						else {
249
						else {
250
							// non-delete when null => clear field and add text
250
							// non-delete when null => clear field and add text
251
							DataTypeByte.this._textComponent.updateText("");
251
							DataTypeShort.this._textComponent.updateText("");
252
							// fall through to normal processing of this key stroke
252
							// fall through to normal processing of this key stroke
253
						}
253
						}
254
					}
254
					}
255
					else {
255
					else {
256
						// check for user deletes last thing in field
256
						// check for user deletes last thing in field
257
						if ((c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE)) {
257
						if ((c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE)) {
258
							if (text.length() <= 1 ) {
258
							if (text.length() <= 1 ) {
259
								// about to delete last thing in field, so replace with null
259
								// about to delete last thing in field, so replace with null
260
								DataTypeByte.this._textComponent.updateText("<null>");
260
								DataTypeShort.this._textComponent.updateText("<null>");
261
								e.consume();
261
								e.consume();
262
							}
262
							}
263
						}
263
						}
264
					}
264
					}
265
				}
265
				}
266
				else {
266
				else {
267
                    // field is not nullable
267
                    // field is not nullable
268
                    //
268
                    //
269
                    handleNotNullableField(text, c, e, _textComponent);
269
                    handleNotNullableField(text, c, e, _textComponent);
270
				}
270
				}
271
			}
271
			}
272
		}
272
		}
273
	
273
	
274
	
274
	
275
	/*
275
	/*
276
	 * DataBase-related functions
276
	 * DataBase-related functions
277
	 */
277
	 */
278
	 
278
	 
279
	 /**
279
	 /**
280
	  * On input from the DB, read the data from the ResultSet into the appropriate
280
	  * On input from the DB, read the data from the ResultSet into the appropriate
281
	  * type of object to be stored in the table cell.
281
	  * type of object to be stored in the table cell.
282
	  */
282
	  */
283
	public Object readResultSet(ResultSet rs, int index, boolean limitDataRead)
283
	public Object readResultSet(ResultSet rs, int index, boolean limitDataRead)
284
		throws java.sql.SQLException {
284
		throws java.sql.SQLException {
285
		
285
		
286
		byte data = rs.getByte(index);
286
		short data = rs.getShort(index);
287
		if (rs.wasNull()) {
287
		if (rs.wasNull()) {
288
			return null;
288
			return null;
289
        } else {
289
        } else {
290
            return Byte.valueOf(data);
290
            return Short.valueOf(data);
291
        }
291
        }
292
	}
292
	}
293
	/**
293
	/**
294
	 * When updating the database, generate a string form of this object value
294
	 * When updating the database, generate a string form of this object value
295
	 * that can be used in the WHERE clause to match the value in the database.
295
	 * that can be used in the WHERE clause to match the value in the database.
296
	 * A return value of null means that this column cannot be used in the WHERE
296
	 * A return value of null means that this column cannot be used in the WHERE
297
	 * clause, while a return of "null" (or "is null", etc) means that the column
297
	 * clause, while a return of "null" (or "is null", etc) means that the column
298
	 * can be used in the WHERE clause and the value is actually a null value.
298
	 * can be used in the WHERE clause and the value is actually a null value.
299
	 * This function must also include the column label so that its output
299
	 * This function must also include the column label so that its output
300
	 * is of the form:
300
	 * is of the form:
301
	 * 	"columnName = value"
301
	 * 	"columnName = value"
302
	 * or
302
	 * or
303
	 * 	"columnName is null"
303
	 * 	"columnName is null"
304
	 * or whatever is appropriate for this column in the database.
304
	 * or whatever is appropriate for this column in the database.
305
	 */
305
	 */
306
	public String getWhereClauseValue(Object value, ISQLDatabaseMetaData md) {
306
	public String getWhereClauseValue(Object value, ISQLDatabaseMetaData md) {
307
		if (value == null || value.toString() == null || value.toString().length() == 0)
307
		if (value == null || value.toString() == null || value.toString().length() == 0)
308
			return _colDef.getLabel() + " IS NULL";
308
			return _colDef.getLabel() + " IS NULL";
309
		else
309
		else
310
			return _colDef.getLabel() + "=" + value.toString();
310
			return _colDef.getLabel() + "=" + value.toString();
311
	}
311
	}
312
	
312
	
313
	
313
	
314
	/**
314
	/**
315
	 * When updating the database, insert the appropriate datatype into the
315
	 * When updating the database, insert the appropriate datatype into the
316
	 * prepared statment at the given variable position.
316
	 * prepared statment at the given variable position.
317
	 */
317
	 */
318
	public void setPreparedStatementValue(PreparedStatement pstmt, Object value, int position)
318
	public void setPreparedStatementValue(PreparedStatement pstmt, Object value, int position)
319
		throws java.sql.SQLException {
319
		throws java.sql.SQLException {
320
		if (value == null) {
320
		if (value == null) {
321
			pstmt.setNull(position, _colDef.getSqlType());
321
			pstmt.setNull(position, _colDef.getSqlType());
322
		}
322
		}
323
		else {
323
		else {
324
			pstmt.setInt(position, ((Byte)value).intValue());
324
			pstmt.setInt(position, ((Short)value).intValue());
325
		}
325
		}
326
	}
326
	}
327
	
327
	
328
	/**
328
	/**
329
	 * Get a default value for the table used to input data for a new row
329
	 * Get a default value for the table used to input data for a new row
330
	 * to be inserted into the DB.
330
	 * to be inserted into the DB.
331
	 */
331
	 */
332
	public Object getDefaultValue(String dbDefaultValue) {
332
	public Object getDefaultValue(String dbDefaultValue) {
333
		if (dbDefaultValue != null) {
333
		if (dbDefaultValue != null) {
334
			// try to use the DB default value
334
			// try to use the DB default value
335
			StringBuffer mbuf = new StringBuffer();
335
			StringBuffer mbuf = new StringBuffer();
336
			Object newObject = validateAndConvert(dbDefaultValue, null, mbuf);
336
			Object newObject = validateAndConvert(dbDefaultValue, null, mbuf);
337
			
337
			
338
			// if there was a problem with converting, then just fall through
338
			// if there was a problem with converting, then just fall through
339
			// and continue as if there was no default given in the DB.
339
			// and continue as if there was no default given in the DB.
340
			// Otherwise, use the converted object
340
			// Otherwise, use the converted object
341
			if (mbuf.length() == 0)
341
			if (mbuf.length() == 0)
342
				return newObject;
342
				return newObject;
343
		}
343
		}
344
		
344
		
345
		// no default in DB.  If nullable, use null.
345
		// no default in DB.  If nullable, use null.
346
		if (_isNullable)
346
		if (_isNullable)
347
			return null;
347
			return null;
348
		
348
		
349
		// field is not nullable, so create a reasonable default value
349
		// field is not nullable, so create a reasonable default value
350
		return Byte.valueOf((byte)0);
350
		return Short.valueOf((short)0);
351
	}
351
	}
352
	
352
	
353
	
353
	
354
	/*
354
	/*
355
	 * File IO related functions
355
	 * File IO related functions
356
	 */
356
	 */
357
	 
357
	 
358
	 
358
	 
359
	 /**
359
	 /**
360
	  * Say whether or not object can be exported to and imported from
360
	  * Say whether or not object can be exported to and imported from
361
	  * a file.  We put both export and import together in one test
361
	  * a file.  We put both export and import together in one test
362
	  * on the assumption that all conversions can be done both ways.
362
	  * on the assumption that all conversions can be done both ways.
363
	  */
363
	  */
364
	 public boolean canDoFileIO() {
364
	 public boolean canDoFileIO() {
365
	 	return true;
365
	 	return true;
366
	 }
366
	 }
367
	 
367
	 
368
	 /**
368
	 /**
369
	  * Read a file and construct a valid object from its contents.
369
	  * Read a file and construct a valid object from its contents.
370
	  * Errors are returned by throwing an IOException containing the
370
	  * Errors are returned by throwing an IOException containing the
371
	  * cause of the problem as its message.
371
	  * cause of the problem as its message.
372
	  * <P>
372
	  * <P>
373
	  * DataType is responsible for validating that the imported
373
	  * DataType is responsible for validating that the imported
374
	  * data can be converted to an object, and then must return
374
	  * data can be converted to an object, and then must return
375
	  * a text string that can be used in the Popup window text area.
375
	  * a text string that can be used in the Popup window text area.
376
	  * This object-to-text conversion is the same as is done by
376
	  * This object-to-text conversion is the same as is done by
377
	  * the DataType object internally in the getJTextArea() method.
377
	  * the DataType object internally in the getJTextArea() method.
378
	  * 
378
	  * 
379
	  * <P>
379
	  * <P>
380
	  * File is assumed to be and ASCII string of digits
380
	  * File is assumed to be and ASCII string of digits
381
	  * representing a value of this data type.
381
	  * representing a value of this data type.
382
	  */
382
	  */
383
	public String importObject(FileInputStream inStream)
383
	public String importObject(FileInputStream inStream)
384
	 	throws IOException {
384
	 	throws IOException {
385
	 	
385
	 	
386
	 	InputStreamReader inReader = new InputStreamReader(inStream);
386
	 	InputStreamReader inReader = new InputStreamReader(inStream);
387
	 	
387
	 	
388
	 	int fileSize = inStream.available();
388
	 	int fileSize = inStream.available();
389
	 	
389
	 	
390
	 	char charBuf[] = new char[fileSize];
390
	 	char charBuf[] = new char[fileSize];
391
	 	
391
	 	
392
	 	int count = inReader.read(charBuf, 0, fileSize);
392
	 	int count = inReader.read(charBuf, 0, fileSize);
393
	 	
393
	 	
394
	 	if (count != fileSize)
394
	 	if (count != fileSize)
395
	 		throw new IOException(
395
	 		throw new IOException(
396
	 			"Could read only "+ count +
396
	 			"Could read only "+ count +
397
	 			" chars from a total file size of " + fileSize +
397
	 			" chars from a total file size of " + fileSize +
398
	 			". Import failed.");
398
	 			". Import failed.");
399
	 	
399
	 	
400
	 	// convert file text into a string
400
	 	// convert file text into a string
401
	 	// Special case: some systems tack a newline at the end of
401
	 	// Special case: some systems tack a newline at the end of
402
	 	// the text read.  Assume that if last char is a newline that
402
	 	// the text read.  Assume that if last char is a newline that
403
	 	// we want everything else in the line.
403
	 	// we want everything else in the line.
404
	 	String fileText;
404
	 	String fileText;
405
	 	if (charBuf[count-1] == KeyEvent.VK_ENTER)
405
	 	if (charBuf[count-1] == KeyEvent.VK_ENTER)
406
	 		fileText = new String(charBuf, 0, count-1);
406
	 		fileText = new String(charBuf, 0, count-1);
407
	 	else fileText = new String(charBuf);
407
	 	else fileText = new String(charBuf);
408
	 	
408
	 	
409
	 	// test that the string is valid by converting it into an
409
	 	// test that the string is valid by converting it into an
410
	 	// object of this data type
410
	 	// object of this data type
411
	 	StringBuffer messageBuffer = new StringBuffer();
411
	 	StringBuffer messageBuffer = new StringBuffer();
412
	 	validateAndConvertInPopup(fileText, null, messageBuffer);
412
	 	validateAndConvertInPopup(fileText, null, messageBuffer);
413
	 	if (messageBuffer.length() > 0) {
413
	 	if (messageBuffer.length() > 0) {
414
	 		// convert number conversion issue into IO issue for consistancy
414
	 		// convert number conversion issue into IO issue for consistancy
415
	 		throw new IOException(
415
	 		throw new IOException(
416
	 			"Text does not represent data of type "+getClassName()+
416
	 			"Text does not represent data of type "+getClassName()+
417
	 			".  Text was:\n"+fileText);
417
	 			".  Text was:\n"+fileText);
418
	 	}
418
	 	}
419
	 	
419
	 	
420
	 	// return the text from the file since it does
420
	 	// return the text from the file since it does
421
	 	// represent a valid data value
421
	 	// represent a valid data value
422
	 	return fileText;
422
	 	return fileText;
423
	}
423
	}
424
	 	 
424
	 	 
425
	 /**
425
	 /**
426
	  * Construct an appropriate external representation of the object
426
	  * Construct an appropriate external representation of the object
427
	  * and write it to a file.
427
	  * and write it to a file.
428
	  * Errors are returned by throwing an IOException containing the
428
	  * Errors are returned by throwing an IOException containing the
429
	  * cause of the problem as its message.
429
	  * cause of the problem as its message.
430
	  * <P>
430
	  * <P>
431
	  * DataType is responsible for validating that the given text
431
	  * DataType is responsible for validating that the given text
432
	  * text from a Popup JTextArea can be converted to an object.
432
	  * text from a Popup JTextArea can be converted to an object.
433
	  * This text-to-object conversion is the same as validateAndConvertInPopup,
433
	  * This text-to-object conversion is the same as validateAndConvertInPopup,
434
	  * which may be used internally by the object to do the validation.
434
	  * which may be used internally by the object to do the validation.
435
	  * <P>
435
	  * <P>
436
	  * The DataType object must flush and close the output stream before returning.
436
	  * The DataType object must flush and close the output stream before returning.
437
	  * Typically it will create another object (e.g. an OutputWriter), and
437
	  * Typically it will create another object (e.g. an OutputWriter), and
438
	  * that is the object that must be flushed and closed.
438
	  * that is the object that must be flushed and closed.
439
	  * 
439
	  * 
440
	  * <P>
440
	  * <P>
441
	  * File is assumed to be and ASCII string of digits
441
	  * File is assumed to be and ASCII string of digits
442
	  * representing a value of this data type.
442
	  * representing a value of this data type.
443
	  */
443
	  */
444
	 public void exportObject(FileOutputStream outStream, String text)
444
	 public void exportObject(FileOutputStream outStream, String text)
445
	 	throws IOException {
445
	 	throws IOException {
446
	 	
446
	 	
447
	 	OutputStreamWriter outWriter = new OutputStreamWriter(outStream);
447
	 	OutputStreamWriter outWriter = new OutputStreamWriter(outStream);
448
	 	
448
	 	
449
	 	// check that the text is a valid representation
449
	 	// check that the text is a valid representation
450
	 	StringBuffer messageBuffer = new StringBuffer();
450
	 	StringBuffer messageBuffer = new StringBuffer();
451
	 	validateAndConvertInPopup(text, null, messageBuffer);
451
	 	validateAndConvertInPopup(text, null, messageBuffer);
452
	 	if (messageBuffer.length() > 0) {
452
	 	if (messageBuffer.length() > 0) {
453
	 		// there was an error in the conversion
453
	 		// there was an error in the conversion
454
	 		throw new IOException(new String(messageBuffer));
454
	 		throw new IOException(new String(messageBuffer));
455
	 	}
455
	 	}
456
	 	
456
	 	
457
	 	// just send the text to the output file
457
	 	// just send the text to the output file
458
		outWriter.write(text);
458
		outWriter.write(text);
459
		outWriter.flush();
459
		outWriter.flush();
460
		outWriter.close();
460
		outWriter.close();
461
	 
461
	 
Summary
Number of common nesting structure subtrees0
Number of refactorable cases0
Number of non-refactorable cases0
Time elapsed for finding largest common nesting structure subtrees (ms)0.0
Clones location
Number of node comparisons0