1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php 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.common.layout; 18 19 import com.android.ide.common.api.IMenuCallback; 20 import com.android.ide.common.api.INode; 21 import com.android.ide.common.api.INodeHandler; 22 import com.android.ide.common.api.RuleAction; 23 24 import java.util.List; 25 26 /** 27 * Convenience implementation of {@link IMenuCallback} which can be used to set a 28 * particular property to the new valueId or newValue passed from the {@link IMenuCallback} 29 */ 30 public class PropertyCallback implements IMenuCallback { 31 private final List<? extends INode> mTargetNodes; 32 private final String mUndoLabel; 33 private final String mUri; 34 private final String mAttribute; 35 36 /** 37 * Creates a new property callback. 38 * 39 * @param targetNodes the nodes to apply the property to, or null to use the 40 * nodes pass into the 41 * {@link #action(RuleAction, List, String, Boolean)} method. 42 * @param undoLabel the label to use for the undo action 43 * @param uri the attribute URI to apply 44 * @param attribute the attribute name to apply 45 */ PropertyCallback(List<? extends INode> targetNodes, String undoLabel, String uri, String attribute)46 public PropertyCallback(List<? extends INode> targetNodes, String undoLabel, 47 String uri, String attribute) { 48 super(); 49 mTargetNodes = targetNodes; 50 mUndoLabel = undoLabel; 51 mUri = uri; 52 mAttribute = attribute; 53 } 54 55 // ---- Implements IMenuCallback ---- action(RuleAction action, List<? extends INode> selectedNodes, final String valueId, final Boolean newValue)56 public void action(RuleAction action, List<? extends INode> selectedNodes, 57 final String valueId, final Boolean newValue) { 58 if (mTargetNodes != null && mTargetNodes.size() > 0) { 59 selectedNodes = mTargetNodes; 60 } 61 if (selectedNodes == null || selectedNodes.size() == 0) { 62 return; 63 } 64 final List<? extends INode> nodes = selectedNodes; 65 selectedNodes.get(0).editXml(mUndoLabel, new INodeHandler() { 66 public void handle(INode n) { 67 for (INode targetNode : nodes) { 68 if (valueId != null) { 69 targetNode.setAttribute(mUri, mAttribute, valueId); 70 } else { 71 assert newValue != null; 72 targetNode.setAttribute(mUri, mAttribute, Boolean.toString(newValue)); 73 } 74 } 75 } 76 }); 77 } 78 } 79