1 19 20 package org.netbeans.modules.retouche.hints.infrastructure; 21 22 import java.io.IOException ; 23 import java.util.Enumeration ; 24 import java.util.HashMap ; 25 import java.util.LinkedList ; 26 import java.util.List ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.Set ; 30 import java.util.logging.Level ; 31 import java.util.logging.Logger ; 32 import javax.swing.tree.DefaultMutableTreeNode ; 33 import javax.swing.tree.DefaultMutableTreeNode ; 34 import javax.swing.tree.DefaultTreeModel ; 35 import javax.swing.tree.DefaultTreeModel ; 36 import javax.swing.tree.DefaultTreeModel ; 37 import javax.swing.tree.TreeModel ; 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 55 public class RulesManager { 56 57 public static Logger LOG = Logger.getLogger("org.netbeans.modules.retouche.hints"); 60 private static final String INSTANCE_EXT = ".instance"; 62 63 private static final String RULES_FOLDER = "org-netbeans-modules-retouche-hints/rules/"; private static final String ERRORS = "errors"; private static final String HINTS = "hints"; private static final String SUGGESTIONS = "suggestions"; 68 private static Map <String ,List <ErrorRule>> errors = new HashMap <String , List <ErrorRule>>(); 70 73 private static TreeModel errorsTreeModel; 75 private static TreeModel hintsTreeModel; 76 private static TreeModel suggestionsTreeModel; 77 78 private static RulesManager INSTANCE; 79 80 private RulesManager() { 81 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 <String ,List <ErrorRule>> getErrors() { 95 return errors; 96 } 97 98 106 public TreeModel getErrorsTreeModel() { 107 return errorsTreeModel; 108 } 109 110 public TreeModel getHintsTreeModel() { 111 return hintsTreeModel; 112 } 113 114 public TreeModel getSuggestionsTreeModel() { 115 return suggestionsTreeModel; 116 } 117 118 120 private static void initErrors() { 121 DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode (); 122 errorsTreeModel = new DefaultTreeModel ( rootNode ); 123 FileSystem fs = Repository.getDefault().getDefaultFileSystem(); 124 FileObject folder = fs.getRoot().getFileObject( RULES_FOLDER + ERRORS ); 125 List <Pair<Rule,FileObject>> rules = readRules( folder ); 126 categorizeErrorRules(rules, errors, folder, rootNode); 127 } 128 129 private static void initHints() { 130 DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode (); 131 hintsTreeModel = new DefaultTreeModel ( rootNode ); 132 FileSystem fs = Repository.getDefault().getDefaultFileSystem(); 133 FileObject folder = fs.getRoot().getFileObject( RULES_FOLDER + HINTS ); 134 List <Pair<Rule,FileObject>> rules = readRules(folder); 135 } 137 138 139 private static void initSuggestions() { 140 DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode (); 141 suggestionsTreeModel = new DefaultTreeModel ( rootNode ); 142 FileSystem fs = Repository.getDefault().getDefaultFileSystem(); 143 FileObject folder = fs.getRoot().getFileObject( RULES_FOLDER + SUGGESTIONS ); 144 List <Pair<Rule,FileObject>> rules = readRules(folder); 145 } 147 148 149 private static List <Pair<Rule,FileObject>> readRules( FileObject folder ) { 150 151 List <Pair<Rule,FileObject>> rules = new LinkedList <Pair<Rule,FileObject>>(); 152 153 if (folder == null) { 154 return rules; 155 } 156 157 HashMap <FileObject,DefaultMutableTreeNode > dir2node = new HashMap <FileObject,DefaultMutableTreeNode >(); 158 159 Enumeration e = folder.getData( true ); 161 while( e.hasMoreElements() ) { 162 FileObject o = (FileObject)e.nextElement(); 163 String 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 <Pair<Rule,FileObject>> rules, 179 Map <String ,List <ErrorRule>> dest, 180 FileObject rootFolder, 181 DefaultMutableTreeNode rootNode ) { 182 183 Map <FileObject,DefaultMutableTreeNode > dir2node = new HashMap <FileObject, DefaultMutableTreeNode >(); 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 category = dir2node.get( parent ); 194 if ( category == null ) { 195 category = new DefaultMutableTreeNode ( parent ); 196 rootNode.add( category ); 197 dir2node.put( parent, category ); 198 } 199 category.add( new DefaultMutableTreeNode ( 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 250 @SuppressWarnings ("unchecked") 251 private static void addRule( ErrorRule rule, Map <String ,List <ErrorRule>> dest ) { 252 253 for(String code : (Set <String >) rule.getCodes()) { 254 List <ErrorRule> l = dest.get( code ); 255 if ( l == null ) { 256 l = new LinkedList <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 instance = ic.instanceCreate(); 269 270 if (instance instanceof Rule) { 271 return (Rule) instance; 272 } else { 273 return null; 274 } 275 } catch( IOException e ) { 276 LOG.log(Level.INFO, null, e); 277 } catch ( ClassNotFoundException e ) { 278 LOG.log(Level.INFO, null, e); 279 } 280 281 return null; 282 } 283 } 284 | Popular Tags |