11dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy/*
21dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy * Copyright (C) 2011 The Android Open Source Project
31dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy *
41dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy * Licensed under the Apache License, Version 2.0 (the "License");
51dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy * you may not use this file except in compliance with the License.
61dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy * You may obtain a copy of the License at
71dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy *
81dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy *      http://www.apache.org/licenses/LICENSE-2.0
91dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy *
101dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy * Unless required by applicable law or agreed to in writing, software
111dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy * distributed under the License is distributed on an "AS IS" BASIS,
121dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy * See the License for the specific language governing permissions and
141dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy * limitations under the License.
151dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy */
161dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy
171dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamypackage com.android.ide.eclipse.gltrace.state;
181dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy
191dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamyimport java.util.Collection;
201dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamyimport java.util.EnumMap;
211dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamyimport java.util.Map;
221dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy
231dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy/**
241dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy * A composite property is a container for multiple named properties, kind of like a dictionary.
251dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy */
261dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamypublic class GLCompositeProperty implements IGLProperty {
271dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    private final GLStateType mType;
281dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    private final Map<GLStateType, IGLProperty> mPropertiesMap;
291dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    private IGLProperty mParent;
301dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy
311dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    /** Construct a composite property given a list of {@link IGLProperty} objects. */
321dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    public GLCompositeProperty(GLStateType type, IGLProperty... iglProperties) {
331dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy        mType = type;
341dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy        mPropertiesMap = new EnumMap<GLStateType, IGLProperty>(GLStateType.class);
351dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy
361dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy        for (IGLProperty p : iglProperties) {
371dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy            mPropertiesMap.put(p.getType(), p);
381dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy            p.setParent(this);
391dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy        }
401dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    }
411dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy
421dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    public Collection<IGLProperty> getProperties() {
431dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy        return mPropertiesMap.values();
441dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    }
451dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy
461dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    public IGLProperty getProperty(GLStateType name) {
471dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy        return mPropertiesMap.get(name);
481dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    }
491dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy
501dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    @Override
511dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    public GLCompositeProperty clone() {
521dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy        IGLProperty []props = new IGLProperty[mPropertiesMap.size()];
531dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy
541dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy        int i = 0;
551dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy        for (IGLProperty p : mPropertiesMap.values()) {
561dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy            props[i++] = p.clone();
571dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy        }
581dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy
591dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy        return new GLCompositeProperty(getType(), props);
601dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    }
611dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy
621dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    @Override
631dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    public String toString() {
641dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy        StringBuffer sb = new StringBuffer();
651dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy        sb.append("GLCompositeProperty {");      //$NON-NLS-1$
661dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy
671dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy        for (IGLProperty p : mPropertiesMap.values()) {
681dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy            sb.append(p.toString());
691dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy            sb.append(", ");                     //$NON-NLS-1$
701dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy        }
711dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy
721dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy        sb.append("}");
731dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy        return sb.toString();
741dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    }
751dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy
76b27d365d27c45e0bad310b6e8f832a52e227865aSiva Velusamy    @Override
771dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    public String getStringValue() {
781dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy        // This method is called for displaying objects in the UI.
791dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy        // We do not display any values for composites in the UI as they are only intermediate
801dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy        // nodes in the tree.
811dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy        return "";
821dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    }
831dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy
84b27d365d27c45e0bad310b6e8f832a52e227865aSiva Velusamy    @Override
851dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    public GLStateType getType() {
861dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy        return mType;
871dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    }
881dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy
89b27d365d27c45e0bad310b6e8f832a52e227865aSiva Velusamy    @Override
901dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    public boolean isComposite() {
911dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy        return true;
921dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    }
931dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy
94b27d365d27c45e0bad310b6e8f832a52e227865aSiva Velusamy    @Override
951dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    public boolean isDefault() {
961dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy        for (IGLProperty p : mPropertiesMap.values()) {
971dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy            if (!p.isDefault()) {
981dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy                return false;
991dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy            }
1001dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy        }
1011dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy
1021dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy        return true;
1031dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    }
1041dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy
105b27d365d27c45e0bad310b6e8f832a52e227865aSiva Velusamy    @Override
1061dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    public IGLProperty getParent() {
1071dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy        return mParent;
1081dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    }
1091dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy
110b27d365d27c45e0bad310b6e8f832a52e227865aSiva Velusamy    @Override
1111dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    public void setParent(IGLProperty parent) {
1121dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy        mParent = parent;
1131dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    }
1141dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy
115b27d365d27c45e0bad310b6e8f832a52e227865aSiva Velusamy    @Override
1161dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    public void setValue(Object value) {
1171dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy        throw new UnsupportedOperationException(
1181dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy                "Values cannot be set for composite properties."); //$NON-NLS-1$
1191dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    }
1201dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy
121b27d365d27c45e0bad310b6e8f832a52e227865aSiva Velusamy    @Override
1221dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    public Object getValue() {
1231dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy        throw new UnsupportedOperationException(
1241dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy                "Values cannot be obtained for composite properties."); //$NON-NLS-1$
1251dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy    }
12692348b7fa6bbaf4aa26b2363b1877cfbc2873c50Siva Velusamy
12792348b7fa6bbaf4aa26b2363b1877cfbc2873c50Siva Velusamy    @Override
12892348b7fa6bbaf4aa26b2363b1877cfbc2873c50Siva Velusamy    public void prettyPrint(StatePrettyPrinter pp) {
12992348b7fa6bbaf4aa26b2363b1877cfbc2873c50Siva Velusamy        pp.prettyPrint(mType, null);
13092348b7fa6bbaf4aa26b2363b1877cfbc2873c50Siva Velusamy        pp.incrementIndentLevel();
13192348b7fa6bbaf4aa26b2363b1877cfbc2873c50Siva Velusamy        for (IGLProperty p : mPropertiesMap.values()) {
13292348b7fa6bbaf4aa26b2363b1877cfbc2873c50Siva Velusamy            p.prettyPrint(pp);
13392348b7fa6bbaf4aa26b2363b1877cfbc2873c50Siva Velusamy        }
13492348b7fa6bbaf4aa26b2363b1877cfbc2873c50Siva Velusamy        pp.decrementIndentLevel();
13592348b7fa6bbaf4aa26b2363b1877cfbc2873c50Siva Velusamy    }
1361dec6f718611b008fa8b5ddf524ea5be48c6a675Siva Velusamy}
137