1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
17 package com.android.ide.eclipse.gltrace.state;
18 
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Locale;
22 
23 /**
24  * A list property is a container for a list of properties, addressed by index.
25  */
26 public class GLListProperty implements IGLProperty {
27     private final List<IGLProperty> mList;
28     private final GLStateType mType;
29     private IGLProperty mParent;
30     private IGLProperty mTemplate;
31 
32     /**
33      * Construct a list of properties of given size from the provided template.
34      * @param template property that will be cloned and used as members of the list
35      * @param size size of the list
36      */
GLListProperty(GLStateType type, IGLProperty template, int size)37     public GLListProperty(GLStateType type, IGLProperty template, int size) {
38         mType = type;
39         mTemplate = template;
40 
41         mList = new ArrayList<IGLProperty>(size);
42         for (int i = 0; i < size; i++) {
43             IGLProperty p = template.clone();
44             mList.add(p);
45 
46             p.setParent(this);
47         }
48     }
49 
GLListProperty(GLStateType type, List<IGLProperty> props)50     private GLListProperty(GLStateType type, List<IGLProperty> props) {
51         mList = props;
52         mType = type;
53 
54         for (IGLProperty p : mList) {
55             p.setParent(this);
56         }
57     }
58 
getList()59     public List<IGLProperty> getList() {
60         return mList;
61     }
62 
get(int index)63     public IGLProperty get(int index) {
64         return mList.get(index);
65     }
66 
add(IGLProperty property)67     public boolean add(IGLProperty property) {
68         property.setParent(this);
69         return mList.add(property);
70     }
71 
remove(IGLProperty property)72     public boolean remove(IGLProperty property) {
73         return mList.remove(property);
74     }
75 
set(int index, IGLProperty property)76     public void set(int index, IGLProperty property) {
77         ensureCapacity(index + 1);
78         mList.set(index, property);
79         property.setParent(this);
80     }
81 
ensureCapacity(int capactiy)82     private void ensureCapacity(int capactiy) {
83         for (int i = mList.size(); i < capactiy; i++) {
84             mList.add(mTemplate);
85         }
86     }
87 
88     @Override
clone()89     public GLListProperty clone() {
90         List<IGLProperty> props = new ArrayList<IGLProperty>(
91                 mList.size());
92 
93         for (IGLProperty p : mList) {
94             props.add(p.clone());
95         }
96 
97         return new GLListProperty(getType(), props);
98     }
99 
100     @Override
toString()101     public String toString() {
102         StringBuffer sb = new StringBuffer();
103         sb.append("GLListProperty [");      //$NON-NLS-1$
104 
105         int i = 0;
106         for (IGLProperty p : mList) {
107             sb.append(i);
108             sb.append(':');
109             sb.append(p.toString());
110             sb.append(", ");                //$NON-NLS-1$
111             i++;
112         }
113 
114         sb.append("]");
115         return sb.toString();
116     }
117 
118     @Override
getStringValue()119     public String getStringValue() {
120         // This method is called for displaying objects in the UI.
121         // We do not display any values for composites in the UI as they are only intermediate
122         // nodes in the tree.
123         return "";
124     }
125 
126     @Override
getType()127     public GLStateType getType() {
128         return mType;
129     }
130 
131     @Override
isComposite()132     public boolean isComposite() {
133         return true;
134     }
135 
136     @Override
isDefault()137     public boolean isDefault() {
138         return false;
139     }
140 
141     @Override
getParent()142     public IGLProperty getParent() {
143         return mParent;
144     }
145 
146     @Override
setParent(IGLProperty parent)147     public void setParent(IGLProperty parent) {
148         mParent = parent;
149     }
150 
indexOf(IGLProperty property)151     public int indexOf(IGLProperty property) {
152         return mList.indexOf(property);
153     }
154 
155     @Override
setValue(Object value)156     public void setValue(Object value) {
157         throw new UnsupportedOperationException(
158                 "Values cannot be set for composite properties."); //$NON-NLS-1$
159     }
160 
161     @Override
getValue()162     public Object getValue() {
163         throw new UnsupportedOperationException(
164                 "Values cannot be obtained for composite properties."); //$NON-NLS-1$
165     }
166 
size()167     public int size() {
168         return mList.size();
169     }
170 
171     @Override
prettyPrint(StatePrettyPrinter pp)172     public void prettyPrint(StatePrettyPrinter pp) {
173         pp.prettyPrint(mType, null);
174         pp.incrementIndentLevel();
175         for (int i = 0; i < mList.size(); i++) {
176             pp.prettyPrint(String.format(Locale.US, "Index %d:", i));
177             IGLProperty p = mList.get(i);
178             p.prettyPrint(pp);
179         }
180         pp.decrementIndentLevel();
181     }
182 }
183