1 19 20 package org.netbeans.modules.java; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.Iterator ; 26 import java.util.LinkedList ; 27 import java.util.List ; 28 import org.netbeans.modules.java.ui.nodes.ExElementNodeFactory; 29 import org.netbeans.modules.java.ui.nodes.ExFilterFactory; 30 import org.netbeans.modules.java.ui.nodes.FilterSourceNodeFactory; 31 import org.netbeans.modules.java.ui.nodes.JavaSourceNodeFactory; 32 import org.netbeans.modules.java.ui.nodes.SourceNodeFactory; 33 import org.netbeans.modules.java.ui.nodes.SourceNodes; 34 import org.openide.ErrorManager; 35 import org.openide.cookies.InstanceCookie; 36 import org.openide.loaders.DataFolder; 37 import org.openide.loaders.FolderInstance; 38 import org.openide.src.nodes.ElementNodeFactory; 39 import org.openide.src.nodes.FilterFactory; 40 41 48 final class NodeFactoryPool extends FolderInstance { 49 50 53 ElementNodeFactory base; 54 55 58 LinkedList explicit; 59 60 64 List factories = Collections.EMPTY_LIST; 65 66 69 ElementNodeFactory head; 70 71 74 boolean initialized; 75 76 NodeFactoryPool(DataFolder storage, ElementNodeFactory base) { 77 super(storage); 78 this.base = base; 79 head = base; 80 } 81 82 final Object sync() { 83 return base; 84 } 85 86 90 ElementNodeFactory getHead() { 91 if (!initialized) { 93 recreate(); 94 waitFinished(); 95 initialized = true; 96 } 97 return head; 98 } 99 100 104 protected Object createInstance(InstanceCookie[] cookies) 105 throws java.io.IOException , ClassNotFoundException { 106 List l = new ArrayList (cookies.length + 1); 107 JavaSourceNodeFactory jsnf = new JavaSourceNodeFactory(); 108 jsnf.setGenerateForTree(((JavaElementNodeFactory) base).getGenerateForTree()); 109 l.add(jsnf); 110 for (int i = 0; i < cookies.length; i++) { 111 Object o = cookies[i].instanceCreate(); 112 if (o instanceof FilterSourceNodeFactory) { 113 l.add(o); 114 } else if (o instanceof FilterFactory) { ExFilterFactory eff = new ExFilterFactory((ElementNodeFactory) o); 116 l.add(eff); 117 } 118 } 119 synchronized (sync()) { 120 ElementNodeFactory f = relinkFactories(l); 121 this.factories = l; 122 return head = f; 123 } 124 } 125 126 129 private ElementNodeFactory relinkFactories(Collection first) { 130 Object previousFactory = new ExFilterFactory(base); 131 Iterator it; 132 Collection next = explicit; 133 134 if (first == null) 135 first = factories; 136 137 for (it = first.iterator(); it.hasNext(); ) { 138 Object obj = it.next(); 139 attachFactories(obj, previousFactory); 140 previousFactory = obj; 141 } 142 if (next != null) { 143 for (it = next.iterator(); it.hasNext(); ) { 144 Object obj = it.next(); ExFilterFactory eff = new ExFilterFactory((ElementNodeFactory) obj); 146 attachFactories(previousFactory, eff); 147 previousFactory = obj; 148 } 149 } 150 return previousFactory instanceof ElementNodeFactory? 151 (ElementNodeFactory) previousFactory: 152 SourceNodes.createElementNodeFactory((FilterSourceNodeFactory) previousFactory); 153 } 154 155 160 private static void attachFactories(Object f1, Object f2) { 161 if (f1 instanceof FilterSourceNodeFactory) { 162 FilterSourceNodeFactory f1SNF = (FilterSourceNodeFactory) f1; 163 if (f2 instanceof SourceNodeFactory) { 164 f1SNF.attach((SourceNodeFactory) f2); 165 } else if (f2 instanceof ExElementNodeFactory) { 166 f1SNF.attach(SourceNodes.createSourceNodeFactory((ExElementNodeFactory) f2)); 167 } else { 168 ErrorManager.getDefault().notify( 169 ErrorManager.INFORMATIONAL, new IllegalStateException ("Unsupported factory: " + f2)); } 171 } else if (f1 instanceof ExFilterFactory) { 172 ExFilterFactory f1EFF = (ExFilterFactory) f1; 173 if (f2 instanceof ExElementNodeFactory) { 174 f1EFF.attach((ExElementNodeFactory) f2); 175 } else if (f2 instanceof SourceNodeFactory){ 176 f1EFF.attach(SourceNodes.createElementNodeFactory((SourceNodeFactory) f2)); 177 } else { 178 ErrorManager.getDefault().notify( 179 ErrorManager.INFORMATIONAL, new IllegalStateException ("Unsupported factory: " + f2)); } 181 } else { 182 ErrorManager.getDefault().notify( 183 ErrorManager.INFORMATIONAL, new IllegalStateException ("Unsupported factory: " + f1)); } 185 } 186 187 191 void addFactory(FilterFactory f) { 192 synchronized (sync()) { 193 if (explicit == null) { 194 explicit = new LinkedList (); 195 } 196 explicit.add(f); 197 head = relinkFactories(null); 198 } 199 } 200 201 205 void removeFactory(FilterFactory f) { 206 synchronized (sync()) { 207 if (!explicit.remove(f)) 208 return; 209 relinkFactories(null); 210 } 211 } 212 213 214 } 215 | Popular Tags |