1 19 20 package org.netbeans.modules.xml.schema.ui.nodes.categorized; 21 22 import java.awt.Image ; 23 import java.io.Serializable ; 24 import java.util.ArrayList ; 25 import java.util.Comparator ; 26 import java.util.LinkedHashMap ; 27 import java.util.List ; 28 import org.openide.nodes.Node; 29 import org.netbeans.modules.xml.xam.Named; 30 import org.netbeans.modules.xml.schema.model.Annotation; 31 import org.netbeans.modules.xml.schema.model.Notation; 32 import org.netbeans.modules.xml.schema.model.SchemaModelReference; 33 import org.netbeans.modules.xml.schema.model.GlobalAttribute; 34 import org.netbeans.modules.xml.schema.model.GlobalAttributeGroup; 35 import org.netbeans.modules.xml.schema.model.GlobalComplexType; 36 import org.netbeans.modules.xml.schema.model.GlobalElement; 37 import org.netbeans.modules.xml.schema.model.GlobalGroup; 38 import org.netbeans.modules.xml.schema.model.GlobalSimpleType; 39 import org.netbeans.modules.xml.schema.model.Schema; 40 import org.netbeans.modules.xml.schema.model.SchemaComponent; 41 import org.netbeans.modules.xml.schema.model.SchemaComponentReference; 42 import org.netbeans.modules.xml.schema.model.SchemaModel; 43 import org.netbeans.modules.xml.schema.ui.nodes.FilteredSchemaComponentNodeChildren; 44 import org.netbeans.modules.xml.schema.ui.nodes.ReadOnlyCookie; 45 import org.netbeans.modules.xml.schema.ui.nodes.RefreshableChildren; 46 import org.netbeans.modules.xml.schema.ui.nodes.SchemaUIContext; 47 import org.netbeans.modules.xml.schema.ui.nodes.StructuralSchemaNodeFactory; 48 import org.openide.filesystems.FileObject; 49 import org.openide.filesystems.Repository; 50 import org.openide.loaders.DataObject; 51 import org.openide.loaders.DataObjectNotFoundException; 52 import org.openide.util.Utilities; 53 54 58 public class CategorizedChildren<C extends SchemaComponent> 59 extends RefreshableChildren 60 { 61 65 public CategorizedChildren(SchemaUIContext context, 66 SchemaComponentReference<C> reference) 67 { 68 this(context,reference,null); 69 } 70 71 72 76 public CategorizedChildren(SchemaUIContext context, 77 SchemaComponentReference<C> reference, 78 List <Class <? extends SchemaComponent>> childFilters) 79 { 80 super(); 81 this.context=context; 82 this.reference=reference; 83 this.childFilters=childFilters; 84 85 extension = new CategorizedChildrenExtension(); 86 87 SchemaModel model = reference.get().getModel(); 89 structuralContext=new SchemaUIContext( 90 model, 91 new StructuralSchemaNodeFactory(model, 92 context.getLookup()), 93 context.getLookup()); 94 95 } 107 108 109 113 public SchemaUIContext getContext() 114 { 115 return context; 116 } 117 118 119 123 protected SchemaUIContext getStructuralContext() 124 { 125 return structuralContext; 126 } 127 128 132 public SchemaComponentReference<C> getReference() 133 { 134 return reference; 135 } 136 137 138 142 public List <Class <? extends SchemaComponent>> getChildFilters() 143 { 144 return childFilters; 145 } 146 147 148 152 @Override 153 protected void addNotify() 154 { 155 super.addNotify(); 156 refreshChildren(); 157 } 158 159 160 164 @Override 165 protected void removeNotify() 166 { 167 super.removeNotify(); 168 super.nodes.clear(); 169 refresh(); 170 } 171 172 173 177 public void refreshChildren() 178 { 179 setKeys(createKeys()); 180 } 181 182 183 187 private boolean isChildAllowed( 188 Class <? extends SchemaComponent> componentClass) 189 { 190 if (getChildFilters()==null) 192 return true; 193 194 for (Class <? extends SchemaComponent> clazz: getChildFilters()) 195 { 196 if (clazz.isAssignableFrom(componentClass)) 197 return true; 198 } 199 200 return false; 201 } 202 203 204 208 protected List <Node> createKeys() 209 { 210 C parentComponent=getReference().get(); 211 212 List <Node> keys=new ArrayList <Node>(); 213 214 ReadOnlyCookie roc = (ReadOnlyCookie) getContext().getLookup().lookup( 215 ReadOnlyCookie.class); 216 boolean readOnly = roc!=null && roc.isReadOnly(); 217 if (!readOnly && getChildFilters() != null && 226 isChildAllowed(PrimitiveSimpleType.class)) { 227 keys.addAll(extension.getExtension(parentComponent, getContext())); 228 } 229 230 if(parentComponent instanceof Schema) 232 { 233 for (SchemaComponent child: parentComponent.getChildren(UNCATEGORIZED_TYPES)) 235 { 236 if(isChildAllowed(child.getComponentType())) 237 { 238 Node node=getContext().getFactory().createNode(child); 239 keys.add(node); 240 } 241 } 242 for(Class <? extends SchemaComponent> componentType:CATEGORIZED_TYPES.keySet()) 244 { 245 if(!isChildAllowed(componentType)) continue; 247 if(readOnly && parentComponent.getChildren(componentType).isEmpty()) 250 continue; 251 CategoryNode node=new CategoryNode( 252 getContext(), 253 getReference(), 254 componentType, 255 new FilteredSchemaComponentNodeChildren<C>( 256 getContext(),getReference(),componentType, 257 new SchemaComponentComparator())); 258 259 String badge = CATEGORIZED_TYPES.get(componentType); 260 node.setBadge(badge); 261 keys.add(node); 262 } 263 } 264 else 265 { 266 for (SchemaComponent child: parentComponent.getChildren()) 268 { 269 if(isChildAllowed(child.getComponentType())) 270 { 271 Node node=getContext().getFactory().createNode(child); 272 keys.add(node); 273 } 274 } 275 } 276 277 return keys; 278 } 279 280 281 285 @Override 286 protected Node[] createNodes(Object key) 287 { 288 Node[] result=null; 289 290 if (key instanceof Node) 291 result=new Node[] { (Node)key }; 292 293 return result; 294 } 295 296 297 298 299 303 307 private static class SchemaComponentComparator 308 implements Comparator <SchemaComponent>, Serializable { 309 private static final long serialVersionUID = 1L; 310 311 315 public int compare(SchemaComponent comp1, 316 SchemaComponent comp2) 317 { 318 boolean ref1Named=comp1 instanceof Named; 319 boolean ref2Named=comp2 instanceof Named; 320 321 if (ref1Named && ref2Named) 322 { 323 String ref1Name=((Named)comp1).getName(); 324 String ref2Name=((Named)comp2).getName(); 325 326 if (ref1Name!=null && ref2Name!=null) 327 return ref1Name.compareTo(ref2Name); 328 else 329 { 330 if (ref1Name!=null) 332 return -1; 333 else 334 return 1; 335 } 336 } 337 else 338 { 339 if (ref1Named) 341 return -1; 342 else 343 return 1; 344 } 345 } 346 } 347 348 349 350 private static Node getFolderNode() { 351 FileObject fo = 352 Repository.getDefault().getDefaultFileSystem().getRoot(); 353 Node n = null; 354 try { 355 DataObject dobj = DataObject.find(fo); 356 n = dobj.getNodeDelegate(); 357 } catch (DataObjectNotFoundException ex) { 358 } 361 return n; 362 } 363 364 public static Image getBadgedFolderIcon(int type, Class <? extends SchemaComponent> _class) { 365 Node n = getFolderNode(); 366 Image i = null; 367 if (n != null) { 368 i = n.getIcon(type); 369 } 370 String badge = CATEGORIZED_TYPES.get(_class); 371 if(badge != null) 372 return badgeImage(i, badge); 373 return null; 374 } 375 376 public static Image getOpenedBadgedFolderIcon(int type, Class <? extends SchemaComponent> _class) { 377 Node n = getFolderNode(); 378 Image i = null; 379 if (n != null) { 380 i = n.getOpenedIcon(type); 381 } 382 String badge = CATEGORIZED_TYPES.get(_class); 383 if(badge != null) 384 return badgeImage(i, badge); 385 return null; 386 } 387 388 389 private static Image badgeImage(Image main, String badge) { 390 Image rv = main; 391 if (badge != null) { 392 Image badgeImage = Utilities.loadImage(badge); 393 rv = Utilities.mergeImages(main, badgeImage, 8, 8); 394 } 395 return rv; 396 } 397 398 399 403 private SchemaUIContext context; 404 private SchemaUIContext structuralContext; 405 private SchemaComponentReference<C> reference; 406 private List <Class <? extends SchemaComponent>> childFilters; 407 private CategorizedChildrenExtension extension; 408 409 private static final java.util.Map <Class <? extends SchemaComponent>, String > CATEGORIZED_TYPES; 412 static { 413 CATEGORIZED_TYPES = new LinkedHashMap <Class <? extends SchemaComponent>, String >(); 414 CATEGORIZED_TYPES.put(GlobalAttribute.class, "org/netbeans/modules/xml/schema/ui/nodes/resources/attribute_badge.png"); CATEGORIZED_TYPES.put(GlobalAttributeGroup.class,"org/netbeans/modules/xml/schema/ui/nodes/resources/attribute_badge.png"); CATEGORIZED_TYPES.put(GlobalComplexType.class,"org/netbeans/modules/xml/schema/ui/nodes/resources/complexType_badge.png"); CATEGORIZED_TYPES.put(GlobalElement.class,"org/netbeans/modules/xml/schema/ui/nodes/resources/element_badge.png"); CATEGORIZED_TYPES.put(GlobalGroup.class,"org/netbeans/modules/xml/schema/ui/nodes/resources/group_badge.png"); CATEGORIZED_TYPES.put(SchemaModelReference.class,"org/netbeans/modules/xml/schema/ui/nodes/resources/referencedSchemas_badge.png"); CATEGORIZED_TYPES.put(GlobalSimpleType.class, "org/netbeans/modules/xml/schema/ui/nodes/resources/simpleType_badge.png"); }; 422 public static final List <Class <? extends SchemaComponent>> UNCATEGORIZED_TYPES; 423 static { 424 UNCATEGORIZED_TYPES = new ArrayList <Class <? extends SchemaComponent>>(2); 425 UNCATEGORIZED_TYPES.add(Annotation.class); 426 UNCATEGORIZED_TYPES.add(Notation.class); 427 }; 428 } 429 | Popular Tags |