CompiledResourcesMonitorpublic final class CompiledResourcesMonitor extends Object implements com.android.ide.eclipse.editors.resources.manager.ResourceMonitor.IProjectListener, com.android.ide.eclipse.editors.resources.manager.ResourceMonitor.IFileListenerA 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 void | fileChanged(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.String | getRClassName(org.eclipse.core.resources.IProject project)Returns the class name of the R class, based on the project's manifest's package.
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 void | loadAndParseRClass(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 boolean | parseClass(java.lang.Class rClass, java.util.Map genericValueToNameMap, java.util.Map styleableValueToNameMap, java.util.Map resourceValueMap)Parses a R class, and fills maps.
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 void | projectClosed(org.eclipse.core.resources.IProject project)Processes project close event.
// the ProjectResources object will be removed by the ResourceManager.
| public void | projectDeleted(org.eclipse.core.resources.IProject project)Processes project delete event.
// the ProjectResources object will be removed by the ResourceManager.
| public void | projectOpened(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 void | projectOpenedWithWorkspace(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 void | setupMonitor(ResourceMonitor monitor)Sets up the monitoring system.
monitor.addFileListener(sThis, IResourceDelta.ADDED | IResourceDelta.CHANGED);
monitor.addProjectListener(sThis);
|
|