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
17package com.android.ide.eclipse.adt.internal.lint;
18
19import com.android.ide.eclipse.adt.AdtPlugin;
20import com.android.ide.eclipse.adt.AdtUtils;
21import com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditorDelegate;
22import com.android.ide.eclipse.adt.internal.editors.layout.refactoring.UseCompoundDrawableRefactoring;
23import com.android.tools.lint.checks.UseCompoundDrawableDetector;
24
25import org.eclipse.core.resources.IFile;
26import org.eclipse.core.resources.IMarker;
27import org.eclipse.jface.text.IDocument;
28import org.eclipse.jface.text.ITextSelection;
29import org.eclipse.jface.text.TextSelection;
30import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
31import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation;
32import org.eclipse.swt.graphics.Image;
33import org.eclipse.ui.IWorkbenchWindow;
34import org.eclipse.ui.PlatformUI;
35import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
36import org.w3c.dom.Node;
37
38/** Quickfix for the {@link UseCompoundDrawableDetector} */
39@SuppressWarnings("restriction") // DOM model
40class UseCompoundDrawableDetectorFix extends DocumentFix {
41    protected UseCompoundDrawableDetectorFix(String id, IMarker marker) {
42        super(id, marker);
43    }
44
45    @Override
46    public String getDisplayString() {
47        return "Convert to a compound drawable";
48    }
49
50    @Override
51    public Image getImage() {
52        return AdtPlugin.getAndroidLogo();
53    }
54
55    @Override
56    public boolean needsFocus() {
57        return false;
58    }
59
60    @Override
61    public boolean isCancelable() {
62        return false;
63    }
64
65    @Override
66    public boolean isBulkCapable() {
67        return false;
68    }
69
70    @Override
71    protected void apply(IDocument document, IStructuredModel model, Node node,
72            int start, int end) {
73
74        // Invoke refactoring
75        LayoutEditorDelegate delegate =
76                LayoutEditorDelegate.fromEditor(AdtUtils.getActiveEditor());
77
78        if (delegate != null) {
79            IFile file = (IFile) mMarker.getResource();
80            ITextSelection textSelection = new TextSelection(start,
81                    end - start);
82            UseCompoundDrawableRefactoring refactoring =
83                    new UseCompoundDrawableRefactoring(file, delegate, textSelection, null);
84            RefactoringWizard wizard = refactoring.createWizard();
85            RefactoringWizardOpenOperation op =
86                    new RefactoringWizardOpenOperation(wizard);
87            try {
88                IWorkbenchWindow window = PlatformUI.getWorkbench().
89                        getActiveWorkbenchWindow();
90                op.run(window.getShell(), wizard.getDefaultPageTitle());
91            } catch (InterruptedException e) {
92            }
93        }
94    }
95}
96