LinearLayoutWeightFix.java revision a881b0b34678ad76c9f5eba62fac7a00a22ac606
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.util.XmlUtils.ANDROID_URI;
19import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_HEIGHT;
20import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_WIDTH;
21import static com.android.ide.common.layout.LayoutConstants.ATTR_ORIENTATION;
22import static com.android.ide.common.layout.LayoutConstants.VALUE_VERTICAL;
23import static com.android.ide.common.layout.LayoutConstants.VALUE_ZERO_DP;
24
25import org.eclipse.core.resources.IMarker;
26import org.eclipse.jface.text.IDocument;
27import org.eclipse.swt.graphics.Image;
28import org.eclipse.ui.ISharedImages;
29import org.eclipse.ui.PlatformUI;
30import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
31import org.w3c.dom.Element;
32import org.w3c.dom.Node;
33
34@SuppressWarnings("restriction") // DOM model
35final class LinearLayoutWeightFix extends DocumentFix {
36    private LinearLayoutWeightFix(String id, IMarker marker) {
37        super(id, marker);
38    }
39
40    @Override
41    public boolean needsFocus() {
42        return false;
43    }
44
45    @Override
46    public boolean isCancelable() {
47        return false;
48    }
49
50    @Override
51    protected void apply(IDocument document, IStructuredModel model, Node node, int start,
52            int end) {
53        if (node instanceof Element && node.getParentNode() instanceof Element) {
54            Element element = (Element) node;
55            Element parent = (Element) node.getParentNode();
56            String dimension;
57            if (VALUE_VERTICAL.equals(parent.getAttributeNS(ANDROID_URI,
58                    ATTR_ORIENTATION))) {
59                dimension = ATTR_LAYOUT_HEIGHT;
60            } else {
61                dimension = ATTR_LAYOUT_WIDTH;
62            }
63            element.setAttributeNS(ANDROID_URI, dimension, VALUE_ZERO_DP);
64        }
65    }
66
67    @Override
68    public String getDisplayString() {
69        return "Replace size attribute with 0dp";
70    }
71
72    @Override
73    public Image getImage() {
74        ISharedImages sharedImages = PlatformUI.getWorkbench().getSharedImages();
75        // TODO: Need a better icon here
76        return sharedImages.getImage(ISharedImages.IMG_OBJ_ELEMENT);
77    }
78}
79