Statistics
| Revision:

root / trunk / libraries / libGDBMS / src / test / java / com / hardcode / gdbms / gui / editingTable / EditingTable.java @ 10627

History | View | Annotate | Download (9.04 KB)

1
package com.hardcode.gdbms.gui.editingTable;
2

    
3
import java.awt.BorderLayout;
4
import java.awt.Component;
5
import java.awt.event.KeyAdapter;
6
import java.awt.event.KeyEvent;
7
import java.text.ParseException;
8
import java.util.ArrayList;
9
import java.util.EventObject;
10

    
11
import javax.swing.JPanel;
12
import javax.swing.JScrollPane;
13
import javax.swing.JTable;
14
import javax.swing.JTextField;
15
import javax.swing.KeyStroke;
16
import javax.swing.event.CellEditorListener;
17
import javax.swing.event.ChangeEvent;
18
import javax.swing.event.TableModelEvent;
19
import javax.swing.event.TableModelListener;
20
import javax.swing.table.AbstractTableModel;
21
import javax.swing.table.TableCellEditor;
22
import javax.swing.table.TableModel;
23

    
24
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
25
import com.hardcode.gdbms.driver.exceptions.WriteDriverException;
26
import com.hardcode.gdbms.engine.data.DataSource;
27
import com.hardcode.gdbms.engine.data.DataSourceFactory;
28
import com.hardcode.gdbms.engine.data.driver.DriverException;
29
import com.hardcode.gdbms.engine.data.edition.DataWare;
30
import com.hardcode.gdbms.engine.values.Value;
31
import com.hardcode.gdbms.engine.values.ValueFactory;
32
/**
33
 * @author Fernando Gonz?lez Cort?s
34
 */
35
public class EditingTable extends JPanel {
36

    
37
        private JTable jTable = null;
38
        private JScrollPane jScrollPane = null;
39

    
40
        private DataSource ds;
41
        private DataWare dw = null;
42

    
43
        public void setDataSource(DataSource ds){
44
            this.ds = ds;
45
            MyCellEditor ce = new MyCellEditor();
46
            getJTable().setModel(new DataSourceDataModel());
47
            for (int i = 0; i < jTable.getColumnModel().getColumnCount(); i++) {
48
                        jTable.getColumnModel().getColumn(i).setCellEditor(ce);
49
        }
50
            getJTable().setCellEditor(ce);
51
        }
52

    
53
        /**
54
         * This is the default constructor
55
         */
56
        public EditingTable() {
57
                super();
58
                initialize();
59
        }
60
        /**
61
         * This method initializes this
62
         *
63
         * @return void
64
         */
65
        private  void initialize() {
66
                this.setLayout(new BorderLayout());
67
                this.setSize(300,200);
68
                this.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
69
        }
70
        /**
71
         * This method initializes jTable
72
         *
73
         * @return javax.swing.JTable
74
         */
75
        private JTable getJTable() {
76
                if (jTable == null) {
77
                        jTable = new JTable() {
78
                protected boolean processKeyBinding(KeyStroke ks, KeyEvent e,
79
                        int condition, boolean pressed) {
80
                    boolean value = super.processKeyBinding( ks, e,
81
                            condition, pressed );
82

    
83
                    // Make sure that the editor component has focus.
84
                    if ( isEditing() ) {
85
                        getEditorComponent().requestFocus();
86
                    }
87
                    return value;
88
                }
89
            };
90
                }
91
                return jTable;
92
        }
93
        /**
94
         * This method initializes jScrollPane
95
         *
96
         * @return javax.swing.JScrollPane
97
         */
98
        private JScrollPane getJScrollPane() {
99
                if (jScrollPane == null) {
100
                        jScrollPane = new JScrollPane();
101
                        jScrollPane.setViewportView(getJTable());
102
                }
103
                return jScrollPane;
104
        }
105

    
106

    
107
    /**
108
     * DOCUMENT ME!
109
     *
110
     * @author Fernando Gonz?lez Cort?s
111
     */
112
    public class DataSourceDataModel extends AbstractTableModel {
113

    
114
        public DataSourceDataModel(){
115
            this.addTableModelListener(new TableModelListener() {
116
                public void tableChanged(TableModelEvent e) {
117
                    int row = e.getFirstRow();
118
                    int field = e.getColumn();
119
                    TableModel model = (TableModel)e.getSource();
120
                    String columnName = model.getColumnName(field);
121
                    Object data = model.getValueAt(row, field);
122
                    System.out.println(data);
123
                }
124
            });
125
        }
126

    
127
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
128
            Value v;
129
            try {
130
                v = ValueFactory.createValueByType(aValue.toString(), ds.getFieldType(columnIndex));
131
                dw.setFieldValue(rowIndex, columnIndex, v);
132
            } catch (ReadDriverException e1) {
133
                throw new RuntimeException(e1);
134
            } catch (ParseException e) {
135
                throw new RuntimeException(e);
136
            } catch (WriteDriverException e) {
137
                     throw new RuntimeException(e);
138
                        }
139
        }
140
        /**
141
         * DOCUMENT ME!
142
         *
143
         * @param col DOCUMENT ME!
144
         *
145
         * @return DOCUMENT ME!
146
         */
147
        public String getColumnName(int col) {
148
            try {
149
                return ds.getFieldName(col).trim();
150
            } catch (ReadDriverException e) {
151
                return e.getMessage();
152
            }
153
        }
154

    
155
        /**
156
         * DOCUMENT ME!
157
         *
158
         * @return DOCUMENT ME!
159
         */
160
        public int getColumnCount() {
161
            try {
162
                return ds.getFieldCount();
163
            } catch (ReadDriverException e) {
164
                return 0;
165
            }
166
        }
167

    
168
        /**
169
         * DOCUMENT ME!
170
         *
171
         * @return DOCUMENT ME!
172
         */
173
        public int getRowCount() {
174
            try {
175
                return (int) ds.getRowCount();
176
            } catch (ReadDriverException e) {
177
                return 0;
178
            }
179
        }
180

    
181
        /**
182
         * DOCUMENT ME!
183
         *
184
         * @param row DOCUMENT ME!
185
         * @param col DOCUMENT ME!
186
         *
187
         * @return DOCUMENT ME!
188
         */
189
        public Object getValueAt(int row, int col) {
190
            try {
191
                if (dw != null){
192
                    return dw.getFieldValue(row, col);
193
                }else{
194
                    return ds.getFieldValue(row, col);
195
                }
196
            } catch (ReadDriverException e) {
197
                return ValueFactory.createValue("").toString();
198
            }
199
        }
200

    
201
        public boolean isCellEditable(int rowIndex, int columnIndex) {
202
            return true;
203
        }
204
    }
