1 19 20 package org.netbeans.modules.xml.xam.ui.customizer; 21 22 import java.awt.EventQueue ; 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.HashMap ; 26 import java.util.List ; 27 import org.openide.filesystems.FileObject; 28 import org.openide.filesystems.FileUtil; 29 import org.openide.loaders.DataObject; 30 import org.openide.loaders.DataObjectNotFoundException; 31 import org.openide.nodes.Children; 32 import org.openide.nodes.Node; 33 import org.openide.util.NbBundle; 34 import org.openide.util.RequestProcessor; 35 import org.netbeans.modules.xml.retriever.catalog.Utilities; 36 import org.netbeans.modules.xml.xam.Model; 37 38 43 public class NamespaceChildren extends Children.Keys { 44 45 private HashMap <String , List <FileObject>> nsFilesMap; 46 47 private FileObject[] rootFolders; 48 49 private ExternalReferenceDecorator decorator; 50 51 57 public NamespaceChildren(FileObject[] roots, 58 ExternalReferenceDecorator decorator) { 59 super(); 60 rootFolders = roots; 61 this.decorator = decorator; 62 nsFilesMap = new HashMap <String , List <FileObject>>(); 63 } 64 65 protected Node[] createNodes(Object key) { 66 if (key == WaitNode.WAIT_KEY) { 67 return WaitNode.createNode(); 68 } else if (key instanceof String ) { 69 List <FileObject> fobjs = nsFilesMap.get(key); 70 if (fobjs != null && !fobjs.isEmpty()) { 71 Node[] filterNodes = new Node[fobjs.size()]; 72 int i = 0; 73 for (FileObject fobj:fobjs) { 74 try { 75 Node node = DataObject.find(fobj).getNodeDelegate(); 76 filterNodes[i++] = decorator.createExternalReferenceNode(node); 77 } catch (DataObjectNotFoundException donfe) { 78 } 79 } 80 Children.Array children = new Children.Array(); 81 children.add(filterNodes); 82 Node node = new NamespaceNode(children, (String ) key, decorator); 83 return new Node[] { node }; 84 } 85 } 86 return new Node[] { }; 87 } 88 89 protected void addNotify() { 90 super.addNotify(); 91 setKeys(WaitNode.getKeys()); 92 RequestProcessor.getDefault().post(new Runnable () { 93 public void run() { 94 for (FileObject root : rootFolders) { 95 java.util.Map <FileObject, String > map = 96 Utilities.getFiles2NSMappingInProj( 97 FileUtil.toFile(root), decorator.getDocumentType()); 98 for (java.util.Map.Entry<FileObject, String > entry : map.entrySet()) { 99 String ns = entry.getValue(); 100 List <FileObject> fobjs = nsFilesMap.get(ns); 101 if (fobjs == null) { 102 fobjs = new ArrayList <FileObject>(); 103 } 104 fobjs.add(entry.getKey()); 105 nsFilesMap.put(ns, fobjs); 106 } 107 } 108 EventQueue.invokeLater(new Runnable () { 111 public void run() { 112 setKeys(nsFilesMap.keySet()); 113 } 114 }); 115 } 116 }); 117 } 118 119 @Override 120 protected void removeNotify() { 121 setKeys(Collections.emptySet()); 122 } 123 124 private static class NamespaceNode extends FolderNode 125 implements ExternalReferenceNode { 126 127 private ExternalReferenceDecorator decorator; 128 129 NamespaceNode(Children children, String myNamespace, 130 ExternalReferenceDecorator decorator) { 131 super(children); 132 this.decorator = decorator; 133 setName(myNamespace); 134 if (Utilities.NO_NAME_SPACE.equals(myNamespace)) { 135 setDisplayName(NbBundle.getMessage(NamespaceNode.class, 136 "LBL_NoTargetNamespace")); 137 } 138 } 139 140 public String getHtmlDisplayName() { 141 String name = getDisplayName(); 142 if (decorator != null) { 143 name = decorator.getHtmlDisplayName(name, this); 144 } 145 return name; 146 } 147 148 public Model getModel() { 149 return null; 150 } 151 152 public String getNamespace() { 153 return getName(); 155 } 156 157 public boolean hasModel() { 158 return false; 159 } 160 } 161 } 162 | Popular Tags |