aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/ui/SectionHelper.java
blob: fdb5d829232e9092e28dfa21a7d82993327d45d9 (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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
 * 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.ui;

import com.android.ide.eclipse.adt.AdtPlugin;
import com.android.ide.eclipse.adt.internal.editors.AndroidXmlEditor;

import org.eclipse.jface.text.DefaultInformationControl;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.SectionPart;
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.eclipse.ui.forms.widgets.TableWrapLayout;

import java.lang.reflect.Method;

/**
 * Helper for the AndroidManifest form editor.
 *
 * Helps create a new SectionPart with sensible default parameters,
 * create default layout or add typical widgets.
 *
 * IMPORTANT: This is NOT a generic class. It makes a lot of assumptions on the
 * UI as used by the form editor for the AndroidManifest.
 *
 * TODO: Consider moving to a common package.
 */
public final class SectionHelper {

    /**
     * Utility class that derives from SectionPart, constructs the Section with
     * sensible defaults (with a title and a description) and provide some shorthand
     * methods for creating typically UI (label and text, form text.)
     */
    static public class ManifestSectionPart extends SectionPart {

        /**
         * Construct a SectionPart that uses a title bar and a description.
         * It's up to the caller to call setText() and setDescription().
         * <p/>
         * The section style includes a description and a title bar by default.
         *
         * @param body The parent (e.g. FormPage body)
         * @param toolkit Form Toolkit
         */
        public ManifestSectionPart(Composite body, FormToolkit toolkit) {
            this(body, toolkit, 0, false);
        }

        /**
         * Construct a SectionPart that uses a title bar and a description.
         * It's up to the caller to call setText() and setDescription().
         * <p/>
         * The section style includes a description and a title bar by default.
         * You can add extra styles, like Section.TWISTIE.
         *
         * @param body The parent (e.g. FormPage body).
         * @param toolkit Form Toolkit.
         * @param extra_style Extra styles (on top of description and title bar).
         * @param use_description True if the Section.DESCRIPTION style should be added.
         */
        public ManifestSectionPart(Composite body, FormToolkit toolkit,
                int extra_style, boolean use_description) {
            super(body, toolkit, extra_style |
                    Section.TITLE_BAR |
                    (use_description ? Section.DESCRIPTION : 0));
        }

        // Create non-static methods of helpers just for convenience

        /**
         * Creates a new composite with a TableWrapLayout set with a given number of columns.
         *
         * If the parent composite is a Section, the new composite is set as a client.
         *
         * @param toolkit Form Toolkit
         * @param numColumns Desired number of columns.
         * @return The new composite.
         */
        public Composite createTableLayout(FormToolkit toolkit, int numColumns) {
            return SectionHelper.createTableLayout(getSection(), toolkit, numColumns);
        }

        /**
         * Creates a label widget.
         * If the parent layout if a TableWrapLayout, maximize it to span over all columns.
         *
         * @param parent The parent (e.g. composite from CreateTableLayout())
         * @param toolkit Form Toolkit
         * @param label The string for the label.
         * @param tooltip An optional tooltip for the label and text. Can be null.
         * @return The new created label
         */
        public Label createLabel(Composite parent, FormToolkit toolkit, String label,
                String tooltip) {
            return SectionHelper.createLabel(parent, toolkit, label, tooltip);
        }

        /**
         * Creates two widgets: a label and a text field.
         *
         * This expects the parent composite to have a TableWrapLayout with 2 columns.
         *
         * @param parent The parent (e.g. composite from CreateTableLayout())
         * @param toolkit Form Toolkit
         * @param label The string for the label.
         * @param value The initial value of the text field. Can be null.
         * @param tooltip An optional tooltip for the label and text. Can be null.
         * @return The new created Text field (the label is not returned)
         */
        public Text createLabelAndText(Composite parent, FormToolkit toolkit, String label,
                String value, String tooltip) {
            return SectionHelper.createLabelAndText(parent, toolkit, label, value, tooltip);
        }

        /**
         * Creates a FormText widget.
         *
         * This expects the parent composite to have a TableWrapLayout with 2 columns.
         *
         * @param parent The parent (e.g. composite from CreateTableLayout())
         * @param toolkit Form Toolkit
         * @param isHtml True if the form text will contain HTML that must be interpreted as
         *               rich text (i.e. parse tags & expand URLs).
         * @param label The string for the label.
         * @param setupLayoutData indicates whether the created form text receives a TableWrapData
         * through the setLayoutData method. In some case, creating it will make the table parent
         * huge, which we don't want.
         * @return The new created FormText.
         */
        public FormText createFormText(Composite parent, FormToolkit toolkit, boolean isHtml,
                String label, boolean setupLayoutData) {
            return SectionHelper.createFormText(parent, toolkit, isHtml, label, setupLayoutData);
        }

        /**
         * Forces the section to recompute its layout and redraw.
         * This is needed after the content of the section has been changed at runtime.
         */
        public void layoutChanged() {
            Section section = getSection();

            // Calls getSection().reflow(), which is the same that Section calls
            // when the expandable state is changed and the height changes.
            // Since this is protected, some reflection is needed to invoke it.
            try {
                Method reflow;
                reflow = section.getClass().getDeclaredMethod("reflow", (Class<?>[])null);
                reflow.setAccessible(true);
                reflow.invoke(section);
            } catch (Exception e) {
                AdtPlugin.log(e, "Error when invoking Section.reflow");
            }

            section.layout(true /* changed */, true /* all */);
        }
    }

    /**
     * Creates a new composite with a TableWrapLayout set with a given number of columns.
     *
     * If the parent composite is a Section, the new composite is set as a client.
     *
     * @param composite The parent (e.g. a Section or SectionPart)
     * @param toolkit Form Toolkit
     * @param numColumns Desired number of columns.
     * @return The new composite.
     */
    static public Composite createTableLayout(Composite composite, FormToolkit toolkit,
            int numColumns) {
        Composite table = toolkit.createComposite(composite);
        TableWrapLayout layout = new TableWrapLayout();
        layout.numColumns = numColumns;
        table.setLayout(layout);
        toolkit.paintBordersFor(table);
        if (composite instanceof Section) {
            ((Section) composite).setClient(table);
        }
        return table;
    }

    /**
     * Creates a new composite with a GridLayout set with a given number of columns.
     *
     * If the parent composite is a Section, the new composite is set as a client.
     *
     * @param composite The parent (e.g. a Section or SectionPart)
     * @param toolkit Form Toolkit
     * @param numColumns Desired number of columns.
     * @return The new composite.
     */
    static public Composite createGridLayout(Composite composite, FormToolkit toolkit,
            int numColumns) {
        Composite grid = toolkit.createComposite(composite);
        GridLayout layout = new GridLayout();
        layout.numColumns = numColumns;
        grid.setLayout(layout);
        toolkit.paintBordersFor(grid);
        if (composite instanceof Section) {
            ((Section) composite).setClient(grid);
        }
        return grid;
    }

    /**
     * Creates two widgets: a label and a text field.
     *
     * This expects the parent composite to have a TableWrapLayout with 2 columns.
     *
     * @param parent The parent (e.g. composite from CreateTableLayout())
     * @param toolkit Form Toolkit
     * @param label_text The string for the label.
     * @param value The initial value of the text field. Can be null.
     * @param tooltip An optional tooltip for the label and text. Can be null.
     * @return The new created Text field (the label is not returned)
     */
    static public Text createLabelAndText(Composite parent, FormToolkit toolkit, String label_text,
            String value, String tooltip) {
        Label label = toolkit.createLabel(parent, label_text);
        label.setLayoutData(new TableWrapData(TableWrapData.LEFT, TableWrapData.MIDDLE));
        Text text = toolkit.createText(parent, value);
        text.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.MIDDLE));

        addControlTooltip(label, tooltip);
        return text;
    }

    /**
     * Creates a label widget.
     * If the parent layout if a TableWrapLayout, maximize it to span over all columns.
     *
     * @param parent The parent (e.g. composite from CreateTableLayout())
     * @param toolkit Form Toolkit
     * @param label_text The string for the label.
     * @param tooltip An optional tooltip for the label and text. Can be null.
     * @return The new created label
     */
    static public Label createLabel(Composite parent, FormToolkit toolkit, String label_text,
            String tooltip) {
        Label label = toolkit.createLabel(parent, label_text);

        TableWrapData twd = new TableWrapData(TableWrapData.FILL_GRAB);
        if (parent.getLayout() instanceof TableWrapLayout) {
            twd.colspan = ((TableWrapLayout) parent.getLayout()).numColumns;
        }
        label.setLayoutData(twd);

        addControlTooltip(label, tooltip);
        return label;
    }

    /**
     * Associates a tooltip with a control.
     *
     * This mirrors the behavior from org.eclipse.pde.internal.ui.editor.text.PDETextHover
     *
     * @param control The control to which associate the tooltip.
     * @param tooltip The tooltip string. Can use \n for multi-lines. Will not display if null.
     */
    static public void addControlTooltip(final Control control, String tooltip) {
        if (control == null || tooltip == null || tooltip.length() == 0) {
            return;
        }

        // Some kinds of controls already properly implement tooltip display.
        if (control instanceof Button) {
            control.setToolTipText(tooltip);
            return;
        }

        control.setToolTipText(null);

        final DefaultInformationControl ic = new DefaultInformationControl(control.getShell());
        ic.setInformation(tooltip);
        Point sz = ic.computeSizeHint();
        ic.setSize(sz.x, sz.y);
        ic.setVisible(false); // initially hidden

        control.addMouseTrackListener(new MouseTrackListener() {
            @Override
            public void mouseEnter(MouseEvent e) {
            }

            @Override
            public void mouseExit(MouseEvent e) {
                ic.setVisible(false);
            }

            @Override
            public void mouseHover(MouseEvent e) {
                ic.setLocation(control.toDisplay(10, 25));  // same offset as in PDETextHover
                ic.setVisible(true);
            }
        });
        control.addDisposeListener(new DisposeListener() {
            @Override
            public void widgetDisposed(DisposeEvent e) {
                ic.dispose();
            }
        });
    }

    /**
     * Creates a FormText widget.
     *
     * This expects the parent composite to have a TableWrapLayout with 2 columns.
     *
     * @param parent The parent (e.g. composite from CreateTableLayout())
     * @param toolkit Form Toolkit
     * @param isHtml True if the form text will contain HTML that must be interpreted as
     *               rich text (i.e. parse tags & expand URLs).
     * @param label The string for the label.
     * @param setupLayoutData indicates whether the created form text receives a TableWrapData
     * through the setLayoutData method. In some case, creating it will make the table parent
     * huge, which we don't want.
     * @return The new created FormText.
     */
    static public FormText createFormText(Composite parent, FormToolkit toolkit,
            boolean isHtml, String label, boolean setupLayoutData) {
        FormText text = toolkit.createFormText(parent, true /* track focus */);
        if (setupLayoutData) {
            TableWrapData twd = new TableWrapData(TableWrapData.FILL_GRAB);
            twd.maxWidth = AndroidXmlEditor.TEXT_WIDTH_HINT;
            if (parent.getLayout() instanceof TableWrapLayout) {
                twd.colspan = ((TableWrapLayout) parent.getLayout()).numColumns;
            }
            text.setLayoutData(twd);
        }
        text.setWhitespaceNormalized(true);
        if (isHtml && !label.startsWith("<form>")) {          //$NON-NLS-1$
            // This assertion is violated, for example by the Class attribute for an activity
            //assert label.startsWith("<form>") : "HTML for FormText must be wrapped in <form>...</form>"; //$NON-NLS-1$
            label = "<form>" + label + "</form>";   //$NON-NLS-1$ //$NON-NLS-2$
        }
        text.setText(label, isHtml /* parseTags */, isHtml /* expandURLs */);
        return text;
    }
}