• 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.relative;
17 
18 import static com.android.SdkConstants.ATTR_ID;
19 import static com.android.SdkConstants.VALUE_TRUE;
20 
21 
22 import com.android.SdkConstants;
23 import static com.android.SdkConstants.ANDROID_URI;
24 import com.android.ide.common.api.Segment;
25 
26 /** A match is a potential pairing of two segments with a given {@link ConstraintType}. */
27 class Match {
28     /** the edge of the dragged node that is matched */
29     public final Segment with;
30 
31     /** the "other" edge that the dragged edge is matched with */
32     public final Segment edge;
33 
34     /** the signed distance between the matched edges */
35     public final int delta;
36 
37     /** the type of constraint this is a match for */
38     public final ConstraintType type;
39 
40     /** whether this {@link Match} results in a cycle */
41     public boolean cycle;
42 
43     /** The associated {@link GuidelineHander} which performed the match */
44     private final GuidelineHandler mHandler;
45 
46     /**
47      * Create a new match.
48      *
49      * @param handler the handler which performed the match
50      * @param edge the "other" edge that the dragged edge is matched with
51      * @param with the edge of the dragged node that is matched
52      * @param type the type of constraint this is a match for
53      * @param delta the signed distance between the matched edges
54      */
Match(GuidelineHandler handler, Segment edge, Segment with, ConstraintType type, int delta)55     public Match(GuidelineHandler handler, Segment edge, Segment with,
56             ConstraintType type, int delta) {
57         mHandler = handler;
58 
59         this.edge = edge;
60         this.with = with;
61         this.type = type;
62         this.delta = delta;
63     }
64 
65     /**
66      * Returns the XML constraint attribute value for this match
67      *
68      * @param generateId whether an id should be generated if one is missing
69      * @return the XML constraint attribute value for this match
70      */
getConstraint(boolean generateId)71     public String getConstraint(boolean generateId) {
72         if (type.targetParent) {
73             return type.name + '=' + VALUE_TRUE;
74         } else {
75             String id = edge.id;
76             if (id == null || id.length() == -1) {
77                 if (!generateId) {
78                     // Placeholder to display for the user during dragging
79                     id = "<generated>";
80                 } else {
81                     // Must generate an id on the fly!
82                     // See if it's been set by a different constraint we've already applied
83                     // to this same node
84                     id = edge.node.getStringAttr(ANDROID_URI, ATTR_ID);
85                     if (id == null || id.length() == 0) {
86                         id = mHandler.getRulesEngine().getUniqueId(edge.node.getFqcn());
87                         edge.node.setAttribute(ANDROID_URI, ATTR_ID, id);
88                     }
89                 }
90             }
91             return type.name + '=' + id;
92         }
93     }
94 
95     @Override
toString()96     public String toString() {
97         return "Match [type=" + type + ", delta=" + delta + ", edge=" + edge
98                 + "]";
99     }
100 }
101