aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/WrapInRefactoring.java
blob: 07b00b8da29a57b2d7b5fd1494f7ba3992790be1 (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
 * 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.eclipse.adt.internal.editors.layout.refactoring;

import static com.android.SdkConstants.ANDROID_NS_NAME_PREFIX;
import static com.android.SdkConstants.ANDROID_URI;
import static com.android.SdkConstants.ANDROID_WIDGET_PREFIX;
import static com.android.SdkConstants.ATTR_ID;
import static com.android.SdkConstants.ATTR_LAYOUT_HEIGHT;
import static com.android.SdkConstants.ATTR_LAYOUT_WIDTH;
import static com.android.SdkConstants.EXT_XML;
import static com.android.SdkConstants.VALUE_FILL_PARENT;
import static com.android.SdkConstants.VALUE_MATCH_PARENT;
import static com.android.SdkConstants.VALUE_WRAP_CONTENT;

import com.android.annotations.NonNull;
import com.android.annotations.VisibleForTesting;
import com.android.ide.common.xml.XmlFormatStyle;
import com.android.ide.eclipse.adt.internal.editors.AndroidXmlEditor;
import com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditorDelegate;
import com.android.ide.eclipse.adt.internal.editors.layout.gle2.CanvasViewInfo;
import com.android.ide.eclipse.adt.internal.preferences.AdtPrefs;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.Refactoring;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.TextFileChange;
import org.eclipse.text.edits.DeleteEdit;
import org.eclipse.text.edits.InsertEdit;
import org.eclipse.text.edits.MultiTextEdit;
import org.eclipse.text.edits.TextEdit;
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;
import org.w3c.dom.Attr;
import org.w3c.dom.Element;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Inserts a new layout surrounding the current selection, migrates namespace
 * attributes (if wrapping the root node), and optionally migrates layout
 * attributes and updates references elsewhere.
 */
@SuppressWarnings("restriction") // XML model
public class WrapInRefactoring extends VisualRefactoring {
    private static final String KEY_ID = "name";                           //$NON-NLS-1$
    private static final String KEY_TYPE = "type";                         //$NON-NLS-1$

    private String mId;
    private String mTypeFqcn;
    private String mInitializedAttributes;

    /**
     * This constructor is solely used by {@link Descriptor},
     * to replay a previous refactoring.
     * @param arguments argument map created by #createArgumentMap.
     */
    WrapInRefactoring(Map<String, String> arguments) {
        super(arguments);
        mId = arguments.get(KEY_ID);
        mTypeFqcn = arguments.get(KEY_TYPE);
    }

    public WrapInRefactoring(
            IFile file,
            LayoutEditorDelegate delegate,
            ITextSelection selection,
            ITreeSelection treeSelection) {
        super(file, delegate, selection, treeSelection);
    }

    @VisibleForTesting
    WrapInRefactoring(List<Element> selectedElements, LayoutEditorDelegate editor) {
        super(selectedElements, editor);
    }

    @Override
    public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException,
            OperationCanceledException {
        RefactoringStatus status = new RefactoringStatus();

        try {
            pm.beginTask("Checking preconditions...", 6);

            if (mSelectionStart == -1 || mSelectionEnd == -1) {
                status.addFatalError("No selection to wrap");
                return status;
            }

            // Make sure the selection is contiguous
            if (mTreeSelection != null) {
                // TODO - don't do this if we based the selection on text. In this case,
                // make sure we're -balanced-.

                List<CanvasViewInfo> infos = getSelectedViewInfos();
                if (!validateNotEmpty(infos, status)) {
                    return status;
                }

                // Enforce that the selection is -contiguous-
                if (!validateContiguous(infos, status)) {
                    return status;
                }
            }

            // Ensures that we have a valid DOM model:
            if (mElements.size() == 0) {
                status.addFatalError("Nothing to wrap");
                return status;
            }

            pm.worked(1);
            return status;

        } finally {
            pm.done();
        }
    }

    @Override
    protected VisualRefactoringDescriptor createDescriptor() {
        String comment = getName();
        return new Descriptor(
                mProject.getName(), //project
                comment, //description
                comment, //comment
                createArgumentMap());
    }

    @Override
    protected Map<String, String> createArgumentMap() {
        Map<String, String> args = super.createArgumentMap();
        args.put(KEY_TYPE, mTypeFqcn);
        args.put(KEY_ID, mId);

        return args;
    }

    @Override
    public String getName() {
        return "Wrap in Container";
    }

    void setId(String id) {
        mId = id;
    }

    void setType(String typeFqcn) {
        mTypeFqcn = typeFqcn;
    }

    void setInitializedAttributes(String initializedAttributes) {
        mInitializedAttributes = initializedAttributes;
    }

    @Override
    protected @NonNull List<Change> computeChanges(IProgressMonitor monitor) {
        // (1) Insert the new container in front of the beginning of the
        //      first wrapped view
        // (2) If the container is the new root, transfer namespace declarations
        //      to it
        // (3) Insert the closing tag of the new container at the end of the
        //      last wrapped view
        // (4) Reindent the wrapped views
        // (5) If the user requested it, update all layout references to the
        //      wrapped views with the new container?
        //   For that matter, does RelativeLayout even require it? Probably not,
        //   it can point inside the current layout...

        // Add indent to all lines between mSelectionStart and mEnd
        // TODO: Figure out the indentation amount?
        // For now, use 4 spaces
        String indentUnit = "    "; //$NON-NLS-1$
        boolean separateAttributes = true;
        IStructuredDocument document = mDelegate.getEditor().getStructuredDocument();
        String startIndent = AndroidXmlEditor.getIndentAtOffset(document, mSelectionStart);

        String viewClass = getViewClass(mTypeFqcn);
        String androidNsPrefix = getAndroidNamespacePrefix();


        IFile file = mDelegate.getEditor().getInputFile();
        List<Change> changes = new ArrayList<Change>();
        if (file == null) {
            return changes;
        }
        TextFileChange change = new TextFileChange(file.getName(), file);
        MultiTextEdit rootEdit = new MultiTextEdit();
        change.setTextType(EXT_XML);

        String id = ensureNewId(mId);

        // Update any layout references to the old id with the new id
        if (id != null) {
            String rootId = getRootId();
            IStructuredModel model = mDelegate.getEditor().getModelForRead();
            try {
                IStructuredDocument doc = model.getStructuredDocument();
                if (doc != null) {
                    List<TextEdit> replaceIds = replaceIds(androidNsPrefix,
                            doc, mSelectionStart, mSelectionEnd, rootId, id);
                    for (TextEdit edit : replaceIds) {
                        rootEdit.addChild(edit);
                    }
                }
            } finally {
                model.releaseFromRead();
            }
        }

        // Insert namespace elements?
        StringBuilder namespace = null;
        List<DeleteEdit> deletions = new ArrayList<DeleteEdit>();
        Element primary = getPrimaryElement();
        if (primary != null && getDomDocument().getDocumentElement() == primary) {
            namespace = new StringBuilder();

            List<Attr> declarations = findNamespaceAttributes(primary);
            for (Attr attribute : declarations) {
                if (attribute instanceof IndexedRegion) {
                    // Delete the namespace declaration in the node which is no longer the root
                    IndexedRegion region = (IndexedRegion) attribute;
                    int startOffset = region.getStartOffset();
                    int endOffset = region.getEndOffset();
                    String text = getText(startOffset, endOffset);
                    DeleteEdit deletion = new DeleteEdit(startOffset, endOffset - startOffset);
                    deletions.add(deletion);
                    rootEdit.addChild(deletion);
                    text = text.trim();

                    // Insert the namespace declaration in the new root
                    if (separateAttributes) {
                        namespace.append('\n').append(startIndent).append(indentUnit);
                    } else {
                        namespace.append(' ');
                    }
                    namespace.append(text);
                }
            }
        }

        // Insert begin tag: <type ...>
        StringBuilder sb = new StringBuilder();
        sb.append('<');
        sb.append(viewClass);

        if (namespace != null) {
            sb.append(namespace);
        }

        // Set the ID if any
        if (id != null) {
            if (separateAttributes) {
                sb.append('\n').append(startIndent).append(indentUnit);
            } else {
                sb.append(' ');
            }
            sb.append(androidNsPrefix).append(':');
            sb.append(ATTR_ID).append('=').append('"').append(id).append('"');
        }

        // If any of the elements are fill/match parent, use that instead
        String width = VALUE_WRAP_CONTENT;
        String height = VALUE_WRAP_CONTENT;

        for (Element element : getElements()) {
            String oldWidth = element.getAttributeNS(ANDROID_URI, ATTR_LAYOUT_WIDTH);
            String oldHeight = element.getAttributeNS(ANDROID_URI, ATTR_LAYOUT_HEIGHT);

            if (VALUE_MATCH_PARENT.equals(oldWidth) || VALUE_FILL_PARENT.equals(oldWidth)) {
                width = oldWidth;
            }
            if (VALUE_MATCH_PARENT.equals(oldHeight) || VALUE_FILL_PARENT.equals(oldHeight)) {
                height = oldHeight;
            }
        }

        // Add in width/height.
        if (separateAttributes) {
            sb.append('\n').append(startIndent).append(indentUnit);
        } else {
            sb.append(' ');
        }
        sb.append(androidNsPrefix).append(':');
        sb.append(ATTR_LAYOUT_WIDTH).append('=').append('"').append(width).append('"');

        if (separateAttributes) {
            sb.append('\n').append(startIndent).append(indentUnit);
        } else {
            sb.append(' ');
        }
        sb.append(androidNsPrefix).append(':');
        sb.append(ATTR_LAYOUT_HEIGHT).append('=').append('"').append(height).append('"');

        if (mInitializedAttributes != null && mInitializedAttributes.length() > 0) {
            for (String s : mInitializedAttributes.split(",")) { //$NON-NLS-1$
                sb.append(' ');
                String[] nameValue = s.split("="); //$NON-NLS-1$
                String name = nameValue[0];
                String value = nameValue[1];
                if (name.startsWith(ANDROID_NS_NAME_PREFIX)) {
                    name = name.substring(ANDROID_NS_NAME_PREFIX.length());
                    sb.append(androidNsPrefix).append(':');
                }
                sb.append(name).append('=').append('"').append(value).append('"');
            }
        }

        // Transfer layout_ attributes (other than width and height)
        if (primary != null) {
            List<Attr> layoutAttributes = findLayoutAttributes(primary);
            for (Attr attribute : layoutAttributes) {
                String name = attribute.getLocalName();
                if ((name.equals(ATTR_LAYOUT_WIDTH) || name.equals(ATTR_LAYOUT_HEIGHT))
                        && ANDROID_URI.equals(attribute.getNamespaceURI())) {
                    // Already handled specially
                    continue;
                }

                if (attribute instanceof IndexedRegion) {
                    IndexedRegion region = (IndexedRegion) attribute;
                    int startOffset = region.getStartOffset();
                    int endOffset = region.getEndOffset();
                    String text = getText(startOffset, endOffset);
                    DeleteEdit deletion = new DeleteEdit(startOffset, endOffset - startOffset);
                    rootEdit.addChild(deletion);
                    deletions.add(deletion);

                    if (separateAttributes) {
                        sb.append('\n').append(startIndent).append(indentUnit);
                    } else {
                        sb.append(' ');
                    }
                    sb.append(text.trim());
                }
            }
        }

        // Finish open tag:
        sb.append('>');
        sb.append('\n').append(startIndent).append(indentUnit);

        InsertEdit beginEdit = new InsertEdit(mSelectionStart, sb.toString());
        rootEdit.addChild(beginEdit);

        String nested = getText(mSelectionStart, mSelectionEnd);
        int index = 0;
        while (index != -1) {
            index = nested.indexOf('\n', index);
            if (index != -1) {
                index++;
                InsertEdit newline = new InsertEdit(mSelectionStart + index, indentUnit);
                // Some of the deleted namespaces may have had newlines - be careful
                // not to overlap edits
                boolean covered = false;
                for (DeleteEdit deletion : deletions) {
                    if (deletion.covers(newline)) {
                        covered = true;
                        break;
                    }
                }
                if (!covered) {
                    rootEdit.addChild(newline);
                }
            }
        }

        // Insert end tag: </type>
        sb.setLength(0);
        sb.append('\n').append(startIndent);
        sb.append('<').append('/').append(viewClass).append('>');
        InsertEdit endEdit = new InsertEdit(mSelectionEnd, sb.toString());
        rootEdit.addChild(endEdit);

        if (AdtPrefs.getPrefs().getFormatGuiXml()) {
            MultiTextEdit formatted = reformat(rootEdit, XmlFormatStyle.LAYOUT);
            if (formatted != null) {
                rootEdit = formatted;
            }
        }

        change.setEdit(rootEdit);
        changes.add(change);
        return changes;
    }

    String getOldType() {
        Element primary = getPrimaryElement();
        if (primary != null) {
            String oldType = primary.getTagName();
            if (oldType.indexOf('.') == -1) {
                oldType = ANDROID_WIDGET_PREFIX + oldType;
            }
            return oldType;
        }

        return null;
    }

    @Override
    VisualRefactoringWizard createWizard() {
        return new WrapInWizard(this, mDelegate);
    }

    public static class Descriptor extends VisualRefactoringDescriptor {
        public Descriptor(String project, String description, String comment,
                Map<String, String> arguments) {
            super("com.android.ide.eclipse.adt.refactoring.wrapin", //$NON-NLS-1$
                    project, description, comment, arguments);
        }

        @Override
        protected Refactoring createRefactoring(Map<String, String> args) {
            return new WrapInRefactoring(args);
        }
    }
}