View Javadoc
1 /*** 2 * $Header: /cvsroot/ebxmlrr/jaxr/src/com/sun/xml/registry/client/browser/RegistryObjectsTableModel.java,v 1.21 2003/08/27 21:27:15 farrukh_najmi Exp $ 3 */ 4 package com.sun.xml.registry.client.browser; 5 6 import java.util.*; 7 import java.awt.*; 8 import java.awt.event.*; 9 import java.lang.reflect.*; 10 import java.net.URL; 11 12 import javax.swing.*; 13 import javax.swing.table.*; 14 import javax.swing.tree.*; 15 import javax.xml.registry.*; 16 import javax.xml.registry.infomodel.*; 17 import com.sun.xml.registry.ebxml.infomodel.*; 18 import com.sun.xml.registry.ebxml.util.ProviderProperties; 19 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 import java.util.TreeMap; 23 24 import com.sun.xml.registry.client.browser.conf.bindings.*; 25 26 /*** 27 * A JTable that lists 28 * 29 * @author Jim Glennon 30 * 31 * @author <a href="mailto:Farrukh.Najmi@Sun.COM">Farrukh S. Najmi</a> 32 */ 33 public class RegistryObjectsTableModel extends AbstractTableModel { 34 35 private static final Log log = 36 LogFactory.getLog(RegistryObjectsTableModel.class); 37 38 ArrayList registryObjects = new ArrayList(); 39 RegistryBrowser registryBrowser; 40 HashMap objectTypeToConfigMap = new HashMap(); 41 42 //Current configuration. Changes for each search 43 ObjectTypeConfig otCfg = null; 44 SearchResultsConfig srCfg = null; 45 SearchResultsColumn[] srCols = null; 46 47 48 /*** 49 * Default Constructor 50 */ 51 public RegistryObjectsTableModel() { 52 registryBrowser = RegistryBrowser.getInstance(); 53 loadConfiguration(); 54 } 55 56 private void loadConfiguration() { 57 Configuration cfg = RegistryBrowser.getInstance().getConfiguration(); 58 59 int count = cfg.getObjectTypeConfigCount(); 60 ObjectTypeConfig[] otCfgs = cfg.getObjectTypeConfig(); 61 62 for (int i=0; i<count; i++) { 63 ObjectTypeConfig otCfg = otCfgs[i]; 64 String id = otCfg.getId(); 65 objectTypeToConfigMap.put(id, otCfg); 66 } 67 } 68 69 70 /*** 71 * Get the number of columns in model 72 * 73 * @return number of columns 74 * 75 * @see 76 */ 77 public int getColumnCount() { 78 int cols = 0; 79 if (srCols != null) { 80 cols = srCols.length; 81 } 82 return cols; 83 } 84 85 86 /*** 87 * Get the width of column as a % of the viewport width 88 * 89 * @return string representation of column 90 * 91 */ 92 public int getColumnWidth(int col) { 93 int width = 10; 94 95 SearchResultsColumnType srCol = srCols[col]; 96 width = srCol.getColumnWidth(); 97 98 return width; 99 } 100 101 /*** 102 * Gets the number of rows in the table model. 103 * 104 */ 105 public int getRowCount() { 106 return registryObjects.size(); 107 } 108 109 /*** 110 * Gets the Class for specified column based upon dynamic configuration. 111 */ 112 public Class getColumnClass(int column) { 113 Class clazz = null; 114 115 try { 116 SearchResultsColumnType srCol = srCols[column]; 117 String columnClass = srCol.getColumnClass(); 118 if (columnClass == null) { 119 columnClass = "java.lang.Object"; 120 } 121 clazz = Class.forName(columnClass); 122 123 } 124 catch (ClassNotFoundException e) { 125 RegistryBrowser.displayError(e); 126 } 127 128 return clazz; 129 } 130 131 /* 132 * Gets whether a cell in table is editable or not. 133 */ 134 public boolean isCellEditable(int row, int col) { 135 boolean editable=false; 136 137 SearchResultsColumnType srCol = srCols[col]; 138 editable = srCol.getEditable(); 139 140 return editable; 141 } 142 143 /*** 144 * Get value at specified cell in table. 145 * 146 * @param row the row for cell 147 * @param col the column for cell 148 * 149 * @return value for cell 150 * 151 */ 152 public Object getValueAt(int row, int col) { 153 RegistryObject ro = (RegistryObject)registryObjects.get(row); 154 Object value = null; 155 try 156 { 157 switch (col) 158 { 159 case -1: 160 //Special invisible column that contains the actual object 161 value = ro; 162 break; 163 default: 164 SearchResultsColumnType srCol = srCols[col]; 165 String className = otCfg.getClassName(); 166 String methodName = srCol.getMethod(); 167 Class clazz = Class.forName(className); 168 169 int numParams = srCol.getMethodParameterCount(); 170 Class[] parameterTypes = new Class[numParams]; 171 172 //Setup parameterTypes 173 for (int i=0; i<numParams; i++) { 174 MethodParameter mp = srCol.getMethodParameter(i); 175 String paramTypeName = mp.getType(); 176 parameterTypes[i] = Class.forName(paramTypeName); 177 } 178 179 Method method = clazz.getMethod(methodName, parameterTypes); 180 181 Object[] parameterValues = new Object[numParams]; 182 183 //Setup parameterValues 184 for (int i=0; i<numParams; i++) { 185 MethodParameter mp = srCol.getMethodParameter(i); 186 String paramValue = mp.getValue(); 187 parameterValues[i] = paramValue; 188 } 189 190 //Invoke method to get Value as object. Convert the object to a format suitable for display 191 value = method.invoke(ro, parameterValues); 192 } 193 } 194 catch (ClassNotFoundException e) { 195 RegistryBrowser.displayError(e); 196 } 197 catch (NoSuchMethodException e) { 198 RegistryBrowser.displayError("Invalid configuration: No such method:", e); 199 } 200 catch (IllegalArgumentException e) { 201 RegistryBrowser.displayError(e); 202 } 203 catch (IllegalAccessException e) { 204 RegistryBrowser.displayError(e); 205 } 206 catch (InvocationTargetException e) { 207 //Commented because we were getting wierd swing NPEs 208 //RegistryBrowser.displayError(e.getCause()); 209 } 210 catch (ExceptionInInitializerError e) { 211 RegistryBrowser.displayError(e); 212 } 213 214 return value; 215 } 216 217 /*** 218 * Method Declaration. 219 * 220 * 221 * @param col 222 * 223 * @return 224 * 225 * @see 226 */ 227 public String getColumnName(int col) { 228 String columnName = null; 229 try 230 { 231 columnName = srCols[col].getColumnHeader(); 232 } 233 catch (IndexOutOfBoundsException ex) 234 { 235 log.warn("Index out of bounds"+ex.getMessage()); 236 } 237 return columnName; 238 } 239 240 241 void update(BulkResponse response) { 242 String errMsg = "An error occured in the registry during the request."; 243 Collection registryObjects = null; 244 try { 245 246 // check for errors 247 Collection exceptions = response.getExceptions(); 248 249 if (exceptions != null) { 250 Iterator iter = exceptions.iterator(); 251 Exception exception = null; 252 253 while (iter.hasNext()) { 254 exception = (Exception) iter.next(); 255 RegistryBrowser.displayError(errMsg, exception); 256 } 257 } 258 259 // collection may be empty if there were errors 260 registryObjects = response.getCollection(); 261 262 //Get the most specific object type that is common to all RegistryObjects 263 Concept commonObjectType = this.getCommonObjectType(registryObjects); 264 265 //Dynamically update model configuration basd upon objectType 266 updateConfiguration(commonObjectType); 267 } 268 catch (JAXRException e) { 269 RegistryBrowser.displayError(e); 270 registryObjects = new ArrayList(); 271 } 272 273 if (registryObjects.isEmpty()) { 274 JOptionPane.showMessageDialog(null, "No objects were found.", 275 "Registry Browser", 276 JOptionPane.INFORMATION_MESSAGE); 277 registryObjects = new ArrayList(); 278 } 279 setRegistryObjects(registryObjects); 280 } 281 282 /* 283 * used by table inside action listener 284 */ 285 286 /*** 287 * Method Declaration. 288 * 289 * 290 * @return 291 * 292 * @see 293 */ 294 ArrayList getRegistryObjects() { 295 return registryObjects; 296 } 297 298 /*** 299 * Method Declaration. 300 * 301 * 302 * @param objs 303 * 304 * @see 305 */ 306 void setRegistryObjects(Collection objs) { 307 registryObjects.clear(); 308 registryObjects.addAll(objs); 309 fireTableDataChanged(); 310 } 311 312 /*** 313 * Gets the most specific ObjectType that is common to all RegistryObjects. 314 */ 315 private Concept getCommonObjectType(Collection registryObjects) throws JAXRException { 316 Concept commonType=null; 317 String commonPath = null; 318 319 Iterator iter = registryObjects.iterator(); 320 while (iter.hasNext()) { 321 RegistryObject ro = (RegistryObject)iter.next(); 322 com.sun.xml.registry.ebxml.infomodel.ConceptImpl type = 323 (com.sun.xml.registry.ebxml.infomodel.ConceptImpl)ro.getObjectType(); 324 325 String path = type.getPath(); 326 327 if (commonPath == null) { 328 commonPath = path; 329 } 330 else { 331 if (!(commonPath.equals(path))) { 332 //Determine common base type for both: 333 if (commonPath.startsWith(path+"/")) { 334 //The new type is a baseType of current commonType 335 //Set commonType to new type 336 commonPath = path; 337 } 338 else if (path.startsWith(commonPath+"/")) { 339 //The current commonType is a baseType of new type 340 //Leave commonType unchanged 341 continue; 342 } 343 else { 344 //The current commonType and new type 345 //do not have an ancestor/descendant relationship 346 //Find a common base type between them. 347 348 String smallerPath = commonPath; 349 String biggerPath = path; 350 boolean swap = false; 351 if (commonPath.length() > path.length()) { 352 smallerPath = path; 353 biggerPath = commonPath; 354 swap = true; 355 } 356 357 int len = smallerPath.length(); 358 for (int i=0; i<len; i++) { 359 if (smallerPath.charAt(i) != biggerPath.charAt(i)) { 360 361 commonPath = smallerPath.substring(0, smallerPath.lastIndexOf('/', i)); 362 break; 363 } 364 } 365 } 366 } 367 } 368 } 369 370 if (commonPath == null) { 371 //Use RegistryObject path 372 commonPath = "/urn:uuid:3188a449-18ac-41fb-be9f-99a1adca02cb/RegistryObject"; 373 } 374 375 JAXRClient client = RegistryBrowser.getInstance().getClient(); 376 javax.xml.registry.Connection connection = RegistryBrowser.getInstance().client.getConnection(); 377 javax.xml.registry.RegistryService service = connection.getRegistryService(); 378 BusinessQueryManager bqm = service.getBusinessQueryManager(); 379 commonType = (Concept)bqm.findConceptByPath(commonPath); 380 return commonType; 381 } 382 383 /*** 384 * Gets the ObjectTypeConfig for specified ObjectType. 385 * If exact match is not found then gets the config for the closest ancestor. 386 * 387 **/ 388 private ObjectTypeConfig getObjectTypeConfig(Concept commonObjectType) throws JAXRException { 389 ObjectTypeConfig cfg = null; 390 while (true) { 391 //Now get the ObjectTypeConfig for the commonObjectType 392 cfg = (ObjectTypeConfig)objectTypeToConfigMap.get(commonObjectType.getKey().getId()); 393 if (cfg != null) { 394 break; 395 } 396 else { 397 Concept parent = commonObjectType.getParentConcept(); 398 if (parent == null) { 399 //Use RegistryObject (id urn:uuid:a7ec3db9-9342-4016-820c-cff66c0bb021) as default objectType 400 cfg = (ObjectTypeConfig)objectTypeToConfigMap.get("urn:uuid:a7ec3db9-9342-4016-820c-cff66c0bb021"); 401 break; 402 } 403 else { 404 commonObjectType = parent; 405 } 406 } 407 } 408 409 return cfg; 410 } 411 412 /*** 413 * Update the objectType specific configuration based upon specified objectType. 414 * Dynamically called each time a update is called. 415 */ 416 private void updateConfiguration(Concept objectType) throws JAXRException { 417 //Get the most specific ObjectTypeConfig for the most specific commonObjectType 418 otCfg = getObjectTypeConfig(objectType); 419 srCfg = otCfg.getSearchResultsConfig(); 420 srCols = srCfg.getSearchResultsColumn(); 421 fireTableStructureChanged(); 422 } 423 424 }

This page was automatically generated by Maven