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 17package com.android.adt.gscripts; 18 19public class BaseView implements IViewRule { 20 21 private String mFqcn; 22 23 // Some common Android layout attribute names used by the view rules. 24 // All these belong to the attribute namespace ANDROID_URI. 25 public static String ATTR_ID = "id"; 26 public static String ATTR_TEXT = "text"; 27 public static String ATTR_LAYOUT_WIDTH = "layout_width"; 28 public static String ATTR_LAYOUT_HEIGHT = "layout_height"; 29 30 // Some common Android layout attribute values used by the view rules. 31 public static String VALUE_FILL_PARENT = "fill_parent"; 32 public static String VALUE_MATCH_PARENT = "match_parent"; 33 public static String VALUE_MATCH_CONTENT = "match_content"; 34 35 36 /** 37 * Namespace for the Android resource XML, 38 * i.e. "http://schemas.android.com/apk/res/android" 39 */ 40 public static String ANDROID_URI = "http://schemas.android.com/apk/res/android"; 41 42 public boolean onInitialize(String fqcn) { 43 // This base rule can handle any class. 44 mFqcn = fqcn 45 return true; 46 } 47 48 public void onDispose() { 49 // Nothing to dispose. 50 } 51 52 public String getDisplayName() { 53 // Default is to not override the selection display name. 54 return null; 55 } 56 57 public Map<?, ?> getDefaultAttributes() { 58 // The base rule does not have any custom default attributes. 59 return null; 60 } 61 62 public String getFqcn() { 63 return mFqcn; 64 } 65 66 // ==== Selection ==== 67 68 void onSelected(IGraphics gc, INode selectedNode, 69 String displayName, boolean isMultipleSelection) { 70 Rect r = selectedNode.getBounds(); 71 72 if (!r.isValid()) { 73 return; 74 } 75 76 gc.setLineWidth(1); 77 gc.setLineStyle(IGraphics.LineStyle.LINE_SOLID); 78 gc.drawRect(r); 79 80 if (displayName == null || isMultipleSelection) { 81 return; 82 } 83 84 int xs = r.x + 2; 85 int ys = r.y - gc.getFontHeight(); 86 if (ys < 0) { 87 ys = r.y + r.h; 88 } 89 gc.drawString(displayName, xs, ys); 90 } 91 92 void onChildSelected(IGraphics gc, INode parentNode, INode childNode) { 93 Rect rp = parentNode.getBounds(); 94 Rect rc = childNode.getBounds(); 95 96 if (rp.isValid() && rc.isValid()) { 97 gc.setLineWidth(1); 98 gc.setLineStyle(IGraphics.LineStyle.LINE_DOT); 99 100 // top line 101 int m = rc.x + rc.w / 2; 102 gc.drawLine(m, rc.y, m, rp.y); 103 // bottom line 104 gc.drawLine(m, rc.y + rc.h, m, rp.y + rp.h); 105 // left line 106 m = rc.y + rc.h / 2; 107 gc.drawLine(rc.x, m, rp.x, m); 108 // right line 109 gc.drawLine(rc.x + rc.w, m, rp.x + rp.w, m); 110 } 111 } 112 113 114 // ==== Drag'n'drop support ==== 115 116 // By default Views do not accept drag'n'drop. 117 DropFeedback onDropEnter(INode targetNode, IDragElement[] elements) { 118 return null; 119 } 120 121 DropFeedback onDropMove(INode targetNode, IDragElement[] elements, 122 DropFeedback feedback, Point p) { 123 return null; 124 } 125 126 void onDropLeave(INode targetNode, IDragElement[] elements, DropFeedback feedback) { 127 // ignore 128 } 129 130 void onDropped(INode targetNode, IDragElement[] elements, DropFeedback feedback, Point p) { 131 // ignore 132 } 133 134} 135