1 30 package com.genimen.djeneric.structure; 31 32 import com.genimen.djeneric.language.Messages; 33 import com.genimen.djeneric.repository.DjDomain; 34 import com.genimen.djeneric.repository.DjExtent; 35 import com.genimen.djeneric.repository.DjProperty; 36 import com.genimen.djeneric.repository.exceptions.CatalogException; 37 import com.genimen.djeneric.repository.exceptions.DjenericException; 38 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException; 39 import com.genimen.djeneric.util.DjLogger; 40 41 46 public class PropertyUsage implements Cloneable 47 { 48 DjExtent _extent; 49 DjProperty _property; 50 String _prompt; 51 String _alias = null; 52 String _defaultValue = null; 53 String _expression = null; 54 String _description = null; 55 String _customEditorClass = null; 56 String _relativePath = null; 57 58 int _componentType = DjDomain.COMP_TEXTFIELD; 59 int _seq; 60 int _sortOrder; 61 62 boolean _update = true; 63 boolean _required = false; 64 boolean _query = true; 65 boolean _displayed = true; 66 67 int _displayWidth = 0; 68 int _displayHeight = 0; 69 70 76 public PropertyUsage(DjProperty property) 77 { 78 _extent = property.getExtent(); 79 _property = property; 80 81 _prompt = property.getPrompt(); 82 _required = property.isRequired(); 83 _seq = property.getSeq(); 84 _query = property.isQueryable(); 85 _displayed = true; 86 87 if (property.getType() instanceof DjDomain) 88 { 89 DjDomain dom = (DjDomain) property.getType(); 90 _displayHeight = dom.getDisplayHeight(); 91 _displayWidth = dom.getDisplayWidth(); 92 _componentType = dom.getComponentType(); 93 } 94 else if (property.getType() instanceof DjExtent) 95 { 96 _componentType = DjDomain.COMP_COMBOBOX; 97 _displayWidth = 150; 98 } 99 } 100 101 public Object clone() throws CloneNotSupportedException 102 { 103 return super.clone(); 104 } 105 106 public boolean equals(Object obj) 107 { 108 if (obj == this) return true; 109 if (!(obj instanceof PropertyUsage)) return false; 110 PropertyUsage other = (PropertyUsage) obj; 111 112 return _extent == other._extent && safeCompare(_property, other._property) && safeCompare(_prompt, other._prompt) 113 && safeCompare(_alias, other._alias) && safeCompare(_defaultValue, other._defaultValue) 114 && safeCompare(_expression, other._expression) && safeCompare(_description, other._description) 115 && safeCompare(_customEditorClass, other._customEditorClass) 116 && safeCompare(_relativePath, other._relativePath) && _componentType == other._componentType 117 && _seq == other._seq && _sortOrder == other._sortOrder && _update == other._update 118 && _required == other._required && _query == other._query && _displayed == other._displayed 119 && _displayWidth == other._displayWidth && _displayHeight == other._displayHeight; 120 } 121 122 public int hashCode() 123 { 124 int result = 0; 125 if (_extent != null) result += _extent.hashCode(); 126 if (_property != null) result += _property.hashCode(); 127 if (_prompt != null) result += _prompt.hashCode(); 128 if (_alias != null) result += _alias.hashCode(); 129 130 return result; 131 } 132 133 private boolean safeCompare(Object one, Object two) 134 { 135 if (one == null && two == null) return true; 136 if (one == null || two == null) return false; 137 138 return one.equals(two); 139 } 140 141 149 public PropertyUsage(DjProperty property, String alias) 150 { 151 this(property); 152 setAlias(alias); 153 } 154 155 163 public PropertyUsage(String expression, String alias) 164 { 165 setAlias(alias); 166 setExpression(expression); 167 } 168 169 174 public boolean isBasedOnExpression() 175 { 176 return _expression != null; 177 } 178 179 185 public void setProperty(DjProperty prop) 186 { 187 _property = prop; 188 } 189 190 196 public DjProperty getProperty() 197 { 198 try 199 { 200 return doGetProperty(); 201 } 202 catch (ObjectNotDefinedException e) 203 { 204 DjLogger.log(e.getMessage()); 207 return getBaseProperty(); 208 } 209 } 210 211 private DjProperty doGetProperty() throws ObjectNotDefinedException 212 { 213 if (_expression != null) return null; 216 217 DjProperty result = getBaseProperty(); 218 if (getRelativePath() == null) return result; 219 220 if (!(result.getType() instanceof DjExtent)) 221 { 222 DjLogger.log("Property " + result + " is not of type DjExtent"); 225 return getBaseProperty(); 226 } 227 228 DjExtent extent = (DjExtent) result.getType(); 229 return extent.getProperty(getRelativePath()); 230 } 231 232 public boolean hasValidRelativePath() 233 { 234 try 235 { 236 doGetProperty(); 237 return true; 238 } 239 catch (ObjectNotDefinedException e) 240 { 241 return false; 242 } 243 } 244 245 public DjProperty getBaseProperty() 246 { 247 try 248 { 249 return _extent.getPropertyByInternalId(_property.getInternalId()); 250 } 251 catch (ObjectNotDefinedException onde) 252 { 253 } 256 try 257 { 258 return _extent.getProperty(_property.getName()); 259 } 260 catch (ObjectNotDefinedException onde) 261 { 262 } 264 return null; 265 } 266 267 273 public void setDisplayWidth(int w) 274 { 275 _displayWidth = w; 276 } 277 278 283 public int getDisplayWidth() 284 { 285 if (_displayWidth > 0) return _displayWidth; 286 DjProperty prop = getProperty(); 287 if (getComponentType() == DjDomain.COMP_CHECKBOX) return 40; 288 if (getComponentType() == DjDomain.COMP_COMBOBOX || getComponentType() == DjDomain.COMP_CHOOSER) return 160; 289 if (prop.getTypeCode() == DjDomain.DATE_TYPE) return 100; 290 if (prop.getLength() == 0) return 100; 291 if (prop.getLength() > 40) return 160; 292 return (prop.getLength() + prop.getDecimals()) * 8; 293 } 294 295 301 public void setDisplayHeight(int h) 302 { 303 _displayHeight = h; 304 } 305 306 311 public int getDisplayHeight() 312 { 313 return _displayHeight; 314 } 315 316 322 public void setSortOrder(int s) 323 { 324 _sortOrder = s; 325 } 326 327 332 public int getSortOrder() 333 { 334 return _sortOrder; 335 } 336 337 342 public String getAlias() 343 { 344 return _alias; 345 } 346 347 353 public void setAlias(String alias) 354 { 355 _alias = alias; 356 } 357 358 363 public String getDefaultValue() 364 { 365 return _defaultValue; 366 } 367 368 374 public void setDefaultValue(String value) 375 { 376 if (value != null && value.trim().length() == 0) value = null; 377 if (value != null && value.startsWith("[[$") && value.endsWith("]]")) 379 { 380 value = "{" + value.substring(2); 381 value = value.substring(0, value.length() - 2) + "}"; 382 } 383 384 _defaultValue = value; 385 } 386 387 392 public String getExpression() 393 { 394 return _expression; 395 } 396 397 403 public void setExpression(String expression) 404 { 405 _expression = expression; 406 } 407 408 413 public String getPropertyName() 414 { 415 DjProperty baseProp = getBaseProperty(); 416 417 if (_relativePath == null) 418 { 419 if (baseProp != null) return baseProp.getName(); 420 return getAlias(); 421 } 422 return baseProp.getName() + "." + _relativePath; 423 424 } 425 426 432 public void setPrompt(String prompt) 433 { 434 _prompt = prompt; 435 } 436 437 442 public String getPrompt() 443 { 444 return _prompt; 445 } 446 447 452 public boolean isRequired() 453 { 454 return _required; 455 } 456 457 462 public boolean isQueryable() 463 { 464 return _query; 465 } 466 467 473 public void setRequired(boolean b) 474 { 475 _required = b; 476 } 477 478 public boolean isDisplayed() 479 { 480 return _displayed; 481 } 482 483 public void setDisplayed(boolean b) 484 { 485 _displayed = b; 486 } 487 488 493 public boolean isUpdateable() 494 { 495 return _update; 496 } 497 498 504 public void setQueryable(boolean b) 505 { 506 _query = b; 507 } 508 509 515 public void setUpdateable(boolean b) 516 { 517 _update = b; 518 } 519 520 526 public void setSeq(int seq) 527 { 528 _seq = seq; 529 } 530 531 536 public int getSeq() 537 { 538 return _seq; 539 } 540 541 547 public void setComponentType(int type) 548 { 549 _componentType = type; 550 } 551 552 557 public int getComponentType() 558 { 559 return _componentType; 560 } 561 562 public boolean isCustomComponent() 563 { 564 return getComponentType() == DjDomain.COMP_CUSTOM || getComponentType() == DjDomain.COMP_CUSTOM_MEMO; 565 } 566 567 572 public String getComponentTypeName() 573 { 574 return DjDomain.int2componentType(_componentType); 575 } 576 577 585 public void setComponentType(String typeName) throws CatalogException 586 { 587 _componentType = DjDomain.componentType2int(typeName); 588 } 589 590 595 public String toString() 596 { 597 if (isBasedOnExpression()) return "[E " + getExpression() + "=" + getAlias() + "]"; 598 return "[C " + _extent.getName() + "." + getPropertyName() + "=" + getAlias() + "]"; 599 } 600 601 606 public String getDescription() 607 { 608 if (getBaseProperty() == null) return Messages.getString("PropertyUsage.deleted"); 609 if (_description == null) return getProperty().getDescription(); 610 return _description; 611 } 612 613 619 public void setDescription(String description) 620 { 621 if ("".equals(description)) description = null; 622 623 DjProperty prop = getProperty(); 624 if (prop != null) 625 { 626 627 String descr = getProperty().getDescription(); 628 if (descr != null && descr.equals(description)) description = null; 629 } 630 _description = description; 631 } 632 633 public String getCustomEditorClass() 634 { 635 return _customEditorClass; 636 } 637 638 public void setCustomEditorClass(String customEditorClass) 639 { 640 _customEditorClass = customEditorClass; 641 } 642 643 public String getRelativePath() 644 { 645 return _relativePath; 646 } 647 648 public void setRelativePath(String relativePath) 649 { 650 if (relativePath != null && relativePath.trim().length() == 0) relativePath = null; 651 _relativePath = relativePath; 652 } 653 654 public void validate() throws DjenericException 655 { 656 if (!hasValidRelativePath()) 657 { 658 throw new DjenericException(Messages.getString("PropertyUsageTableModel.InvalidPath", getRelativePath())); 659 } 660 661 } 662 663 } | Popular Tags |