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