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.AdtUtils;
     19 import com.android.ide.eclipse.adt.internal.editors.AndroidXmlEditor;
     20 import com.android.ide.eclipse.adt.internal.refactorings.extractstring.ExtractStringRefactoring;
     21 import com.android.ide.eclipse.adt.internal.refactorings.extractstring.ExtractStringWizard;
     22 
     23 import org.eclipse.core.resources.IFile;
     24 import org.eclipse.core.resources.IMarker;
     25 import org.eclipse.jface.text.IDocument;
     26 import org.eclipse.jface.text.ITextSelection;
     27 import org.eclipse.jface.text.TextSelection;
     28 import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
     29 import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation;
     30 import org.eclipse.swt.graphics.Image;
     31 import org.eclipse.ui.IEditorPart;
     32 import org.eclipse.ui.ISharedImages;
     33 import org.eclipse.ui.IWorkbenchWindow;
     34 import org.eclipse.ui.PlatformUI;
     35 import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
     36 import org.w3c.dom.Node;
     37 
     38 /**
     39  * Fix for extracting strings.
     40  * <p>
     41  * TODO: Look for existing string values, and if it matches one of the
     42  * existing Strings offer to just replace it with the given string!
     43  */
     44 @SuppressWarnings("restriction") // DOM model
     45 final class ExtractStringFix extends DocumentFix {
     46     private ExtractStringFix(String id, IMarker marker) {
     47         super(id, marker);
     48     }
     49 
     50     @Override
     51     public boolean needsFocus() {
     52         return true;
     53     }
     54 
     55     @Override
     56     public boolean isCancelable() {
     57         return true;
     58     }
     59 
     60     @Override
     61     protected void apply(IDocument document, IStructuredModel model, Node node, int start,
     62             int end) {
     63         IEditorPart editorPart = AdtUtils.getActiveEditor();
     64         if (editorPart instanceof AndroidXmlEditor) {
     65             IFile file = (IFile) mMarker.getResource();
     66             ITextSelection selection = new TextSelection(start, end - start);
     67 
     68             ExtractStringRefactoring refactoring =
     69                 new ExtractStringRefactoring(file, editorPart, selection);
     70             RefactoringWizard wizard = new ExtractStringWizard(refactoring, file.getProject());
     71             RefactoringWizardOpenOperation op = new RefactoringWizardOpenOperation(wizard);
     72             try {
     73                 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
     74                 op.run(window.getShell(), wizard.getDefaultPageTitle());
     75             } catch (InterruptedException e) {
     76             }
     77         }
     78     }
     79 
     80     @Override
     81     public String getDisplayString() {
     82         return "Extract String";
     83     }
     84 
     85     @Override
     86     public Image getImage() {
     87         ISharedImages sharedImages = PlatformUI.getWorkbench().getSharedImages();
     88         return sharedImages.getImage(ISharedImages.IMG_OBJ_ADD);
     89     }
     90 }