1 19 20 package org.netbeans.modules.i18n; 21 22 import org.openide.ErrorManager; 23 import org.openide.util.*; 24 import org.netbeans.api.project.Project; 25 import java.util.*; 26 import org.openide.nodes.Node; 27 import org.netbeans.api.java.classpath.ClassPath; 28 import org.netbeans.api.java.queries.SourceForBinaryQuery; 29 import org.openide.filesystems.FileObject; 30 import org.openide.loaders.DataObject; 31 import org.netbeans.api.project.FileOwnerQuery; 32 33 38 public class Util { 39 40 public static String getString(String key) { 41 return NbBundle.getMessage(Util.class, key); 42 } 43 44 public static char getChar(String key) { 45 return getString(key).charAt(0); 46 } 47 48 51 public static void debug(Throwable t) { 52 ErrorManager err = ErrorManager.getDefault(); 53 err.notify(err.INFORMATIONAL, t); 54 } 55 56 59 public static void debug(String annotation, Throwable t) { 60 ErrorManager err = ErrorManager.getDefault(); 61 err.annotate(t, err.INFORMATIONAL, annotation, null, null, null); 62 err.notify(err.INFORMATIONAL, t); 63 } 64 65 public static Project getProjectFor(DataObject dobj) { 66 Project prj = null; 67 FileObject fo = dobj.getPrimaryFile(); 68 return FileOwnerQuery.getOwner(fo); 69 } 70 71 public static Project getProjectFor(Node [] activatedNodes) { 72 Project project = null; 73 74 if (activatedNodes.length > 0) { 75 DataObject dataObject = (DataObject)activatedNodes[0].getCookie(DataObject.class); 76 if(dataObject != null && dataObject.getPrimaryFile() != null) 77 project = FileOwnerQuery.getOwner(dataObject.getPrimaryFile()); 78 } 79 return project; 80 } 81 82 87 public static ClassPath getExecClassPath(FileObject srcFile, FileObject resFile) { 88 ClassPath ecp = ClassPath.getClassPath( srcFile, ClassPath.EXECUTE ); 90 if ((ecp != null) && (ecp.getResourceName( resFile, '.',false) != null)) 91 return ecp; 92 93 94 ClassPath scp = ClassPath.getClassPath( srcFile, ClassPath.SOURCE); 96 if ((scp != null) && (scp.getResourceName( resFile, '.',false) != null)) 98 return scp; 99 100 ClassPath rcp = ClassPath.getClassPath( resFile, ClassPath.SOURCE); 102 if ((rcp!=null) && (rcp.getResourceName( resFile, '.',false) != null)) 104 return rcp; 105 106 107 return null; 108 } 109 110 114 public static FileObject getResource(FileObject srcFile, String bundleName) { 115 ClassPath scp = ClassPath.getClassPath( srcFile, ClassPath.SOURCE); 117 if (scp != null) { 118 FileObject ret = scp.findResource(bundleName); 119 if (ret != null) return ret; 120 } 121 122 ClassPath ecp = ClassPath.getClassPath( srcFile, ClassPath.EXECUTE); 124 Iterator it = ecp.entries().iterator(); 125 while (it.hasNext()) { 126 ClassPath.Entry e = (ClassPath.Entry)it.next(); 127 SourceForBinaryQuery.Result r = SourceForBinaryQuery.findSourceRoots(e.getURL()); 128 FileObject[] sourceRoots = r.getRoots(); 129 for (int i=0; i < sourceRoots.length; i++) { 130 ClassPath cp = ClassPath.getClassPath(sourceRoots[i], ClassPath.SOURCE); 132 if (cp != null) { 133 FileObject ret = cp.findResource(bundleName); 134 if (ret != null) 135 return ret; 136 } 137 } 138 } 139 140 return null; 141 } 142 143 144 149 public static String getResourceName(FileObject srcFile, FileObject resFile, char separator, boolean bpar) { 150 ClassPath ecp = ClassPath.getClassPath( srcFile, ClassPath.EXECUTE ); 152 if (ecp!=null) { 153 String ret = ecp.getResourceName( resFile, separator, bpar); 154 if (ret != null) return ret; 155 } 156 157 ClassPath scp = ClassPath.getClassPath( srcFile, ClassPath.SOURCE ); 158 if (scp!= null) { 159 String ret = scp.getResourceName( resFile, separator, bpar); 160 if (ret!=null) return ret; 161 } 162 163 ClassPath rcp = ClassPath.getClassPath( resFile, ClassPath.SOURCE ); 164 if (rcp != null) { 165 String ret = rcp.getResourceName( resFile, separator, bpar); 166 if (ret!=null) return ret; 167 } 168 169 return null; 170 171 } 172 173 public static boolean isNbBundleAvailable(DataObject srcDataObject) { 174 ClassPath classPath = ClassPath.getClassPath(srcDataObject.getPrimaryFile(), ClassPath.EXECUTE); 180 if (classPath != null && classPath.findResource("org/openide/util/NbBundle.class") != null) return true; 182 183 Project p = FileOwnerQuery.getOwner(srcDataObject.getPrimaryFile()); 185 if (p != null && p.getClass().getName().startsWith("org.netbeans.modules.apisupport.") && p.getClass().getName().endsWith("Project")) return true; 188 189 return false; 190 } 191 } 192 | Popular Tags |