1 56 package org.objectstyle.cayenne.modeler.editor; 57 58 import java.util.ArrayList ; 59 60 import javax.swing.JOptionPane ; 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 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 IN_PARAMETER = "IN"; 82 public static final String OUT_PARAMETER = "OUT"; 83 public static final String IN_OUT_PARAMETER = "INOUT"; 84 85 public static final String [] PARAMETER_DIRECTION_NAMES = 86 new String [] { "", 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 [] PARAMETER_NAMES = 98 new String [] { "No.", "Name", "Direction", "Type", "Max Length", "Precision" }; 99 100 protected Procedure procedure; 101 102 public ProcedureParameterTableModel( 103 Procedure procedure, 104 ProjectController mediator, 105 Object eventSource) { 106 107 super(mediator, eventSource, new ArrayList (procedure.getCallParameters())); 108 this.procedure = procedure; 109 } 110 111 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 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 ) newVal, parameter); 134 fireTableCellUpdated(rowIndex, columnIndex); 135 break; 136 case PARAMETER_DIRECTION : 137 setParameterDirection((String ) newVal, parameter); 138 break; 139 case PARAMETER_TYPE : 140 setParameterType((String ) newVal, parameter); 141 break; 142 case PARAMETER_LENGTH : 143 setMaxLength((String ) newVal, parameter); 144 break; 145 case PARAMETER_PRECISION : 146 setPrecision((String ) newVal, parameter); 147 break; 148 } 149 mediator.fireProcedureParameterEvent(event); 150 } 151 152 protected void setPrecision(String 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 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 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 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 newVal, ProcedureParameter parameter) { 190 parameter.setType(TypesMapping.getSqlTypeByName(newVal)); 191 } 192 193 protected void setParameterDirection( 194 String 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 newVal, ProcedureParameter parameter) { 211 ProjectUtil.setProcedureParameterName(parameter, newVal.trim()); 212 } 213 214 public Class getElementsClass() { 215 return ProcedureParameter.class; 216 } 217 218 public int getColumnCount() { 219 return PARAMETER_INDEXES.length; 220 } 221 222 public Object 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 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 getParameterPrecision(ProcedureParameter parameter) { 259 return (parameter.getPrecision() >= 0) 260 ? String.valueOf(parameter.getPrecision()) 261 : ""; 262 } 263 264 protected Object getParameterLength(ProcedureParameter parameter) { 265 return (parameter.getMaxLength() >= 0) 266 ? String.valueOf(parameter.getMaxLength()) 267 : ""; 268 } 269 270 protected Object getParameterType(ProcedureParameter parameter) { 271 return TypesMapping.getSqlNameByType(parameter.getType()); 272 } 273 274 protected Object 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 getParameterName(ProcedureParameter parameter) { 289 return parameter.getName(); 290 } 291 292 public String getColumnName(int col) { 293 return PARAMETER_NAMES[col]; 294 } 295 296 public Class getColumnClass(int col) { 297 return String .class; 298 } 299 300 305 public void orderList() { 306 } 308 309 public boolean isCellEditable(int row, int col) { 310 return col != PARAMETER_NUMBER; 311 } 312 } 313 | Popular Tags |