KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > retouche > hints > infrastructure > RulesManager


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.retouche.hints.infrastructure;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.logging.Level JavaDoc;
31 import java.util.logging.Logger JavaDoc;
32 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
33 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
34 import javax.swing.tree.DefaultTreeModel JavaDoc;
35 import javax.swing.tree.DefaultTreeModel JavaDoc;
36 import javax.swing.tree.DefaultTreeModel JavaDoc;
37 import javax.swing.tree.TreeModel JavaDoc;
38 import org.netbeans.modules.retouche.hints.Pair;
39 import org.netbeans.modules.retouche.hints.spi.ErrorRule;
40 import org.netbeans.modules.retouche.hints.spi.Rule;
41 import org.netbeans.modules.retouche.hints.spi.TreeRule;
42 import org.openide.cookies.InstanceCookie;
43 import org.openide.filesystems.FileObject;
44 import org.openide.filesystems.FileSystem;
45 import org.openide.filesystems.Repository;
46 import org.openide.loaders.DataObject;
47 import org.openide.loaders.DataObjectNotFoundException;
48
49 /** Manages rules read from the system filesystem.
50  *
51  * @todo This class was lobotomized for GSF; restore
52  *
53  * @author Petr Hrebejk
54  */

55 public class RulesManager {
56
57     // The logger
58
public static Logger JavaDoc LOG = Logger.getLogger("org.netbeans.modules.retouche.hints"); // NOI18N
59

60     // Extensions of files
61
private static final String JavaDoc INSTANCE_EXT = ".instance";
62
63     private static final String JavaDoc RULES_FOLDER = "org-netbeans-modules-retouche-hints/rules/"; // NOI18N
64
private static final String JavaDoc ERRORS = "errors"; // NOI18N
65
private static final String JavaDoc HINTS = "hints"; // NOI18N
66
private static final String JavaDoc SUGGESTIONS = "suggestions"; // NOI18N
67

68     // Maps of registered rules
69
private static Map JavaDoc<String JavaDoc,List JavaDoc<ErrorRule>> errors = new HashMap JavaDoc<String JavaDoc, List JavaDoc<ErrorRule>>();
70     //private static Map<Tree.Kind,List<TreeRule>> hints = new HashMap<Tree.Kind,List<TreeRule>>();
71
//private static Map<Tree.Kind,List<TreeRule>> suggestions = new HashMap<Tree.Kind, List<TreeRule>>();
72

73     // Tree models for the settings GUI
74
private static TreeModel JavaDoc errorsTreeModel;
75     private static TreeModel JavaDoc hintsTreeModel;
76     private static TreeModel JavaDoc suggestionsTreeModel;
77
78     private static RulesManager INSTANCE;
79
80     private RulesManager() {
81         // XXX Start listening on the rules folder. To handle module set changes.
82
initErrors();
83         initHints();
84         initSuggestions();
85     }
86
87     public static synchronized RulesManager getInstance() {
88         if ( INSTANCE == null ) {
89             INSTANCE = new RulesManager();
90         }
91         return INSTANCE;
92     }
93
94     public Map JavaDoc<String JavaDoc,List JavaDoc<ErrorRule>> getErrors() {
95         return errors;
96     }
97
98 // public Map<Tree.Kind,List<TreeRule>> getHints() {
99
// return hints;
100
// }
101
//
102
// public Map<Tree.Kind,List<TreeRule>> getSuggestions() {
103
// return suggestions;
104
// }
105

106     public TreeModel JavaDoc getErrorsTreeModel() {
107         return errorsTreeModel;
108     }
109
110     public TreeModel JavaDoc getHintsTreeModel() {
111         return hintsTreeModel;
112     }
113
114     public TreeModel JavaDoc getSuggestionsTreeModel() {
115         return suggestionsTreeModel;
116     }
117
118     // Private methods ---------------------------------------------------------
119

120     private static void initErrors() {
121         DefaultMutableTreeNode JavaDoc rootNode = new DefaultMutableTreeNode JavaDoc();
122         errorsTreeModel = new DefaultTreeModel JavaDoc( rootNode );
123         FileSystem fs = Repository.getDefault().getDefaultFileSystem();
124         FileObject folder = fs.getRoot().getFileObject( RULES_FOLDER + ERRORS );
125         List JavaDoc<Pair<Rule,FileObject>> rules = readRules( folder );
126         categorizeErrorRules(rules, errors, folder, rootNode);
127     }
128
129     private static void initHints() {
130         DefaultMutableTreeNode JavaDoc rootNode = new DefaultMutableTreeNode JavaDoc();
131         hintsTreeModel = new DefaultTreeModel JavaDoc( rootNode );
132         FileSystem fs = Repository.getDefault().getDefaultFileSystem();
133         FileObject folder = fs.getRoot().getFileObject( RULES_FOLDER + HINTS );
134         List JavaDoc<Pair<Rule,FileObject>> rules = readRules(folder);
135         //categorizeTreeRules( rules, hints, folder, rootNode );
136
}
137
138
139     private static void initSuggestions() {
140         DefaultMutableTreeNode JavaDoc rootNode = new DefaultMutableTreeNode JavaDoc();
141         suggestionsTreeModel = new DefaultTreeModel JavaDoc( rootNode );
142         FileSystem fs = Repository.getDefault().getDefaultFileSystem();
143         FileObject folder = fs.getRoot().getFileObject( RULES_FOLDER + SUGGESTIONS );
144         List JavaDoc<Pair<Rule,FileObject>> rules = readRules(folder);
145         //categorizeTreeRules(rules, suggestions, folder, rootNode);
146
}
147
148     /** Read rules from system filesystem */
149     private static List JavaDoc<Pair<Rule,FileObject>> readRules( FileObject folder ) {
150
151         List JavaDoc<Pair<Rule,FileObject>> rules = new LinkedList JavaDoc<Pair<Rule,FileObject>>();
152         
153         if (folder == null) {
154             return rules;
155         }
156
157         HashMap JavaDoc<FileObject,DefaultMutableTreeNode JavaDoc> dir2node = new HashMap JavaDoc<FileObject,DefaultMutableTreeNode JavaDoc>();
158
159         // XXX Probably not he best order
160
Enumeration JavaDoc e = folder.getData( true );
161         while( e.hasMoreElements() ) {
162             FileObject o = (FileObject)e.nextElement();
163             String JavaDoc name = o.getNameExt().toLowerCase();
164
165             if ( o.canRead() ) {
166                 Rule r = null;
167                 if ( name.endsWith( INSTANCE_EXT ) ) {
168                     r = instantiateRule(o);
169                 }
170                 if ( r != null ) {
171                     rules.add( new Pair<Rule,FileObject>( r, o ) );
172                 }
173             }
174         }
175         return rules;
176     }
177
178     private static void categorizeErrorRules( List JavaDoc<Pair<Rule,FileObject>> rules,
179                                              Map JavaDoc<String JavaDoc,List JavaDoc<ErrorRule>> dest,
180                                              FileObject rootFolder,
181                                              DefaultMutableTreeNode JavaDoc rootNode ) {
182
183         Map JavaDoc<FileObject,DefaultMutableTreeNode JavaDoc> dir2node = new HashMap JavaDoc<FileObject, DefaultMutableTreeNode JavaDoc>();
184         dir2node.put(rootFolder, rootNode);
185
186         for( Pair<Rule,FileObject> pair : rules ) {
187             Rule rule = pair.getA();
188             FileObject fo = pair.getB();
189
190             if ( rule instanceof ErrorRule ) {
191                 addRule( (ErrorRule)rule, dest );
192                 FileObject parent = fo.getParent();
193                 DefaultMutableTreeNode JavaDoc category = dir2node.get( parent );
194                 if ( category == null ) {
195                     category = new DefaultMutableTreeNode JavaDoc( parent );
196                     rootNode.add( category );
197                     dir2node.put( parent, category );
198                 }
199                 category.add( new DefaultMutableTreeNode JavaDoc( rule, false ) );
200             }
201             else {
202                 LOG.log( Level.WARNING, "The rule defined in " + fo.getPath() + "is not instance of ErrorRule" );
203             }
204         }
205     }
206
207 // private static void categorizeTreeRules( List<Pair<Rule,FileObject>> rules,
208
// Map<Tree.Kind,List<TreeRule>> dest,
209
// FileObject rootFolder,
210
// DefaultMutableTreeNode rootNode ) {
211
//
212
// Map<FileObject,DefaultMutableTreeNode> dir2node = new HashMap<FileObject, DefaultMutableTreeNode>();
213
// dir2node.put(rootFolder, rootNode);
214
//
215
// for( Pair<Rule,FileObject> pair : rules ) {
216
// Rule rule = pair.getA();
217
// FileObject fo = pair.getB();
218
//
219
// if ( rule instanceof TreeRule ) {
220
// addRule( (TreeRule)rule, dest );
221
// FileObject parent = fo.getParent();
222
// DefaultMutableTreeNode category = dir2node.get( parent );
223
// if ( category == null ) {
224
// category = new DefaultMutableTreeNode( parent );
225
// rootNode.add( category );
226
// dir2node.put( parent, category );
227
// }
228
// category.add( new DefaultMutableTreeNode( rule, false ) );
229
// }
230
// else {
231
// LOG.log( Level.WARNING, "The rule defined in " + fo.getPath() + "is not instance of TreeRule" );
232
// }
233
//
234
// }
235
// }
236
//
237
// private static void addRule( TreeRule rule, Map<Tree.Kind,List<TreeRule>> dest ) {
238
//
239
// for( Tree.Kind kind : rule.getTreeKinds() ) {
240
// List<TreeRule> l = dest.get( kind );
241
// if ( l == null ) {
242
// l = new LinkedList<TreeRule>();
243
// dest.put( kind, l );
244
// }
245
// l.add( rule );
246
// }
247
//
248
// }
249

250     @SuppressWarnings JavaDoc("unchecked")
251     private static void addRule( ErrorRule rule, Map JavaDoc<String JavaDoc,List JavaDoc<ErrorRule>> dest ) {
252
253         for(String JavaDoc code : (Set JavaDoc<String JavaDoc>) rule.getCodes()) {
254             List JavaDoc<ErrorRule> l = dest.get( code );
255             if ( l == null ) {
256                 l = new LinkedList JavaDoc<ErrorRule>();
257                 dest.put( code, l );
258             }
259             l.add( rule );
260         }
261
262     }
263
264     private static Rule instantiateRule( FileObject fileObject ) {
265         try {
266             DataObject dobj = DataObject.find(fileObject);
267             InstanceCookie ic = dobj.getCookie( InstanceCookie.class );
268             Object JavaDoc instance = ic.instanceCreate();
269             
270             if (instance instanceof Rule) {
271                 return (Rule) instance;
272             } else {
273                 return null;
274             }
275         } catch( IOException JavaDoc e ) {
276             LOG.log(Level.INFO, null, e);
277         } catch ( ClassNotFoundException JavaDoc e ) {
278             LOG.log(Level.INFO, null, e);
279         }
280
281         return null;
282     }
283 }
284
Popular Tags