1 19 20 package org.netbeans.modules.i18n.wizard; 21 22 import java.util.*; 23 import org.netbeans.api.queries.VisibilityQuery; 24 import org.netbeans.modules.i18n.FactoryRegistry; 25 import org.netbeans.modules.i18n.I18nUtil; 26 import org.netbeans.modules.properties.PropertiesDataObject; 27 import org.netbeans.api.java.classpath.GlobalPathRegistry; 28 import org.netbeans.api.java.classpath.ClassPath; 29 import org.netbeans.api.project.Project; 30 import org.openide.filesystems.FileStateInvalidException; 31 import org.openide.loaders.DataFolder; 32 import org.openide.loaders.DataObject; 33 import org.openide.loaders.DataObjectNotFoundException; 34 import org.openide.loaders.DataFilter; 35 import org.openide.nodes.Node; 36 import org.openide.nodes.Children; 37 import org.openide.nodes.AbstractNode; 38 import org.openide.nodes.FilterNode; 39 import org.openide.util.*; 40 import org.openide.cookies.EditorCookie; 41 import org.openide.filesystems.FileObject; 42 import org.netbeans.api.project.FileOwnerQuery; 43 44 49 final class Util extends org.netbeans.modules.i18n.Util { 50 51 public static String getString(String key) { 52 return NbBundle.getMessage(org.netbeans.modules.i18n.wizard.Util.class, key); 53 } 54 55 public static char getChar(String key) { 56 return getString(key).charAt(0); 57 } 58 59 61 64 public static Map createWizardSourceMap() { 65 return new TreeMap(new DataObjectComparator()); 66 } 67 68 75 public static Map createWizardSourceMap(Node[] activatedNodes) { 76 Map sourceMap = createWizardSourceMap(); 77 78 if (activatedNodes != null && activatedNodes.length > 0) { 79 final VisibilityQuery visQuery = VisibilityQuery.getDefault(); 80 for (int i = 0; i < activatedNodes.length; i++) { 81 DataObject dobj = (DataObject) activatedNodes[i].getCookie(DataObject.class); 82 if (dobj != null && !visQuery.isVisible(dobj.getPrimaryFile())) { 83 continue; 84 } 85 86 DataObject.Container container = (DataObject.Container) activatedNodes[i].getCookie(DataObject.Container.class); 87 88 if (container != null) { 89 Iterator it = I18nUtil.getAcceptedDataObjects(container).iterator(); 90 91 while(it.hasNext()) { 92 addSource(sourceMap, (DataObject)it.next()); 93 } 94 } 95 96 if (dobj == null) continue; 97 98 if (FactoryRegistry.hasFactory(dobj.getClass())) { 99 addSource(sourceMap, dobj); 100 } 101 } 102 } 103 104 return sourceMap; 105 } 106 107 112 public static void addSource(Map sourceMap, DataObject source) { 113 if(sourceMap.containsKey(source)) 114 return; 115 116 DataFolder folder = source.getFolder(); 117 118 if(folder == null) { 119 sourceMap.put(source, null); 120 return; 121 } 122 123 125 DataObject[] children = folder.getChildren(); 126 127 for(int i = 0; i < children.length; i++) { 128 if(children[i] instanceof PropertiesDataObject) { sourceMap.put(source, new SourceData(children[i])); 130 return; 131 } 132 } 133 134 sourceMap.put(source, null); 136 } 137 138 139 static boolean wizardEnabled(Node[] activatedNodes) { 140 if (activatedNodes == null || activatedNodes.length == 0) { 141 return false; 142 } 143 144 for (int i = 0; i<activatedNodes.length; i++) { 145 Object o; 146 DataObject dobj = null; 147 Node node = activatedNodes[i]; 148 149 161 o = node.getCookie(DataObject.class); 162 if (o != null) { 163 dobj = (DataObject) o; 164 FileObject primaryFile = dobj.getPrimaryFile(); 165 166 boolean isLocal; 167 try { 168 isLocal = !primaryFile.isVirtual() 169 && primaryFile.isValid() 170 && primaryFile.getURL().getProtocol() 171 .equals("file"); } catch (FileStateInvalidException ex) { 173 isLocal = false; 174 } 175 176 if (isLocal == false) { 177 return false; 178 } 179 } 180 181 Object container = node.getCookie(DataObject.Container.class); 182 if (container != null) continue; 183 187 if (dobj == null) return false; 188 189 if (FileOwnerQuery.getOwner(dobj.getPrimaryFile()) == null) return false; 191 } 192 return true; 193 } 194 195 198 private static class DataObjectComparator implements Comparator { 199 200 201 public int compare(Object o1, Object o2) { 202 if(!(o1 instanceof DataObject) || !(o2 instanceof DataObject)) 203 return 0; 204 205 DataObject d1 = (DataObject)o1; 206 DataObject d2 = (DataObject)o2; 207 208 if(d1 == d2) 209 return 0; 210 211 if(d1 == null) 212 return -1; 213 214 if(d2 == null) 215 return 1; 216 217 return d1.getPrimaryFile().getPath().compareTo( d2.getPrimaryFile().getPath() ); 219 } 220 221 222 public boolean equals(Object obj) { 223 if(this == obj) 224 return true; 225 else 226 return false; 227 } 228 } 229 230 } 231 | Popular Tags |