001: /*
002: * User: tom
003: * Date: Jul 9, 2002
004: * Time: 1:18:38 PM
005: */
006: package org.acm.seguin.ide.common.options;
007:
008: import java.util.Comparator;
009: import java.util.Iterator;
010: import java.util.Map;
011: import java.util.StringTokenizer;
012: import java.util.TreeMap;
013: import java.util.Set;
014: import java.util.HashSet;
015:
016: import javax.swing.JCheckBox;
017: import javax.swing.JOptionPane;
018: import org.acm.seguin.ide.common.IDEPlugin;
019:
020: import org.acm.seguin.ide.common.options.PropertiesFile;
021:
022: import org.acm.seguin.pmd.Rule;
023: import org.acm.seguin.pmd.RuleSet;
024: import org.acm.seguin.pmd.RuleSetFactory;
025: import org.acm.seguin.pmd.RuleSetNotFoundException;
026:
027: /**
028: * Description of the Class
029: *
030: *@author jiger.p
031: *@created April 22, 2003
032: */
033: public class SelectedRules {
034:
035: private static Set notFoundRulesets = new HashSet();
036: // Rule -> JCheckBox
037: private Map defaultRules = null;
038: private Map rules = new TreeMap(new Comparator() {
039: public int compare(Object o1, Object o2) {
040: Rule r1 = (Rule) o1;
041: Rule r2 = (Rule) o2;
042: return r1.getName().compareTo(r2.getName());
043: }
044: });
045:
046: private PropertiesFile props;
047:
048: /**
049: * Constructor for the SelectedRules object
050: *
051: *@param project Description of Parameter
052: *@param parent Description of Parameter
053: *@exception RuleSetNotFoundException Description of the Exception
054: */
055: public SelectedRules(String project, java.awt.Component parent)
056: throws RuleSetNotFoundException {
057: if ("default".equals(project)) {
058: notFoundRulesets = new HashSet();
059: } else {
060: if (defaultRules != null) {
061: rules.putAll(defaultRules);
062: }
063: }
064:
065: props = IDEPlugin.getProperties("pmd", project);
066: RuleSetFactory rsf = new RuleSetFactory();
067: for (Iterator i = rsf.getRegisteredRuleSets(); i.hasNext();) {
068: addRuleSet2Rules((RuleSet) i.next());
069: }
070:
071: //Load project modified RuleSets if any.
072: java.io.File dir = org.acm.seguin.util.FileSettings
073: .getRefactorySettingsRoot();
074: dir = new java.io.File(dir, "projects");
075: dir = new java.io.File(dir, project);
076: if (dir.exists()) {
077: dir = new java.io.File(dir, "rules");
078: if (!dir.exists()) {
079: dir.mkdir();
080: }
081: java.io.File[] files = dir.listFiles();
082: for (int i = 0; i < files.length; i++) {
083: try {
084: String rulesetName = files[i].getCanonicalPath();
085: if (rulesetName.toLowerCase().endsWith(".xml")) {
086: addRuleSet2Rules(rsf
087: .createRuleSet(new java.io.FileInputStream(
088: files[i])));
089: }
090: } catch (Exception e) {
091: e.printStackTrace();
092: }
093: }
094: }
095:
096: //Load custom RuleSets if any.
097: if (props.isLocalProperty("pmd.path")) {
098: String customRuleSetPath = props.getString("pmd.path");
099: if (!(customRuleSetPath == null)) {
100: StringTokenizer strtok = new StringTokenizer(
101: customRuleSetPath, ",");
102: while (strtok.hasMoreTokens()) {
103: String rulesetName = strtok.nextToken();
104: if (rulesetName != null
105: && !rulesetName.equals("<none>")) {
106: try {
107: addRuleSet2Rules(rsf
108: .createRuleSet(rulesetName));
109: } catch (Exception e) {
110: if (notFoundRulesets.add(rulesetName)) {
111: e.printStackTrace();
112: JOptionPane.showMessageDialog(parent,
113: "Can't load custom ruleset \""
114: + rulesetName + "\"",
115: "JavaStyle",
116: JOptionPane.ERROR_MESSAGE);
117: }
118: }
119: }
120: }
121: }
122: }
123: if ("default".equals(project)) {
124: defaultRules = rules;
125: }
126: }
127:
128: /**
129: * Gets the rule attribute of the SelectedRules object
130: *
131: *@param candidate Description of the Parameter
132: *@return The rule value
133: */
134: public Rule getRule(JCheckBox candidate) {
135: for (Iterator i = rules.keySet().iterator(); i.hasNext();) {
136: Rule rule = (Rule) i.next();
137: JCheckBox box = (JCheckBox) rules.get(rule);
138: if (box.equals(candidate)) {
139: return rule;
140: }
141: }
142: throw new RuntimeException(
143: "Couldn't find a rule that mapped to the passed in JCheckBox "
144: + candidate);
145: }
146:
147: /**
148: * Description of the Method
149: *
150: *@param key Description of the Parameter
151: *@return Description of the Return Value
152: */
153: public JCheckBox get(Object key) {
154: return (JCheckBox) rules.get(key);
155: }
156:
157: /**
158: * Gets the allBoxes attribute of the SelectedRules object
159: *
160: *@return The allBoxes value
161: */
162: public Object[] getAllBoxes() {
163: Object[] foo = new Object[rules.size()];
164: int idx = 0;
165: for (Iterator i = rules.values().iterator(); i.hasNext();) {
166: foo[idx] = i.next();
167: idx++;
168: }
169: return foo;
170: }
171:
172: /**
173: * Gets the selectedRules attribute of the SelectedRules object
174: *
175: *@return The selectedRules value
176: */
177: public RuleSet getSelectedRules() {
178: RuleSet newRuleSet = new RuleSet();
179: for (Iterator i = rules.keySet().iterator(); i.hasNext();) {
180: Rule rule = (Rule) i.next();
181: if (get(rule).isSelected()) {
182: newRuleSet.addRule(rule);
183: }
184: }
185: return newRuleSet;
186: }
187:
188: /**
189: * Description of the Method
190: *
191: *@return Description of the Return Value
192: */
193: public int size() {
194: return rules.size();
195: }
196:
197: /** Description of the Method */
198: public void save() {
199: for (Iterator i = rules.keySet().iterator(); i.hasNext();) {
200: Rule rule = (Rule) i.next();
201: props.setString(rule.getName(), Boolean.toString(get(rule)
202: .isSelected()));
203: }
204: props.save();
205: }
206:
207: /**
208: * Description of the Method
209: *
210: *@param name Description of the Parameter
211: *@return Description of the Return Value
212: */
213: private JCheckBox createCheckBox(String name) {
214: JCheckBox box = new JCheckBox(name);
215: try {
216: box.setSelected(props.getBoolean(name));
217: } catch (Exception e) {
218: box.setSelected(true);
219: }
220: return box;
221: }
222:
223: /**
224: * Adds a feature to the RuleSet2Rules attribute of the SelectedRules object
225: *
226: *@param rs The feature to be added to the RuleSet2Rules attribute
227: */
228: private void addRuleSet2Rules(RuleSet rs) {
229: for (Iterator j = rs.getRules().iterator(); j.hasNext();) {
230: Rule rule = (Rule) j.next();
231: rules.remove(rule); // FIXME? this shouldn't be necessary?!?!
232: rules.put(rule, createCheckBox(rule.getName()));
233: }
234: }
235: }
|