KickJava   Java API By Example, From Geeks To Geeks.

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


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
60 import javax.swing.JOptionPane JavaDoc;
61
62 import org.objectstyle.cayenne.map.DbEntity;
63 import org.objectstyle.cayenne.map.DbRelationship;
64 import org.objectstyle.cayenne.map.Relationship;
65 import org.objectstyle.cayenne.map.event.RelationshipEvent;
66 import org.objectstyle.cayenne.modeler.Application;
67 import org.objectstyle.cayenne.modeler.ProjectController;
68 import org.objectstyle.cayenne.modeler.util.CayenneTableModel;
69 //import org.objectstyle.cayenne.modeler.util.ProjectUtil;
70

71 /**
72  * Table model for DbRelationship table.
73  *
74  * @author Michael Misha Shengaout
75  * @author Andrei Adamchik
76  */

77 public class DbRelationshipTableModel extends CayenneTableModel {
78
79     // Columns
80
static final int NAME = 0;
81     static final int TARGET = 1;
82     static final int TO_DEPENDENT_KEY = 2;
83     static final int CARDINALITY = 3;
84
85     protected DbEntity entity;
86
87     public DbRelationshipTableModel(DbEntity entity, ProjectController mediator,
88             Object JavaDoc eventSource) {
89
90         super(mediator, eventSource, new ArrayList JavaDoc(entity.getRelationships()));
91         this.entity = entity;
92     }
93
94     /**
95      * Returns DbRelationship class.
96      */

97     public Class JavaDoc getElementsClass() {
98         return DbRelationship.class;
99     }
100
101     public int getColumnCount() {
102         return 4;
103     }
104
105     public String JavaDoc getColumnName(int col) {
106         switch (col) {
107             case NAME:
108                 return "Name";
109             case TARGET:
110                 return "Target";
111             case TO_DEPENDENT_KEY:
112                 return "To Dep PK";
113             case CARDINALITY:
114                 return "To Many";
115             default:
116                 return null;
117         }
118     }
119
120     public Class JavaDoc getColumnClass(int col) {
121         switch (col) {
122             case TARGET:
123                 return DbEntity.class;
124             case TO_DEPENDENT_KEY:
125             case CARDINALITY:
126                 return Boolean JavaDoc.class;
127             default:
128                 return String JavaDoc.class;
129         }
130     }
131
132     public DbRelationship getRelationship(int row) {
133         return (row >= 0 && row < objectList.size()) ? (DbRelationship) objectList
134                 .get(row) : null;
135     }
136
137     public Object JavaDoc getValueAt(int row, int col) {
138         DbRelationship rel = getRelationship(row);
139         if (rel == null) {
140             return null;
141         }
142
143         switch (col) {
144             case NAME:
145                 return rel.getName();
146             case TARGET:
147                 return rel.getTargetEntity();
148             case TO_DEPENDENT_KEY:
149                 return rel.isToDependentPK() ? Boolean.TRUE : Boolean.FALSE;
150             case CARDINALITY:
151                 return rel.isToMany() ? Boolean.TRUE : Boolean.FALSE;
152             default:
153                 return null;
154         }
155     }
156
157     public void setUpdatedValueAt(Object JavaDoc aValue, int row, int column) {
158
159         DbRelationship rel = getRelationship(row);
160         // If name column
161
if (column == NAME) {
162             RelationshipEvent e = new RelationshipEvent(
163                     eventSource,
164                     rel,
165                     entity,
166                     rel.getName());
167             rel.setName((String JavaDoc)aValue);
168             //String text = (String) aValue;
169
//ProjectUtil.setRelationshipName(entity, rel, text);
170
mediator.fireDbRelationshipEvent(e);
171             fireTableCellUpdated(row, column);
172         }
173         // If target column
174
else if (column == TARGET) {
175             DbEntity target = (DbEntity) aValue;
176
177             // clear joins...
178
rel.removeAllJoins();
179             rel.setTargetEntity(target);
180
181             RelationshipEvent e = new RelationshipEvent(eventSource, rel, entity);
182             mediator.fireDbRelationshipEvent(e);
183         }
184         else if (column == TO_DEPENDENT_KEY) {
185             boolean flag = ((Boolean JavaDoc) aValue).booleanValue();
186
187             // make sure reverse relationship "to-dep-pk" is unset.
188
if (flag) {
189                 DbRelationship reverse = rel.getReverseRelationship();
190                 if (reverse != null && reverse.isToDependentPK()) {
191                     String JavaDoc message = "Unset reverse relationship's \"To Dep PK\" setting?";
192                     int answer = JOptionPane.showConfirmDialog(Application
193                             .getFrame(), message);
194                     if (answer != JOptionPane.YES_OPTION) {
195                         // no action needed
196
return;
197                     }
198
199                     // unset reverse
200
reverse.setToDependentPK(false);
201                 }
202             }
203
204             rel.setToDependentPK(flag);
205             RelationshipEvent e = new RelationshipEvent(eventSource, rel, entity);
206             mediator.fireDbRelationshipEvent(e);
207         }
208         else if (column == CARDINALITY) {
209             Boolean JavaDoc temp = (Boolean JavaDoc) aValue;
210             rel.setToMany(temp.booleanValue());
211             RelationshipEvent e = new RelationshipEvent(eventSource, rel, entity);
212             mediator.fireDbRelationshipEvent(e);
213         }
214         fireTableRowsUpdated(row, row);
215     }
216
217     /**
218      * Relationship just needs to be removed from the model. It is already removed from
219      * the DataMap.
220      */

221     void removeRelationship(Relationship rel) {
222         objectList.remove(rel);
223         fireTableDataChanged();
224     }
225
226     public boolean isCellEditable(int row, int col) {
227         DbRelationship rel = getRelationship(row);
228         if (rel == null) {
229             return false;
230         }
231         else if (col == TO_DEPENDENT_KEY) {
232             return rel.isValidForDepPk();
233         }
234         return true;
235     }
236 }
Popular Tags