1 /* 2 * Copyright (C) 2011 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 package com.android.ide.common.layout; 17 18 import static com.android.SdkConstants.ANDROID_URI; 19 import static com.android.SdkConstants.ATTR_LAYOUT_GRAVITY; 20 import static com.android.SdkConstants.GRAVITY_VALUE_BOTTOM; 21 import static com.android.SdkConstants.GRAVITY_VALUE_CENTER; 22 import static com.android.SdkConstants.GRAVITY_VALUE_CENTER_HORIZONTAL; 23 import static com.android.SdkConstants.GRAVITY_VALUE_CENTER_VERTICAL; 24 import static com.android.SdkConstants.GRAVITY_VALUE_FILL; 25 import static com.android.SdkConstants.GRAVITY_VALUE_FILL_HORIZONTAL; 26 import static com.android.SdkConstants.GRAVITY_VALUE_FILL_VERTICAL; 27 import static com.android.SdkConstants.GRAVITY_VALUE_LEFT; 28 import static com.android.SdkConstants.GRAVITY_VALUE_RIGHT; 29 import static com.android.SdkConstants.GRAVITY_VALUE_TOP; 30 31 import org.w3c.dom.Element; 32 33 /** Helper class for looking up the gravity masks of gravity attributes */ 34 public class GravityHelper { 35 // From SDK constants; temporary 36 public static final String GRAVITY_VALUE_START = "start"; //$NON-NLS-1$ 37 public static final String GRAVITY_VALUE_END = "end"; //$NON-NLS-1$ 38 39 /** Bitmask for a gravity which includes left */ 40 @SuppressWarnings("PointlessBitwiseExpression") // for symmetry with other fields 41 public static final int GRAVITY_LEFT = 1 << 0; 42 43 /** Bitmask for a gravity which includes right */ 44 public static final int GRAVITY_RIGHT = 1 << 1; 45 46 /** Bitmask for a gravity which includes center horizontal */ 47 public static final int GRAVITY_CENTER_HORIZ = 1 << 2; 48 49 /** Bitmask for a gravity which includes fill horizontal */ 50 public static final int GRAVITY_FILL_HORIZ = 1 << 3; 51 52 /** Bitmask for a gravity which includes center vertical */ 53 public static final int GRAVITY_CENTER_VERT = 1 << 4; 54 55 /** Bitmask for a gravity which includes fill vertical */ 56 public static final int GRAVITY_FILL_VERT = 1 << 5; 57 58 /** Bitmask for a gravity which includes top */ 59 public static final int GRAVITY_TOP = 1 << 6; 60 61 /** Bitmask for a gravity which includes bottom */ 62 public static final int GRAVITY_BOTTOM = 1 << 7; 63 64 /** Bitmask for a gravity which includes start */ 65 public static final int GRAVITY_START = 1 << 8; 66 67 /** Bitmask for a gravity which includes end */ 68 public static final int GRAVITY_END = 1 << 9; 69 70 /** Bitmask for a gravity which includes any horizontal constraint */ 71 public static final int GRAVITY_HORIZ_MASK = GRAVITY_CENTER_HORIZ | GRAVITY_FILL_HORIZ 72 | GRAVITY_LEFT | GRAVITY_RIGHT | GRAVITY_START | GRAVITY_END; 73 74 /** Bitmask for a gravity which any vertical constraint */ 75 public static final int GRAVITY_VERT_MASK = GRAVITY_CENTER_VERT | GRAVITY_FILL_VERT 76 | GRAVITY_TOP | GRAVITY_BOTTOM; 77 78 /** 79 * Returns the gravity of the given element 80 * 81 * @param element the element to look up the gravity for 82 * @return a bit mask corresponding to the selected gravities 83 */ getGravity(Element element)84 public static int getGravity(Element element) { 85 String gravityString = element.getAttributeNS(ANDROID_URI, ATTR_LAYOUT_GRAVITY); 86 return getGravity(gravityString, GRAVITY_LEFT | GRAVITY_TOP); 87 } 88 89 /** 90 * Returns the gravity bitmask for the given gravity string description 91 * 92 * @param gravityString the gravity string description 93 * @param defaultMask the default/initial bitmask to start with 94 * @return a bitmask corresponding to the gravity description 95 */ getGravity(String gravityString, int defaultMask)96 public static int getGravity(String gravityString, int defaultMask) { 97 int gravity = defaultMask; 98 if (gravityString != null && !gravityString.isEmpty()) { 99 String[] anchors = gravityString.split("\\|"); //$NON-NLS-1$ 100 for (String anchor : anchors) { 101 if (GRAVITY_VALUE_CENTER.equals(anchor)) { 102 gravity = GRAVITY_CENTER_HORIZ | GRAVITY_CENTER_VERT; 103 } else if (GRAVITY_VALUE_FILL.equals(anchor)) { 104 gravity = GRAVITY_FILL_HORIZ | GRAVITY_FILL_VERT; 105 } else if (GRAVITY_VALUE_CENTER_VERTICAL.equals(anchor)) { 106 gravity = (gravity & GRAVITY_HORIZ_MASK) | GRAVITY_CENTER_VERT; 107 } else if (GRAVITY_VALUE_CENTER_HORIZONTAL.equals(anchor)) { 108 gravity = (gravity & GRAVITY_VERT_MASK) | GRAVITY_CENTER_HORIZ; 109 } else if (GRAVITY_VALUE_FILL_VERTICAL.equals(anchor)) { 110 gravity = (gravity & GRAVITY_HORIZ_MASK) | GRAVITY_FILL_VERT; 111 } else if (GRAVITY_VALUE_FILL_HORIZONTAL.equals(anchor)) { 112 gravity = (gravity & GRAVITY_VERT_MASK) | GRAVITY_FILL_HORIZ; 113 } else if (GRAVITY_VALUE_TOP.equals(anchor)) { 114 gravity = (gravity & GRAVITY_HORIZ_MASK) | GRAVITY_TOP; 115 } else if (GRAVITY_VALUE_BOTTOM.equals(anchor)) { 116 gravity = (gravity & GRAVITY_HORIZ_MASK) | GRAVITY_BOTTOM; 117 } else if (GRAVITY_VALUE_LEFT.equals(anchor)) { 118 gravity = (gravity & (GRAVITY_VERT_MASK|GRAVITY_START)) | GRAVITY_LEFT; 119 } else if (GRAVITY_VALUE_RIGHT.equals(anchor)) { 120 gravity = (gravity & (GRAVITY_VERT_MASK|GRAVITY_END)) | GRAVITY_RIGHT; 121 } else if (GRAVITY_VALUE_START.equals(anchor)) { 122 gravity = (gravity & (GRAVITY_VERT_MASK|GRAVITY_LEFT)) | GRAVITY_START; 123 } else if (GRAVITY_VALUE_END.equals(anchor)) { 124 gravity = (gravity & (GRAVITY_VERT_MASK|GRAVITY_RIGHT)) | GRAVITY_END; 125 } // else: "clip" not supported 126 } 127 } 128 129 return gravity; 130 } 131 132 /** 133 * Returns true if the given gravity bitmask is constrained horizontally 134 * 135 * @param gravity the gravity bitmask 136 * @return true if the given gravity bitmask is constrained horizontally 137 */ isConstrainedHorizontally(int gravity)138 public static boolean isConstrainedHorizontally(int gravity) { 139 return (gravity & GRAVITY_HORIZ_MASK) != 0; 140 } 141 142 /** 143 * Returns true if the given gravity bitmask is constrained vertically 144 * 145 * @param gravity the gravity bitmask 146 * @return true if the given gravity bitmask is constrained vertically 147 */ isConstrainedVertically(int gravity)148 public static boolean isConstrainedVertically(int gravity) { 149 return (gravity & GRAVITY_VERT_MASK) != 0; 150 } 151 152 /** 153 * Returns true if the given gravity bitmask is left aligned 154 * 155 * @param gravity the gravity bitmask 156 * @return true if the given gravity bitmask is left aligned 157 */ isLeftAligned(int gravity)158 public static boolean isLeftAligned(int gravity) { 159 return (gravity & (GRAVITY_LEFT|GRAVITY_START)) != 0; 160 } 161 162 /** 163 * Returns true if the given gravity bitmask is top aligned 164 * 165 * @param gravity the gravity bitmask 166 * @return true if the given gravity bitmask is aligned 167 */ isTopAligned(int gravity)168 public static boolean isTopAligned(int gravity) { 169 return (gravity & GRAVITY_TOP) != 0; 170 } 171 172 /** Returns a gravity value string from the given gravity bitmask 173 * 174 * @param gravity the gravity bitmask 175 * @return the corresponding gravity string suitable as an XML attribute value 176 */ getGravity(int gravity)177 public static String getGravity(int gravity) { 178 if (gravity == 0) { 179 return ""; 180 } 181 182 if ((gravity & (GRAVITY_CENTER_HORIZ | GRAVITY_CENTER_VERT)) == 183 (GRAVITY_CENTER_HORIZ | GRAVITY_CENTER_VERT)) { 184 return GRAVITY_VALUE_CENTER; 185 } 186 187 StringBuilder sb = new StringBuilder(30); 188 int horizontal = gravity & GRAVITY_HORIZ_MASK; 189 int vertical = gravity & GRAVITY_VERT_MASK; 190 191 if ((horizontal & (GRAVITY_LEFT|GRAVITY_START)) != 0) { 192 if ((horizontal & GRAVITY_LEFT) != 0) { 193 sb.append(GRAVITY_VALUE_LEFT); 194 } 195 if ((horizontal & GRAVITY_START) != 0) { 196 if (sb.length() > 0) { 197 sb.append('|'); 198 } 199 sb.append(GRAVITY_VALUE_START); 200 } 201 } else if ((horizontal & (GRAVITY_RIGHT|GRAVITY_END)) != 0) { 202 if ((horizontal & GRAVITY_RIGHT) != 0) { 203 sb.append(GRAVITY_VALUE_RIGHT); 204 } 205 if ((horizontal & GRAVITY_END) != 0) { 206 if (sb.length() > 0) { 207 sb.append('|'); 208 } 209 sb.append(GRAVITY_VALUE_END); 210 } 211 } else if ((horizontal & GRAVITY_CENTER_HORIZ) != 0) { 212 sb.append(GRAVITY_VALUE_CENTER_HORIZONTAL); 213 } else if ((horizontal & GRAVITY_FILL_HORIZ) != 0) { 214 sb.append(GRAVITY_VALUE_FILL_HORIZONTAL); 215 } 216 217 if (sb.length() > 0 && vertical != 0) { 218 sb.append('|'); 219 } 220 221 if ((vertical & GRAVITY_TOP) != 0) { 222 sb.append(GRAVITY_VALUE_TOP); 223 } else if ((vertical & GRAVITY_BOTTOM) != 0) { 224 sb.append(GRAVITY_VALUE_BOTTOM); 225 } else if ((vertical & GRAVITY_CENTER_VERT) != 0) { 226 sb.append(GRAVITY_VALUE_CENTER_VERTICAL); 227 } else if ((vertical & GRAVITY_FILL_VERT) != 0) { 228 sb.append(GRAVITY_VALUE_FILL_VERTICAL); 229 } 230 231 return sb.toString(); 232 } 233 } 234