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