aboutsummaryrefslogtreecommitdiffstats
path: root/eclipse/plugins/com.android.ide.eclipse.adt/gscripts/android.widget.LinearLayout.groovy
blob: 7f74bfb0dabafbef04cc0e47169a20ccbd2b68b4 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * Copyright (C) 2010 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.adt.gscripts;

/**
 * An {@link IViewRule} for android.widget.LinearLayout and all its derived classes.
 */
public class AndroidWidgetLinearLayoutRule extends BaseLayout {

    public static String ATTR_ORIENTATION = "orientation";
    public static String VALUE_HORIZONTAL = "horizontal";
    public static String VALUE_VERTICAL = "vertical";

    /**
     * Add an explicit Orientation toggle to the context menu.
     */
    public List<MenuAction> getContextMenu(INode selectedNode) {

        def curr_orient = selectedNode.getStringAttr(ANDROID_URI, ATTR_ORIENTATION);
        if (!curr_orient) {
            curr_orient = VALUE_VERTICAL;
        }

        def onChange = { MenuAction.Action action, String valueId, Boolean newValue ->
            def actionId = action.getId();
            def node = selectedNode;

            if (actionId == "_orientation") {
                node.editXml("Change LinearLayout " + ATTR_ORIENTATION) {
                    node.setAttribute(ANDROID_URI, ATTR_ORIENTATION, valueId);
                }
            }
        }

        return super.getContextMenu(selectedNode) +
               [ new MenuAction.Choices("_orientation", "Orientation",
                        [ horizontal : "Horizontal",
                          vertical : "Vertical" ],
                        curr_orient,
                        onChange ),
               ];
    }

    // ==== Drag'n'drop support ====

    DropFeedback onDropEnter(INode targetNode, IDragElement[] elements) {

        if (elements.length == 0) {
            return null;
        }

        def bn = targetNode.getBounds();
        if (!bn.isValid()) {
            return;
        }

        boolean isVertical =
            targetNode.getStringAttr(ANDROID_URI, ATTR_ORIENTATION) == VALUE_VERTICAL;

        // Prepare a list of insertion points: X coords for horizontal, Y for vertical.
        // Each list is a tuple: 0=pixel coordinate, 1=index of children or -1 for "at end".
        def indexes = [ ];

        int last = isVertical ? bn.y : bn.x;
        int pos = 0;
        targetNode.getChildren().each {
            def bc = it.getBounds();
            if (bc.isValid()) {
                // add an insertion point between the last point and the start of this child
                int v = isVertical ? bc.y : bc.x;
                v = (last + v) / 2;
                indexes.add( [v, pos++] );

                last = isVertical ? (bc.y + bc.h) : (bc.x + bc.w);
            }
        }

        int v = isVertical ? (bn.y + bn.h) : (bn.x + bn.w);
        v = (last + v) / 2;
        indexes.add( [v, -1] );

        return new DropFeedback(
          [ "isVertical": isVertical,   // boolean: True if vertical linear layout
            "indexes": indexes,         // list(tuple(0:int, 1:int)): insert points (pixels + index)
            "currX": null,              // int: Current marker X position
            "currY": null,              // int: Current marker Y position
            "insertPos": -1             // int: Current drop insert index (-1 for "at the end")
          ],
          {
            gc, node, feedback ->
            // Paint closure for the LinearLayout.
            // This is called by the canvas when a draw is needed.

            drawFeedback(gc, node, elements, feedback);
        });
    }

