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