From 6d62f10bf1158488089f432b9975a857b806a231 Mon Sep 17 00:00:00 2001 From: Xavier Ducrohet Date: Thu, 2 Dec 2010 11:53:05 -0800 Subject: [PATCH] LayoutLib API: move add/moveChild API back to using index. Also add an optional data bundle to SceneResult. Change-Id: I9f9d4ca1f1f05d536a87a005a7939a6d42d0d8a4 --- .../editors/layout/gle2/GraphicalEditorPart.java | 8 +-- .../editors/layout/gle2/PaletteComposite.java | 3 +- .../editors/layout/gle2/ViewHierarchy.java | 3 +- .../layoutRendering/ApiDemosRenderingTest.java | 3 +- .../ide/common/layoutlib/LayoutBridgeWrapper.java | 5 +- .../src/com/android/layoutlib/api/LayoutScene.java | 27 ++++---- .../src/com/android/layoutlib/api/SceneResult.java | 72 ++++++++++------------ 7 files changed, 59 insertions(+), 62 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 1e519941f..a493b56e9 100755 --- 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 @@ -53,8 +53,8 @@ import com.android.layoutlib.api.IXmlPullParser; import com.android.layoutlib.api.LayoutBridge; import com.android.layoutlib.api.LayoutScene; import com.android.layoutlib.api.SceneParams; -import com.android.layoutlib.api.SceneResult; import com.android.layoutlib.api.SceneParams.RenderingMode; +import com.android.layoutlib.api.SceneResult.SceneStatus; import com.android.sdklib.IAndroidTarget; import com.android.sdklib.SdkConstants; import com.android.sdkuilib.internal.widgets.ResolutionChooserDialog; @@ -1018,8 +1018,8 @@ public class GraphicalEditorPart extends EditorPart // no root view yet indicates success and then update the canvas with it. mCanvasViewer.getCanvas().setResult( - new BasicLayoutScene(SceneResult.SUCCESS, null /*rootViewInfo*/, - null /*image*/), + new BasicLayoutScene(SceneStatus.SUCCESS.getResult(), + null /*rootViewInfo*/, null /*image*/), null /*explodeNodes*/); return; } @@ -1274,7 +1274,7 @@ public class GraphicalEditorPart extends EditorPart canvas.setResult(scene, explodeNodes); // update the UiElementNode with the layout info. - if (scene.getResult() != SceneResult.SUCCESS) { + if (scene.getResult().isSuccess() == false) { // An error was generated. Print it. displayError(scene.getResult().getErrorMessage()); diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/PaletteComposite.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/PaletteComposite.java index fc4d5d1e8..2bc9f34ca 100755 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/PaletteComposite.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/PaletteComposite.java @@ -39,7 +39,6 @@ import com.android.ide.eclipse.adt.internal.editors.uimodel.UiElementNode; import com.android.ide.eclipse.adt.internal.sdk.AndroidTargetData; import com.android.layoutlib.api.LayoutBridge; import com.android.layoutlib.api.LayoutScene; -import com.android.layoutlib.api.SceneResult; import com.android.layoutlib.api.ViewInfo; import com.android.sdklib.SdkConstants; @@ -751,7 +750,7 @@ public class PaletteComposite extends Composite { } if (scene != null) { - if (scene.getResult() == SceneResult.SUCCESS) { + if (scene.getResult().isSuccess()) { BufferedImage image = scene.getImage(); if (image != null) { BufferedImage cropped; diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/ViewHierarchy.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/ViewHierarchy.java index 686b0bada..2278cfd04 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/ViewHierarchy.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/ViewHierarchy.java @@ -21,7 +21,6 @@ import com.android.ide.eclipse.adt.internal.editors.layout.gre.NodeProxy; import com.android.ide.eclipse.adt.internal.editors.layout.uimodel.UiViewElementNode; import com.android.ide.eclipse.adt.internal.editors.uimodel.UiElementNode; import com.android.layoutlib.api.LayoutScene; -import com.android.layoutlib.api.SceneResult; import com.android.layoutlib.api.ViewInfo; import org.eclipse.swt.graphics.Rectangle; @@ -130,7 +129,7 @@ public class ViewHierarchy { } mScene = scene; - mIsResultValid = (scene != null && scene.getResult() == SceneResult.SUCCESS); + mIsResultValid = (scene != null && scene.getResult().isSuccess()); mExplodedParents = false; if (mIsResultValid && scene != null) { diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/functests/layoutRendering/ApiDemosRenderingTest.java b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/functests/layoutRendering/ApiDemosRenderingTest.java index 024e9e387..53a36fbc3 100644 --- a/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/functests/layoutRendering/ApiDemosRenderingTest.java +++ b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/functests/layoutRendering/ApiDemosRenderingTest.java @@ -37,7 +37,6 @@ import com.android.layoutlib.api.IResourceValue; import com.android.layoutlib.api.IXmlPullParser; import com.android.layoutlib.api.LayoutScene; import com.android.layoutlib.api.SceneParams; -import com.android.layoutlib.api.SceneResult; import com.android.layoutlib.api.SceneParams.RenderingMode; import com.android.sdklib.IAndroidTarget; import com.android.sdklib.SdkConstants; @@ -217,7 +216,7 @@ public class ApiDemosRenderingTest extends SdkTestCase { null //logger )); - if (scene.getResult() != SceneResult.SUCCESS) { + if (scene.getResult().isSuccess() == false) { if (projectCallBack.mCustomViewAttempt == false) { System.out.println("FAILED"); fail(String.format("Rendering %1$s: %2$s", layout.getName(), diff --git a/ide_common/src/com/android/ide/common/layoutlib/LayoutBridgeWrapper.java b/ide_common/src/com/android/ide/common/layoutlib/LayoutBridgeWrapper.java index e1b8241df..f773a5a8d 100644 --- a/ide_common/src/com/android/ide/common/layoutlib/LayoutBridgeWrapper.java +++ b/ide_common/src/com/android/ide/common/layoutlib/LayoutBridgeWrapper.java @@ -25,6 +25,7 @@ import com.android.layoutlib.api.SceneResult; import com.android.layoutlib.api.ViewInfo; import com.android.layoutlib.api.ILayoutResult.ILayoutViewInfo; import com.android.layoutlib.api.SceneParams.RenderingMode; +import com.android.layoutlib.api.SceneResult.SceneStatus; import java.lang.reflect.Field; import java.util.ArrayList; @@ -146,10 +147,10 @@ class LayoutBridgeWrapper extends LayoutBridge { ViewInfo rootViewInfo; if (result.getSuccess() == ILayoutResult.SUCCESS) { - sceneResult = SceneResult.SUCCESS; + sceneResult = SceneStatus.SUCCESS.getResult(); rootViewInfo = convertToViewInfo(result.getRootView()); } else { - sceneResult = new SceneResult(result.getErrorMessage()); + sceneResult = new SceneResult(SceneStatus.ERROR_UNKNOWN, result.getErrorMessage()); rootViewInfo = null; } diff --git a/layoutlib_api/src/com/android/layoutlib/api/LayoutScene.java b/layoutlib_api/src/com/android/layoutlib/api/LayoutScene.java index 8c3d954db..871661edd 100644 --- a/layoutlib_api/src/com/android/layoutlib/api/LayoutScene.java +++ b/layoutlib_api/src/com/android/layoutlib/api/LayoutScene.java @@ -158,8 +158,8 @@ public class LayoutScene { /** * Inserts a new child in a ViewGroup object, and renders the result. *

