aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/VisualRefactoringAction.java
blob: f1cc988d70295c1edd4c7cfd815c6b54f79d6e3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.eclipse.org/org/documents/epl-v10.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.ide.eclipse.adt.internal.editors.layout.refactoring;

import com.android.ide.eclipse.adt.AdtConstants;
import com.android.ide.eclipse.adt.AdtPlugin;
import com.android.ide.eclipse.adt.AdtUtils;
import com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditorDelegate;
import com.android.ide.eclipse.adt.internal.editors.layout.gle2.CanvasViewInfo;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.part.FileEditorInput;

abstract class VisualRefactoringAction implements IWorkbenchWindowActionDelegate {
    protected IWorkbenchWindow mWindow;
    protected ITextSelection mTextSelection;
    protected ITreeSelection mTreeSelection;
    protected LayoutEditorDelegate mDelegate;
    protected IFile mFile;

    /**
     * Keep track of the current workbench window.
     */
    @Override
    public void init(IWorkbenchWindow window) {
        mWindow = window;
    }

    @Override
    public void dispose() {
    }

    /**
     * Examine the selection to determine if the action should be enabled or not.
     * <p/>
     * Keep a link to the relevant selection structure
     */
    @Override
    public void selectionChanged(IAction action, ISelection selection) {
        // Look for selections in XML and in the layout UI editor

        // Note, two kinds of selections are returned here:
        // ITextSelection on a Java source window
        // IStructuredSelection in the outline or navigator
        // This simply deals with the refactoring based on a non-empty selection.
        // At that point, just enable the action and later decide if it's valid when it actually
        // runs since we don't have access to the AST yet.

        mTextSelection = null;
        mTreeSelection = null;
        mFile = null;

        IEditorPart editor = null;

        if (selection instanceof ITextSelection) {
            mTextSelection = (ITextSelection) selection;
            editor = AdtUtils.getActiveEditor();
            mFile = getSelectedFile(editor);
        } else if (selection instanceof ITreeSelection) {
             Object firstElement = ((ITreeSelection)selection).getFirstElement();
             if (firstElement instanceof CanvasViewInfo) {
                 mTreeSelection = (ITreeSelection) selection;
                 editor = AdtUtils.getActiveEditor();
                 mFile = getSelectedFile(editor);
             }
        }

        mDelegate = LayoutEditorDelegate.fromEditor(editor);

        action.setEnabled((mTextSelection != null || mTreeSelection != null)
                && mFile != null && mDelegate != null);
    }

    /**
     * Create a new instance of our refactoring and a wizard to configure it.
     */
    @Override
    public abstract void run(IAction action);

    /**
     * Returns the active {@link IFile} (hopefully matching our selection) or null.
     * The file is only returned if it's a file from a project with an Android nature.
     * <p/>
     * At that point we do not try to analyze if the selection nor the file is suitable
     * for the refactoring. This check is performed when the refactoring is invoked since
     * it can then produce meaningful error messages as needed.
     */
    private IFile getSelectedFile(IEditorPart editor) {
        if (editor != null) {
            IEditorInput input = editor.getEditorInput();

            if (input instanceof FileEditorInput) {
                FileEditorInput fi = (FileEditorInput) input;
                IFile file = fi.getFile();
                if (file.exists()) {
                    IProject proj = file.getProject();
                    try {
                        if (proj != null && proj.hasNature(AdtConstants.NATURE_DEFAULT)) {
                            return file;
                        }
                    } catch (CoreException e) {
                        // ignore
                    }
                }
            }
        }

        return null;
    }

    public static IAction create(String title, LayoutEditorDelegate editorDelegate,
            Class<? extends VisualRefactoringAction> clz) {
        return new ActionWrapper(title, editorDelegate, clz);
    }

    private static class ActionWrapper extends Action {
        private Class<? extends VisualRefactoringAction> mClass;
        private LayoutEditorDelegate mEditorDelegate;

        ActionWrapper(String title, LayoutEditorDelegate editorDelegate,
                Class<? extends VisualRefactoringAction> clz) {
            super(title);
            mEditorDelegate = editorDelegate;
            mClass = clz;
        }

        @Override
        public void run() {
            VisualRefactoringAction action;
            try {
                action = mClass.newInstance();
            } catch (Exception e) {
                AdtPlugin.log(e, null);
                return;
            }
            IEditorSite site = mEditorDelegate.getEditor().getEditorSite();
            action.init(site.getWorkbenchWindow());
            ISelection selection = site.getSelectionProvider().getSelection();
            action.selectionChanged(ActionWrapper.this, selection);
            if (isEnabled()) {
                action.run(ActionWrapper.this);
            }
        }
    }
}