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.ATTR_ORIENTATION; 23 import static com.android.ide.common.layout.LayoutConstants.FQCN_LINEAR_LAYOUT; 24 import static com.android.ide.common.layout.LayoutConstants.VALUE_HORIZONTAL; 25 26 import com.android.ide.common.api.DrawingStyle; 27 import com.android.ide.common.api.DropFeedback; 28 import com.android.ide.common.api.IDragElement; 29 import com.android.ide.common.api.IGraphics; 30 import com.android.ide.common.api.INode; 31 import com.android.ide.common.api.IViewRule; 32 import com.android.ide.common.api.InsertType; 33 import com.android.ide.common.api.Point; 34 import com.android.ide.common.api.Rect; 35 36 /** 37 * An {@link IViewRule} for android.widget.HorizontalScrollView. 38 */ 39 public class HorizontalScrollViewRule extends FrameLayoutRule { 40 41 @Override 42 public void onChildInserted(INode child, INode parent, InsertType insertType) { 43 super.onChildInserted(child, parent, insertType); 44 45 // The child of the ScrollView should fill in both directions 46 String fillParent = getFillParentValueName(); 47 child.setAttribute(ANDROID_URI, ATTR_LAYOUT_WIDTH, fillParent); 48 child.setAttribute(ANDROID_URI, ATTR_LAYOUT_HEIGHT, fillParent); 49 } 50 51 @Override 52 public void onCreate(INode node, INode parent, InsertType insertType) { 53 super.onCreate(node, parent, insertType); 54 55 if (insertType.isCreate()) { 56 // Insert a horizontal linear layout which is commonly used with horizontal scrollbars 57 // as described by the documentation for HorizontalScrollbars. 58 INode linearLayout = node.appendChild(FQCN_LINEAR_LAYOUT); 59 linearLayout.setAttribute(ANDROID_URI, ATTR_ORIENTATION, 60 VALUE_HORIZONTAL); 61 } 62 } 63 64 @Override 65 public DropFeedback onDropMove(INode targetNode, IDragElement[] elements, 66 DropFeedback feedback, Point p) { 67 DropFeedback f = super.onDropMove(targetNode, elements, feedback, p); 68 69 // HorizontalScrollViews only allow a single child 70 if (targetNode.getChildren().length > 0) { 71 f.invalidTarget = true; 72 } 73 return f; 74 } 75 76 @Override 77 protected void drawFeedback( 78 IGraphics gc, 79 INode targetNode, 80 IDragElement[] elements, 81 DropFeedback feedback) { 82 if (targetNode.getChildren().length > 0) { 83 Rect b = targetNode.getBounds(); 84 if (b.isValid()) { 85 gc.useStyle(DrawingStyle.DROP_RECIPIENT); 86 gc.drawRect(b); 87 } 88 } else { 89 super.drawFeedback(gc, targetNode, elements, feedback); 90 } 91 } 92 } 93