FileDocCategorySizeDatePackage
CompiledResourcesMonitor.javaAPI DocAndroid 1.5 API9474Wed May 06 22:41:10 BST 2009com.android.ide.eclipse.editors.resources.manager

CompiledResourcesMonitor

public final class CompiledResourcesMonitor extends Object implements com.android.ide.eclipse.editors.resources.manager.ResourceMonitor.IProjectListener, com.android.ide.eclipse.editors.resources.manager.ResourceMonitor.IFileListener
A monitor for the compiled resources. This only monitors changes in the resources of type {@link ResourceType#ID}.

Fields Summary
private static final CompiledResourcesMonitor
sThis
Constructors Summary
private CompiledResourcesMonitor()
private constructor to prevent construction.

    
Methods Summary
public voidfileChanged(org.eclipse.core.resources.IFile file, org.eclipse.core.resources.IMarkerDelta[] markerDeltas, int kind)

        if (file.getName().equals(AndroidConstants.FN_COMPILED_RESOURCE_CLASS)) {
            loadAndParseRClass(file.getProject());
        }
    
private java.lang.StringgetRClassName(org.eclipse.core.resources.IProject project)
Returns the class name of the R class, based on the project's manifest's package.

return
A class name (e.g. "my.app.R") or null if there's no valid package in the manifest.

        try {
            IFile manifestFile = AndroidManifestParser.getManifest(project);
            AndroidManifestParser data = AndroidManifestParser.parseForData(manifestFile);
            if (data != null) {
                String javaPackage = data.getPackage();
                return javaPackage + ".R"; //$NON-NLS-1$
            }
        } catch (CoreException e) {
            // This will typically happen either because the manifest file is not present
            // and/or the workspace needs to be refreshed.
            AdtPlugin.logAndPrintError(e,
                    "Android Resources",
                    "Failed to find the package of the AndroidManifest of project %1$s. Reason: %2$s",
                    project.getName(),
                    e.getMessage());
        }
        return null;
    
private voidloadAndParseRClass(org.eclipse.core.resources.IProject project)

        try {
            // first check there's a ProjectResources to store the content
            ProjectResources projectResources = ResourceManager.getInstance().getProjectResources(
                    project);

            if (projectResources != null) {
                // create the classname
                String className = getRClassName(project);
                if (className == null) {
                    // We need to abort.
                    AdtPlugin.log(IStatus.ERROR,
                            "loadAndParseRClass: failed to find manifest package for project %1$s", //$NON-NLS-1$
                            project.getName());
                    return;
                }

                // create a temporary class loader to load it. 
                ProjectClassLoader loader = new ProjectClassLoader(null /* parentClassLoader */,
                        project);
                
                try {
                    Class<?> clazz = loader.loadClass(className);
                    
                    if (clazz != null) {
                        // create the maps to store the result of the parsing
                        Map<String, Map<String, Integer>> resourceValueMap =
                            new HashMap<String, Map<String, Integer>>();
                        Map<Integer, String[]> genericValueToNameMap =
                            new HashMap<Integer, String[]>();
                        Map<IntArrayWrapper, String> styleableValueToNameMap =
                            new HashMap<IntArrayWrapper, String>();
                        
                        // parse the class
                        if (parseClass(clazz, genericValueToNameMap, styleableValueToNameMap,
                                resourceValueMap)) {
                            // now we associate the maps to the project.
                            projectResources.setCompiledResources(genericValueToNameMap,
                                    styleableValueToNameMap, resourceValueMap);
                        }
                    }
                } catch (Error e) {
                    // Log this error with the class name we're trying to load and abort.
                    AdtPlugin.log(e, "loadAndParseRClass failed to find class %1$s", className); //$NON-NLS-1$
                }
            }
        } catch (ClassNotFoundException e) {
            // pass
        }
    
private booleanparseClass(java.lang.Class rClass, java.util.Map genericValueToNameMap, java.util.Map styleableValueToNameMap, java.util.Map resourceValueMap)
Parses a R class, and fills maps.

param
rClass the class to parse
param
genericValueToNameMap
param
styleableValueToNameMap
param
resourceValueMap
return
True if we managed to parse the R class.

        try {
            for (Class<?> inner : rClass.getDeclaredClasses()) {
                String resType = inner.getSimpleName();

                Map<String, Integer> fullMap = new HashMap<String, Integer>();
                resourceValueMap.put(resType, fullMap);
                
                for (Field f : inner.getDeclaredFields()) {
                    // only process static final fields.
                    int modifiers = f.getModifiers();
                    if (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)) {
                        Class<?> type = f.getType();
                        if (type.isArray() && type.getComponentType() == int.class) {
                            // if the object is an int[] we put it in the styleable map
                            styleableValueToNameMap.put(new IntArrayWrapper((int[]) f.get(null)),
                                    f.getName());
                        } else if (type == int.class) {
                            Integer value = (Integer) f.get(null); 
                            genericValueToNameMap.put(value, new String[] { f.getName(), resType });
                            fullMap.put(f.getName(), value);
                        } else {
                            assert false;
                        }
                    }
                }
            }

            return true;
        } catch (IllegalArgumentException e) {
        } catch (IllegalAccessException e) {
        }
        return false;
    
public voidprojectClosed(org.eclipse.core.resources.IProject project)
Processes project close event.

        // the ProjectResources object will be removed by the ResourceManager.
    
public voidprojectDeleted(org.eclipse.core.resources.IProject project)
Processes project delete event.

        // the ProjectResources object will be removed by the ResourceManager.
    
public voidprojectOpened(org.eclipse.core.resources.IProject project)
Processes project open event.

        // when the project is opened, we get an ADDED event for each file, so we don't
        // need to do anything here.
    
public voidprojectOpenedWithWorkspace(org.eclipse.core.resources.IProject project)
Processes existing project at init time.

        try {
            // check this is an android project
            if (project.hasNature(AndroidConstants.NATURE)) {
                loadAndParseRClass(project);
            }
        } catch (CoreException e) {
            // pass
        }
    
public static voidsetupMonitor(ResourceMonitor monitor)
Sets up the monitoring system.

param
monitor The main Resource Monitor.

    
                    
         
        monitor.addFileListener(sThis, IResourceDelta.ADDED | IResourceDelta.CHANGED);
        monitor.addProjectListener(sThis);