diff options
author | Xavier Ducrohet <xav@android.com> | 2010-11-19 15:48:17 -0800 |
---|---|---|
committer | Xavier Ducrohet <xav@android.com> | 2010-11-19 16:17:27 -0800 |
commit | 9223b6737c56619c02125ce988bb21fc4fde264b (patch) | |
tree | 6fe8d5691504dbe77d998e37e40350a81cb5d60d /tools | |
parent | 7370ab5cdb03fdf58a022a047820280ee2761719 (diff) | |
download | frameworks_base-9223b6737c56619c02125ce988bb21fc4fde264b.zip frameworks_base-9223b6737c56619c02125ce988bb21fc4fde264b.tar.gz frameworks_base-9223b6737c56619c02125ce988bb21fc4fde264b.tar.bz2 |
Layoutlib: support for editing embedded layouts.
When Resources.getLayout(int) is called to return a parser
for an embedded layout, this queries the current parser for
a custom parser (Eclipse will provide one on top of the current
XML model being edited)
Change-Id: Ia9e837358f67daed0a835e1b3f4f50c0516ceee9
Diffstat (limited to 'tools')
5 files changed, 130 insertions, 18 deletions
diff --git a/tools/layoutlib/bridge/src/android/graphics/Canvas_Delegate.java b/tools/layoutlib/bridge/src/android/graphics/Canvas_Delegate.java index a5f3456..cea07af 100644 --- a/tools/layoutlib/bridge/src/android/graphics/Canvas_Delegate.java +++ b/tools/layoutlib/bridge/src/android/graphics/Canvas_Delegate.java @@ -18,6 +18,7 @@ package android.graphics; import com.android.layoutlib.api.ILayoutLog; import com.android.layoutlib.bridge.impl.DelegateManager; +import com.android.layoutlib.bridge.impl.Stack; import android.graphics.Paint_Delegate.FontInfo; import android.text.TextUtils; @@ -32,7 +33,6 @@ import java.awt.RenderingHints; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.util.List; -import java.util.Stack; /** diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java index ac7fada..2ddabdf 100644 --- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java +++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java @@ -22,6 +22,7 @@ import com.android.layoutlib.api.IResourceValue; import com.android.layoutlib.api.IStyleResourceValue; import com.android.layoutlib.bridge.Bridge; import com.android.layoutlib.bridge.BridgeConstants; +import com.android.layoutlib.bridge.impl.Stack; import com.android.layoutlib.bridge.impl.TempResourceValue; import android.app.Activity; @@ -65,7 +66,6 @@ import java.io.InputStream; import java.util.HashMap; import java.util.IdentityHashMap; import java.util.Map; -import java.util.Stack; import java.util.TreeMap; import java.util.Map.Entry; @@ -191,14 +191,33 @@ public final class BridgeContext extends Activity { return mDefaultPropMaps.get(key); } + /** + * Adds a parser to the stack. + * @param parser the parser to add. + */ public void pushParser(BridgeXmlBlockParser parser) { mParserStack.push(parser); } + /** + * Removes the parser at the top of the stack + */ public void popParser() { mParserStack.pop(); } + /** + * Returns the current parser at the top the of the stack. + * @return a parser or null. + */ + public BridgeXmlBlockParser getCurrentParser() { + return mParserStack.peek(); + } + + /** + * Returns the previous parser. + * @return a parser or null if there isn't any previous parser + */ public BridgeXmlBlockParser getPreviousParser() { if (mParserStack.size() < 2) { return null; diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeResources.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeResources.java index bfbb01a8..1011173 100644 --- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeResources.java +++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeResources.java @@ -220,24 +220,37 @@ public final class BridgeResources extends Resources { IResourceValue value = getResourceValue(id, mPlatformResourceFlag); if (value != null) { - File xml = new File(value.getValue()); - if (xml.isFile()) { - // we need to create a pull parser around the layout XML file, and then - // give that to our XmlBlockParser - try { - KXmlParser parser = new KXmlParser(); - parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); - parser.setInput(new FileReader(xml)); + XmlPullParser parser = null; - return new BridgeXmlBlockParser(parser, mContext, mPlatformResourceFlag[0]); - } catch (XmlPullParserException e) { - mContext.getLogger().error(e); + try { + // check if the current parser can provide us with a custom parser. + BridgeXmlBlockParser currentParser = mContext.getCurrentParser(); + if (currentParser != null) { + parser = currentParser.getParser(value.getName()); + } + + // create a new one manually if needed. + if (parser == null) { + File xml = new File(value.getValue()); + if (xml.isFile()) { + // we need to create a pull parser around the layout XML file, and then + // give that to our XmlBlockParser + parser = new KXmlParser(); + parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); + parser.setInput(new FileReader(xml)); + } + } - // we'll return null below. - } catch (FileNotFoundException e) { - // this shouldn't happen since we check above. + if (parser != null) { + return new BridgeXmlBlockParser(parser, mContext, mPlatformResourceFlag[0]); } + } catch (XmlPullParserException e) { + mContext.getLogger().error(e); + // we'll return null below. + } catch (FileNotFoundException e) { + // this shouldn't happen since we check above. } + } // id was not found or not resolved. Throw a NotFoundException. diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeXmlBlockParser.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeXmlBlockParser.java index 073a019..c3d0b14 100644 --- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeXmlBlockParser.java +++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeXmlBlockParser.java @@ -56,13 +56,23 @@ public class BridgeXmlBlockParser implements XmlResourceParser { mPlatformFile = platformFile; mAttrib = new BridgeXmlPullAttributes(parser, context, mPlatformFile); - mContext.pushParser(this); + if (mContext != null) { + mContext.pushParser(this); + } } public boolean isPlatformFile() { return mPlatformFile; } + public IXmlPullParser getParser(String layoutName) { + if (mParser instanceof IXmlPullParser) { + return ((IXmlPullParser)mParser).getParser(layoutName); + } + + return null; + } + public Object getViewKey() { if (mParser instanceof IXmlPullParser) { return ((IXmlPullParser)mParser).getViewKey(); @@ -238,7 +248,7 @@ public class BridgeXmlBlockParser implements XmlResourceParser { } int ev = mParser.next(); - if (ev == END_TAG && mParser.getDepth() == 1) { + if (ev == END_TAG && mParser.getDepth() == 1 && mContext != null) { // done with parser remove it from the context stack. mContext.popParser(); } diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/Stack.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/Stack.java new file mode 100644 index 0000000..9bd0015 --- /dev/null +++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/Stack.java @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.layoutlib.bridge.impl; + +import java.util.ArrayList; + +/** + * Custom Stack implementation on top of an {@link ArrayList} instead of + * using {@link java.util.Stack} which is on top of a vector. + * + * @param <T> + */ +public class Stack<T> extends ArrayList<T> { + + private static final long serialVersionUID = 1L; + + public Stack() { + super(); + } + + public Stack(int size) { + super(size); + } + + /** + * Pushes the given object to the stack + * @param object the object to push + */ + public void push(T object) { + add(object); + } + + /** + * Remove the object at the top of the stack and returns it. + * @return the removed object or null if the stack was empty. + */ + public T pop() { + if (size() > 0) { + return remove(size() - 1); + } + + return null; + } + + /** + * Returns the object at the top of the stack. + * @return the object at the top or null if the stack is empty. + */ + public T peek() { + if (size() > 0) { + return get(size() - 1); + } + + return null; + } +} |