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.SdkConstants.ANDROID_URI; 20 import static com.android.SdkConstants.ATTR_LAYOUT_HEIGHT; 21 import static com.android.SdkConstants.ATTR_LAYOUT_WIDTH; 22 import static com.android.SdkConstants.ATTR_ORIENTATION; 23 import static com.android.SdkConstants.FQCN_LINEAR_LAYOUT; 24 import static com.android.SdkConstants.VALUE_VERTICAL; 25 26 import com.android.annotations.NonNull; 27 import com.android.annotations.Nullable; 28 import com.android.ide.common.api.DrawingStyle; 29 import com.android.ide.common.api.DropFeedback; 30 import com.android.ide.common.api.IDragElement; 31 import com.android.ide.common.api.IGraphics; 32 import com.android.ide.common.api.INode; 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 38 /** 39 * An {@link IViewRule} for android.widget.ScrollView. 40 */ 41 public class ScrollViewRule extends FrameLayoutRule { 42 43 @Override onChildInserted(@onNull INode child, @NonNull INode parent, @NonNull InsertType insertType)44 public void onChildInserted(@NonNull INode child, @NonNull INode parent, 45 @NonNull InsertType insertType) { 46 super.onChildInserted(child, parent, insertType); 47 48 // The child of the ScrollView should fill in both directions 49 String fillParent = getFillParentValueName(); 50 child.setAttribute(ANDROID_URI, ATTR_LAYOUT_WIDTH, fillParent); 51 child.setAttribute(ANDROID_URI, ATTR_LAYOUT_HEIGHT, fillParent); 52 } 53 54 @Override onCreate(@onNull INode node, @NonNull INode parent, @NonNull InsertType insertType)55 public void onCreate(@NonNull INode node, @NonNull INode parent, 56 @NonNull InsertType insertType) { 57 super.onCreate(node, parent, insertType); 58 59 if (insertType.isCreate()) { 60 // Insert a default linear layout (which will in turn be registered as 61 // a child of this node and the create child method above will set its 62 // fill parent attributes, its id, etc. 63 INode linear = node.appendChild(FQCN_LINEAR_LAYOUT); 64 linear.setAttribute(ANDROID_URI, ATTR_ORIENTATION, VALUE_VERTICAL); 65 } 66 } 67 68 @Override onDropMove(@onNull INode targetNode, @NonNull IDragElement[] elements, @Nullable DropFeedback feedback, @NonNull Point p)69 public DropFeedback onDropMove(@NonNull INode targetNode, @NonNull IDragElement[] elements, 70 @Nullable DropFeedback feedback, @NonNull Point p) { 71 DropFeedback f = super.onDropMove(targetNode, elements, feedback, p); 72 73 // ScrollViews only allow a single child 74 if (targetNode.getChildren().length > 0) { 75 f.invalidTarget = true; 76 } 77 return f; 78 } 79 80 @Override drawFeedback( IGraphics gc, INode targetNode, IDragElement[] elements, DropFeedback feedback)81 protected void drawFeedback( 82 IGraphics gc, 83 INode targetNode, 84 IDragElement[] elements, 85 DropFeedback feedback) { 86 if (targetNode.getChildren().length > 0) { 87 Rect b = targetNode.getBounds(); 88 if (b.isValid()) { 89 gc.useStyle(DrawingStyle.DROP_RECIPIENT); 90 gc.drawRect(b); 91 } 92 } else { 93 super.drawFeedback(gc, targetNode, elements, feedback); 94 } 95 } 96 } 97