205

    
206

    
207
    /**
208
     * @throws DriverException
209
     *
210
     */
211
    public void startEditing() throws ReadDriverException {
212
        dw = ds.getDataWare(DataSourceFactory.DATA_WARE_COHERENT_ROW_ORDER);
213
        dw.start();
214
        dw.beginTrans();
215
    }
216

    
217
    public void commit() throws ReadDriverException, WriteDriverException {
218
        dw.commitTrans();
219
        dw.stop();
220
        dw = null;
221
    }
222

    
223
    public void rollBack() throws ReadDriverException, WriteDriverException {
224
        dw.rollBackTrans();
225
        dw.stop();
226
        dw = null;
227
    }
228

    
229
    public class MyCellEditor extends JTextField implements TableCellEditor {
230

    
231
        private ArrayList listeners = new ArrayList();
232
        private String initialValue;
233

    
234
        public MyCellEditor() {
235
            addKeyListener(new KeyAdapter() {
236
                public void keyReleased(KeyEvent e) {
237
                    if (e.getKeyCode() == KeyEvent.VK_ENTER){
238
                        stopCellEditing();
239
                    }else if (e.getKeyCode() == KeyEvent.VK_ESCAPE){
240
                        cancelCellEditing();
241
                    }
242
                }
243
            });
244
        }
245

    
246
        /**
247
         * @see javax.swing.table.TableCellEditor#getTableCellEditorComponent(javax.swing.JTable, java.lang.Object, boolean, int, int)
248
         */
249
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
250
            this.setText(value.toString());
251
            return this;
252
        }
253

    
254
        /**
255
         * @see javax.swing.CellEditor#cancelCellEditing()
256
         */
257
        public void cancelCellEditing() {
258
            setText(initialValue);
259
            for (int i = 0; i < listeners.size(); i++) {
260
                CellEditorListener l = (CellEditorListener) listeners.get(i);
261
                ChangeEvent evt = new ChangeEvent(this);
262
                l.editingCanceled(evt);
263
            }
264
        }
265

    
266
        /**
267
         * @see javax.swing.CellEditor#stopCellEditing()
268
         */
269
        public boolean stopCellEditing() {
270
            for (int i = 0; i < listeners.size(); i++) {
271
                CellEditorListener l = (CellEditorListener) listeners.get(i);
272
                ChangeEvent evt = new ChangeEvent(this);
273
                l.editingStopped(evt);
274
            }
275

    
276
            return true;
277
        }
278

    
279
        /**
280
         * @see javax.swing.CellEditor#getCellEditorValue()
281
         */
282
        public Object getCellEditorValue() {
283
            return getText();
284
        }
285

    
286
        /**
287
         * @see javax.swing.CellEditor#isCellEditable(java.util.EventObject)
288
         */
289
        public boolean isCellEditable(EventObject anEvent) {
290
            return true;
291
        }
292

    
293
        /**
294
         * @see javax.swing.CellEditor#shouldSelectCell(java.util.EventObject)
295
         */
296
        public boolean shouldSelectCell(EventObject anEvent) {
297
            return false;
298
        }
299

    
300
        /**
301
         * @see javax.swing.CellEditor#addCellEditorListener(javax.swing.event.CellEditorListener)
302
         */
303
        public void addCellEditorListener(CellEditorListener l) {
304
            listeners.add(l);
305
        }
306

    
307
        /**
308
         * @see javax.swing.CellEditor#removeCellEditorListener(javax.swing.event.CellEditorListener)
309
         */
310
        public void removeCellEditorListener(CellEditorListener l) {
311
            listeners.remove(l);
312
        }
313

    
314
    }
315

    
316
}