1 19 20 package org.netbeans.modules.j2ee.common; 21 22 import java.awt.Component ; 23 import javax.swing.JLabel ; 24 import java.awt.Container ; 25 import java.io.File ; 26 import java.net.URL ; 27 import java.util.ArrayList ; 28 import javax.swing.JComponent ; 29 import java.util.Iterator ; 30 import java.util.Collection ; 31 import java.util.Collections ; 32 import java.util.HashMap ; 33 import java.util.HashSet ; 34 import java.util.List ; 35 import java.util.Map ; 36 import java.util.Set ; 37 import org.netbeans.api.java.classpath.ClassPath; 38 import org.netbeans.api.java.project.JavaProjectConstants; 39 import org.netbeans.api.java.queries.UnitTestForSourceQuery; 40 import org.netbeans.api.project.Project; 41 import org.netbeans.api.project.ProjectUtils; 42 import org.netbeans.api.project.SourceGroup; 43 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment; 44 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule; 45 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform; 46 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 47 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 48 import org.netbeans.spi.java.queries.SourceLevelQueryImplementation; 49 import org.openide.ErrorManager; 50 51 import org.openide.filesystems.FileObject; 52 import org.openide.filesystems.URLMapper; 53 54 public class Util { 55 56 59 public static void changeLabelInComponent(JComponent component, String oldLabel, String newLabel) { 60 JLabel label = findLabel(component, oldLabel); 61 if(label != null) { 62 label.setText(newLabel); 63 } 64 } 65 66 69 public static void hideLabelAndLabelFor(JComponent component, String lab) { 70 JLabel label = findLabel(component, lab); 71 if(label != null) { 72 label.setVisible(false); 73 Component c = label.getLabelFor(); 74 if(c != null) { 75 c.setVisible(false); 76 } 77 } 78 } 79 80 83 public static void getAllComponents( Component [] components, Collection <Component > allComponents ) { 84 for( int i = 0; i < components.length; i++ ) { 85 if( components[i] != null ) { 86 allComponents.add( components[i] ); 87 if( ( ( Container )components[i] ).getComponentCount() != 0 ) { 88 getAllComponents( ( ( Container )components[i] ).getComponents(), allComponents ); 89 } 90 } 91 } 92 } 93 94 97 public static JLabel findLabel(JComponent comp, String labelText) { 98 List <Component > allComponents = new ArrayList <Component >(); 99 getAllComponents(comp.getComponents(), allComponents); 100 Iterator <Component > iterator = allComponents.iterator(); 101 while(iterator.hasNext()) { 102 Component c = iterator.next(); 103 if(c instanceof JLabel ) { 104 JLabel label = (JLabel )c; 105 if(label.getText().equals(labelText)) { 106 return label; 107 } 108 } 109 } 110 return null; 111 } 112 113 120 public static SourceGroup[] getJavaSourceGroups(Project project) { 121 SourceGroup[] sourceGroups = ProjectUtils.getSources(project).getSourceGroups( 122 JavaProjectConstants.SOURCES_TYPE_JAVA); 123 Set <SourceGroup> testGroups = getTestSourceGroups(sourceGroups); 124 List <SourceGroup> result = new ArrayList <SourceGroup>(); 125 for (int i = 0; i < sourceGroups.length; i++) { 126 if (!testGroups.contains(sourceGroups[i])) { 127 result.add(sourceGroups[i]); 128 } 129 } 130 return result.toArray(new SourceGroup[result.size()]); 131 } 132 133 private static Set <SourceGroup> getTestSourceGroups(SourceGroup[] sourceGroups) { 134 Map <FileObject, SourceGroup> foldersToSourceGroupsMap = createFoldersToSourceGroupsMap(sourceGroups); 135 Set <SourceGroup> testGroups = new HashSet <SourceGroup>(); 136 for (int i = 0; i < sourceGroups.length; i++) { 137 testGroups.addAll(getTestTargets(sourceGroups[i], foldersToSourceGroupsMap)); 138 } 139 return testGroups; 140 } 141 142 private static Map <FileObject, SourceGroup> createFoldersToSourceGroupsMap(final SourceGroup[] sourceGroups) { 143 Map <FileObject, SourceGroup> result; 144 if (sourceGroups.length == 0) { 145 result = Collections.<FileObject, SourceGroup>emptyMap(); 146 } else { 147 result = new HashMap <FileObject, SourceGroup>(2 * sourceGroups.length, .5f); 148 for (int i = 0; i < sourceGroups.length; i++) { 149 SourceGroup sourceGroup = sourceGroups[i]; 150 result.put(sourceGroup.getRootFolder(), sourceGroup); 151 } 152 } 153 return result; 154 } 155 156 private static List <FileObject> getFileObjects(URL [] urls) { 157 List <FileObject> result = new ArrayList <FileObject>(); 158 for (int i = 0; i < urls.length; i++) { 159 FileObject sourceRoot = URLMapper.findFileObject(urls[i]); 160 if (sourceRoot != null) { 161 result.add(sourceRoot); 162 } else { 163 int severity = ErrorManager.INFORMATIONAL; 164 if (ErrorManager.getDefault().isNotifiable(severity)) { 165 ErrorManager.getDefault().notify(severity, new IllegalStateException ( 166 "No FileObject found for the following URL: " + urls[i])); } 168 } 169 } 170 return result; 171 } 172 173 private static List <SourceGroup> getTestTargets(SourceGroup sourceGroup, Map <FileObject, SourceGroup> foldersToSourceGroupsMap) { 174 final URL [] rootURLs = UnitTestForSourceQuery.findUnitTests(sourceGroup.getRootFolder()); 175 if (rootURLs.length == 0) { 176 return new ArrayList <SourceGroup>(); 177 } 178 List <SourceGroup> result = new ArrayList <SourceGroup>(); 179 List <FileObject> sourceRoots = getFileObjects(rootURLs); 180 for (int i = 0; i < sourceRoots.size(); i++) { 181 FileObject sourceRoot = sourceRoots.get(i); 182 SourceGroup srcGroup = foldersToSourceGroupsMap.get(sourceRoot); 183 if (srcGroup != null) { 184 result.add(srcGroup); 185 } 186 } 187 return result; 188 } 189 190 public static ClassPath getFullClasspath(FileObject fo) { 191 if (fo == null) { 192 return null; 193 } 194 return ClassPathSupport.createProxyClassPath(new ClassPath[]{ 195 ClassPath.getClassPath(fo, ClassPath.SOURCE), 196 ClassPath.getClassPath(fo, ClassPath.BOOT), 197 ClassPath.getClassPath(fo, ClassPath.COMPILE) 198 }); 199 } 200 201 207 public static boolean isJavaEE5orHigher(Project project) { 208 if (project == null) { 209 return false; 210 } 211 J2eeModuleProvider j2eeModuleProvider = project.getLookup().lookup(J2eeModuleProvider.class); 212 if (j2eeModuleProvider != null) { 213 J2eeModule j2eeModule = j2eeModuleProvider.getJ2eeModule(); 214 if (j2eeModule != null) { 215 Object type = j2eeModule.getModuleType(); 216 double version = Double.parseDouble(j2eeModule.getModuleVersion()); 217 if (J2eeModule.EJB.equals(type) && (version > 2.1)) { 218 return true; 219 }; 220 if (J2eeModule.WAR.equals(type) && (version > 2.4)) { 221 return true; 222 } 223 if (J2eeModule.CLIENT.equals(type) && (version > 1.4)) { 224 return true; 225 } 226 } 227 } 228 return false; 229 } 230 231 237 public static String getSourceLevel(Project project) { 238 SourceLevelQueryImplementation sl = project.getLookup().lookup(SourceLevelQueryImplementation.class); 239 return sl.getSourceLevel(project.getProjectDirectory()); 240 } 241 242 248 public static boolean isSourceLevel14orLower(Project project) { 249 String srcLevel = getSourceLevel(project); 250 if (srcLevel != null) { 251 double sourceLevel = Double.parseDouble(srcLevel); 252 return (sourceLevel <= 1.4); 253 } else 254 return false; 255 } 256 257 263 public static boolean isSourceLevel16orHigher(Project project) { 264 String srcLevel = getSourceLevel(project); 265 if (srcLevel != null) { 266 double sourceLevel = Double.parseDouble(srcLevel); 267 return (sourceLevel >= 1.6); 268 } else 269 return false; 270 } 271 272 282 public static boolean isValidServerInstance(Project project) { 283 J2eeModuleProvider j2eeModuleProvider = project.getLookup().lookup(J2eeModuleProvider.class); 284 if (j2eeModuleProvider == null) { 285 return false; 286 } 287 return isValidServerInstance(j2eeModuleProvider); 288 } 289 290 300 public static boolean isValidServerInstance(J2eeModuleProvider j2eeModuleProvider) { 301 String serverInstanceID = j2eeModuleProvider.getServerInstanceID(); 302 if (serverInstanceID == null) { 303 return false; 304 } 305 return Deployment.getDefault().getServerID(serverInstanceID) != null; 306 } 307 308 public static File [] getJ2eePlatformClasspathEntries(Project project) { 309 List <FileObject> j2eePlatformRoots = new ArrayList <FileObject>(); 310 if (project != null) { 311 J2eeModuleProvider j2eeModuleProvider = project.getLookup().lookup(J2eeModuleProvider.class); 312 if (j2eeModuleProvider != null) { 313 J2eePlatform j2eePlatform = Deployment.getDefault().getJ2eePlatform(j2eeModuleProvider.getServerInstanceID()); 314 if (j2eePlatform != null) { 315 return j2eePlatform.getClasspathEntries(); 316 } 317 } 318 } 319 return new File [0]; 320 } 321 322 } 323 | Popular Tags |