KickJava   Java API By Example, From Geeks To Geeks.

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


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.dba.TypesMapping;
63 import org.objectstyle.cayenne.map.Procedure;
64 import org.objectstyle.cayenne.map.ProcedureParameter;
65 import org.objectstyle.cayenne.map.event.ProcedureParameterEvent;
66 import org.objectstyle.cayenne.modeler.ProjectController;
67 import org.objectstyle.cayenne.modeler.util.CayenneTableModel;
68 import org.objectstyle.cayenne.modeler.util.ProjectUtil;
69
70 /**
71  * @author Andrei Adamchik
72  */

73 public class ProcedureParameterTableModel extends CayenneTableModel {
74     public static final int PARAMETER_NUMBER = 0;
75     public static final int PARAMETER_NAME = 1;
76     public static final int PARAMETER_DIRECTION = 2;
77     public static final int PARAMETER_TYPE = 3;
78     public static final int PARAMETER_LENGTH = 4;
79     public static final int PARAMETER_PRECISION = 5;
80
81     public static final String JavaDoc IN_PARAMETER = "IN";
82     public static final String JavaDoc OUT_PARAMETER = "OUT";
83     public static final String JavaDoc IN_OUT_PARAMETER = "INOUT";
84
85     public static final String JavaDoc[] PARAMETER_DIRECTION_NAMES =
86         new String JavaDoc[] { "", IN_PARAMETER, OUT_PARAMETER, IN_OUT_PARAMETER };
87
88     private static final int[] PARAMETER_INDEXES =
89         new int[] {
90             PARAMETER_NUMBER,
91             PARAMETER_NAME,
92             PARAMETER_DIRECTION,
93             PARAMETER_TYPE,
94             PARAMETER_LENGTH,
95             PARAMETER_PRECISION };
96
97     private static final String JavaDoc[] PARAMETER_NAMES =
98         new String JavaDoc[] { "No.", "Name", "Direction", "Type", "Max Length", "Precision" };
99
100     protected Procedure procedure;
101
102     public ProcedureParameterTableModel(
103         Procedure procedure,
104         ProjectController mediator,
105         Object JavaDoc eventSource) {
106
107         super(mediator, eventSource, new ArrayList JavaDoc(procedure.getCallParameters()));
108         this.procedure = procedure;
109     }
110
111     /**
112      * Returns procedure parameter at the specified row.
113      * Returns NULL if row index is outside the valid range.
114      */

115     public ProcedureParameter getParameter(int row) {
116         return (row >= 0 && row < objectList.size())
117             ? (ProcedureParameter) objectList.get(row)
118             : null;
119     }
120
121     public void setUpdatedValueAt(Object JavaDoc newVal, int rowIndex, int columnIndex) {
122         ProcedureParameter parameter = getParameter(rowIndex);
123
124         if (parameter == null) {
125             return;
126         }
127
128         ProcedureParameterEvent event =
129             new ProcedureParameterEvent(eventSource, parameter);
130         switch (columnIndex) {
131             case PARAMETER_NAME :
132                 event.setOldName(parameter.getName());
133                 setParameterName((String JavaDoc) newVal, parameter);
134                 fireTableCellUpdated(rowIndex, columnIndex);
135                 break;
136             case PARAMETER_DIRECTION :
137                 setParameterDirection((String JavaDoc) newVal, parameter);
138                 break;
139             case PARAMETER_TYPE :
140                 setParameterType((String JavaDoc) newVal, parameter);
141                 break;
142             case PARAMETER_LENGTH :
143                 setMaxLength((String JavaDoc) newVal, parameter);
144                 break;
145             case PARAMETER_PRECISION :
146                 setPrecision((String JavaDoc) newVal, parameter);
147                 break;
148         }
149         mediator.fireProcedureParameterEvent(event);
150     }
151
152     protected void setPrecision(String JavaDoc newVal, ProcedureParameter parameter) {
153         if (newVal == null || newVal.trim().length() <= 0) {
154             parameter.setPrecision(-1);
155         }
156         else {
157             try {
158                 parameter.setPrecision(Integer.parseInt(newVal));
159             }
160             catch (NumberFormatException JavaDoc ex) {
161                 JOptionPane.showMessageDialog(
162                     null,
163                     "Invalid precision (" + newVal + "), only numbers are allowed.",
164                     "Invalid Precision Value",
165                     JOptionPane.ERROR_MESSAGE);
166             }
167         }
168     }
169
170     protected void setMaxLength(String JavaDoc newVal, ProcedureParameter parameter) {
171         if (newVal == null || newVal.trim().length() <= 0) {
172             parameter.setMaxLength(-1);
173         }
174         else {
175             try {
176                 parameter.setMaxLength(Integer.parseInt(newVal));
177             }
178             catch (NumberFormatException JavaDoc ex) {
179                 JOptionPane.showMessageDialog(
180                     null,
181                     "Invalid Max Length (" + newVal + "), only numbers are allowed",
182                     "Invalid Maximum Length",
183                     JOptionPane.ERROR_MESSAGE);
184                 return;
185             }
186         }
187     }
188
189     protected void setParameterType(String JavaDoc newVal, ProcedureParameter parameter) {
190         parameter.setType(TypesMapping.getSqlTypeByName(newVal));
191     }
192
193     protected void setParameterDirection(
194         String JavaDoc direction,
195         ProcedureParameter parameter) {
196         if (ProcedureParameterTableModel.IN_PARAMETER.equals(direction)) {
197             parameter.setDirection(ProcedureParameter.IN_PARAMETER);
198         }
199         else if (ProcedureParameterTableModel.OUT_PARAMETER.equals(direction)) {
200             parameter.setDirection(ProcedureParameter.OUT_PARAMETER);
201         }
202         else if (ProcedureParameterTableModel.IN_OUT_PARAMETER.equals(direction)) {
203             parameter.setDirection(ProcedureParameter.IN_OUT_PARAMETER);
204         }
205         else {
206             parameter.setDirection(-1);
207         }
208     }
209
210     protected void setParameterName(String JavaDoc newVal, ProcedureParameter parameter) {
211         ProjectUtil.setProcedureParameterName(parameter, newVal.trim());
212     }
213
214     public Class JavaDoc getElementsClass() {
215         return ProcedureParameter.class;
216     }
217
218     public int getColumnCount() {
219         return PARAMETER_INDEXES.length;
220     }
221
222     public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
223         ProcedureParameter parameter = getParameter(rowIndex);
224
225         if (parameter == null) {
226             return "";
227         }
228
229         switch (columnIndex) {
230             case PARAMETER_NUMBER :
231                 return getParameterNumber(rowIndex, parameter);
232             case PARAMETER_NAME :
233                 return getParameterName(parameter);
234             case PARAMETER_DIRECTION :
235                 return getParameterDirection(parameter);
236             case PARAMETER_TYPE :
237                 return getParameterType(parameter);
238             case PARAMETER_LENGTH :
239                 return getParameterLength(parameter);
240             case PARAMETER_PRECISION :
241                 return getParameterPrecision(parameter);
242             default :
243                 return "";
244         }
245     }
246
247     protected Object JavaDoc getParameterNumber(int rowIndex, ProcedureParameter parameter) {
248         boolean hasReturnValue = parameter.getProcedure().isReturningValue();
249
250         if (hasReturnValue) {
251             return (rowIndex == 0) ? "R" : "" + rowIndex;
252         }
253         else {
254             return "" + (rowIndex + 1);
255         }
256     }
257
258     protected Object JavaDoc getParameterPrecision(ProcedureParameter parameter) {
259         return (parameter.getPrecision() >= 0)
260             ? String.valueOf(parameter.getPrecision())
261             : "";
262     }
263
264     protected Object JavaDoc getParameterLength(ProcedureParameter parameter) {
265         return (parameter.getMaxLength() >= 0)
266             ? String.valueOf(parameter.getMaxLength())
267             : "";
268     }
269
270     protected Object JavaDoc getParameterType(ProcedureParameter parameter) {
271         return TypesMapping.getSqlNameByType(parameter.getType());
272     }
273
274     protected Object JavaDoc getParameterDirection(ProcedureParameter parameter) {
275         int direction = parameter.getDirection();
276         switch (direction) {
277             case ProcedureParameter.IN_PARAMETER :
278                 return ProcedureParameterTableModel.IN_PARAMETER;
279             case ProcedureParameter.OUT_PARAMETER :
280                 return ProcedureParameterTableModel.OUT_PARAMETER;
281             case ProcedureParameter.IN_OUT_PARAMETER :
282                 return ProcedureParameterTableModel.IN_OUT_PARAMETER;
283             default :
284                 return "";
285         }
286     }
287
288     protected Object JavaDoc getParameterName(ProcedureParameter parameter) {
289         return parameter.getName();
290     }
291
292     public String JavaDoc getColumnName(int col) {
293         return PARAMETER_NAMES[col];
294     }
295
296     public Class JavaDoc getColumnClass(int col) {
297         return String JavaDoc.class;
298     }
299
300     /**
301      * Suppressed ordering operations defined in a superclass.
302      * Since stored procedure parameters are positional,
303      * no reordering is allowed.
304      */

305     public void orderList() {
306         // NOOP
307
}
308
309     public boolean isCellEditable(int row, int col) {
310         return col != PARAMETER_NUMBER;
311     }
312 }
313
Popular Tags