From f6122efcd8b66ee3a5cf3d08adfc5593aa3ef45f Mon Sep 17 00:00:00 2001 From: Tor Norbye Date: Fri, 20 May 2011 20:41:35 -0700 Subject: [PATCH] Detect and add special warning for wrong themes. DO NOT MERGE If you open a layout designed for a particular theme in another theme, you can get many confusing error messages. Instead of "attribute missing" it may tell you that it failed to convert a resource to a color or drawable, and so on. To help guide users to the root problem (wrong theme chosen) this changeset detects the scenario where theme attributes can't be resolved, and when these are found the top of the error log will start with a bold message stating that theme resources were not found and to check whether the correct theme is chosen. To do this, the resource resolver emits a new sub-type of the resource missing tag into the error log, which is used in the IDE to prefix the errors with the special error message. Change-Id: Ifa188cd67412e4536bc10bda64716a62dfa9695d --- .../editors/layout/gle2/GraphicalEditorPart.java | 12 +++++++++++- .../internal/editors/layout/gle2/RenderLogger.java | 5 ++--- .../ide/common/resources/ResourceResolver.java | 19 ++++++++++++++----- .../android/ide/common/rendering/api/LayoutLog.java | 7 +++++++ 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/GraphicalEditorPart.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/GraphicalEditorPart.java index 29811dc69..18cb51614 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/GraphicalEditorPart.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/GraphicalEditorPart.java @@ -1755,7 +1755,9 @@ public class GraphicalEditorPart extends EditorPart } if (!append) { - mErrorLabel.setText(""); + mErrorLabel.setText(""); //$NON-NLS-1$ + } else { + addText(mErrorLabel, "\n"); //$NON-NLS-1$ } if (missingClasses.size() > 0) { addText(mErrorLabel, "The following classes could not be found:\n"); @@ -1833,6 +1835,14 @@ public class GraphicalEditorPart extends EditorPart AdtPlugin.log(e, null); } + if (logger.seenTagPrefix(LayoutLog.TAG_RESOURCES_RESOLVE_THEME_ATTR)) { + addBoldText(mErrorLabel, + "Missing styles. Is the correct theme chosen for this layout?\n"); + addText(mErrorLabel, + "Use the Theme combo box above the layout to choose a different layout, " + + "or fix the theme style references.\n\n"); + } + if (hasAaptErrors && logger.seenTagPrefix(LayoutLog.TAG_RESOURCES_PREFIX)) { // Text will automatically be wrapped by the error widget so no reason // to insert linebreaks in this error message: diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/RenderLogger.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/RenderLogger.java index e44c81b7e..5ec50c91c 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/RenderLogger.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/RenderLogger.java @@ -72,7 +72,7 @@ class RenderLogger extends LayoutLog { } if (mFidelityWarnings != null) { - sb.append("The graphics preview may not be accurate:\n"); + sb.append("The graphics preview in the layout editor may not be accurate:\n"); for (String warning : mFidelityWarnings) { sb.append("* "); sb.append(warning).append('\n'); @@ -91,6 +91,7 @@ class RenderLogger extends LayoutLog { @Override public void error(String tag, String message, Object data) { String description = describe(message); + AdtPlugin.log(IStatus.ERROR, "%1$s: %2$s", mName, description); addError(tag, description); @@ -211,6 +212,4 @@ class RenderLogger extends LayoutLog { return false; } } - - } diff --git a/ide_common/src/com/android/ide/common/resources/ResourceResolver.java b/ide_common/src/com/android/ide/common/resources/ResourceResolver.java index 89a4cbaba..938a6ef8d 100644 --- a/ide_common/src/com/android/ide/common/resources/ResourceResolver.java +++ b/ide_common/src/com/android/ide/common/resources/ResourceResolver.java @@ -67,14 +67,13 @@ public class ResourceResolver extends RenderResources { } /** - * Creates a new ResourceResolver object. + * Creates a new {@link ResourceResolver} object. * - * @param IFrameworkResourceIdProvider an optional framework resource ID provider * @param projectResources the project resources. * @param frameworkResources the framework resources. * @param themeName the name of the current theme. * @param isProjectTheme Is this a project theme? - * @return + * @return a new {@link ResourceResolver} */ public static ResourceResolver create( Map> projectResources, @@ -231,12 +230,22 @@ public class ResourceResolver extends RenderResources { } // Now look for the item in the theme, starting with the current one. + ResourceValue item; if (frameworkOnly) { // FIXME for now we do the same as if it didn't specify android: - return findItemInStyle(mTheme, referenceName); + item = findItemInStyle(mTheme, referenceName); + } else { + item = findItemInStyle(mTheme, referenceName); + } + + if (item == null && mLogger != null) { + mLogger.warning(LayoutLog.TAG_RESOURCES_RESOLVE_THEME_ATTR, + String.format("Couldn't find theme resource %1$s for the current theme", + reference), + new ResourceValue(ResourceType.ATTR, referenceName, frameworkOnly)); } - return findItemInStyle(mTheme, referenceName); + return item; } else if (reference.startsWith(PREFIX_RESOURCE_REF)) { boolean frameworkOnly = false; diff --git a/layoutlib_api/src/com/android/ide/common/rendering/api/LayoutLog.java b/layoutlib_api/src/com/android/ide/common/rendering/api/LayoutLog.java index 26d0479e7..df2953774 100644 --- a/layoutlib_api/src/com/android/ide/common/rendering/api/LayoutLog.java +++ b/layoutlib_api/src/com/android/ide/common/rendering/api/LayoutLog.java @@ -57,6 +57,13 @@ public class LayoutLog { public final static String TAG_RESOURCES_RESOLVE = TAG_RESOURCES_PREFIX + "resolve"; /** + * Tag for resource resolution failure, specifically for theme attributes. + * In this case the warning/error data object will be a ResourceValue containing the type + * and name of the resource that failed to resolve + */ + public final static String TAG_RESOURCES_RESOLVE_THEME_ATTR = TAG_RESOURCES_RESOLVE + ".theme"; + + /** * Tag for failure when reading the content of a resource file. */ public final static String TAG_RESOURCES_READ = TAG_RESOURCES_PREFIX + "read"; -- 2.11.0