• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.annotations.NonNull;
20 import com.android.annotations.Nullable;
21 import com.android.ide.common.api.IMenuCallback;
22 import com.android.ide.common.api.INode;
23 import com.android.ide.common.api.INodeHandler;
24 import com.android.ide.common.api.RuleAction;
25 
26 import java.util.List;
27 
28 /**
29  * Convenience implementation of {@link IMenuCallback} which can be used to set a
30  * particular property to the new valueId or newValue passed from the {@link IMenuCallback}
31  */
32 public class PropertyCallback implements IMenuCallback {
33     private final List<? extends INode> mTargetNodes;
34     private final String mUndoLabel;
35     private final String mUri;
36     private final String mAttribute;
37 
38     /**
39      * Creates a new property callback.
40      *
41      * @param targetNodes the nodes to apply the property to, or null to use the
42      *            nodes pass into the
43      *            {@link #action(RuleAction, List, String, Boolean)} method.
44      * @param undoLabel the label to use for the undo action
45      * @param uri the attribute URI to apply
46      * @param attribute the attribute name to apply
47      */
PropertyCallback(List<? extends INode> targetNodes, String undoLabel, String uri, String attribute)48     public PropertyCallback(List<? extends INode> targetNodes, String undoLabel,
49             String uri, String attribute) {
50         super();
51         mTargetNodes = targetNodes;
52         mUndoLabel = undoLabel;
53         mUri = uri;
54         mAttribute = attribute;
55     }
56 
57     // ---- Implements IMenuCallback ----
58     @Override
action(@onNull RuleAction action, @NonNull List<? extends INode> selectedNodes, final @Nullable String valueId, final @Nullable Boolean newValue)59     public void action(@NonNull RuleAction action, @NonNull List<? extends INode> selectedNodes,
60             final @Nullable String valueId, final @Nullable Boolean newValue) {
61         if (mTargetNodes != null && mTargetNodes.size() > 0) {
62             selectedNodes = mTargetNodes;
63         }
64         if (selectedNodes == null || selectedNodes.size() == 0) {
65             return;
66         }
67         final List<? extends INode> nodes = selectedNodes;
68         selectedNodes.get(0).editXml(mUndoLabel, new INodeHandler() {
69             @Override
70             public void handle(@NonNull INode n) {
71                 for (INode targetNode : nodes) {
72                     if (valueId != null) {
73                         targetNode.setAttribute(mUri, mAttribute, valueId);
74                     } else {
75                         assert newValue != null;
76                         targetNode.setAttribute(mUri, mAttribute, Boolean.toString(newValue));
77                     }
78                 }
79             }
80         });
81     }
82 }
83