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
17package com.android.ide.eclipse.gltrace.state;
18
19import java.util.Collection;
20import java.util.EnumMap;
21import java.util.Map;
22
23/**
24 * A composite property is a container for multiple named properties, kind of like a dictionary.
25 */
26public class GLCompositeProperty implements IGLProperty {
27    private final GLStateType mType;
28    private final Map<GLStateType, IGLProperty> mPropertiesMap;
29    private IGLProperty mParent;
30
31    /** Construct a composite property given a list of {@link IGLProperty} objects. */
32    public GLCompositeProperty(GLStateType type, IGLProperty... iglProperties) {
33        mType = type;
34        mPropertiesMap = new EnumMap<GLStateType, IGLProperty>(GLStateType.class);
35
36        for (IGLProperty p : iglProperties) {
37            mPropertiesMap.put(p.getType(), p);
38            p.setParent(this);
39        }
40    }
41
42    public Collection<IGLProperty> getProperties() {
43        return mPropertiesMap.values();
44    }
45
46    public IGLProperty getProperty(GLStateType name) {
47        return mPropertiesMap.get(name);
48    }
49
50    @Override
51    public GLCompositeProperty clone() {
52        IGLProperty []props = new IGLProperty[mPropertiesMap.size()];
53
54        int i = 0;
55        for (IGLProperty p : mPropertiesMap.values()) {
56            props[i++] = p.clone();
57        }
58
59        return new GLCompositeProperty(getType(), props);
60    }
61
62    @Override
63    public String toString() {
64        StringBuffer sb = new StringBuffer();
65        sb.append("GLCompositeProperty {");      //$NON-NLS-1$
66
67        for (IGLProperty p : mPropertiesMap.values()) {
68            sb.append(p.toString());
69            sb.append(", ");                     //$NON-NLS-1$
70        }
71
72        sb.append("}");
73        return sb.toString();
74    }
75
76    @Override
77    public String getStringValue() {
78        // This method is called for displaying objects in the UI.
79        // We do not display any values for composites in the UI as they are only intermediate
80        // nodes in the tree.
81        return "";
82    }
83
84    @Override
85    public GLStateType getType() {
86        return mType;
87    }
88
89    @Override
90    public boolean isComposite() {
91        return true;
92    }
93
94    @Override
95    public boolean isDefault() {
96        for (IGLProperty p : mPropertiesMap.values()) {
97            if (!p.isDefault()) {
98                return false;
99            }
100        }
101
102        return true;
103    }
104
105    @Override
106    public IGLProperty getParent() {
107        return mParent;
108    }
109
110    @Override
111    public void setParent(IGLProperty parent) {
112        mParent = parent;
113    }
114
115    @Override
116    public void setValue(Object value) {
117        throw new UnsupportedOperationException(
118                "Values cannot be set for composite properties."); //$NON-NLS-1$
119    }
120
121    @Override
122    public Object getValue() {
123        throw new UnsupportedOperationException(
124                "Values cannot be obtained for composite properties."); //$NON-NLS-1$
125    }
126
127    @Override
128    public void prettyPrint(StatePrettyPrinter pp) {
129        pp.prettyPrint(mType, null);
130        pp.incrementIndentLevel();
131        for (IGLProperty p : mPropertiesMap.values()) {
132            p.prettyPrint(pp);
133        }
134        pp.decrementIndentLevel();
135    }
136}
137