• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.ide.common.layout.LayoutConstants.ANDROID_URI;
19 import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_GRAVITY;
20 import static com.android.ide.common.layout.LayoutConstants.GRAVITY_VALUE_BOTTOM;
21 import static com.android.ide.common.layout.LayoutConstants.GRAVITY_VALUE_CENTER;
22 import static com.android.ide.common.layout.LayoutConstants.GRAVITY_VALUE_CENTER_HORIZONTAL;
23 import static com.android.ide.common.layout.LayoutConstants.GRAVITY_VALUE_CENTER_VERTICAL;
24 import static com.android.ide.common.layout.LayoutConstants.GRAVITY_VALUE_FILL;
25 import static com.android.ide.common.layout.LayoutConstants.GRAVITY_VALUE_FILL_HORIZONTAL;
26 import static com.android.ide.common.layout.LayoutConstants.GRAVITY_VALUE_FILL_VERTICAL;
27 import static com.android.ide.common.layout.LayoutConstants.GRAVITY_VALUE_LEFT;
28 import static com.android.ide.common.layout.LayoutConstants.GRAVITY_VALUE_RIGHT;
29 import static com.android.ide.common.layout.LayoutConstants.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     public static final int GRAVITY_LEFT         = 1 << 0;
36     public static final int GRAVITY_RIGHT        = 1 << 1;
37     public static final int GRAVITY_CENTER_HORIZ = 1 << 2;
38     public static final int GRAVITY_FILL_HORIZ   = 1 << 3;
39     public static final int GRAVITY_CENTER_VERT  = 1 << 4;
40     public static final int GRAVITY_FILL_VERT    = 1 << 5;
41     public static final int GRAVITY_TOP          = 1 << 6;
42     public static final int GRAVITY_BOTTOM       = 1 << 7;
43     public static final int GRAVITY_HORIZ_MASK = GRAVITY_CENTER_HORIZ | GRAVITY_FILL_HORIZ
44             | GRAVITY_LEFT | GRAVITY_RIGHT;
45     public static final int GRAVITY_VERT_MASK = GRAVITY_CENTER_VERT | GRAVITY_FILL_VERT
46             | GRAVITY_TOP | GRAVITY_BOTTOM;
47 
48     /**
49      * Returns the gravity of the given element
50      *
51      * @param element the element to look up the gravity for
52      * @return a bit mask corresponding to the selected gravities
53      */
getGravity(Element element)54     public static int getGravity(Element element) {
55         String gravityString = element.getAttributeNS(ANDROID_URI, ATTR_LAYOUT_GRAVITY);
56         return getGravity(gravityString, GRAVITY_LEFT | GRAVITY_TOP);
57     }
58 
59     /**
60      * Returns the gravity bitmask for the given gravity string description
61      *
62      * @param gravityString the gravity string description
63      * @param defaultMask the default/initial bitmask to start with
64      * @return a bitmask corresponding to the gravity description
65      */
getGravity(String gravityString, int defaultMask)66     public static int getGravity(String gravityString, int defaultMask) {
67         int gravity = defaultMask;
68         if (gravityString != null && gravityString.length() > 0) {
69             String[] anchors = gravityString.split("\\|"); //$NON-NLS-1$
70             for (String anchor : anchors) {
71                 if (GRAVITY_VALUE_CENTER.equals(anchor)) {
72                     gravity = GRAVITY_CENTER_HORIZ | GRAVITY_CENTER_VERT;
73                 } else if (GRAVITY_VALUE_FILL.equals(anchor)) {
74                     gravity = GRAVITY_FILL_HORIZ | GRAVITY_FILL_VERT;
75                 } else if (GRAVITY_VALUE_CENTER_VERTICAL.equals(anchor)) {
76                     gravity = (gravity & GRAVITY_HORIZ_MASK) | GRAVITY_CENTER_VERT;
77                 } else if (GRAVITY_VALUE_CENTER_HORIZONTAL.equals(anchor)) {
78                     gravity = (gravity & GRAVITY_VERT_MASK) | GRAVITY_CENTER_HORIZ;
79                 } else if (GRAVITY_VALUE_FILL_VERTICAL.equals(anchor)) {
80                     gravity = (gravity & GRAVITY_HORIZ_MASK) | GRAVITY_FILL_VERT;
81                 } else if (GRAVITY_VALUE_FILL_HORIZONTAL.equals(anchor)) {
82                     gravity = (gravity & GRAVITY_VERT_MASK) | GRAVITY_FILL_HORIZ;
83                 } else if (GRAVITY_VALUE_TOP.equals(anchor)) {
84                     gravity = (gravity & GRAVITY_HORIZ_MASK) | GRAVITY_TOP;
85                 } else if (GRAVITY_VALUE_BOTTOM.equals(anchor)) {
86                     gravity = (gravity & GRAVITY_HORIZ_MASK) | GRAVITY_BOTTOM;
87                 } else if (GRAVITY_VALUE_LEFT.equals(anchor)) {
88                     gravity = (gravity & GRAVITY_VERT_MASK) | GRAVITY_LEFT;
89                 } else if (GRAVITY_VALUE_RIGHT.equals(anchor)) {
90                     gravity = (gravity & GRAVITY_VERT_MASK) | GRAVITY_RIGHT;
91                 } else {
92                     // "clip" not supported
93                 }
94             }
95         }
96 
97         return gravity;
98     }
99 }
100