• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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 static com.android.ide.common.layout.LayoutConstants.ANDROID_URI;
20 import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_GRAVITY;
21 import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_HEIGHT;
22 import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_WIDTH;
23 
24 import com.android.ide.common.api.DrawingStyle;
25 import com.android.ide.common.api.DropFeedback;
26 import com.android.ide.common.api.IDragElement;
27 import com.android.ide.common.api.IFeedbackPainter;
28 import com.android.ide.common.api.IGraphics;
29 import com.android.ide.common.api.INode;
30 import com.android.ide.common.api.INodeHandler;
31 import com.android.ide.common.api.IViewMetadata;
32 import com.android.ide.common.api.IViewMetadata.FillPreference;
33 import com.android.ide.common.api.IViewRule;
34 import com.android.ide.common.api.InsertType;
35 import com.android.ide.common.api.Point;
36 import com.android.ide.common.api.Rect;
37 import com.android.ide.common.api.RuleAction;
38 import com.android.util.Pair;
39 
40 import java.util.List;
41 import java.util.Map;
42 
43 /**
44  * An {@link IViewRule} for android.widget.FrameLayout and all its derived
45  * classes.
46  */
47 public class FrameLayoutRule extends BaseLayoutRule {
48 
49     // ==== Drag'n'drop support ====
50     // The FrameLayout accepts any drag'n'drop anywhere on its surface.
51 
52     @Override
onDropEnter(INode targetNode, Object targetView, final IDragElement[] elements)53     public DropFeedback onDropEnter(INode targetNode, Object targetView,
54             final IDragElement[] elements) {
55         if (elements.length == 0) {
56             return null;
57         }
58 
59         return new DropFeedback(null, new IFeedbackPainter() {
60             public void paint(IGraphics gc, INode node, DropFeedback feedback) {
61                 drawFeedback(gc, node, elements, feedback);
62             }
63         });
64     }
65 
66     protected void drawFeedback(
67             IGraphics gc,
68             INode targetNode,
69             IDragElement[] elements,
70             DropFeedback feedback) {
71         Rect b = targetNode.getBounds();
72         if (!b.isValid()) {
73             return;
74         }
75 
76         gc.useStyle(DrawingStyle.DROP_RECIPIENT);
77         gc.drawRect(b);
78 
79         // Get the drop point
80         Point p = (Point) feedback.userData;
81 
82         if (p == null) {
83             return;
84         }
85 
86         Rect be = elements[0].getBounds();
87 
88         gc.useStyle(DrawingStyle.DROP_PREVIEW);
89         if (be.isValid()) {
90             // At least the first element has a bound. Draw rectangles
91             // for all dropped elements with valid bounds, offset at
92             // (0,0)
93             for (IDragElement it : elements) {
94                 Rect currBounds = it.getBounds();
95                 if (currBounds.isValid()) {
96                     int offsetX = b.x - currBounds.x;
97                     int offsetY = b.y - currBounds.y;
98                     drawElement(gc, it, offsetX, offsetY);
99                 }
100             }
101         } else {
102             // We don't have bounds for new elements. In this case
103             // just draw insert lines indicating the top left corner where
104             // the item will be placed
105 
106             // +1: Place lines fully within the view (the stroke width is 2) to
107             // make
108             // it even more visually obvious
109             gc.drawLine(b.x + 1, b.y, b.x + 1, b.y + b.h);
110             gc.drawLine(b.x, b.y + 1, b.x + b.w, b.y + 1);
111         }
112     }
113 
114     @Override
115     public DropFeedback onDropMove(INode targetNode, IDragElement[] elements,
116             DropFeedback feedback, Point p) {
117         feedback.userData = p;
118         feedback.requestPaint = true;
119         return feedback;
120     }
121 
122     @Override
123     public void onDropLeave(INode targetNode, IDragElement[] elements, DropFeedback feedback) {
124         // ignore
125     }
126 
127     @Override
128     public void onDropped(final INode targetNode, final IDragElement[] elements,
129             final DropFeedback feedback, final Point p) {
130         Rect b = targetNode.getBounds();
131         if (!b.isValid()) {
132             return;
133         }
134 
135         // Collect IDs from dropped elements and remap them to new IDs
136         // if this is a copy or from a different canvas.
137         final Map<String, Pair<String, String>> idMap = getDropIdMap(targetNode, elements,
138                 feedback.isCopy || !feedback.sameCanvas);
139 
140         targetNode.editXml("Add elements to FrameLayout", new INodeHandler() {
141 
142             public void handle(INode node) {
143 
144                 // Now write the new elements.
145                 for (IDragElement element : elements) {
146                     String fqcn = element.getFqcn();
147 
148                     INode newChild = targetNode.appendChild(fqcn);
149 
150                     // Copy all the attributes, modifying them as needed.
151                     addAttributes(newChild, element, idMap, DEFAULT_ATTR_FILTER);
152 
153                     addInnerElements(newChild, element, idMap);
154                 }
155             }
156         });
157     }
158 
159     @Override
160     public void addLayoutActions(List<RuleAction> actions, final INode parentNode,
161             final List<? extends INode> children) {
162         super.addLayoutActions(actions, parentNode, children);
163         actions.add(RuleAction.createSeparator(25));
164         actions.add(createMarginAction(parentNode, children));
165         if (children != null && children.size() > 0) {
166             actions.add(createGravityAction(children, ATTR_LAYOUT_GRAVITY));
167         }
168     }
169 
170     @Override
171     public void onChildInserted(INode node, INode parent, InsertType insertType) {
172         // Look at the fill preferences and fill embedded layouts etc
173         String fqcn = node.getFqcn();
174         IViewMetadata metadata = mRulesEngine.getMetadata(fqcn);
175         if (metadata != null) {
176             FillPreference fill = metadata.getFillPreference();
177             String fillParent = getFillParentValueName();
178             if (fill.fillHorizontally(true)) {
179                 node.setAttribute(ANDROID_URI, ATTR_LAYOUT_WIDTH, fillParent);
180             }
181             if (fill.fillVertically(false)) {
182                 node.setAttribute(ANDROID_URI, ATTR_LAYOUT_HEIGHT, fillParent);
183             }
184         }
185     }
186 }
187