View Javadoc
1 package net.sourceforge.pmd.swingui; 2 3 import net.sourceforge.pmd.Rule; 4 import net.sourceforge.pmd.RuleSet; 5 import net.sourceforge.pmd.swingui.event.RulesTreeModelEvent; 6 7 import javax.swing.tree.DefaultMutableTreeNode; 8 import java.util.Arrays; 9 import java.util.Comparator; 10 import java.util.Enumeration; 11 12 /*** 13 * 14 * @author Donald A. Leckie 15 * @since August 29, 2002 16 * @version $Revision: 1.17 $, $Date: 2003/03/14 19:04:11 $ 17 */ 18 public class RulesTreeNode extends DefaultMutableTreeNode implements Constants { 19 private RuleSet m_ruleSet; 20 private Rule m_rule; 21 private String m_className; 22 private String m_name; 23 private String m_message; 24 private String m_description; 25 private String m_example; 26 private String m_propertyValue; 27 private String m_propertyValueType; 28 private int m_type; 29 private boolean m_include; 30 private int m_priority; 31 32 // Constant 33 private static final int IS_ROOT = 0x01; 34 private static final int IS_RULE_SET = 0x02; 35 private static final int IS_RULE = 0x04; 36 private static final int IS_PROPERTY = 0x08; 37 38 /*** 39 *************************************************************************** 40 * 41 * @param name 42 */ 43 protected RulesTreeNode(String text) { 44 super(); 45 46 m_name = trim(text); 47 m_type = IS_ROOT; 48 m_include = true; 49 50 setDisplayName(); 51 } 52 53 /*** 54 *************************************************************************** 55 * 56 * @param name 57 */ 58 protected RulesTreeNode(RuleSet ruleSet) { 59 super(); 60 61 m_name = trim(ruleSet.getName()); 62 m_description = trim(ruleSet.getDescription()); 63 m_ruleSet = ruleSet; 64 m_type = IS_RULE_SET; 65 m_include = ruleSet.include(); 66 setDisplayName(); 67 } 68 69 /*** 70 *************************************************************************** 71 * 72 * @param name 73 */ 74 protected RulesTreeNode(RulesTreeNode ruleSetNode, Rule rule) { 75 super(); 76 77 m_name = trim(rule.getName()); 78 m_className = trim(rule.getClass().getName()); 79 m_message = trim(rule.getMessage()); 80 m_description = trim(rule.getDescription()); 81 m_example = trim(rule.getExample()); 82 m_ruleSet = ruleSetNode.getRuleSet(); 83 m_rule = rule; 84 m_type = IS_RULE; 85 m_include = rule.include(); 86 m_priority = rule.getPriority(); 87 setDisplayName(); 88 } 89 90 /*** 91 *************************************************************************** 92 * 93 * @param name 94 */ 95 protected RulesTreeNode(RulesTreeNode ruleNode, String propertyName, String propertyValue, String propertyValueType) { 96 super(); 97 98 m_name = trim(propertyName); 99 m_propertyValue = trim(propertyValue); 100 m_propertyValueType = trim(propertyValueType); 101 m_type = IS_PROPERTY; 102 m_rule = ruleNode.getRule(); 103 m_ruleSet = ((RulesTreeNode) ruleNode.getParent()).getRuleSet(); 104 m_include = true; 105 setDisplayName(); 106 } 107 108 /*** 109 *************************************************************************** 110 * 111 * @param childName 112 * 113 * @return 114 */ 115 protected RulesTreeNode getChildNode(String childName) { 116 Enumeration children = children(); 117 118 while (children.hasMoreElements()) { 119 RulesTreeNode childNode = (RulesTreeNode) children.nextElement(); 120 121 if (childNode.getName().equalsIgnoreCase(childName)) { 122 return childNode; 123 } 124 } 125 126 return null; 127 } 128 129 /*** 130 *************************************************************************** 131 * 132 * @return 133 */ 134 protected String getClassName() { 135 return m_className; 136 } 137 138 /*** 139 *************************************************************************** 140 * 141 * @return 142 */ 143 protected String getDescription() { 144 return m_description; 145 } 146 147 /*** 148 *************************************************************************** 149 * 150 * @return 151 */ 152 protected String getExample() { 153 return m_example; 154 } 155 156 /*** 157 *************************************************************************** 158 * 159 * @return 160 */ 161 protected String getMessage() { 162 return m_message; 163 } 164 165 /*** 166 *************************************************************************** 167 * 168 * @return 169 */ 170 protected String getName() { 171 return m_name; 172 } 173 174 /*** 175 *************************************************************************** 176 * 177 * @return 178 */ 179 protected RulesTreeNode getParentRuleData() { 180 if (isProperty()) { 181 return (RulesTreeNode) getParent(); 182 } 183 184 return null; 185 } 186 187 /*** 188 *************************************************************************** 189 * 190 * @return 191 */ 192 protected RulesTreeNode getParentRuleSetData() { 193 if (isProperty()) { 194 return (RulesTreeNode) getParent().getParent(); 195 } 196 197 if (isRule()) { 198 return (RulesTreeNode) getParent(); 199 } 200 201 return null; 202 } 203 204 /*** 205 *************************************************************************** 206 * 207 * @return 208 */ 209 protected String getPropertyValue() { 210 return m_propertyValue; 211 } 212 213 /*** 214 *************************************************************************** 215 * 216 * @return 217 */ 218 protected String getPropertyValueType() { 219 return m_propertyValueType; 220 } 221 222 /*** 223 ******************************************************************************* 224 * 225 * @return 226 */ 227 protected RulesTreeNode getSibling(String name) { 228 RulesTreeNode parentNode = (RulesTreeNode) getParent(); 229 230 if (parentNode != null) { 231 return parentNode.getChildNode(name); 232 } 233 234 return null; 235 } 236 237 /*** 238 *************************************************************************** 239 * 240 * @return 241 */ 242 protected boolean include() { 243 return m_include; 244 } 245 246 /*** 247 *************************************************************************** 248 * 249 * @return 250 */ 251 protected boolean includeAncestor() { 252 boolean include = true; 253 254 if (include) { 255 if (isRule()) { 256 RulesTreeNode ruleSetNode; 257 258 ruleSetNode = (RulesTreeNode) getParent(); 259 include = ruleSetNode.include(); 260 } else if (isProperty()) { 261 RulesTreeNode ruleNode = (RulesTreeNode) getParent(); 262 263 if (ruleNode.include()) { 264 RulesTreeNode ruleSetNode; 265 266 ruleSetNode = (RulesTreeNode) ruleNode.getParent(); 267 include = ruleSetNode.include(); 268 } 269 } 270 } 271 272 return include; 273 } 274 275 /*** 276 *************************************************************************** 277 * 278 * @return 279 */ 280 protected boolean isProperty() { 281 return m_type == IS_PROPERTY; 282 } 283 284 /*** 285 *************************************************************************** 286 * 287 * @return 288 */ 289 protected boolean isRule() { 290 return m_type == IS_RULE; 291 } 292 293 /*** 294 *************************************************************************** 295 * 296 * @return 297 */ 298 protected boolean isRuleSet() { 299 return m_type == IS_RULE_SET; 300 } 301 302 /*** 303 *************************************************************************** 304 * 305 * @return 306 */ 307 public boolean isRoot() { 308 return m_type == IS_ROOT; 309 } 310 311 /*** 312 *************************************************************************** 313 * 314 * @return 315 */ 316 protected Rule getRule() { 317 return m_rule; 318 } 319 320 /*** 321 *************************************************************************** 322 * 323 * @return 324 */ 325 protected RuleSet getRuleSet() { 326 return m_ruleSet; 327 } 328 329 /*** 330 *************************************************************************** 331 * 332 * @return 333 */ 334 protected int getPriority() { 335 return m_priority; 336 } 337 338 /*** 339 ************************************************************************** 340 * 341 * @param newName 342 */ 343 protected void setDisplayName() { 344 String displayName; 345 346 if (isProperty()) { 347 displayName = m_name + ":" + m_propertyValue; 348 } else { 349 displayName = m_name; 350 } 351 352 setUserObject(displayName); 353 } 354 355 /*** 356 ************************************************************************** 357 * 358 * @param newName 359 */ 360 protected void setName(String newName) { 361 m_name = trim(newName); 362 363 setDisplayName(); 364 } 365 366 /*** 367 ************************************************************************** 368 * 369 * @param newName 370 */ 371 protected void setMessage(String newMessage) { 372 m_message = trim(newMessage); 373 } 374 375 /*** 376 ************************************************************************** 377 * 378 * @param newName 379 */ 380 protected void setDescription(String newDescription) { 381 m_description = trim(newDescription); 382 } 383 384 /*** 385 ************************************************************************** 386 * 387 * @param newName 388 */ 389 protected void setExample(String newExample) { 390 } 391 392 /*** 393 ************************************************************************** 394 * 395 * @param newName 396 */ 397 protected void setPropertyValue(String newValue) { 398 m_propertyValue = trim(newValue); 399 400 setDisplayName(); 401 } 402 403 /*** 404 ************************************************************************** 405 * 406 * @param newName 407 */ 408 protected void setPropertyValueType(String newValue) { 409 m_propertyValueType = trim(newValue); 410 } 411 412 /*** 413 ************************************************************************** 414 * 415 * @param newName 416 */ 417 protected void setInclude(boolean include) { 418 m_include = include; 419 } 420 421 /*** 422 ************************************************************************** 423 * 424 * @param priority 425 */ 426 protected void setPriority(int priority) { 427 m_priority = priority; 428 } 429 430 /*** 431 ************************************************************************* 432 * 433 * @param newClass 434 */ 435 protected void setClassName(String newClassName) { 436 m_className = trim(newClassName); 437 } 438 439 /*** 440 ************************************************************************* 441 * 442 */ 443 protected void saveData() { 444 if (isRuleSet()) { 445 m_ruleSet.setName(m_name); 446 m_ruleSet.setDescription(m_description); 447 m_ruleSet.setInclude(m_include); 448 } else if (isRule()) { 449 m_rule.setName(m_name); 450 m_rule.setMessage(m_message); 451 m_rule.setDescription(m_description); 452 m_rule.setExample(m_example); 453 m_rule.setInclude(m_include); 454 m_rule.setPriority(m_priority); 455 } else if (isProperty()) { 456 m_rule.getProperties().setValue(m_name, m_propertyValue); 457 m_rule.getProperties().setValueType(m_name, m_propertyValueType); 458 } 459 } 460 461 /*** 462 ************************************************************************* 463 * 464 * @param text 465 * 466 * @return 467 */ 468 private String trim(String text) { 469 if (text == null) { 470 text = EMPTY_STRING; 471 } else { 472 text = text.trim(); 473 474 if (text.length() == 0) { 475 text = EMPTY_STRING; 476 } 477 } 478 479 return text; 480 } 481 482 /*** 483 *************************************************************************** 484 * 485 * @param event 486 */ 487 protected void sortChildren() { 488 int childCount = getChildCount(); 489 RulesTreeNode[] treeNodes = new RulesTreeNode[childCount]; 490 boolean needToSort = false; 491 492 for (int n = 0; n < childCount; n++) { 493 treeNodes[n] = (RulesTreeNode) getChildAt(n); 494 495 if ((n > 0) && (needToSort == false)) { 496 String previousNodeName = treeNodes[n - 1].getName(); 497 String currentNodeName = treeNodes[n].getName(); 498 499 if (currentNodeName.compareToIgnoreCase(previousNodeName) < 0) { 500 needToSort = true; 501 } 502 } 503 } 504 505 if (needToSort) { 506 Arrays.sort(treeNodes, new SortComparator()); 507 removeAllChildren(); 508 509 for (int n = 0; n < treeNodes.length; n++) { 510 add(treeNodes[n]); 511 } 512 513 RulesTreeModelEvent.notifyReload(this, this); 514 } 515 516 for (int n = 0; n < treeNodes.length; n++) { 517 treeNodes[n] = null; 518 } 519 } 520 521 /*** 522 ******************************************************************************* 523 ******************************************************************************* 524 ******************************************************************************* 525 */ 526 private class SortComparator implements Comparator { 527 528 /*** 529 *************************************************************************** 530 * 531 * @param object1 532 * @param object2 533 * 534 * @return 535 */ 536 public int compare(Object object1, Object object2) { 537 String name1 = ((RulesTreeNode) object1).getName(); 538 String name2 = ((RulesTreeNode) object2).getName(); 539 540 return name1.compareToIgnoreCase(name2); 541 } 542 543 /*** 544 *************************************************************************** 545 * 546 * @param object 547 * 548 * @return 549 */ 550 public boolean equals(Object object) { 551 return object == this; 552 } 553 } 554 }

This page was automatically generated by Maven