- * The child is first inflated and then added to its parent, before the given sibling. - * If the sibling is null, then it is added at the end of the ViewGroup. + * The child is first inflated and then added to its new parent, at the given index + * position. If the index is -1 then the child is added at the end of the parent. *

* If an animation listener is passed then the rendering is done asynchronously and the * result is sent to the listener. @@ -168,15 +168,18 @@ public class LayoutScene { * The child stays in the view hierarchy after the rendering is done. To remove it call * {@link #removeChild(Object, int)}. * + * The returned {@link SceneResult} object will contain the android.view.View object for + * the newly inflated child. It is accessible through {@link SceneResult#getData()}. + * * @param parentView the parent View object to receive the new child. * @param childXml an {@link IXmlPullParser} containing the content of the new child. - * @param beforeSibling the object in parentView to insert before. If - * null, insert at the end. + * @param index the index at which position to add the new child into the parent. -1 means at + * the end. * @param listener an optional {@link IAnimationListener}. * * @return a {@link SceneResult} indicating the status of the action. */ - public SceneResult insertChild(Object parentView, IXmlPullParser childXml, Object beforeSibling, + public SceneResult insertChild(Object parentView, IXmlPullParser childXml, int index, IAnimationListener listener) { return NOT_IMPLEMENTED.getResult(); } @@ -184,9 +187,11 @@ public class LayoutScene { /** * Move a new child to a different ViewGroup object. *

- * The child is first removed from its current parent, and then added to its new parent, before - * the given sibling. If the sibling is null, then it is added at the end - * of the ViewGroup. + * The child is first removed from its current parent, and then added to its new parent, at the + * given index position. In case the parentView is the current parent of + * childView then the index must be the value with the childView removed + * from its parent. If the index is -1 then the child is added at the end of + * the parent. *

* If an animation listener is passed then the rendering is done asynchronously and the * result is sent to the listener. @@ -198,13 +203,13 @@ public class LayoutScene { * @param parentView the parent View object to receive the child. Can be the current parent * already. * @param childView the view to move. - * @param beforeSibling the object in parentView to insert before. If - * null, insert at the end. + * @param index the index at which position to add the new child into the parent. -1 means at + * the end. * @param listener an optional {@link IAnimationListener}. * * @return a {@link SceneResult} indicating the status of the action. */ - public SceneResult moveChild(Object parentView, Object childView, Object beforeSibling, + public SceneResult moveChild(Object parentView, Object childView, int index, IAnimationListener listener) { return NOT_IMPLEMENTED.getResult(); } diff --git a/layoutlib_api/src/com/android/layoutlib/api/SceneResult.java b/layoutlib_api/src/com/android/layoutlib/api/SceneResult.java index f72c97f17..8fada8007 100644 --- a/layoutlib_api/src/com/android/layoutlib/api/SceneResult.java +++ b/layoutlib_api/src/com/android/layoutlib/api/SceneResult.java @@ -24,6 +24,7 @@ public class SceneResult { private final SceneStatus mStatus; private final String mErrorMessage; private final Throwable mThrowable; + private Object mData; public enum SceneStatus { SUCCESS, @@ -45,43 +46,25 @@ public class SceneResult { // don't want to get generic error that way. assert this != ERROR_UNKNOWN; - if (this == SUCCESS) { - return SceneResult.SUCCESS; - } - return new SceneResult(this); } } /** - * Singleton SUCCESS {@link SceneResult} object. - */ - public static final SceneResult SUCCESS = new SceneResult(SceneStatus.SUCCESS); - - /** - * Creates a {@link SceneResult} object, with {@link SceneStatus#ERROR_UNKNOWN} status, and - * the given message. - */ - public SceneResult(String errorMessage) { - this(SceneStatus.ERROR_UNKNOWN, errorMessage, null); - } - - /** - * Creates a {@link SceneResult} object, with {@link SceneStatus#ERROR_UNKNOWN} status, and - * the given message and {@link Throwable} + * Creates a {@link SceneResult} object with the given SceneStatus. + * + * @param status the status. Must not be null. */ - public SceneResult(String errorMessage, Throwable t) { - this(SceneStatus.ERROR_UNKNOWN, errorMessage, t); + public SceneResult(SceneStatus status) { + this(status, null, null); } /** * Creates a {@link SceneResult} object with the given SceneStatus, and the given message * and {@link Throwable}. - *

- * This should not be used to create {@link SceneResult} object with - * {@link SceneStatus#SUCCESS}. Use {@link SceneResult#SUCCESS} instead. * - * @param status the status + * @param status the status. Must not be null. + * @param errorMessage an optional error message. */ public SceneResult(SceneStatus status, String errorMessage) { this(status, errorMessage, null); @@ -90,31 +73,26 @@ public class SceneResult { /** * Creates a {@link SceneResult} object with the given SceneStatus, and the given message * and {@link Throwable} - *

- * This should not be used to create {@link SceneResult} object with - * {@link SceneStatus#SUCCESS}. Use {@link SceneResult#SUCCESS} instead. * - * @param status the status + * @param status the status. Must not be null. + * @param errorMessage an optional error message. + * @param t an optional exception. */ public SceneResult(SceneStatus status, String errorMessage, Throwable t) { - assert status != SceneStatus.SUCCESS; + assert status != null; mStatus = status; mErrorMessage = errorMessage; mThrowable = t; } /** - * Creates a {@link SceneResult} object with the given SceneStatus. + * Returns whether the status is successful. *

- * This should not be used to create {@link SceneResult} object with - * {@link SceneStatus#SUCCESS}. Use {@link SceneResult#SUCCESS} instead. - * - * @param status the status + * This is the same as calling getStatus() == SceneStatus.SUCCESS + * @return true if the status is successful. */ - public SceneResult(SceneStatus status) { - mStatus = status; - mErrorMessage = null; - mThrowable = null; + public boolean isSuccess() { + return mStatus == SceneStatus.SUCCESS; } /** @@ -139,4 +117,20 @@ public class SceneResult { public Throwable getException() { return mThrowable; } + + /** + * Sets an optional data bundle in the result object. + * @param data the data bundle + */ + public void SetData(Object data) { + mData = data; + } + + /** + * Returns the optional data bundle stored in the result object. + * @return the data bundle or null if none have been set. + */ + public Object getData() { + return mData; + } } -- 2.11.0