KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > modeler > editor > ObjAttributeTableModel


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.modeler.editor;
57
58 import java.util.ArrayList JavaDoc;
59 import java.util.Collections JavaDoc;
60 import java.util.Comparator JavaDoc;
61
62 import org.apache.log4j.Logger;
63 import org.objectstyle.cayenne.dba.TypesMapping;
64 import org.objectstyle.cayenne.map.DbAttribute;
65 import org.objectstyle.cayenne.map.DbEntity;
66 import org.objectstyle.cayenne.map.ObjAttribute;
67 import org.objectstyle.cayenne.map.ObjEntity;
68 import org.objectstyle.cayenne.map.event.AttributeEvent;
69 import org.objectstyle.cayenne.modeler.ProjectController;
70 import org.objectstyle.cayenne.modeler.util.CayenneTableModel;
71 import org.objectstyle.cayenne.modeler.util.ProjectUtil;
72 import org.objectstyle.cayenne.util.Util;
73
74 /**
75  * Model for the Object Entity attributes and for Obj to
76  * DB Attribute Mapping tables. Allows adding/removing attributes,
77  * modifying the types and the names.
78  *
79  * @author Michael Misha Shengaout.
80  * @author Andrei Adamchik
81  */

82 public class ObjAttributeTableModel extends CayenneTableModel {
83     // Columns
84
static final int OBJ_ATTRIBUTE = 0;
85     static final int OBJ_ATTRIBUTE_TYPE = 1;
86     static final int DB_ATTRIBUTE = 2;
87     static final int DB_ATTRIBUTE_TYPE = 3;
88     static final int LOCKING = 4;
89
90     static final Logger logObj = Logger.getLogger(ObjAttributeTableModel.class);
91
92     protected ObjEntity entity;
93     protected DbEntity dbEntity;
94
95     public ObjAttributeTableModel(
96         ObjEntity entity,
97         ProjectController mediator,
98         Object JavaDoc eventSource) {
99         super(mediator, eventSource, new ArrayList JavaDoc(entity.getAttributes()));
100         // take a copy
101
this.entity = entity;
102         this.dbEntity = entity.getDbEntity();
103
104         // order using local comparator
105
Collections.sort(objectList, new AttributeComparator());
106     }
107
108     protected void orderList() {
109         // NOOP
110
}
111
112     public Class JavaDoc getColumnClass(int col) {
113         switch (col) {
114             case LOCKING :
115                 return Boolean JavaDoc.class;
116             default :
117                 return String JavaDoc.class;
118         }
119     }
120
121     /**
122      * Returns ObjAttribute class.
123      */

124     public Class JavaDoc getElementsClass() {
125         return ObjAttribute.class;
126     }
127
128     public DbEntity getDbEntity() {
129         return dbEntity;
130     }
131
132     public ObjAttribute getAttribute(int row) {
133         return (row >= 0 && row < objectList.size())
134             ? (ObjAttribute) objectList.get(row)
135             : null;
136     }
137
138     /** Refreshes DbEntity to current db entity within ObjEntity.*/
139     public void resetDbEntity() {
140         if (dbEntity == entity.getDbEntity()) {
141             return;
142         }
143
144         boolean wasShowing = isShowingDb();
145         dbEntity = entity.getDbEntity();
146         boolean isShowing = isShowingDb();
147
148         if (wasShowing != isShowing) {
149             fireTableStructureChanged();
150         }
151
152         fireTableDataChanged();
153     }
154
155     private boolean isShowingDb() {
156         return dbEntity != null;
157     }
158
159     public int getColumnCount() {
160         return 5;
161     }
162
163     public String JavaDoc getColumnName(int column) {
164         switch (column) {
165             case OBJ_ATTRIBUTE :
166                 return "ObjAttribute";
167             case OBJ_ATTRIBUTE_TYPE :
168                 return "Java Type";
169             case DB_ATTRIBUTE :
170                 return "DbAttribute";
171             case DB_ATTRIBUTE_TYPE :
172                 return "DB Type";
173             case LOCKING :
174                 return "Used for Locking";
175             default :
176                 return "";
177         }
178     }
179
180     public Object JavaDoc getValueAt(int row, int column) {
181         ObjAttribute attribute = getAttribute(row);
182
183         if (column == OBJ_ATTRIBUTE) {
184             return attribute.getName();
185         }
186         else if (column == OBJ_ATTRIBUTE_TYPE) {
187             return attribute.getType();
188         }
189         else if (column == LOCKING) {
190             return attribute.isUsedForLocking() ? Boolean.TRUE : Boolean.FALSE;
191         }
192         else {
193             DbAttribute dbAttribute = attribute.getDbAttribute();
194             if (dbAttribute == null) {
195                 return null;
196             }
197             else if (column == DB_ATTRIBUTE)
198                 return dbAttribute.getName();
199             else if (column == DB_ATTRIBUTE_TYPE) {
200                 return TypesMapping.getSqlNameByType(dbAttribute.getType());
201             }
202             else {
203                 return null;
204             }
205         }
206     }
207
208     public void setUpdatedValueAt(Object JavaDoc value, int row, int column) {
209
210         ObjAttribute attribute = getAttribute(row);
211         AttributeEvent event = new AttributeEvent(eventSource, attribute, entity);
212
213         if (column == OBJ_ATTRIBUTE) {
214             event.setOldName(attribute.getName());
215             ProjectUtil.setAttributeName(
216                 attribute,
217                 value != null ? value.toString().trim() : null);
218             fireTableCellUpdated(row, column);
219         }
220         else if (column == OBJ_ATTRIBUTE_TYPE) {
221             attribute.setType(value != null ? value.toString() : null);
222             fireTableCellUpdated(row, column);
223         }
224         else if (column == LOCKING) {
225             attribute.setUsedForLocking(
226                 (value instanceof Boolean JavaDoc) && ((Boolean JavaDoc) value).booleanValue());
227             fireTableCellUpdated(row, column);
228         }
229         else {
230             DbAttribute dbAttribute = attribute.getDbAttribute();
231             if (column == DB_ATTRIBUTE) {
232                 // If db attrib exist, associate it with obj attribute
233
if (value != null) {
234                     dbAttribute = (DbAttribute) dbEntity.getAttribute(value.toString());
235                     attribute.setDbAttribute(dbAttribute);
236                 }
237                 // If name is erased, remove db attribute from obj attribute.
238
else if (dbAttribute != null) {
239                     attribute.setDbAttribute(null);
240                 }
241             }
242
243             fireTableRowsUpdated(row, row);
244         }
245
246         mediator.fireObjAttributeEvent(event);
247     }
248
249     private boolean isInherited(int row) {
250         ObjAttribute attribute = getAttribute(row);
251         return (attribute != null) ? attribute.getEntity() != entity : false;
252     }
253
254     public boolean isCellEditable(int row, int col) {
255         if (isInherited(row)) {
256             return false;
257         }
258
259         if (dbEntity == null) {
260             return col != DB_ATTRIBUTE_TYPE && col != DB_ATTRIBUTE;
261         }
262
263         return col != DB_ATTRIBUTE_TYPE;
264     }
265
266     public ObjEntity getEntity() {
267         return entity;
268     }
269
270     final class AttributeComparator implements Comparator JavaDoc {
271         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
272             ObjAttribute a1 = (ObjAttribute) o1;
273             ObjAttribute a2 = (ObjAttribute) o2;
274
275             int delta = getWeight(a1) - getWeight(a2);
276
277             return (delta != 0)
278                 ? delta
279                 : Util.nullSafeCompare(true, a1.getName(), a2.getName());
280         }
281
282         private int getWeight(ObjAttribute a) {
283             return a.getEntity() == entity ? 1 : -1;
284         }
285     }
286 }
287
Popular Tags