aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/common/CommonXmlDelegate.java
blob: d9ee8a5d8b99c41afcb7cc2cd422906a2c2e18b1 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 * Copyright (C) 2012 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.common;

import com.android.annotations.NonNull;
import com.android.annotations.Nullable;
import com.android.ide.eclipse.adt.internal.editors.AndroidXmlEditor;
import com.android.ide.eclipse.adt.internal.editors.uimodel.UiElementNode;
import com.android.resources.ResourceFolderType;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IURIEditorInput;
import org.eclipse.ui.forms.editor.IFormPage;
import org.eclipse.ui.part.EditorActionBarContributor;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.part.MultiPageEditorPart;
import org.w3c.dom.Document;

/**
 * Implementation of form editor for /res XML files.
 * <p/>
 * All delegates must have one {@link IDelegateCreator} instance
 * registered in the {@code DELEGATES[]} array of {@link CommonXmlEditor}.
 */
public abstract class CommonXmlDelegate {

    /** The editor that created the delegate. Never null. */
    private final CommonXmlEditor mEditor;

    /** Root node of the UI element hierarchy. Can be null. */
    private UiElementNode mUiRootNode;

    private IContentAssistProcessor mContentAssist;

    /**
     * Static creator for {@link CommonXmlDelegate}s. Delegates implement a method
     * that will decide whether this delegate can be created for the given file input.
     */
    public interface IDelegateCreator {
        /**
         * Determines whether this delegate can handle the given file, typically
         * based on its resource path (e.g. ResourceManager#getResourceFolder).
         *
         * @param delegator The non-null instance of {@link CommonXmlEditor}.
         * @param type The {@link ResourceFolderType} of the folder containing the file,
         *   if it can be determined. Null otherwise.
         * @return A new delegate that can handle that file or null.
         */
        public @Nullable <T extends CommonXmlDelegate> T createForFile(
                            @NonNull CommonXmlEditor delegator,
                            @Nullable ResourceFolderType type);
    }

    /** Implemented by delegates that need to support {@link EditorActionBarContributor} */
    public interface IActionContributorDelegate {
        /** Called from {@link EditorActionBarContributor#setActiveEditor(IEditorPart)}. */
        public void setActiveEditor(IEditorPart part, IActionBars bars);
    }

    protected CommonXmlDelegate(
            CommonXmlEditor editor,
            IContentAssistProcessor contentAssist) {
        mEditor = editor;
        mContentAssist = contentAssist;
    }

    public void dispose() {
    }

    /**
     * Returns the editor that created this delegate.
     *
     * @return the editor that created this delegate. Never null.
     */
    public @NonNull CommonXmlEditor getEditor() {
        return mEditor;
    }

    /**
     * @return The root node of the UI element hierarchy
     */
    public UiElementNode getUiRootNode() {
        return mUiRootNode;
    }

    protected void setUiRootNode(UiElementNode uiRootNode) {
        mUiRootNode = uiRootNode;
    }

    /** Called to compute the initial {@code UiRootNode}. */
    public abstract void delegateInitUiRootNode(boolean force);

    /**
     * Returns true, indicating the "save as" operation is supported by this editor.
     */
    public boolean isSaveAsAllowed() {
        return true;
    }

    /**
     * Create the various form pages.
     */
    public abstract void delegateCreateFormPages();

    public void delegatePostCreatePages() {
        // pass
    }

    /**
     * Changes the tab/title name to include the project name.
     */
    public void delegateSetInput(IEditorInput input) {
        if (input instanceof FileEditorInput) {
            FileEditorInput fileInput = (FileEditorInput) input;
            IFile file = fileInput.getFile();
            getEditor().setPartName(file.getName());
        } else if (input instanceof IURIEditorInput) {
            IURIEditorInput uriInput = (IURIEditorInput) input;
            String name = uriInput.getName();
            getEditor().setPartName(name);
        }
    }

    /**
     * Processes the new XML Model, which XML root node is given.
     *
     * @param xml_doc The XML document, if available, or null if none exists.
     */
    public abstract void delegateXmlModelChanged(Document xml_doc);

    public void delegatePageChange(int newPageIndex) {
        // pass
    }

    public void delegatePostPageChange(int newPageIndex) {
        // pass
    }
    /**
     * Save the XML.
     * <p/>
     * The actual save operation is done in the super class by committing
     * all data to the XML model and then having the Structured XML Editor
     * save the XML.
     * <p/>
     * Here we just need to tell the graphical editor that the model has
     * been saved.
     */
    public void delegateDoSave(IProgressMonitor monitor) {
        // pass
    }

    /**
     * Tells the editor to start a Lint check.
     * It's up to the caller to check whether this should be done depending on preferences.
     */
    public Job delegateRunLint() {
        return getEditor().startLintJob();
    }


    /**
     * Returns the custom IContentOutlinePage or IPropertySheetPage when asked for it.
     */
    public Object delegateGetAdapter(Class<?> adapter) {
        return null;
    }

    /**
     * Returns the {@link IContentAssistProcessor} associated with this editor.
     * Most implementations should lazily allocate one processor and always return the
     * same instance.
     * Must return null if there's no specific content assist processor for this editor.
     */
    public IContentAssistProcessor getAndroidContentAssistProcessor() {
        return mContentAssist;
    }

    /**
     * Does this editor participate in the "format GUI editor changes" option?
     *
     * @return false since editors do not support automatically formatting XML
     *         affected by GUI changes unless they explicitly opt in to it.
     */
    public boolean delegateSupportsFormatOnGuiEdit() {
        return false;
    }

    /**
     * Called after the editor's active page has been set.
     *
     * @param superReturned the return value from
     *            {@link MultiPageEditorPart#setActivePage(int)}
     * @param pageIndex the index of the page to be activated; the index must be
     *            valid
     * @return the page, or null
     * @see MultiPageEditorPart#setActivePage(int)
     */
    public IFormPage delegatePostSetActivePage(IFormPage superReturned, String pageIndex) {
        return superReturned;
    }

    /** Called after an editor has been activated */
    public void delegateActivated() {
    }

    /** Called after an editor has been deactivated */
    public void delegateDeactivated() {
    }

    /**
     * Returns the name of the editor to be shown in the editor tab etc. Return
     * null to keep the default.
     *
     * @return the part name, or null to use the default
     */
    public String delegateGetPartName() {
        return null;
    }

    /**
     * Returns the persistence category, as described in
     * {@link AndroidXmlEditor#getPersistenceCategory}.
     *
     * @return the persistence category to use for this editor
     */
    public int delegateGetPersistenceCategory() {
        return AndroidXmlEditor.CATEGORY_OTHER;
    }
}