001: package org.acm.seguin.pmd.swingui;
002:
003: import org.acm.seguin.pmd.PMDException;
004: import org.acm.seguin.pmd.Rule;
005: import org.acm.seguin.pmd.RuleProperties;
006: import org.acm.seguin.pmd.RuleSet;
007: import org.acm.seguin.pmd.swingui.event.ListenerList;
008: import org.acm.seguin.pmd.swingui.event.PMDDirectoryRequestEvent;
009: import org.acm.seguin.pmd.swingui.event.PMDDirectoryReturnedEvent;
010: import org.acm.seguin.pmd.swingui.event.PMDDirectoryReturnedEventListener;
011: import org.acm.seguin.pmd.swingui.event.RulesTreeModelEvent;
012: import org.acm.seguin.pmd.swingui.event.RulesTreeModelEventListener;
013:
014: import javax.swing.tree.DefaultTreeModel;
015: import java.util.ArrayList;
016: import java.util.Arrays;
017: import java.util.Comparator;
018: import java.util.Enumeration;
019: import java.util.List;
020: import java.util.Set;
021:
022: /**
023: *
024: * @author Donald A. Leckie
025: * @since December 13, 2002
026: * @version $Revision: 1.1 $, $Date: 2003/07/29 20:51:59 $
027: */
028: class RulesTreeModel extends DefaultTreeModel {
029:
030: private PMDDirectoryReturnedEventHandler m_pmdDirectoryReturnedEventHandler;
031: private RulesTreeModelEventHandler m_rulesTreeModelEventHandler;
032: private static RulesTreeModel m_rulesTreeModel;
033:
034: /**
035: ****************************************************************************
036: *
037: * @param rootNode
038: */
039: private RulesTreeModel(RulesTreeNode rootNode) {
040: super (rootNode);
041:
042: m_pmdDirectoryReturnedEventHandler = new PMDDirectoryReturnedEventHandler();
043: m_rulesTreeModelEventHandler = new RulesTreeModelEventHandler();
044: ListenerList
045: .addListener((PMDDirectoryReturnedEventListener) m_pmdDirectoryReturnedEventHandler);
046: }
047:
048: /**
049: ***************************************************************************
050: *
051: * @param ruleSetName
052: *
053: * @return
054: */
055: protected RulesTreeNode getRuleSetNode(String ruleSetName) {
056: if (ruleSetName != null) {
057: RulesTreeNode rootNode = (RulesTreeNode) getRoot();
058: Enumeration ruleSetNodes = rootNode.children();
059:
060: while (ruleSetNodes.hasMoreElements()) {
061: RulesTreeNode ruleSetNode = (RulesTreeNode) ruleSetNodes
062: .nextElement();
063:
064: if (ruleSetNode.getName().equalsIgnoreCase(ruleSetName)) {
065: return ruleSetNode;
066: }
067: }
068: }
069:
070: return null;
071: }
072:
073: /**
074: ***************************************************************************
075: *
076: * @param ruleSetName
077: * @param ruleName
078: *
079: * @return
080: */
081: protected RulesTreeNode getRuleNode(String ruleSetName,
082: String ruleName) {
083: if ((ruleSetName != null) && (ruleName != null)) {
084: RulesTreeNode rootNode = (RulesTreeNode) getRoot();
085: Enumeration ruleSetNodes = rootNode.children();
086:
087: while (ruleSetNodes.hasMoreElements()) {
088: RulesTreeNode ruleSetNode = (RulesTreeNode) ruleSetNodes
089: .nextElement();
090:
091: if (ruleSetNode.getName().equalsIgnoreCase(ruleSetName)) {
092: Enumeration ruleNodes = ruleSetNode.children();
093:
094: while (ruleNodes.hasMoreElements()) {
095: RulesTreeNode ruleNode = (RulesTreeNode) ruleNodes
096: .nextElement();
097:
098: if (ruleNode.getName().equalsIgnoreCase(
099: ruleName)) {
100: return ruleNode;
101: }
102: }
103: }
104: }
105: }
106:
107: return null;
108: }
109:
110: /**
111: ***************************************************************************
112: * @return
113: */
114: protected static final RulesTreeModel get() throws PMDException {
115: if (m_rulesTreeModel == null) {
116: RulesTreeNode rootNode;
117:
118: rootNode = new RulesTreeNode("Rules");
119: m_rulesTreeModel = new RulesTreeModel(rootNode);
120: m_rulesTreeModel.buildTree();
121: }
122:
123: return m_rulesTreeModel;
124: }
125:
126: /**
127: *******************************************************************************
128: *
129: */
130: private void buildTree() throws PMDException {
131: RuleSet[] ruleSets;
132: RulesTreeNode rootNode;
133:
134: ruleSets = loadRuleSets();
135: rootNode = (RulesTreeNode) getRoot();
136:
137: for (int n1 = 0; n1 < ruleSets.length; n1++) {
138: RulesTreeNode ruleSetNode = new RulesTreeNode(ruleSets[n1]);
139:
140: rootNode.add(ruleSetNode);
141: loadRuleTreeNodes(ruleSetNode);
142: }
143:
144: }
145:
146: /**
147: *******************************************************************************
148: *
149: * @return
150: */
151: private RuleSet[] loadRuleSets() throws PMDException {
152: PMDDirectoryRequestEvent.notifyRequestAllRuleSets(this );
153:
154: // The event is processed. The requested rule set is assembled by another class
155: // that calls notifyReturnedAllRuleSets. The list of rule sets is stored in the
156: // inner class PMDDirectoryReturnedEventHandler. Then processing will then continue here.
157: //
158:
159: List ruleSetList = m_pmdDirectoryReturnedEventHandler
160: .getRuleSetList();
161:
162: if (ruleSetList == null) {
163: ruleSetList = new ArrayList();
164: }
165:
166: //
167: // Sort the rule sets by name in ascending order.
168: //
169: RuleSet[] ruleSets = new RuleSet[ruleSetList.size()];
170:
171: ruleSetList.toArray(ruleSets);
172: Arrays.sort(ruleSets, new RuleSetNameComparator());
173:
174: return ruleSets;
175: }
176:
177: /**
178: *******************************************************************************
179: *
180: * @param ruleSetNode
181: */
182: private void loadRuleTreeNodes(RulesTreeNode ruleSetNode) {
183: RuleSet ruleSet;
184: Set setOfRules;
185: Rule[] rules;
186:
187: ruleSet = ruleSetNode.getRuleSet();
188: setOfRules = ruleSet.getRules();
189: rules = new Rule[setOfRules.size()];
190:
191: setOfRules.toArray(rules);
192: Arrays.sort(rules, new RuleNameComparator());
193:
194: for (int n = 0; n < rules.length; n++) {
195: RulesTreeNode ruleNode = new RulesTreeNode(ruleSetNode,
196: rules[n]);
197:
198: ruleSetNode.add(ruleNode);
199: loadProperties(ruleNode);
200:
201: rules[n] = null;
202: }
203: }
204:
205: /**
206: *******************************************************************************
207: *
208: * @param ruleNode
209: */
210: private void loadProperties(RulesTreeNode ruleNode) {
211: Rule rule;
212: RuleProperties properties;
213: String[] propertyNames;
214: Enumeration keys;
215: int index;
216:
217: rule = ruleNode.getRule();
218: properties = rule.getProperties();
219: propertyNames = new String[properties.size()];
220: keys = properties.keys();
221: index = 0;
222:
223: while (keys.hasMoreElements()) {
224: propertyNames[index] = (String) keys.nextElement();
225: index++;
226: }
227:
228: Arrays.sort(propertyNames, new PropertyNameComparator());
229:
230: for (int n = 0; n < propertyNames.length; n++) {
231: String propertyName;
232: String propertyValue;
233: String propertyValueType;
234: RulesTreeNode propertyNode;
235:
236: propertyName = propertyNames[n];
237: propertyValue = properties.getValue(propertyName);
238: propertyValueType = properties.getValueType(propertyName);
239: propertyNode = new RulesTreeNode(ruleNode, propertyName,
240: propertyValue, propertyValueType);
241:
242: ruleNode.add(propertyNode);
243:
244: propertyNames[n] = null;
245: }
246: }
247:
248: /**
249: ****************************************************************************
250: ****************************************************************************
251: ****************************************************************************
252: */
253: private class RulesTreeModelEventHandler implements
254: RulesTreeModelEventListener {
255:
256: /**
257: ***********************************************************************
258: *
259: */
260: private RulesTreeModelEventHandler() {
261: ListenerList
262: .addListener((RulesTreeModelEventListener) this );
263: }
264:
265: /**
266: ************************************************************************
267: *
268: * @param parentNode
269: */
270: public void reload(RulesTreeModelEvent event) {
271: RulesTreeNode parentNode = event.getParentNode();
272: RulesTreeModel.this .reload(parentNode);
273: }
274:
275: /**
276: ****************************************************************************
277: *
278: * @param event
279: */
280: public void requestSelectedRule(RulesTreeModelEvent event) {
281: }
282:
283: /**
284: ****************************************************************************
285: *
286: * @param event
287: */
288: public void returnedSelectedRule(RulesTreeModelEvent event) {
289: }
290: }
291:
292: /**
293: *******************************************************************************
294: *******************************************************************************
295: *******************************************************************************
296: */
297: private class PropertyNameComparator implements Comparator {
298:
299: /**
300: ************************************************************************
301: *
302: * @param objectA
303: * @param objectB
304: *
305: * @return
306: */
307: public int compare(Object objectA, Object objectB) {
308: String propertyNameA = (String) objectA;
309: String propertyNameB = (String) objectB;
310:
311: return propertyNameA.compareToIgnoreCase(propertyNameB);
312: }
313: }
314:
315: /**
316: *******************************************************************************
317: *******************************************************************************
318: *******************************************************************************
319: */
320: private class RuleSetNameComparator implements Comparator {
321:
322: /**
323: ************************************************************************
324: *
325: * @param objectA
326: * @param objectB
327: *
328: * @return
329: */
330: public int compare(Object objectA, Object objectB) {
331: String ruleSetNameA = ((RuleSet) objectA).getName();
332: String ruleSetNameB = ((RuleSet) objectB).getName();
333:
334: return ruleSetNameA.compareToIgnoreCase(ruleSetNameB);
335: }
336: }
337:
338: /**
339: *******************************************************************************
340: *******************************************************************************
341: *******************************************************************************
342: */
343: private class RuleNameComparator implements Comparator {
344:
345: /**
346: ************************************************************************
347: *
348: * @param objectA
349: * @param objectB
350: *
351: * @return
352: */
353: public int compare(Object objectA, Object objectB) {
354: String ruleNameA = ((Rule) objectA).getName();
355: String ruleNameB = ((Rule) objectB).getName();
356:
357: return ruleNameA.compareToIgnoreCase(ruleNameB);
358: }
359: }
360:
361: /**
362: *******************************************************************************
363: *******************************************************************************
364: *******************************************************************************
365: */
366: private class PMDDirectoryReturnedEventHandler implements
367: PMDDirectoryReturnedEventListener {
368:
369: private List m_ruleSetList;
370: private String m_ruleSetPath;
371:
372: /**
373: ***************************************************************************
374: */
375: private String getRuleSetPath() {
376: return m_ruleSetPath;
377: }
378:
379: /**
380: ***************************************************************************
381: */
382: private List getRuleSetList() {
383: return m_ruleSetList;
384: }
385:
386: /**
387: ***************************************************************************
388: *
389: * @param event
390: */
391: public void returnedRuleSetPath(PMDDirectoryReturnedEvent event) {
392: m_ruleSetPath = event.getRuleSetPath();
393: }
394:
395: /**
396: ***************************************************************************
397: *
398: * @param event
399: */
400: public void returnedAllRuleSets(PMDDirectoryReturnedEvent event) {
401: m_ruleSetList = event.getRuleSetList();
402: }
403:
404: /**
405: ***************************************************************************
406: *
407: * @param event
408: */
409: public void returnedDefaultRuleSets(
410: PMDDirectoryReturnedEvent event) {
411: m_ruleSetList = event.getRuleSetList();
412: }
413:
414: /**
415: ***************************************************************************
416: *
417: * @param event
418: */
419: public void returnedIncludedRules(
420: PMDDirectoryReturnedEvent event) {
421: m_ruleSetList = event.getRuleSetList();
422: }
423: }
424: }
|