001: package org.acm.seguin.pmd.swingui;
002:
003: import org.acm.seguin.pmd.Rule;
004: import org.acm.seguin.pmd.RuleSet;
005: import org.acm.seguin.pmd.swingui.event.RulesTreeModelEvent;
006:
007: import javax.swing.tree.DefaultMutableTreeNode;
008: import java.util.Arrays;
009: import java.util.Comparator;
010: import java.util.Enumeration;
011:
012: /**
013: *
014: * @author Donald A. Leckie
015: * @since August 29, 2002
016: * @version $Revision: 1.1 $, $Date: 2003/07/29 20:51:59 $
017: */
018: public class RulesTreeNode extends DefaultMutableTreeNode implements
019: Constants {
020: private RuleSet m_ruleSet;
021: private Rule m_rule;
022: private String m_className;
023: private String m_name;
024: private String m_message;
025: private String m_description;
026: private String m_example;
027: private String m_propertyValue;
028: private String m_propertyValueType;
029: private int m_type;
030: private boolean m_include;
031: private int m_priority;
032:
033: // Constant
034: private static final int IS_ROOT = 0x01;
035: private static final int IS_RULE_SET = 0x02;
036: private static final int IS_RULE = 0x04;
037: private static final int IS_PROPERTY = 0x08;
038:
039: /**
040: ***************************************************************************
041: *
042: * @param name
043: */
044: protected RulesTreeNode(String text) {
045: super ();
046:
047: m_name = trim(text);
048: m_type = IS_ROOT;
049: m_include = true;
050:
051: setDisplayName();
052: }
053:
054: /**
055: ***************************************************************************
056: *
057: * @param name
058: */
059: protected RulesTreeNode(RuleSet ruleSet) {
060: super ();
061:
062: m_name = trim(ruleSet.getName());
063: m_description = trim(ruleSet.getDescription());
064: m_ruleSet = ruleSet;
065: m_type = IS_RULE_SET;
066: m_include = ruleSet.include();
067: setDisplayName();
068: }
069:
070: /**
071: ***************************************************************************
072: *
073: * @param name
074: */
075: protected RulesTreeNode(RulesTreeNode ruleSetNode, Rule rule) {
076: super ();
077:
078: m_name = trim(rule.getName());
079: m_className = trim(rule.getClass().getName());
080: m_message = trim(rule.getMessage());
081: m_description = trim(rule.getDescription());
082: m_example = trim(rule.getExample());
083: m_ruleSet = ruleSetNode.getRuleSet();
084: m_rule = rule;
085: m_type = IS_RULE;
086: m_include = rule.include();
087: m_priority = rule.getPriority();
088: setDisplayName();
089: }
090:
091: /**
092: ***************************************************************************
093: *
094: * @param name
095: */
096: protected RulesTreeNode(RulesTreeNode ruleNode,
097: String propertyName, String propertyValue,
098: String propertyValueType) {
099: super ();
100:
101: m_name = trim(propertyName);
102: m_propertyValue = trim(propertyValue);
103: m_propertyValueType = trim(propertyValueType);
104: m_type = IS_PROPERTY;
105: m_rule = ruleNode.getRule();
106: m_ruleSet = ((RulesTreeNode) ruleNode.getParent()).getRuleSet();
107: m_include = true;
108: setDisplayName();
109: }
110:
111: /**
112: ***************************************************************************
113: *
114: * @param childName
115: *
116: * @return
117: */
118: protected RulesTreeNode getChildNode(String childName) {
119: Enumeration children = children();
120:
121: while (children.hasMoreElements()) {
122: RulesTreeNode childNode = (RulesTreeNode) children
123: .nextElement();
124:
125: if (childNode.getName().equalsIgnoreCase(childName)) {
126: return childNode;
127: }
128: }
129:
130: return null;
131: }
132:
133: /**
134: ***************************************************************************
135: *
136: * @return
137: */
138: protected String getClassName() {
139: return m_className;
140: }
141:
142: /**
143: ***************************************************************************
144: *
145: * @return
146: */
147: protected String getDescription() {
148: return m_description;
149: }
150:
151: /**
152: ***************************************************************************
153: *
154: * @return
155: */
156: protected String getExample() {
157: return m_example;
158: }
159:
160: /**
161: ***************************************************************************
162: *
163: * @return
164: */
165: protected String getMessage() {
166: return m_message;
167: }
168:
169: /**
170: ***************************************************************************
171: *
172: * @return
173: */
174: protected String getName() {
175: return m_name;
176: }
177:
178: /**
179: ***************************************************************************
180: *
181: * @return
182: */
183: protected RulesTreeNode getParentRuleData() {
184: if (isProperty()) {
185: return (RulesTreeNode) getParent();
186: }
187:
188: return null;
189: }
190:
191: /**
192: ***************************************************************************
193: *
194: * @return
195: */
196: protected RulesTreeNode getParentRuleSetData() {
197: if (isProperty()) {
198: return (RulesTreeNode) getParent().getParent();
199: }
200:
201: if (isRule()) {
202: return (RulesTreeNode) getParent();
203: }
204:
205: return null;
206: }
207:
208: /**
209: ***************************************************************************
210: *
211: * @return
212: */
213: protected String getPropertyValue() {
214: return m_propertyValue;
215: }
216:
217: /**
218: ***************************************************************************
219: *
220: * @return
221: */
222: protected String getPropertyValueType() {
223: return m_propertyValueType;
224: }
225:
226: /**
227: *******************************************************************************
228: *
229: * @return
230: */
231: protected RulesTreeNode getSibling(String name) {
232: RulesTreeNode parentNode = (RulesTreeNode) getParent();
233:
234: if (parentNode != null) {
235: return parentNode.getChildNode(name);
236: }
237:
238: return null;
239: }
240:
241: /**
242: ***************************************************************************
243: *
244: * @return
245: */
246: protected boolean include() {
247: return m_include;
248: }
249:
250: /**
251: ***************************************************************************
252: *
253: * @return
254: */
255: protected boolean includeAncestor() {
256: boolean include = true;
257:
258: if (include) {
259: if (isRule()) {
260: RulesTreeNode ruleSetNode;
261:
262: ruleSetNode = (RulesTreeNode) getParent();
263: include = ruleSetNode.include();
264: } else if (isProperty()) {
265: RulesTreeNode ruleNode = (RulesTreeNode) getParent();
266:
267: if (ruleNode.include()) {
268: RulesTreeNode ruleSetNode;
269:
270: ruleSetNode = (RulesTreeNode) ruleNode.getParent();
271: include = ruleSetNode.include();
272: }
273: }
274: }
275:
276: return include;
277: }
278:
279: /**
280: ***************************************************************************
281: *
282: * @return
283: */
284: protected boolean isProperty() {
285: return m_type == IS_PROPERTY;
286: }
287:
288: /**
289: ***************************************************************************
290: *
291: * @return
292: */
293: protected boolean isRule() {
294: return m_type == IS_RULE;
295: }
296:
297: /**
298: ***************************************************************************
299: *
300: * @return
301: */
302: protected boolean isRuleSet() {
303: return m_type == IS_RULE_SET;
304: }
305:
306: /**
307: ***************************************************************************
308: *
309: * @return
310: */
311: public boolean isRoot() {
312: return m_type == IS_ROOT;
313: }
314:
315: /**
316: ***************************************************************************
317: *
318: * @return
319: */
320: protected Rule getRule() {
321: return m_rule;
322: }
323:
324: /**
325: ***************************************************************************
326: *
327: * @return
328: */
329: protected RuleSet getRuleSet() {
330: return m_ruleSet;
331: }
332:
333: /**
334: ***************************************************************************
335: *
336: * @return
337: */
338: protected int getPriority() {
339: return m_priority;
340: }
341:
342: /**
343: **************************************************************************
344: *
345: * @param newName
346: */
347: protected void setDisplayName() {
348: String displayName;
349:
350: if (isProperty()) {
351: displayName = m_name + ":" + m_propertyValue;
352: } else {
353: displayName = m_name;
354: }
355:
356: setUserObject(displayName);
357: }
358:
359: /**
360: **************************************************************************
361: *
362: * @param newName
363: */
364: protected void setName(String newName) {
365: m_name = trim(newName);
366:
367: setDisplayName();
368: }
369:
370: /**
371: **************************************************************************
372: *
373: * @param newName
374: */
375: protected void setMessage(String newMessage) {
376: m_message = trim(newMessage);
377: }
378:
379: /**
380: **************************************************************************
381: *
382: * @param newName
383: */
384: protected void setDescription(String newDescription) {
385: m_description = trim(newDescription);
386: }
387:
388: /**
389: **************************************************************************
390: *
391: * @param newName
392: */
393: protected void setExample(String newExample) {
394: }
395:
396: /**
397: **************************************************************************
398: *
399: * @param newName
400: */
401: protected void setPropertyValue(String newValue) {
402: m_propertyValue = trim(newValue);
403:
404: setDisplayName();
405: }
406:
407: /**
408: **************************************************************************
409: *
410: * @param newName
411: */
412: protected void setPropertyValueType(String newValue) {
413: m_propertyValueType = trim(newValue);
414: }
415:
416: /**
417: **************************************************************************
418: *
419: * @param newName
420: */
421: protected void setInclude(boolean include) {
422: m_include = include;
423: }
424:
425: /**
426: **************************************************************************
427: *
428: * @param priority
429: */
430: protected void setPriority(int priority) {
431: m_priority = priority;
432: }
433:
434: /**
435: *************************************************************************
436: *
437: * @param newClass
438: */
439: protected void setClassName(String newClassName) {
440: m_className = trim(newClassName);
441: }
442:
443: /**
444: *************************************************************************
445: *
446: */
447: protected void saveData() {
448: if (isRuleSet()) {
449: m_ruleSet.setName(m_name);
450: m_ruleSet.setDescription(m_description);
451: m_ruleSet.setInclude(m_include);
452: } else if (isRule()) {
453: m_rule.setName(m_name);
454: m_rule.setMessage(m_message);
455: m_rule.setDescription(m_description);
456: m_rule.setExample(m_example);
457: m_rule.setInclude(m_include);
458: m_rule.setPriority(m_priority);
459: } else if (isProperty()) {
460: m_rule.getProperties().setValue(m_name, m_propertyValue);
461: m_rule.getProperties().setValueType(m_name,
462: m_propertyValueType);
463: }
464: }
465:
466: /**
467: *************************************************************************
468: *
469: * @param text
470: *
471: * @return
472: */
473: private String trim(String text) {
474: if (text == null) {
475: text = EMPTY_STRING;
476: } else {
477: text = text.trim();
478:
479: if (text.length() == 0) {
480: text = EMPTY_STRING;
481: }
482: }
483:
484: return text;
485: }
486:
487: /**
488: ***************************************************************************
489: *
490: * @param event
491: */
492: protected void sortChildren() {
493: int childCount = getChildCount();
494: RulesTreeNode[] treeNodes = new RulesTreeNode[childCount];
495: boolean needToSort = false;
496:
497: for (int n = 0; n < childCount; n++) {
498: treeNodes[n] = (RulesTreeNode) getChildAt(n);
499:
500: if ((n > 0) && (needToSort == false)) {
501: String previousNodeName = treeNodes[n - 1].getName();
502: String currentNodeName = treeNodes[n].getName();
503:
504: if (currentNodeName
505: .compareToIgnoreCase(previousNodeName) < 0) {
506: needToSort = true;
507: }
508: }
509: }
510:
511: if (needToSort) {
512: Arrays.sort(treeNodes, new SortComparator());
513: removeAllChildren();
514:
515: for (int n = 0; n < treeNodes.length; n++) {
516: add(treeNodes[n]);
517: }
518:
519: RulesTreeModelEvent.notifyReload(this , this );
520: }
521:
522: for (int n = 0; n < treeNodes.length; n++) {
523: treeNodes[n] = null;
524: }
525: }
526:
527: /**
528: *******************************************************************************
529: *******************************************************************************
530: *******************************************************************************
531: */
532: private class SortComparator implements Comparator {
533:
534: /**
535: ***************************************************************************
536: *
537: * @param object1
538: * @param object2
539: *
540: * @return
541: */
542: public int compare(Object object1, Object object2) {
543: String name1 = ((RulesTreeNode) object1).getName();
544: String name2 = ((RulesTreeNode) object2).getName();
545:
546: return name1.compareToIgnoreCase(name2);
547: }
548:
549: /**
550: ***************************************************************************
551: *
552: * @param object
553: *
554: * @return
555: */
556: public boolean equals(Object object) {
557: return object == this;
558: }
559: }
560: }
|