KickJava   Java API By Example, From Geeks To Geeks.

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


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.objectstyle.cayenne.map.DbEntity;
63 import org.objectstyle.cayenne.map.DbRelationship;
64 import org.objectstyle.cayenne.map.DeleteRule;
65 import org.objectstyle.cayenne.map.ObjEntity;
66 import org.objectstyle.cayenne.map.ObjRelationship;
67 import org.objectstyle.cayenne.map.Relationship;
68 import org.objectstyle.cayenne.map.event.RelationshipEvent;
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  * Table model to display ObjRelationships.
76  *
77  * @author Misha Shengaout
78  * @author Andrei Adamchik
79  */

80 public class ObjRelationshipTableModel extends CayenneTableModel {
81     // Columns
82
static final int REL_NAME = 0;
83     static final int REL_TARGET = 1;
84     static final int REL_CARDINALITY = 2;
85     static final int REL_DELETERULE = 3;
86     static final int REL_LOCKING = 4;
87
88     protected ObjEntity entity;
89
90     public ObjRelationshipTableModel(
91         ObjEntity entity,
92         ProjectController mediator,
93         Object JavaDoc eventSource) {
94         super(mediator, eventSource, new ArrayList JavaDoc(entity.getRelationships()));
95         this.entity = entity;
96
97         // order using local comparator
98
Collections.sort(objectList, new RelationshipComparator());
99     }
100
101     protected void orderList() {
102         // NOOP
103
}
104
105     public ObjEntity getEntity() {
106         return entity;
107     }
108
109     /**
110      * Returns ObjRelationship class.
111      */

112     public Class JavaDoc getElementsClass() {
113         return ObjRelationship.class;
114     }
115
116     public int getColumnCount() {
117         return 5;
118     }
119
120     public String JavaDoc getColumnName(int column) {
121         switch (column) {
122             case REL_NAME :
123                 return "Name";
124             case REL_TARGET :
125                 return "Target";
126             case REL_LOCKING :
127                 return "Used for Locking";
128             case REL_CARDINALITY :
129                 return "To Many";
130             case REL_DELETERULE :
131                 return "Delete Rule";
132
133             default :
134                 return null;
135         }
136     }
137
138     public Class JavaDoc getColumnClass(int col) {
139         switch (col) {
140             case REL_TARGET :
141                 return ObjEntity.class;
142             case REL_CARDINALITY :
143             case REL_LOCKING :
144                 return Boolean JavaDoc.class;
145             default :
146                 return String JavaDoc.class;
147         }
148     }
149
150     public ObjRelationship getRelationship(int row) {
151         return (row >= 0 && row < objectList.size())
152             ? (ObjRelationship) objectList.get(row)
153             : null;
154     }
155
156     public Object JavaDoc getValueAt(int row, int column) {
157         ObjRelationship rel = getRelationship(row);
158
159         if (column == REL_NAME) {
160             return rel.getName();
161         }
162         else if (column == REL_TARGET) {
163             return rel.getTargetEntity();
164         }
165         else if (column == REL_LOCKING) {
166             return rel.isUsedForLocking() ? Boolean.TRUE : Boolean.FALSE;
167         }
168         else if (column == REL_CARDINALITY) {
169             return rel.isToMany() ? Boolean.TRUE : Boolean.FALSE;
170         }
171         else if (column == REL_DELETERULE) {
172             return DeleteRule.deleteRuleName(rel.getDeleteRule());
173         }
174         else {
175             return null;
176         }
177     }
178
179     public void setUpdatedValueAt(Object JavaDoc value, int row, int column) {
180         ObjRelationship relationship = getRelationship(row);
181         RelationshipEvent event =
182             new RelationshipEvent(eventSource, relationship, entity);
183
184         if (column == REL_NAME) {
185             String JavaDoc text = (String JavaDoc) value;
186             event.setOldName(relationship.getName());
187             ProjectUtil.setRelationshipName(entity, relationship, text);
188             fireTableCellUpdated(row, column);
189         }
190         else if (column == REL_TARGET) {
191             ObjEntity target = (ObjEntity) value;
192             relationship.setTargetEntity(target);
193
194             // now try to connect DbEntities if we can do it in one step
195
if (target != null) {
196                 DbEntity srcDB =
197                     ((ObjEntity) relationship.getSourceEntity()).getDbEntity();
198                 DbEntity targetDB = target.getDbEntity();
199                 if (srcDB != null && targetDB != null) {
200                     Relationship anyConnector = srcDB.getAnyRelationship(targetDB);
201                     if (anyConnector != null) {
202                         relationship.addDbRelationship((DbRelationship) anyConnector);
203                     }
204                 }
205             }
206
207             fireTableRowsUpdated(row, row);
208         }
209         else if (column == REL_DELETERULE) {
210             relationship.setDeleteRule(DeleteRule.deleteRuleForName((String JavaDoc) value));
211             fireTableCellUpdated(row, column);
212         }
213         else if (column == REL_LOCKING) {
214             relationship.setUsedForLocking(
215                 (value instanceof Boolean JavaDoc) && ((Boolean JavaDoc) value).booleanValue());
216             fireTableCellUpdated(row, column);
217         }
218
219         mediator.fireObjRelationshipEvent(event);
220     }
221
222     public void removeRow(int row) {
223         if (row < 0)
224             return;
225         Relationship rel = getRelationship(row);
226         RelationshipEvent e;
227         e = new RelationshipEvent(eventSource, rel, entity, RelationshipEvent.REMOVE);
228         mediator.fireObjRelationshipEvent(e);
229         objectList.remove(row);
230         entity.removeRelationship(rel.getName());
231         fireTableRowsDeleted(row, row);
232     }
233
234     private boolean isInherited(int row) {
235         ObjRelationship relationship = getRelationship(row);
236         return (relationship != null) ? relationship.getSourceEntity() != entity : false;
237     }
238
239     public boolean isCellEditable(int row, int col) {
240         return !isInherited(row) && col != REL_CARDINALITY;
241     }
242
243     final class RelationshipComparator implements Comparator JavaDoc {
244         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
245             ObjRelationship r1 = (ObjRelationship) o1;
246             ObjRelationship r2 = (ObjRelationship) o2;
247
248             int delta = getWeight(r1) - getWeight(r2);
249
250             return (delta != 0)
251                 ? delta
252                 : Util.nullSafeCompare(true, r1.getName(), r2.getName());
253         }
254
255         private int getWeight(ObjRelationship r) {
256             return r.getSourceEntity() == entity ? 1 : -1;
257         }
258     }
259 }
260
Popular Tags