1/*
2 * Copyright (C) 2012 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 */
16package com.android.ide.eclipse.adt.internal.lint;
17
18import static com.android.SdkConstants.ANDROID_URI;
19import static com.android.SdkConstants.ATTR_LAYOUT_HEIGHT;
20import static com.android.SdkConstants.ATTR_LAYOUT_WIDTH;
21import static com.android.SdkConstants.HORIZONTAL_SCROLL_VIEW;
22import static com.android.SdkConstants.VALUE_WRAP_CONTENT;
23
24import org.eclipse.core.resources.IMarker;
25import org.eclipse.jface.text.IDocument;
26import org.eclipse.swt.graphics.Image;
27import org.eclipse.ui.ISharedImages;
28import org.eclipse.ui.PlatformUI;
29import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
30import org.w3c.dom.Element;
31import org.w3c.dom.Node;
32
33@SuppressWarnings("restriction") // DOM model
34final class SetScrollViewSizeFix extends DocumentFix {
35    private SetScrollViewSizeFix(String id, IMarker marker) {
36        super(id, marker);
37    }
38
39    @Override
40    public boolean needsFocus() {
41        return false;
42    }
43
44    @Override
45    public boolean isCancelable() {
46        return false;
47    }
48
49    @Override
50    protected void apply(IDocument document, IStructuredModel model, Node node, int start,
51            int end) {
52        if (node instanceof Element && node.getParentNode() instanceof Element) {
53            Element element = (Element) node;
54            Element parent = (Element) node.getParentNode();
55
56            boolean isHorizontal = HORIZONTAL_SCROLL_VIEW.equals(parent.getTagName());
57            String attributeName = isHorizontal ? ATTR_LAYOUT_WIDTH : ATTR_LAYOUT_HEIGHT;
58            element.setAttributeNS(ANDROID_URI, attributeName, VALUE_WRAP_CONTENT);
59        }
60    }
61
62    @Override
63    public String getDisplayString() {
64        return "Replace size attribute with wrap_content";
65    }
66
67    @Override
68    public Image getImage() {
69        ISharedImages sharedImages = PlatformUI.getWorkbench().getSharedImages();
70        // TODO: Need a better icon here
71        return sharedImages.getImage(ISharedImages.IMG_OBJ_ELEMENT);
72    }
73}
74