aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/relative/Match.java
blob: 6f3f0d0f7bf066aec221f99ed8bd3fc6f4f6ed7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.eclipse.org/org/documents/epl-v10.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.ide.common.layout.relative;

import static com.android.SdkConstants.ATTR_ID;
import static com.android.SdkConstants.VALUE_TRUE;


import com.android.SdkConstants;
import static com.android.SdkConstants.ANDROID_URI;
import com.android.ide.common.api.Segment;

/** A match is a potential pairing of two segments with a given {@link ConstraintType}. */
class Match {
    /** the edge of the dragged node that is matched */
    public final Segment with;

    /** the "other" edge that the dragged edge is matched with */
    public final Segment edge;

    /** the signed distance between the matched edges */
    public final int delta;

    /** the type of constraint this is a match for */
    public final ConstraintType type;

    /** whether this {@link Match} results in a cycle */
    public boolean cycle;

    /** The associated {@link GuidelineHander} which performed the match */
    private final GuidelineHandler mHandler;

    /**
     * Create a new match.
     *
     * @param handler the handler which performed the match
     * @param edge the "other" edge that the dragged edge is matched with
     * @param with the edge of the dragged node that is matched
     * @param type the type of constraint this is a match for
     * @param delta the signed distance between the matched edges
     */
    public Match(GuidelineHandler handler, Segment edge, Segment with,
            ConstraintType type, int delta) {
        mHandler = handler;

        this.edge = edge;
        this.with = with;
        this.type = type;
        this.delta = delta;
    }

    /**
     * Returns the XML constraint attribute value for this match
     *
     * @param generateId whether an id should be generated if one is missing
     * @return the XML constraint attribute value for this match
     */
    public String getConstraint(boolean generateId) {
        if (type.targetParent) {
            return type.name + '=' + VALUE_TRUE;
        } else {
            String id = edge.id;
            if (id == null || id.length() == -1) {
                if (!generateId) {
                    // Placeholder to display for the user during dragging
                    id = "<generated>";
                } else {
                    // Must generate an id on the fly!
                    // See if it's been set by a different constraint we've already applied
                    // to this same node
                    id = edge.node.getStringAttr(ANDROID_URI, ATTR_ID);
                    if (id == null || id.length() == 0) {
                        id = mHandler.getRulesEngine().getUniqueId(edge.node.getFqcn());
                        edge.node.setAttribute(ANDROID_URI, ATTR_ID, id);
                    }
                }
            }
            return type.name + '=' + id;
        }
    }

    @Override
    public String toString() {
        return "Match [type=" + type + ", delta=" + delta + ", edge=" + edge
                + "]";
    }
}