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 com.android.ide.eclipse.adt.AdtPlugin;
     19 import com.android.tools.lint.checks.TypographyDetector;
     20 
     21 import org.eclipse.core.resources.IMarker;
     22 import org.eclipse.jface.text.BadLocationException;
     23 import org.eclipse.jface.text.IDocument;
     24 import org.eclipse.swt.graphics.Image;
     25 import org.eclipse.ui.ISharedImages;
     26 import org.eclipse.ui.PlatformUI;
     27 import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
     28 import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
     29 import org.w3c.dom.Element;
     30 import org.w3c.dom.Node;
     31 import org.w3c.dom.NodeList;
     32 
     33 import java.util.List;
     34 
     35 @SuppressWarnings("restriction") // DOM model
     36 final class TypographyFix extends DocumentFix {
     37     private TypographyFix(String id, IMarker marker) {
     38         super(id, marker);
     39     }
     40 
     41     @Override
     42     public boolean needsFocus() {
     43         return false;
     44     }
     45 
     46     @Override
     47     public boolean isCancelable() {
     48         return false;
     49     }
     50 
     51     @Override
     52     public boolean isBulkCapable() {
     53         return false;
     54     }
     55 
     56     @Override
     57     protected void apply(IDocument document, IStructuredModel model, Node node, int start,
     58             int end) {
     59         if (node instanceof Element) {
     60             Element element = (Element) node;
     61             // Find the text node which contains the character in question
     62             NodeList childNodes = element.getChildNodes();
     63             for (int i = 0, n = childNodes.getLength(); i < n; i++) {
     64                 Node child = childNodes.item(i);
     65                 if (child.getNodeType() == Node.TEXT_NODE) {
     66                     IndexedRegion region = (IndexedRegion) child;
     67                     String message = mMarker.getAttribute(IMarker.MESSAGE, "");
     68                     List<TypographyDetector.ReplaceEdit> edits =
     69                             TypographyDetector.getEdits(mId, message, child);
     70                     for (TypographyDetector.ReplaceEdit edit : edits) {
     71                         try {
     72                             document.replace(edit.offset + region.getStartOffset(),
     73                                     edit.length, edit.replaceWith);
     74                         } catch (BadLocationException e) {
     75                             AdtPlugin.log(e, null);
     76                         }
     77                     }
     78                 }
     79             }
     80         }
     81     }
     82 
     83     @Override
     84     public String getDisplayString() {
     85         return "Replace with suggested characters";
     86     }
     87 
     88     @Override
     89     public Image getImage() {
     90         ISharedImages sharedImages = PlatformUI.getWorkbench().getSharedImages();
     91         // TODO: Need a better icon here
     92         return sharedImages.getImage(ISharedImages.IMG_OBJ_ELEMENT);
     93     }
     94 }