2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.hierarchyviewer.scene;
19 import java.awt.Image;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
24 import java.util.regex.Pattern;
26 public class ViewNode {
30 public List<Property> properties = new ArrayList<Property>();
31 public Map<String, Property> namedProperties = new HashMap<String, Property>();
33 public ViewNode parent;
34 public List<ViewNode> children = new ArrayList<ViewNode>();
44 public int paddingLeft;
45 public int paddingRight;
46 public int paddingTop;
47 public int paddingBottom;
48 public int marginLeft;
49 public int marginRight;
51 public int marginBottom;
53 public boolean willNotDraw;
54 public boolean hasMargins;
59 public boolean decoded;
60 public boolean filtered;
62 private String shortName;
63 private StateListener listener;
66 id = namedProperties.get("mID").value;
68 left = getInt("mLeft", 0);
69 top = getInt("mTop", 0);
70 width = getInt("getWidth()", 0);
71 height = getInt("getHeight()", 0);
72 scrollX = getInt("mScrollX", 0);
73 scrollY = getInt("mScrollY", 0);
74 paddingLeft = getInt("mPaddingLeft", 0);
75 paddingRight = getInt("mPaddingRight", 0);
76 paddingTop = getInt("mPaddingTop", 0);
77 paddingBottom = getInt("mPaddingBottom", 0);
78 marginLeft = getInt("layout_leftMargin", Integer.MIN_VALUE);
79 marginRight = getInt("layout_rightMargin", Integer.MIN_VALUE);
80 marginTop = getInt("layout_topMargin", Integer.MIN_VALUE);
81 marginBottom = getInt("layout_bottomMargin", Integer.MIN_VALUE);
82 baseline = getInt("getBaseline()", 0);
83 willNotDraw = getBoolean("willNotDraw()", false);
84 hasFocus = getBoolean("hasFocus()", false);
86 hasMargins = marginLeft != Integer.MIN_VALUE &&
87 marginRight != Integer.MIN_VALUE &&
88 marginTop != Integer.MIN_VALUE &&
89 marginBottom != Integer.MIN_VALUE;
94 private boolean getBoolean(String name, boolean defaultValue) {
95 Property p = namedProperties.get(name);
98 return Boolean.parseBoolean(p.value);
99 } catch (NumberFormatException e) {
106 private int getInt(String name, int defaultValue) {
107 Property p = namedProperties.get(name);
110 return Integer.parseInt(p.value);
111 } catch (NumberFormatException e) {
118 public void filter(Pattern pattern) {
119 if (pattern == null || pattern.pattern().length() == 0) {
122 filtered = pattern.matcher(shortName).find() || pattern.matcher(id).find();
124 listener.nodeStateChanged(this);
127 void computeIndex() {
128 index = parent == null ? 0 : parent.children.indexOf(this);
129 listener.nodeIndexChanged(this);
132 void setShortName(String shortName) {
133 this.shortName = shortName;
136 void setStateListener(StateListener listener) {
137 this.listener = listener;
140 @SuppressWarnings({"StringEquality"})
142 public boolean equals(Object obj) {
146 if (getClass() != obj.getClass()) {
149 final ViewNode other = (ViewNode) obj;
150 return !(this.name != other.name && (this.name == null || !this.name.equals(other.name)));
154 public String toString() {
159 public int hashCode() {
161 hash = 67 * hash + (this.name != null ? this.name.hashCode() : 0);
165 public static class Property {
170 public String toString() {
171 return name + '=' + value;
174 @SuppressWarnings({"StringEquality"})
176 public boolean equals(Object obj) {
180 if (getClass() != obj.getClass()) {
183 final Property other = (Property) obj;
184 if (this.name != other.name && (this.name == null || !this.name.equals(other.name))) {
187 return !(this.value != other.value && (this.value == null || !this.value.equals(other.value)));
191 public int hashCode() {
193 hash = 61 * hash + (this.name != null ? this.name.hashCode() : 0);
194 hash = 61 * hash + (this.value != null ? this.value.hashCode() : 0);
199 interface StateListener {
200 void nodeStateChanged(ViewNode node);
201 void nodeIndexChanged(ViewNode node);