KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > dataview > DOTableModel


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.dataview;
21
22 import javax.swing.table.AbstractTableModel JavaDoc;
23
24 import org.apache.cayenne.DataObject;
25
26 /**
27  * A Swing TableModel wrapping {@link DataObjectList}.
28  *
29  * @since 1.1
30  */

31 public class DOTableModel extends AbstractTableModel JavaDoc implements DataObjectChangeListener {
32
33     private ObjEntityView view;
34     private int[] columnMap;
35     private String JavaDoc[] columnNames;
36     private boolean[] editableColumns;
37     private Class JavaDoc[] columnClasses;
38     private int columnCount = 0;
39     private DataObjectList dataObjects = new DataObjectList(1);
40
41     public DOTableModel() {
42         updateModel();
43     }
44
45     public ObjEntityView getView() {
46         return view;
47     }
48
49     public void setView(ObjEntityView view) {
50         this.view = view;
51         updateModel();
52     }
53
54     public void updateModel() {
55         if (view != null) {
56             int fieldCount = view.getFieldCount();
57             columnMap = new int[fieldCount];
58             columnNames = new String JavaDoc[fieldCount];
59             editableColumns = new boolean[fieldCount];
60             columnClasses = new Class JavaDoc[fieldCount];
61             columnCount = 0;
62             for (int i = 0; i < fieldCount; i++) {
63                 ObjEntityViewField field = view.getField(i);
64                 if (!field.isVisible())
65                     continue;
66                 columnMap[columnCount] = i;
67                 columnNames[columnCount] = field.getCaption();
68                 editableColumns[columnCount] = field.isEditable();
69                 columnClasses[columnCount] = field.getJavaClass();
70                 if (columnClasses[columnCount] == null)
71                     columnClasses[columnCount] = String JavaDoc.class;
72                 columnCount++;
73             }
74         }
75         else {
76             columnMap = new int[0];
77             columnNames = new String JavaDoc[0];
78             editableColumns = new boolean[0];
79             columnClasses = new Class JavaDoc[0];
80             columnCount = 0;
81             dataObjects = new DataObjectList(1);
82         }
83         fireTableStructureChanged();
84     }
85
86     public int getColumnCount() {
87         return columnCount;
88     }
89
90     public boolean isCellEditable(int rowIndex, int columnIndex) {
91         return editableColumns[columnIndex];
92     }
93
94     public String JavaDoc getColumnName(int column) {
95         return columnNames[column];
96     }
97
98     public Class JavaDoc getColumnClass(int columnIndex) {
99         return columnClasses[columnIndex];
100     }
101
102     public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
103         ObjEntityViewField field = getField(columnIndex);
104         DataObject obj = getDataObject(rowIndex);
105         Object JavaDoc value = field.getValue(obj);
106         return value;
107     }
108
109     public int getRowCount() {
110         return dataObjects.size();
111     }
112
113     public void setValueAt(Object JavaDoc value, int rowIndex, int columnIndex) {
114         ObjEntityViewField field = getField(columnIndex);
115         DataObject obj = getDataObject(rowIndex);
116         field.setValue(obj, value);
117     }
118
119     public DataObject getDataObject(int rowIndex) {
120         return dataObjects.getDataObject(rowIndex);
121     }
122
123     public ObjEntityViewField getField(int columnIndex) {
124         return view.getField(columnMap[columnIndex]);
125     }
126
127     public DataObjectList getDataObjects() {
128         return dataObjects;
129     }
130
131     public void setDataObjects(DataObjectList dataObjects) {
132         this.dataObjects.removeDataObjectChangeListener(this);
133         this.dataObjects = dataObjects;
134         this.dataObjects.addDataObjectChangeListener(this);
135         fireTableDataChanged();
136     }
137
138     public void dataChanged(DataObjectChangeEvent event) {
139         if (event.isMultiObjectChange()) {
140             fireTableDataChanged();
141             return;
142         }
143         int affectedRow = event.getAffectedDataObjectIndex();
144         switch (event.getId()) {
145             case DataObjectChangeEvent.DATAOBJECT_ADDED:
146                 fireTableRowsInserted(affectedRow, affectedRow);
147                 break;
148             case DataObjectChangeEvent.DATAOBJECT_REMOVED:
149                 fireTableRowsDeleted(affectedRow, affectedRow);
150                 break;
151             case DataObjectChangeEvent.DATAOBJECT_CHANGED:
152                 fireTableRowsUpdated(affectedRow, affectedRow);
153                 break;
154             default:
155                 fireTableDataChanged();
156         }
157     }
158 }
159
Popular Tags