1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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 
17 package androidx.constraintlayout.core.dsl;
18 
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.Map;
22 
23 public abstract class Chain extends Helper {
24     public enum Style {
25         PACKED,
26         SPREAD,
27         SPREAD_INSIDE
28     }
29 
30     private Style mStyle = null;
31     protected ArrayList<Ref> references = new ArrayList<>();
32     final static protected Map<Style, String> styleMap = new HashMap<>();
33     static {
styleMap.put(Style.SPREAD, "'spread'")34         styleMap.put(Style.SPREAD, "'spread'");
styleMap.put(Style.SPREAD_INSIDE, "'spread_inside'")35         styleMap.put(Style.SPREAD_INSIDE, "'spread_inside'");
styleMap.put(Style.PACKED, "'packed'")36         styleMap.put(Style.PACKED, "'packed'");
37     }
38 
Chain(String name)39     public Chain(String name) {
40         super(name, new HelperType(""));
41     }
42 
getStyle()43     public Style getStyle() {
44         return mStyle;
45     }
46 
setStyle(Style style)47     public void setStyle(Style style) {
48         mStyle = style;
49         configMap.put("style", styleMap.get(style));
50     }
51 
52     /**
53      * convert references to a String representation
54      *
55      * @return a String representation of references
56      */
referencesToString()57     public String referencesToString() {
58         if (references.isEmpty()) {
59             return "";
60         }
61 
62         StringBuilder builder = new StringBuilder("[");
63         for (Ref ref : references) {
64             builder.append(ref.toString());
65         }
66         builder.append("]");
67         return builder.toString();
68     }
69 
70     /**
71      * Add a new reference
72      *
73      * @param ref reference
74      * @return Chain
75      */
addReference(Ref ref)76     public Chain addReference(Ref ref) {
77         references.add(ref);
78         configMap.put("contains", referencesToString());
79         return this;
80     }
81 
82     /**
83      * Add a new reference
84      *
85      * @param ref reference in a String representation
86      * @return Chain
87      */
addReference(String ref)88     public Chain addReference(String ref) {
89         return addReference(Ref.parseStringToRef(ref));
90     }
91 
92     public class Anchor {
93         final Constraint.Side mSide;
94         Constraint.Anchor mConnection = null;
95         int mMargin;
96         int mGoneMargin = Integer.MIN_VALUE;
97 
Anchor(Constraint.Side side)98         Anchor(Constraint.Side side) {
99             mSide = side;
100         }
101 
getId()102         public String getId() {
103             return name;
104         }
105 
build(StringBuilder builder)106         public void build(StringBuilder builder) {
107             if (mConnection != null) {
108                 builder.append(mSide.toString().toLowerCase())
109                         .append(":").append(this).append(",\n");
110             }
111         }
112 
113         @Override
toString()114         public String toString() {
115             StringBuilder ret = new StringBuilder("[");
116 
117             if (mConnection != null) {
118                 ret.append("'").append(mConnection.getId()).append("',")
119                         .append("'").append(mConnection.mSide.toString().toLowerCase()).append("'");
120             }
121 
122             if (mMargin != 0) {
123                 ret.append(",").append(mMargin);
124             }
125 
126             if (mGoneMargin != Integer.MIN_VALUE) {
127                 if ( mMargin == 0) {
128                     ret.append(",0,").append(mGoneMargin);
129                 } else {
130                     ret.append(",").append(mGoneMargin);
131                 }
132             }
133             ret.append("]");
134             return ret.toString();
135         }
136     }
137 }
138