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.transforms; 18 19 import com.android.ide.eclipse.gltrace.state.GLListProperty; 20 import com.android.ide.eclipse.gltrace.state.IGLProperty; 21 22 /** 23 * A {@link ListElementAddTransform} provides the ability to add a new property to a list of 24 * properties in the GL State. 25 */ 26 public class ListElementAddTransform implements IStateTransform { 27 private final IGLPropertyAccessor mAccessor; 28 private final IGLProperty mElement; 29 30 public ListElementAddTransform(IGLPropertyAccessor accessor, IGLProperty element) { 31 mAccessor = accessor; 32 mElement = element; 33 } 34 35 @Override 36 public void apply(IGLProperty currentState) { 37 GLListProperty list = getList(currentState); 38 if (list != null) { 39 list.add(mElement); 40 } 41 } 42 43 @Override 44 public void revert(IGLProperty currentState) { 45 GLListProperty list = getList(currentState); 46 if (list != null) { 47 list.remove(mElement); 48 } 49 } 50 51 @Override 52 public IGLProperty getChangedProperty(IGLProperty currentState) { 53 return getList(currentState); 54 } 55 56 private GLListProperty getList(IGLProperty state) { 57 IGLProperty p = state; 58 59 if (mAccessor != null) { 60 p = mAccessor.getProperty(p); 61 } 62 63 if (p instanceof GLListProperty) { 64 return (GLListProperty) p; 65 } else { 66 return null; 67 } 68 } 69 } 70