PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/relative/ConstraintType.java

https://gitlab.com/Codeaurora/platform_sdk
Java | 237 lines | 156 code | 23 blank | 58 comment | 22 complexity | e0fef0d7ccae93aeadb65df7e4f28843 MD5 | raw file
  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.relative;
  17. import static com.android.ide.common.api.SegmentType.BASELINE;
  18. import static com.android.ide.common.api.SegmentType.BOTTOM;
  19. import static com.android.ide.common.api.SegmentType.CENTER_HORIZONTAL;
  20. import static com.android.ide.common.api.SegmentType.CENTER_VERTICAL;
  21. import static com.android.ide.common.api.SegmentType.LEFT;
  22. import static com.android.ide.common.api.SegmentType.RIGHT;
  23. import static com.android.ide.common.api.SegmentType.TOP;
  24. import static com.android.ide.common.api.SegmentType.UNKNOWN;
  25. import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_ABOVE;
  26. import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_ALIGN_BASELINE;
  27. import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_ALIGN_BOTTOM;
  28. import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_ALIGN_LEFT;
  29. import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_ALIGN_PARENT_BOTTOM;
  30. import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_ALIGN_PARENT_LEFT;
  31. import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_ALIGN_PARENT_RIGHT;
  32. import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_ALIGN_PARENT_TOP;
  33. import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_ALIGN_RIGHT;
  34. import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_ALIGN_TOP;
  35. import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_BELOW;
  36. import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_CENTER_HORIZONTAL;
  37. import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_CENTER_IN_PARENT;
  38. import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_CENTER_VERTICAL;
  39. import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_TO_LEFT_OF;
  40. import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_TO_RIGHT_OF;
  41. import com.android.ide.common.api.SegmentType;
  42. import java.util.HashMap;
  43. import java.util.Map;
  44. /**
  45. * Each constraint type corresponds to a type of constraint available for the
  46. * RelativeLayout; for example, {@link #LAYOUT_ABOVE} corresponds to the layout_above constraint.
  47. */
  48. enum ConstraintType {
  49. LAYOUT_ABOVE(ATTR_LAYOUT_ABOVE,
  50. null /* sourceX */, BOTTOM, null /* targetX */, TOP,
  51. false /* targetParent */, true /* horizontalEdge */, false /* verticalEdge */,
  52. true /* relativeToMargin */),
  53. LAYOUT_BELOW(ATTR_LAYOUT_BELOW, null, TOP, null, BOTTOM, false, true, false, true),
  54. ALIGN_TOP(ATTR_LAYOUT_ALIGN_TOP, null, TOP, null, TOP, false, true, false, false),
  55. ALIGN_BOTTOM(ATTR_LAYOUT_ALIGN_BOTTOM, null, BOTTOM, null, BOTTOM, false, true, false, false),
  56. ALIGN_LEFT(ATTR_LAYOUT_ALIGN_LEFT, LEFT, null, LEFT, null, false, false, true, false),
  57. ALIGN_RIGHT(ATTR_LAYOUT_ALIGN_RIGHT, RIGHT, null, RIGHT, null, false, false, true, false),
  58. LAYOUT_LEFT_OF(ATTR_LAYOUT_TO_LEFT_OF, RIGHT, null, LEFT, null, false, false, true, true),
  59. LAYOUT_RIGHT_OF(ATTR_LAYOUT_TO_RIGHT_OF, LEFT, null, RIGHT, null, false, false, true, true),
  60. ALIGN_PARENT_TOP(ATTR_LAYOUT_ALIGN_PARENT_TOP, null, TOP, null, TOP, true, true, false, false),
  61. ALIGN_BASELINE(ATTR_LAYOUT_ALIGN_BASELINE, null, BASELINE, null, BASELINE, false, true, false,
  62. false),
  63. ALIGN_PARENT_LEFT(ATTR_LAYOUT_ALIGN_PARENT_LEFT, LEFT, null, LEFT, null, true, false, true,
  64. false),
  65. ALIGN_PARENT_RIGHT(ATTR_LAYOUT_ALIGN_PARENT_RIGHT, RIGHT, null, RIGHT, null, true, false, true,
  66. false),
  67. ALIGN_PARENT_BOTTOM(ATTR_LAYOUT_ALIGN_PARENT_BOTTOM, null, BOTTOM, null, BOTTOM, true, true,
  68. false, false),
  69. LAYOUT_CENTER_HORIZONTAL(ATTR_LAYOUT_CENTER_HORIZONTAL, CENTER_VERTICAL, null, CENTER_VERTICAL,
  70. null, true, true, false, false),
  71. LAYOUT_CENTER_VERTICAL(ATTR_LAYOUT_CENTER_VERTICAL, null, CENTER_HORIZONTAL, null,
  72. CENTER_HORIZONTAL, true, false, true, false),
  73. LAYOUT_CENTER_IN_PARENT(ATTR_LAYOUT_CENTER_IN_PARENT, CENTER_VERTICAL, CENTER_HORIZONTAL,
  74. CENTER_VERTICAL, CENTER_HORIZONTAL, true, true, true, false);
  75. private ConstraintType(String name, SegmentType sourceSegmentTypeX,
  76. SegmentType sourceSegmentTypeY, SegmentType targetSegmentTypeX,
  77. SegmentType targetSegmentTypeY, boolean targetParent, boolean horizontalEdge,
  78. boolean verticalEdge, boolean relativeToMargin) {
  79. assert horizontalEdge || verticalEdge;
  80. this.name = name;
  81. this.sourceSegmentTypeX = sourceSegmentTypeX != null ? sourceSegmentTypeX : UNKNOWN;
  82. this.sourceSegmentTypeY = sourceSegmentTypeY != null ? sourceSegmentTypeY : UNKNOWN;
  83. this.targetSegmentTypeX = targetSegmentTypeX != null ? targetSegmentTypeX : UNKNOWN;
  84. this.targetSegmentTypeY = targetSegmentTypeY != null ? targetSegmentTypeY : UNKNOWN;
  85. this.targetParent = targetParent;
  86. this.horizontalEdge = horizontalEdge;
  87. this.verticalEdge = verticalEdge;
  88. this.relativeToMargin = relativeToMargin;
  89. }
  90. /** The attribute name of the constraint */
  91. public final String name;
  92. /** The horizontal position of the source of the constraint */
  93. public final SegmentType sourceSegmentTypeX;
  94. /** The vertical position of the source of the constraint */
  95. public final SegmentType sourceSegmentTypeY;
  96. /** The horizontal position of the target of the constraint */
  97. public final SegmentType targetSegmentTypeX;
  98. /** The vertical position of the target of the constraint */
  99. public final SegmentType targetSegmentTypeY;
  100. /**
  101. * If true, the constraint targets the parent layout, otherwise it targets another
  102. * view
  103. */
  104. public final boolean targetParent;
  105. /** If true, this constraint affects the horizontal dimension */
  106. public final boolean horizontalEdge;
  107. /** If true, this constraint affects the vertical dimension */
  108. public final boolean verticalEdge;
  109. /**
  110. * Whether this constraint is relative to the margin bounds of the node rather than
  111. * the node's actual bounds
  112. */
  113. public final boolean relativeToMargin;
  114. /** Map from attribute name to constraint type */
  115. private static Map<String, ConstraintType> sNameToType;
  116. /**
  117. * Returns the {@link ConstraintType} corresponding to the given attribute name, or
  118. * null if not found.
  119. *
  120. * @param attribute the name of the attribute to look up
  121. * @return the corresponding {@link ConstraintType}
  122. */
  123. public static ConstraintType fromAttribute(String attribute) {
  124. if (sNameToType == null) {
  125. ConstraintType[] types = ConstraintType.values();
  126. Map<String, ConstraintType> map = new HashMap<String, ConstraintType>(types.length);
  127. for (ConstraintType type : types) {
  128. map.put(type.name, type);
  129. }
  130. sNameToType = map;
  131. }
  132. return sNameToType.get(attribute);
  133. }
  134. /**
  135. * Returns true if this constraint type represents a constraint where the target edge
  136. * is one of the parent edges (actual edge, not center/baseline segments)
  137. *
  138. * @return true if the target segment is a parent edge
  139. */
  140. public boolean isRelativeToParentEdge() {
  141. return this == ALIGN_PARENT_LEFT || this == ALIGN_PARENT_RIGHT || this == ALIGN_PARENT_TOP
  142. || this == ALIGN_PARENT_BOTTOM;
  143. }
  144. /**
  145. * Returns a {@link ConstraintType} for a potential match of edges.
  146. *
  147. * @param withParent if true, the target is the parent
  148. * @param from the source edge
  149. * @param to the target edge
  150. * @return a {@link ConstraintType}, or null
  151. */
  152. public static ConstraintType forMatch(boolean withParent, SegmentType from, SegmentType to) {
  153. // Attached to parent edge?
  154. if (withParent) {
  155. switch (from) {
  156. case TOP:
  157. return ALIGN_PARENT_TOP;
  158. case BOTTOM:
  159. return ALIGN_PARENT_BOTTOM;
  160. case LEFT:
  161. return ALIGN_PARENT_LEFT;
  162. case RIGHT:
  163. return ALIGN_PARENT_RIGHT;
  164. case CENTER_HORIZONTAL:
  165. return LAYOUT_CENTER_VERTICAL;
  166. case CENTER_VERTICAL:
  167. return LAYOUT_CENTER_HORIZONTAL;
  168. }
  169. return null;
  170. }
  171. // Attached to some other node.
  172. switch (from) {
  173. case TOP:
  174. switch (to) {
  175. case TOP:
  176. return ALIGN_TOP;
  177. case BOTTOM:
  178. return LAYOUT_BELOW;
  179. case BASELINE:
  180. return ALIGN_BASELINE;
  181. }
  182. break;
  183. case BOTTOM:
  184. switch (to) {
  185. case TOP:
  186. return LAYOUT_ABOVE;
  187. case BOTTOM:
  188. return ALIGN_BOTTOM;
  189. case BASELINE:
  190. return ALIGN_BASELINE;
  191. }
  192. break;
  193. case LEFT:
  194. switch (to) {
  195. case LEFT:
  196. return ALIGN_LEFT;
  197. case RIGHT:
  198. return LAYOUT_RIGHT_OF;
  199. }
  200. break;
  201. case RIGHT:
  202. switch (to) {
  203. case LEFT:
  204. return LAYOUT_LEFT_OF;
  205. case RIGHT:
  206. return ALIGN_RIGHT;
  207. }
  208. break;
  209. case BASELINE:
  210. return ALIGN_BASELINE;
  211. }
  212. return null;
  213. }
  214. }