Home | History | Annotate | Download | only in lint
      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  */
     16 package com.android.ide.eclipse.adt.internal.lint;
     17 
     18 import static com.android.SdkConstants.ANDROID_URI;
     19 import static com.android.SdkConstants.ATTR_LAYOUT_HEIGHT;
     20 import static com.android.SdkConstants.ATTR_LAYOUT_WIDTH;
     21 import static com.android.SdkConstants.ATTR_ORIENTATION;
     22 import static com.android.SdkConstants.VALUE_VERTICAL;
     23 import static com.android.SdkConstants.VALUE_ZERO_DP;
     24 
     25 import org.eclipse.core.resources.IMarker;
     26 import org.eclipse.jface.text.IDocument;
     27 import org.eclipse.swt.graphics.Image;
     28 import org.eclipse.ui.ISharedImages;
     29 import org.eclipse.ui.PlatformUI;
     30 import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
     31 import org.w3c.dom.Element;
     32 import org.w3c.dom.Node;
     33 
     34 @SuppressWarnings("restriction") // DOM model
     35 final 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