    void drawFeedback(IGraphics gc,
                      INode node,
                      IDragElement[] elements,
                      DropFeedback feedback) {
        Rect b = node.getBounds();
        if (!b.isValid()) {
            return;
        }

        // Highlight the receiver
        gc.setForeground(gc.registerColor(0x00FFFF00));
        gc.setLineStyle(IGraphics.LineStyle.LINE_SOLID);
        gc.setLineWidth(2);
        gc.drawRect(b);

        gc.setLineStyle(IGraphics.LineStyle.LINE_DOT);
        gc.setLineWidth(1);

        def indexes = feedback.userData.indexes;
        boolean isVertical = feedback.userData.isVertical;

        indexes.each {
            int i = it[0];
            if (isVertical) {
                // draw horizontal lines
                gc.drawLine(b.x, i, b.x + b.w, i);
            } else {
                // draw vertical lines
                gc.drawLine(i, b.y, i, b.y + b.h);
            }
        }

        def currX = feedback.userData.currX;
        def currY = feedback.userData.currY;

        if (currX != null && currY != null) {
            int x = currX;
            int y = currY;

            // Draw a mark at the drop point.
            gc.setLineStyle(IGraphics.LineStyle.LINE_SOLID);
            gc.setLineWidth(2);

            gc.drawLine(x - 10, y - 10, x + 10, y + 10);
            gc.drawLine(x + 10, y - 10, x - 10, y + 10);
            gc.drawOval(x - 10, y - 10, x + 10, y + 10);

            Rect be = elements[0].getBounds();

            if (be.isValid()) {
                // At least the first element has a bound. Draw rectangles
                // for all dropped elements with valid bounds, offset at
                // the drop point.

                int offsetX = x - be.x;
                int offsetY = y - be.y;

                // If there's a parent, keep the X/Y coordinate the same relative to the parent.
                Rect pb = elements[0].getParentBounds();
                if (pb.isValid()) {
                    if (isVertical) {
                        offsetX = b.x - pb.x;
                    } else {
                        offsetY = b.y - pb.y;
                    }
                }

                for (element in elements) {
                    drawElement(gc, element, offsetX, offsetY);
                }
            }
        }
    }

    DropFeedback onDropMove(INode targetNode,
                            IDragElement[] elements,
                            DropFeedback feedback,
                            Point p) {
        def data = feedback.userData;

        Rect b = targetNode.getBounds();
        if (!b.isValid()) {
            return feedback;
        }

        boolean isVertical = data.isVertical;

        int bestDist = Integer.MAX_VALUE;
        int bestIndex = Integer.MIN_VALUE;
        int bestPos = null;

        for(index in data.indexes) {
            int i   = index[0];
            int pos = index[1];
            int dist = (isVertical ? p.y : p.x) - i;
            if (dist < 0) dist = - dist;
            if (dist < bestDist) {
                bestDist = dist;
                bestIndex = i;
                bestPos = pos;
                if (bestDist <= 0) break;
            }
        }

        if (bestIndex != Integer.MIN_VALUE) {
            def old_x = data.currX;
            def old_y = data.currY;

            if (isVertical) {
                data.currX = b.x + b.w / 2;
                data.currY = bestIndex;
            } else {
                data.currX = bestIndex;
                data.currY = b.y + b.h / 2;
            }

            data.insertPos = bestPos;

            feedback.requestPaint = (old_x != data.currX) || (old_y != data.currY);
        }

        return feedback;
    }

    void onDropLeave(INode targetNode, IDragElement[] elements, DropFeedback feedback) {
        // ignore
    }

    void onDropped(INode targetNode,
                   IDragElement[] elements,
                   DropFeedback feedback,
                   Point p) {

        int insertPos = feedback.userData.insertPos;

        // Collect IDs from dropped elements and remap them to new IDs
        // if this is a copy or from a different canvas.
        def idMap = getDropIdMap(targetNode, elements, feedback.isCopy || !feedback.sameCanvas);

        targetNode.editXml("Add elements to LinearLayout") {

            // Now write the new elements.
            for (element in elements) {
                String fqcn = element.getFqcn();
                Rect be = element.getBounds();

                INode newChild = targetNode.insertChildAt(fqcn, insertPos);

                // insertPos==-1 means to insert at the end. Otherwise
                // increment the insertion position.
                if (insertPos >= 0) {
                    insertPos++;
                }

                // Copy all the attributes, modifying them as needed.
                def attrFilter = getLayoutAttrFilter();
                addAttributes(newChild, element, idMap) {
                    uri, name, value ->
                    // TODO need a better way to exclude other layout attributes dynamically
                    if (uri == ANDROID_URI && name in attrFilter) {
                        return false; // don't set these attributes
                    } else {
                        return value;
                    }
                };

                addInnerElements(newChild, element, idMap);
            }
        }


    }
}