1 /* 2 * Copyright (C) 2012 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 com.android.sdklib.util.SparseArray; 20 21 import java.util.ArrayList; 22 import java.util.List; 23 import java.util.Locale; 24 25 public class GLSparseArrayProperty implements IGLProperty { 26 private final GLStateType mType; 27 private final IGLProperty mDefaultValue; 28 private final boolean mCreateOnAccess; 29 private final SparseArray<IGLProperty> mSparseArray; 30 private IGLProperty mParent; 31 32 public GLSparseArrayProperty(GLStateType type, IGLProperty defaultValue) { 33 this(type, defaultValue, false); 34 } 35 36 /** 37 * Constructs a sparse array property. 38 * @param type GL state corresponding to this property 39 * @param defaultValue default value of each item 40 * @param createOnAccess create an item on access if it is not present 41 */ 42 public GLSparseArrayProperty(GLStateType type, IGLProperty defaultValue, 43 boolean createOnAccess) { 44 mType = type; 45 mDefaultValue = defaultValue; 46 mCreateOnAccess = createOnAccess; 47 mSparseArray = new SparseArray<IGLProperty>(20); 48 } 49 50 private GLSparseArrayProperty(GLStateType type, IGLProperty defaultValue, 51 boolean createOnAccess, SparseArray<IGLProperty> contents) { 52 mType = type; 53 mDefaultValue = defaultValue; 54 mCreateOnAccess = createOnAccess; 55 mSparseArray = contents; 56 } 57 58 public List<IGLProperty> getValues() { 59 List<IGLProperty> values = new ArrayList<IGLProperty>(mSparseArray.size()); 60 61 for (int i = 0; i < mSparseArray.size(); i++) { 62 values.add(mSparseArray.valueAt(i)); 63 } 64 65 return values; 66 } 67 68 public IGLProperty getProperty(int key) { 69 IGLProperty p = mSparseArray.get(key); 70 if (p == null && mCreateOnAccess) { 71 add(key); 72 p = mSparseArray.get(key); 73 } 74 return p; 75 } 76 77 public int keyFor(IGLProperty element) { 78 int index = mSparseArray.indexOfValue(element); 79 return mSparseArray.keyAt(index); 80 } 81 82 public void put(int key, IGLProperty element) { 83 element.setParent(this); 84 mSparseArray.put(key, element); 85 } 86 87 public void add(int key) { 88 IGLProperty prop = mDefaultValue.clone(); 89 prop.setParent(this); 90 mSparseArray.put(key, prop); 91 } 92 93 public void delete(int key) { 94 mSparseArray.delete(key); 95 } 96 97 @Override 98 public GLStateType getType() { 99 return mType; 100 } 101 102 @Override 103 public boolean isComposite() { 104 return true; 105 } 106 107 @Override 108 public boolean isDefault() { 109 return false; 110 } 111 112 @Override 113 public IGLProperty getParent() { 114 return mParent; 115 } 116 117 @Override 118 public void setParent(IGLProperty parent) { 119 mParent = parent; 120 } 121 122 @Override 123 public GLSparseArrayProperty clone() { 124 SparseArray<IGLProperty> copy = new SparseArray<IGLProperty>(mSparseArray.size()); 125 for (int i = 0; i < mSparseArray.size(); i++) { 126 int key = mSparseArray.keyAt(i); 127 IGLProperty value = mSparseArray.get(key); 128 copy.put(key, value); 129 } 130 131 return new GLSparseArrayProperty(mType, mDefaultValue, mCreateOnAccess, copy); 132 } 133 134 @Override 135 public String getStringValue() { 136 // This method is called for displaying objects in the UI. 137 // We do not display any values for composites in the UI as they are only intermediate 138 // nodes in the tree. 139 return ""; 140 } 141 142 @Override 143 public void setValue(Object value) { 144 throw new UnsupportedOperationException( 145 "Values cannot be set for composite properties."); //$NON-NLS-1$ 146 } 147 148 @Override 149 public Object getValue() { 150 throw new UnsupportedOperationException( 151 "Values cannot be obtained for composite properties."); //$NON-NLS-1$ 152 } 153 154 @Override 155 public void prettyPrint(StatePrettyPrinter pp) { 156 pp.prettyPrint(mType, null); 157 pp.incrementIndentLevel(); 158 for (int i = 0; i < mSparseArray.size(); i++) { 159 int key = mSparseArray.keyAt(i); 160 pp.prettyPrint(String.format(Locale.US, "Index %d:", key)); 161 IGLProperty prop = mSparseArray.get(key); 162 163 assert prop != null; 164 if (prop != null) { 165 prop.prettyPrint(pp); 166 } 167 } 168 pp.decrementIndentLevel(); 169 } 170 } 171