aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/properties/PropertySheetPage.java
blob: 58fddc0ee7ea8834c5fb90f7cda0130bff5096fe (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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
 * 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.layout.properties;

import com.android.ide.eclipse.adt.AdtPlugin;
import com.android.ide.eclipse.adt.internal.editors.IconFactory;
import com.android.ide.eclipse.adt.internal.editors.layout.gle2.CanvasViewInfo;
import com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart;
import com.android.ide.eclipse.adt.internal.editors.layout.properties.PropertyFactory.SortingMode;
import com.android.ide.eclipse.adt.internal.editors.layout.uimodel.UiViewElementNode;
import com.android.ide.eclipse.adt.internal.editors.uimodel.IUiUpdateListener;
import com.android.ide.eclipse.adt.internal.editors.uimodel.UiElementNode;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.Page;
import org.eclipse.ui.views.properties.IPropertySheetPage;
import org.eclipse.wb.internal.core.editor.structure.IPage;
import org.eclipse.wb.internal.core.model.property.Property;
import org.eclipse.wb.internal.core.model.property.table.IPropertyExceptionHandler;
import org.eclipse.wb.internal.core.model.property.table.PropertyTable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * Property sheet page used when the graphical layout editor is chosen
 */
public class PropertySheetPage extends Page
        implements IPropertySheetPage, IUiUpdateListener, IPage {
    private PropertyTable mPropertyTable;
    private final GraphicalEditorPart mEditor;
    private Property mActiveProperty;
    private Action mDefaultValueAction;
    private Action mShowAdvancedPropertiesAction;
    private Action mSortAlphaAction;
    private Action mCollapseAll;
    private Action mExpandAll;
    private List<CanvasViewInfo> mSelection;

    private static final String EXPAND_DISABLED_ICON = "expandall-disabled";          //$NON-NLS-1$
    private static final String EXPAND_ICON = "expandall";                            //$NON-NLS-1$
    private static final String DEFAULT_ICON = "properties_default";                  //$NON-NLS-1$
    private static final String ADVANCED_ICON = "filter_advanced_properties";         //$NON-NLS-1$
    private static final String ALPHA_ICON = "sort_alpha";                            //$NON-NLS-1$
    // TODO: goto-definition.png

    /**
     * Constructs a new {@link PropertySheetPage} associated with the given
     * editor
     *
     * @param editor the editor associated with this property sheet page
     */
    public PropertySheetPage(GraphicalEditorPart editor) {
        mEditor = editor;
    }

    private PropertyFactory getPropertyFactory() {
        return mEditor.getPropertyFactory();
    }

    @Override
    public void createControl(Composite parent) {
        assert parent != null;
        mPropertyTable = new PropertyTable(parent, SWT.NONE);
        mPropertyTable.setExceptionHandler(new IPropertyExceptionHandler() {
            @Override
            public void handle(Throwable e) {
                AdtPlugin.log(e, null);
            }
        });
        mPropertyTable.setDefaultCollapsedNames(Arrays.asList(
                "Deprecated",
                "Layout Parameters",
                "Layout Parameters|Margins"));

        createActions();
        setPropertyTableContextMenu();
    }

    @Override
    public void selectionChanged(IWorkbenchPart part, ISelection selection) {
        if (selection instanceof TreeSelection
                && mPropertyTable != null && !mPropertyTable.isDisposed()) {
            TreeSelection treeSelection = (TreeSelection) selection;

            // We get a lot of repeated selection requests for the same selection
            // as before, so try to eliminate these
            if (mSelection != null) {
                if (mSelection.isEmpty()) {
                    if (treeSelection.isEmpty()) {
                        return;
                    }
                } else {
                    int selectionCount = treeSelection.size();
                    if (selectionCount == mSelection.size()) {
                        boolean same = true;
                        Iterator<?> iterator = treeSelection.iterator();
                        for (int i = 0, n = selectionCount; i < n && iterator.hasNext(); i++) {
                            Object next = iterator.next();
                            if (next instanceof CanvasViewInfo) {
                                CanvasViewInfo info = (CanvasViewInfo) next;
                                if (info != mSelection.get(i)) {
                                    same = false;
                                    break;
                                }
                            } else {
                                same = false;
                                break;
                            }
                        }
                        if (same) {
                            return;
                        }
                    }
                }
            }

            stopTrackingSelection();

            if (treeSelection.isEmpty()) {
                mSelection = Collections.emptyList();
            } else {
                int selectionCount = treeSelection.size();
                List<CanvasViewInfo> newSelection = new ArrayList<CanvasViewInfo>(selectionCount);
                Iterator<?> iterator = treeSelection.iterator();
                while (iterator.hasNext()) {
                    Object next = iterator.next();
                    if (next instanceof CanvasViewInfo) {
                        CanvasViewInfo info = (CanvasViewInfo) next;
                        newSelection.add(info);
                    }
                }
                mSelection = newSelection;
            }

            startTrackingSelection();

            refreshProperties();
        }
    }

    @Override
    public void dispose() {
        stopTrackingSelection();
        super.dispose();
    }

    private void startTrackingSelection() {
        if (mSelection != null && !mSelection.isEmpty()) {
            for (CanvasViewInfo item : mSelection) {
                UiViewElementNode node = item.getUiViewNode();
                if (node != null) {
                    node.addUpdateListener(this);
                }
            }
        }
    }

    private void stopTrackingSelection() {
        if (mSelection != null && !mSelection.isEmpty()) {
            for (CanvasViewInfo item : mSelection) {
                UiViewElementNode node = item.getUiViewNode();
                if (node != null) {
                    node.removeUpdateListener(this);
                }
            }
        }
        mSelection = null;
    }

    // Implements IUiUpdateListener
    @Override
    public void uiElementNodeUpdated(UiElementNode node, UiUpdateState state) {
        refreshProperties();
    }

    @Override
    public Control getControl() {
        return mPropertyTable;
    }

    @Override
    public void setFocus() {
        mPropertyTable.setFocus();
    }

    @Override
    public void makeContributions(IMenuManager menuManager,
            IToolBarManager toolBarManager, IStatusLineManager statusLineManager) {
        toolBarManager.add(mShowAdvancedPropertiesAction);
        toolBarManager.add(new Separator());
        toolBarManager.add(mSortAlphaAction);
        toolBarManager.add(new Separator());
        toolBarManager.add(mDefaultValueAction);
        toolBarManager.add(new Separator());
        toolBarManager.add(mExpandAll);
        toolBarManager.add(mCollapseAll);
        toolBarManager.add(new Separator());
    }

    private void createActions() {
        ISharedImages sharedImages = PlatformUI.getWorkbench().getSharedImages();
        IconFactory iconFactory = IconFactory.getInstance();

        mExpandAll = new PropertySheetAction(
                IAction.AS_PUSH_BUTTON,
                "Expand All",
                ACTION_EXPAND,
                iconFactory.getImageDescriptor(EXPAND_ICON),
                iconFactory.getImageDescriptor(EXPAND_DISABLED_ICON));

        mCollapseAll = new PropertySheetAction(
                IAction.AS_PUSH_BUTTON,
                "Collapse All",
                ACTION_COLLAPSE,
                sharedImages.getImageDescriptor(ISharedImages.IMG_ELCL_COLLAPSEALL),
                sharedImages.getImageDescriptor(ISharedImages.IMG_ELCL_COLLAPSEALL_DISABLED));

        mShowAdvancedPropertiesAction = new PropertySheetAction(
                IAction.AS_CHECK_BOX,
                "Show Advanced Properties",
                ACTION_SHOW_ADVANCED,
                iconFactory.getImageDescriptor(ADVANCED_ICON),
                null);

        mSortAlphaAction = new PropertySheetAction(
                IAction.AS_CHECK_BOX,
                "Sort Alphabetically",
                ACTION_SORT_ALPHA,
                iconFactory.getImageDescriptor(ALPHA_ICON),
                null);

        mDefaultValueAction = new PropertySheetAction(
                IAction.AS_PUSH_BUTTON,
                "Restore Default Value",
                ACTION_DEFAULT_VALUE,
                iconFactory.getImageDescriptor(DEFAULT_ICON),
                null);

        // Listen on the selection in the property sheet so we can update the
        // Restore Default Value action
        ISelectionChangedListener listener = new ISelectionChangedListener() {
            @Override
            public void selectionChanged(SelectionChangedEvent event) {
                StructuredSelection selection = (StructuredSelection) event.getSelection();
                mActiveProperty = (Property) selection.getFirstElement();
                updateDefaultValueAction();
            }
        };
        mPropertyTable.addSelectionChangedListener(listener);
    }

    /**
     * Updates the state of {@link #mDefaultValueAction}.
     */
    private void updateDefaultValueAction() {
        if (mActiveProperty != null) {
            try {
                mDefaultValueAction.setEnabled(mActiveProperty.isModified());
            } catch (Exception e) {
                AdtPlugin.log(e, null);
            }
        } else {
            mDefaultValueAction.setEnabled(false);
        }
    }

    /**
     * Sets the context menu for {@link #mPropertyTable}.
     */
    private void setPropertyTableContextMenu() {
        final MenuManager manager = new MenuManager();
        manager.setRemoveAllWhenShown(true);
        manager.addMenuListener(new IMenuListener() {
            @Override
            public void menuAboutToShow(IMenuManager m) {
                // dispose items to avoid caching
                for (MenuItem item : manager.getMenu().getItems()) {
                    item.dispose();
                }
                // apply new items
                fillContextMenu();
            }

            private void fillContextMenu() {
                manager.add(mDefaultValueAction);
                manager.add(mSortAlphaAction);
                manager.add(mShowAdvancedPropertiesAction);
            }
        });

        mPropertyTable.setMenu(manager.createContextMenu(mPropertyTable));
    }

    /**
     * Shows {@link Property}'s of current objects.
     */
    private void refreshProperties() {
        PropertyFactory factory = getPropertyFactory();
        mPropertyTable.setInput(factory.getProperties(mSelection));
        updateDefaultValueAction();
    }

    // ---- Actions ----

    private static final int ACTION_DEFAULT_VALUE = 1;
    private static final int ACTION_SHOW_ADVANCED = 2;
    private static final int ACTION_COLLAPSE = 3;
    private static final int ACTION_EXPAND = 4;
    private static final int ACTION_SORT_ALPHA = 5;

    private class PropertySheetAction extends Action {
        private final int mAction;

        private PropertySheetAction(int style, String label, int action,
                ImageDescriptor imageDesc, ImageDescriptor disabledImageDesc) {
            super(label, style);
            mAction = action;
            setImageDescriptor(imageDesc);
            if (disabledImageDesc != null) {
                setDisabledImageDescriptor(disabledImageDesc);
            }
            setToolTipText(label);
        }

        @Override
        public void run() {
            switch (mAction) {
                case ACTION_COLLAPSE: {
                    mPropertyTable.collapseAll();
                    break;
                }
                case ACTION_EXPAND: {
                    mPropertyTable.expandAll();
                    break;
                }
                case ACTION_SHOW_ADVANCED: {
                    boolean show = mShowAdvancedPropertiesAction.isChecked();
                    mPropertyTable.setShowAdvancedProperties(show);
                    break;
                }
                case ACTION_SORT_ALPHA: {
                    boolean isAlphabetical = mSortAlphaAction.isChecked();
                    getPropertyFactory().setSortingMode(
                        isAlphabetical ? SortingMode.ALPHABETICAL : PropertyFactory.DEFAULT_MODE);
                    refreshProperties();
                    break;
                }
                case ACTION_DEFAULT_VALUE:
                    try {
                        mActiveProperty.setValue(Property.UNKNOWN_VALUE);
                    } catch (Exception e) {
                        // Ignore warnings from setters
                    }
                    break;
                default:
                    assert false : mAction;
            }
        }
    }

    @Override
    public void setToolBar(IToolBarManager toolBarManager) {
        makeContributions(null, toolBarManager, null);
        toolBarManager.update(false);
    }
}