KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
60 import java.util.Iterator JavaDoc;
61
62 import javax.swing.JOptionPane JavaDoc;
63
64 import org.objectstyle.cayenne.dba.TypesMapping;
65 import org.objectstyle.cayenne.map.DbAttribute;
66 import org.objectstyle.cayenne.map.DbEntity;
67 import org.objectstyle.cayenne.map.DbRelationship;
68 import org.objectstyle.cayenne.map.event.AttributeEvent;
69 import org.objectstyle.cayenne.modeler.Application;
70 import org.objectstyle.cayenne.modeler.ProjectController;
71 import org.objectstyle.cayenne.modeler.util.CayenneTableModel;
72 import org.objectstyle.cayenne.modeler.util.ProjectUtil;
73
74 /**
75  * Model for DbEntity attributes. Allows adding/removing attributes, modifying types and
76  * names.
77  *
78  * @author Misha Shengaout
79  * @author Andrei Adamchik
80  */

81 public class DbAttributeTableModel extends CayenneTableModel {
82
83     // Columns
84
private static final int DB_ATTRIBUTE_NAME = 0;
85     private static final int DB_ATTRIBUTE_TYPE = 1;
86     private static final int DB_ATTRIBUTE_PRIMARY_KEY = 2;
87     private static final int DB_ATTRIBUTE_MANDATORY = 3;
88     private static final int DB_ATTRIBUTE_MAX = 4;
89     private static final int DB_ATTRIBUTE_PRECISION = 5;
90     private static final int DB_ATTRIBUTE_GENERATED = 6;
91
92     protected DbEntity entity;
93
94     public DbAttributeTableModel(DbEntity entity, ProjectController mediator,
95             Object JavaDoc eventSource) {
96         this(entity, mediator, eventSource, new ArrayList JavaDoc(entity.getAttributes()));
97         this.entity = entity;
98     }
99
100     public DbAttributeTableModel(DbEntity entity, ProjectController mediator,
101             Object JavaDoc eventSource, java.util.List JavaDoc objectList) {
102         super(mediator, eventSource, objectList);
103     }
104
105     public int nameColumnInd() {
106         return DB_ATTRIBUTE_NAME;
107     }
108
109     public int typeColumnInd() {
110         return DB_ATTRIBUTE_TYPE;
111     }
112
113     public int mandatoryColumnInd() {
114         return DB_ATTRIBUTE_MANDATORY;
115     }
116
117     /**
118      * Returns DbAttribute class.
119      */

120     public Class JavaDoc getElementsClass() {
121         return DbAttribute.class;
122     }
123
124     /**
125      * Returns the number of columns in the table.
126      */

127     public int getColumnCount() {
128         return 7;
129     }
130
131     public DbAttribute getAttribute(int row) {
132         return (row >= 0 && row < objectList.size())
133                 ? (DbAttribute) objectList.get(row)
134                 : null;
135     }
136
137     public String JavaDoc getColumnName(int col) {
138         switch (col) {
139             case DB_ATTRIBUTE_NAME:
140                 return "Name";
141             case DB_ATTRIBUTE_TYPE:
142                 return "Type";
143             case DB_ATTRIBUTE_PRIMARY_KEY:
144                 return "PK";
145             case DB_ATTRIBUTE_PRECISION:
146                 return "Precision";
147             case DB_ATTRIBUTE_MANDATORY:
148                 return "Mandatory";
149             case DB_ATTRIBUTE_MAX:
150                 return "Max Length";
151             case DB_ATTRIBUTE_GENERATED:
152                 return "Generated";
153             default:
154                 return "";
155         }
156     }
157
158     public Class JavaDoc getColumnClass(int col) {
159         switch (col) {
160             case DB_ATTRIBUTE_PRIMARY_KEY:
161             case DB_ATTRIBUTE_MANDATORY:
162             case DB_ATTRIBUTE_GENERATED:
163                 return Boolean JavaDoc.class;
164             default:
165                 return String JavaDoc.class;
166         }
167     }
168
169     public Object JavaDoc getValueAt(int row, int column) {
170         DbAttribute attr = getAttribute(row);
171
172         if (attr == null) {
173             return "";
174         }
175
176         switch (column) {
177             case DB_ATTRIBUTE_NAME:
178                 return getAttributeName(attr);
179             case DB_ATTRIBUTE_TYPE:
180                 return getAttributeType(attr);
181             case DB_ATTRIBUTE_PRIMARY_KEY:
182                 return isPrimaryKey(attr);
183             case DB_ATTRIBUTE_PRECISION:
184                 return getPrecision(attr);
185             case DB_ATTRIBUTE_MANDATORY:
186                 return isMandatory(attr);
187             case DB_ATTRIBUTE_MAX:
188                 return getMaxLength(attr);
189             case DB_ATTRIBUTE_GENERATED:
190                 return isGenerated(attr);
191             default:
192                 return "";
193         }
194     }
195
196     public void setUpdatedValueAt(Object JavaDoc newVal, int row, int col) {
197         DbAttribute attr = getAttribute(row);
198         if (attr == null) {
199             return;
200         }
201
202         AttributeEvent e = new AttributeEvent(eventSource, attr, entity);
203
204         switch (col) {
205             case DB_ATTRIBUTE_NAME:
206                 e.setOldName(attr.getName());
207                 attr.setName((String JavaDoc)newVal);
208                 //setAttributeName((String) newVal, attr);
209
fireTableCellUpdated(row, col);
210                 break;
211             case DB_ATTRIBUTE_TYPE:
212                 setAttributeType((String JavaDoc) newVal, attr);
213                 break;
214             case DB_ATTRIBUTE_PRIMARY_KEY:
215                 if (!setPrimaryKey(((Boolean JavaDoc) newVal), attr, row)) {
216                     return;
217                 }
218                 break;
219             case DB_ATTRIBUTE_PRECISION:
220                 setPrecision((String JavaDoc) newVal, attr);
221                 break;
222             case DB_ATTRIBUTE_MANDATORY:
223                 setMandatory((Boolean JavaDoc) newVal, attr);
224                 break;
225             case DB_ATTRIBUTE_GENERATED:
226                 setGenerated((Boolean JavaDoc) newVal, attr);
227                 break;
228             case DB_ATTRIBUTE_MAX:
229                 setMaxLength((String JavaDoc) newVal, attr);
230                 break;
231         }
232
233         mediator.fireDbAttributeEvent(e);
234     }
235
236     public String JavaDoc getMaxLength(DbAttribute attr) {
237         return (attr.getMaxLength() >= 0) ? String.valueOf(attr.getMaxLength()) : "";
238     }
239
240     public String JavaDoc getAttributeName(DbAttribute attr) {
241         return attr.getName();
242     }
243
244     public String JavaDoc getAttributeType(DbAttribute attr) {
245         return TypesMapping.getSqlNameByType(attr.getType());
246     }
247
248     public String JavaDoc getPrecision(DbAttribute attr) {
249         return (attr.getPrecision() >= 0) ? String.valueOf(attr.getPrecision()) : "";
250     }
251
252     public Boolean JavaDoc isPrimaryKey(DbAttribute attr) {
253         return (attr.isPrimaryKey()) ? Boolean.TRUE : Boolean.FALSE;
254     }
255
256     public Boolean JavaDoc isMandatory(DbAttribute attr) {
257         return (attr.isMandatory()) ? Boolean.TRUE : Boolean.FALSE;
258     }
259
260     public Boolean JavaDoc isGenerated(DbAttribute attr) {
261         return (attr.isGenerated()) ? Boolean.TRUE : Boolean.FALSE;
262     }
263
264     public void setMaxLength(String JavaDoc newVal, DbAttribute attr) {
265         if (newVal == null || newVal.trim().length() <= 0) {
266             attr.setMaxLength(-1);
267         }
268         else {
269             try {
270                 attr.setMaxLength(Integer.parseInt(newVal));
271             }
272             catch (NumberFormatException JavaDoc ex) {
273                 JOptionPane.showMessageDialog(
274                         null,
275                         "Invalid Max Length (" + newVal + "), only numbers are allowed",
276                         "Invalid Maximum Length",
277                         JOptionPane.ERROR_MESSAGE);
278                 return;
279             }
280         }
281     }
282
283     public void setAttributeName(String JavaDoc newVal, DbAttribute attr) {
284         String JavaDoc newName = newVal.trim();
285         ProjectUtil.setAttributeName(attr, newName);
286     }
287
288     public void setAttributeType(String JavaDoc newVal, DbAttribute attr) {
289         attr.setType(TypesMapping.getSqlTypeByName(newVal));
290     }
291
292     public void setPrecision(String JavaDoc newVal, DbAttribute attr) {
293         if (newVal == null || newVal.trim().length() <= 0) {
294             attr.setPrecision(-1);
295         }
296         else {
297             try {
298                 attr.setPrecision(Integer.parseInt(newVal));
299             }
300             catch (NumberFormatException JavaDoc ex) {
301                 JOptionPane.showMessageDialog(
302                         null,
303                         "Invalid precision (" + newVal + "), only numbers are allowed.",
304                         "Invalid Precision Value",
305                         JOptionPane.ERROR_MESSAGE);
306             }
307         }
308     }
309
310     public boolean setPrimaryKey(Boolean JavaDoc newVal, DbAttribute attr, int row) {
311
312         boolean flag = newVal.booleanValue();
313
314         // make sure "to-dep-pk" relationships are fixed when the primary key is unset.
315
if (!flag) {
316             Collection JavaDoc relationships = ProjectUtil
317                     .getRelationshipsUsingAttributeAsTarget(attr);
318             relationships
319                     .addAll(ProjectUtil.getRelationshipsUsingAttributeAsSource(attr));
320
321             if (relationships.size() > 0) {
322                 Iterator JavaDoc it = relationships.iterator();
323                 while (it.hasNext()) {
324                     DbRelationship relationship = (DbRelationship) it.next();
325                     if (!relationship.isToDependentPK()) {
326                         it.remove();
327                     }
328                 }
329
330                 // filtered only those that are to dep PK
331
if (relationships.size() > 0) {
332                     String JavaDoc message = (relationships.size() == 1)
333                             ? "Fix \"To Dep PK\" relationship using this attribute?"
334                             : "Fix "
335                                     + relationships.size()
336                                     + " \"To Dep PK\" relationships using this attribute?";
337
338                     int answer = JOptionPane.showConfirmDialog(
339                             Application.getFrame(),
340                             message);
341                     if (answer != JOptionPane.YES_OPTION) {
342                         // no action needed
343
return false;
344                     }
345
346                     // fix target relationships
347
Iterator JavaDoc fixIt = relationships.iterator();
348                     while (fixIt.hasNext()) {
349                         DbRelationship relationship = (DbRelationship) fixIt.next();
350                         relationship.setToDependentPK(false);
351                     }
352                 }
353             }
354         }
355
356         attr.setPrimaryKey(flag);
357         if (flag) {
358             attr.setMandatory(true);
359             fireTableCellUpdated(row, DB_ATTRIBUTE_MANDATORY);
360         }
361         return true;
362     }
363
364     public void setMandatory(Boolean JavaDoc newVal, DbAttribute attr) {
365         attr.setMandatory(newVal.booleanValue());
366     }
367
368     public void setGenerated(Boolean JavaDoc newVal, DbAttribute attr) {
369         attr.setGenerated(newVal.booleanValue());
370     }
371
372     public boolean isCellEditable(int row, int col) {
373         DbAttribute attrib = getAttribute(row);
374         if (null == attrib) {
375             return false;
376         }
377         else if (col == mandatoryColumnInd()) {
378             if (attrib.isPrimaryKey()) {
379                 return false;
380             }
381         }
382         return true;
383     }
384 }
Popular Tags