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 |
|