aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/manifest/pages/ApplicationToggle.java
blob: 159f08959263e201d349fa965a06522d45ea26f1 (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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 * Copyright (C) 2007 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.manifest.pages;

import com.android.ide.eclipse.adt.AdtPlugin;
import com.android.ide.eclipse.adt.internal.editors.descriptors.DescriptorsUtils;
import com.android.ide.eclipse.adt.internal.editors.manifest.ManifestEditor;
import com.android.ide.eclipse.adt.internal.editors.ui.UiElementPart;
import com.android.ide.eclipse.adt.internal.editors.uimodel.IUiUpdateListener;
import com.android.ide.eclipse.adt.internal.editors.uimodel.IUiUpdateListener.UiUpdateState;
import com.android.ide.eclipse.adt.internal.editors.uimodel.UiElementNode;
import com.android.ide.eclipse.adt.internal.sdk.Sdk;
import com.android.utils.SdkUtils;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.widgets.FormText;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.widgets.TableWrapData;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.Text;

/**
 * Appllication Toogle section part for application page.
 */
final class ApplicationToggle extends UiElementPart {

    /** Checkbox indicating whether an application node is present */
    private Button mCheckbox;
    /** Listen to changes to the UI node for <application> and updates the checkbox */
    private AppNodeUpdateListener mAppNodeUpdateListener;
    /** Internal flag to know where we're programmatically modifying the checkbox and we want to
     *  avoid triggering the checkbox's callback. */
    public boolean mInternalModification;
    private FormText mTooltipFormText;

    public ApplicationToggle(Composite body, FormToolkit toolkit, ManifestEditor editor,
            UiElementNode applicationUiNode) {
        super(body, toolkit, editor, applicationUiNode,
                "Application Toggle",
                null, /* description */
                Section.TWISTIE | Section.EXPANDED);
    }

    @Override
    public void dispose() {
        super.dispose();
        if (getUiElementNode() != null && mAppNodeUpdateListener != null) {
            getUiElementNode().removeUpdateListener(mAppNodeUpdateListener);
            mAppNodeUpdateListener = null;
        }
    }

    /**
     * Changes and refreshes the Application UI node handle by the this part.
     */
    @Override
    public void setUiElementNode(UiElementNode uiElementNode) {
        super.setUiElementNode(uiElementNode);

        updateTooltip();

        // Set the state of the checkbox
        mAppNodeUpdateListener.uiElementNodeUpdated(getUiElementNode(),
                UiUpdateState.CHILDREN_CHANGED);
    }

    /**
     * Create the controls to edit the attributes for the given ElementDescriptor.
     * <p/>
     * This MUST not be called by the constructor. Instead it must be called from
     * <code>initialize</code> (i.e. right after the form part is added to the managed form.)
     *
     * @param managedForm The owner managed form
     */
    @Override
    protected void createFormControls(IManagedForm managedForm) {
        FormToolkit toolkit = managedForm.getToolkit();
        Composite table = createTableLayout(toolkit, 1 /* numColumns */);

        mTooltipFormText = createFormText(table, toolkit, true, "<form></form>",
                false /* setupLayoutData */);
        updateTooltip();

        mCheckbox = toolkit.createButton(table,
                "Define an <application> tag in the AndroidManifest.xml",
                SWT.CHECK);
        mCheckbox.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP));
        mCheckbox.setSelection(false);
        mCheckbox.addSelectionListener(new CheckboxSelectionListener());

        mAppNodeUpdateListener = new AppNodeUpdateListener();
        getUiElementNode().addUpdateListener(mAppNodeUpdateListener);

        // Initialize the state of the checkbox
        mAppNodeUpdateListener.uiElementNodeUpdated(getUiElementNode(),
                UiUpdateState.CHILDREN_CHANGED);

        // Tell the section that the layout has changed.
        layoutChanged();
    }

    /**
     * Updates the application tooltip in the form text.
     * If there is no tooltip, the form text is hidden.
     */
    private void updateTooltip() {
        boolean isVisible = false;

        String tooltip = getUiElementNode().getDescriptor().getTooltip();
        if (tooltip != null) {
            tooltip = DescriptorsUtils.formatFormText(tooltip,
                    getUiElementNode().getDescriptor(),
                    Sdk.getCurrent().getDocumentationBaseUrl());

            mTooltipFormText.setText(tooltip, true /* parseTags */, true /* expandURLs */);
            mTooltipFormText.setImage(DescriptorsUtils.IMAGE_KEY, AdtPlugin.getAndroidLogo());
            mTooltipFormText.addHyperlinkListener(getEditor().createHyperlinkListener());
            isVisible = true;
        }

        mTooltipFormText.setVisible(isVisible);
    }

    /**
     * This listener synchronizes the XML application node when the checkbox
     * is changed by the user.
     */
    private class CheckboxSelectionListener extends SelectionAdapter {
        private Node mUndoXmlNode;
        private Node mUndoXmlParent;
        private Node mUndoXmlNextNode;
        private Node mUndoXmlNextElement;
        private Document mUndoXmlDocument;

        @Override
        public void widgetSelected(SelectionEvent e) {
            super.widgetSelected(e);
            if (!mInternalModification && getUiElementNode() != null) {
                getUiElementNode().getEditor().wrapUndoEditXmlModel(
                        mCheckbox.getSelection()
                            ? "Create or restore Application node"
                            : "Remove Application node",
                        new Runnable() {
                            @Override
                            public void run() {
                                if (mCheckbox.getSelection()) {
                                    // The user wants an <application> node.
                                    // Either restore a previous one
                                    // or create a full new one.
                                    boolean create = true;
                                    if (mUndoXmlNode != null) {
                                        create = !restoreApplicationNode();
                                    }
                                    if (create) {
                                        getUiElementNode().createXmlNode();
                                    }
                                } else {
                                    // Users no longer wants the <application> node.
                                    removeApplicationNode();
                                }
                            }
                });
            }
        }

        /**
         * Restore a previously "saved" application node.
         *
         * @return True if the node could be restored, false otherwise.
         */
        private boolean restoreApplicationNode() {
            if (mUndoXmlDocument == null || mUndoXmlNode == null) {
                return false;
            }

            // Validate node references...
            mUndoXmlParent = validateNode(mUndoXmlDocument, mUndoXmlParent);
            mUndoXmlNextNode = validateNode(mUndoXmlDocument, mUndoXmlNextNode);
            mUndoXmlNextElement = validateNode(mUndoXmlDocument, mUndoXmlNextElement);

            if (mUndoXmlParent == null){
                // If the parent node doesn't exist, try to find a new manifest node.
                // If it doesn't exist, create it.
                mUndoXmlParent = getUiElementNode().getUiParent().prepareCommit();
                mUndoXmlNextNode = null;
                mUndoXmlNextElement = null;
            }

            boolean success = false;
            if (mUndoXmlParent != null) {
                // If the parent is still around, reuse the same node.

                // Ideally we want to insert the node before what used to be its next sibling.
                // If that's not possible, we try to insert it before its next sibling element.
                // If that's not possible either, it will be inserted at the end of the parent's.
                Node next = mUndoXmlNextNode;
                if (next == null) {
                    next = mUndoXmlNextElement;
                }
                mUndoXmlParent.insertBefore(mUndoXmlNode, next);
                if (next == null) {
                    Text sep = mUndoXmlDocument.createTextNode(SdkUtils.getLineSeparator());
                    mUndoXmlParent.insertBefore(sep, null);  // insert separator before end tag
                }
                success = true;
            }

            // Remove internal references to avoid using them twice
            mUndoXmlParent = null;
            mUndoXmlNextNode = null;
            mUndoXmlNextElement = null;
            mUndoXmlNode = null;
            mUndoXmlDocument = null;
            return success;
        }

        /**
         * Validates that the given xml_node is still either the root node or one of its
         * direct descendants.
         *
         * @param root_node The root of the node hierarchy to examine.
         * @param xml_node The XML node to find.
         * @return Returns xml_node if it is, otherwise returns null.
         */
        private Node validateNode(Node root_node, Node xml_node) {
            if (root_node == xml_node) {
                return xml_node;
            } else {
                for (Node node = root_node.getFirstChild(); node != null;
                        node = node.getNextSibling()) {
                    if (root_node == xml_node || validateNode(node, xml_node) != null) {
                        return xml_node;
                    }
                }
            }
            return null;
        }

        /**
         * Removes the <application> node from the hierarchy.
         * Before doing that, we try to remember where it was so that we can put it back
         * in the same place.
         */
        private void removeApplicationNode() {
            // Make sure the node actually exists...
            Node xml_node = getUiElementNode().getXmlNode();
            if (xml_node == null) {
                return;
            }

            // Save its parent, next sibling and next element
            mUndoXmlDocument = xml_node.getOwnerDocument();
            mUndoXmlParent = xml_node.getParentNode();
            mUndoXmlNextNode = xml_node.getNextSibling();
            mUndoXmlNextElement = mUndoXmlNextNode;
            while (mUndoXmlNextElement != null &&
                    mUndoXmlNextElement.getNodeType() != Node.ELEMENT_NODE) {
                mUndoXmlNextElement = mUndoXmlNextElement.getNextSibling();
            }

            // Actually remove the node from the hierarchy and keep it here.
            // The returned node looses its parents/siblings pointers.
            mUndoXmlNode = getUiElementNode().deleteXmlNode();
        }
    }

    /**
     * This listener synchronizes the UI (i.e. the checkbox) with the
     * actual presence of the application XML node.
     */
    private class AppNodeUpdateListener implements IUiUpdateListener {
        @Override
        public void uiElementNodeUpdated(UiElementNode ui_node, UiUpdateState state) {
            // The UiElementNode for the application XML node always exists, even
            // if there is no corresponding XML node in the XML file.
            //
            // To update the checkbox to reflect the actual state, we just need
            // to check if the XML node is null.
            try {
                mInternalModification = true;
                boolean exists = ui_node.getXmlNode() != null;
                if (mCheckbox.getSelection() != exists) {
                    mCheckbox.setSelection(exists);
                }
            } finally {
                mInternalModification = false;
            }

        }
    }
}