aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/configuration/ThemeMenuAction.java
blob: b1ce21d3688a5609e975929d4fff4c7b3ef3b790 (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
/*
 * 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.configuration;

import static com.android.SdkConstants.ANDROID_STYLE_RESOURCE_PREFIX;

import com.android.ide.eclipse.adt.internal.editors.Hyperlinks;
import com.android.ide.eclipse.adt.internal.editors.layout.gle2.SubmenuAction;
import com.android.ide.eclipse.adt.internal.editors.manifest.ManifestInfo;
import com.android.ide.eclipse.adt.internal.editors.manifest.ManifestInfo.ActivityAttributes;
import com.android.ide.eclipse.adt.internal.resources.ResourceHelper;
import com.android.sdklib.IAndroidTarget;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.text.hyperlink.IHyperlink;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.ToolItem;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Action which creates a submenu displaying available themes
 */
class ThemeMenuAction extends SubmenuAction {
    private static final String DEVICE_LIGHT_PREFIX =
            ANDROID_STYLE_RESOURCE_PREFIX + "Theme.DeviceDefault.Light";  //$NON-NLS-1$
    private static final String HOLO_LIGHT_PREFIX =
            ANDROID_STYLE_RESOURCE_PREFIX + "Theme.Holo.Light";           //$NON-NLS-1$
    private static final String DEVICE_PREFIX =
            ANDROID_STYLE_RESOURCE_PREFIX + "Theme.DeviceDefault";        //$NON-NLS-1$
    private static final String HOLO_PREFIX =
            ANDROID_STYLE_RESOURCE_PREFIX + "Theme.Holo";                 //$NON-NLS-1$
    private static final String LIGHT_PREFIX =
            ANDROID_STYLE_RESOURCE_PREFIX +"Theme.Light";                 //$NON-NLS-1$
    private static final String THEME_PREFIX =
            ANDROID_STYLE_RESOURCE_PREFIX +"Theme";                       //$NON-NLS-1$

    // Constants used to indicate what type of menu is being shown, such that
    // the submenus can lazily construct their contents
    private static final int MENU_MANIFEST = 1;
    private static final int MENU_PROJECT = 2;
    private static final int MENU_THEME = 3;
    private static final int MENU_THEME_LIGHT = 4;
    private static final int MENU_HOLO = 5;
    private static final int MENU_HOLO_LIGHT = 6;
    private static final int MENU_DEVICE = 7;
    private static final int MENU_DEVICE_LIGHT = 8;
    private static final int MENU_ALL = 9;

    private final ConfigurationChooser mConfigChooser;
    private final List<String> mThemeList;
    /** Type of menu; one of the constants {@link #MENU_ALL} etc */
    private final int mType;

    ThemeMenuAction(int type, String title, ConfigurationChooser configuration,
            List<String> themeList) {
        super(title);
        mType = type;
        mConfigChooser = configuration;
        mThemeList = themeList;
    }

    static void showThemeMenu(ConfigurationChooser configChooser, ToolItem combo,
            List<String> themeList) {
        MenuManager manager = new MenuManager();

        // First show the currently selected theme (grayed out since you can't
        // reselect it)
        Configuration configuration = configChooser.getConfiguration();
        String currentTheme = configuration.getTheme();
        String currentName = null;
        if (currentTheme != null) {
            currentName = ResourceHelper.styleToTheme(currentTheme);
            SelectThemeAction action = new SelectThemeAction(configChooser,
                    currentName,
                    currentTheme,
                    true /* selected */);
            action.setEnabled(false);
            manager.add(action);
            manager.add(new Separator());
        }

        String preferred = configuration.computePreferredTheme();
        if (preferred != null && !preferred.equals(currentTheme)) {
            manager.add(new SelectThemeAction(configChooser,
                    ResourceHelper.styleToTheme(preferred),
                    preferred, false /* selected */));
            manager.add(new Separator());
        }

        IAndroidTarget target = configuration.getTarget();
        int apiLevel = target != null ? target.getVersion().getApiLevel() : 1;
        boolean hasHolo = apiLevel >= 11;   // Honeycomb
        boolean hasDeviceDefault = apiLevel >= 14; // ICS

        // TODO: Add variations of the current theme here, e.g.
        // if you're using Theme.Holo, add Theme.Holo.Dialog, Theme.Holo.Panel,
        // Theme.Holo.Wallpaper etc

        manager.add(new ThemeMenuAction(MENU_PROJECT, "Project Themes",
                configChooser, themeList));
        manager.add(new ThemeMenuAction(MENU_MANIFEST, "Manifest Themes",
                configChooser, themeList));

        manager.add(new Separator());

        if (hasHolo) {
            manager.add(new ThemeMenuAction(MENU_HOLO, "Holo",
                    configChooser, themeList));
            manager.add(new ThemeMenuAction(MENU_HOLO_LIGHT, "Holo.Light",
                    configChooser, themeList));
        }
        if (hasDeviceDefault) {
            manager.add(new ThemeMenuAction(MENU_DEVICE, "DeviceDefault",
                    configChooser, themeList));
            manager.add(new ThemeMenuAction(MENU_DEVICE_LIGHT, "DeviceDefault.Light",
                    configChooser, themeList));
        }
        manager.add(new ThemeMenuAction(MENU_THEME, "Theme",
                configChooser, themeList));
        manager.add(new ThemeMenuAction(MENU_THEME_LIGHT, "Theme.Light",
                configChooser, themeList));

        // TODO: Add generic types like Wallpaper, Dialog, Alert, etc here, with
        // submenus for picking it within each theme category?

        manager.add(new Separator());
        manager.add(new ThemeMenuAction(MENU_ALL, "All",
                configChooser, themeList));

        if (currentTheme != null) {
            assert currentName != null;
            manager.add(new Separator());
            String title = String.format("Open %1$s Declaration...", currentName);
            manager.add(new OpenThemeAction(title, configChooser.getEditedFile(), currentTheme));
        }

        Menu menu = manager.createContextMenu(configChooser.getShell());

        Rectangle bounds = combo.getBounds();
        Point location = new Point(bounds.x, bounds.y + bounds.height);
        location = combo.getParent().toDisplay(location);
        menu.setLocation(location.x, location.y);
        menu.setVisible(true);
    }

    @Override
    protected void addMenuItems(Menu menu) {
        switch (mType) {
            case MENU_ALL:
                addMenuItems(menu, mThemeList);
                break;

            case MENU_MANIFEST: {
                IProject project = mConfigChooser.getEditedFile().getProject();
                ManifestInfo manifest = ManifestInfo.get(project);
                Configuration configuration = mConfigChooser.getConfiguration();
                String activity = configuration.getActivity();
                if (activity != null) {
                    ActivityAttributes attributes = manifest.getActivityAttributes(activity);
                    if (attributes != null) {
                        String theme = attributes.getTheme();
                        if (theme != null) {
                            addMenuItem(menu, theme, isSelectedTheme(theme));
                        }
                    }
                }

                String manifestTheme = manifest.getManifestTheme();
                boolean found = false;
                Set<String> allThemes = new HashSet<String>();
                if (manifestTheme != null) {
                    found = true;
                    allThemes.add(manifestTheme);
                }
                for (ActivityAttributes info : manifest.getActivityAttributesMap().values()) {
                    if (info.getTheme() != null) {
                        found = true;
                        allThemes.add(info.getTheme());
                    }
                }
                List<String> sorted = new ArrayList<String>(allThemes);
                Collections.sort(sorted);
                String current = configuration.getTheme();
                for (String theme : sorted) {
                    boolean selected = theme.equals(current);
                    addMenuItem(menu, theme, selected);
                }
                if (!found) {
                    addDisabledMessageItem("No themes are registered in the manifest");
                }
                break;
            }
            case MENU_PROJECT: {
                int size = mThemeList.size();
                List<String> themes = new ArrayList<String>(size);
                for (int i = 0; i < size; i++) {
                    String theme = mThemeList.get(i);
                    if (ResourceHelper.isProjectStyle(theme)) {
                        themes.add(theme);
                    }
                }
                if (themes.isEmpty()) {
                    addDisabledMessageItem("There are no local theme styles in the project");
                } else {
                    addMenuItems(menu, themes);
                }
                break;
            }
            case MENU_THEME: {
                // Can't just use the usual filterThemes() call here because we need
                // to exclude on multiple prefixes: Holo, DeviceDefault, Light, ...
                List<String> themes = new ArrayList<String>(mThemeList.size());
                for (String theme : mThemeList) {
                    if (theme.startsWith(THEME_PREFIX)
                            && !theme.startsWith(LIGHT_PREFIX)
                            && !theme.startsWith(HOLO_PREFIX)
                            && !theme.startsWith(DEVICE_PREFIX)) {
                        themes.add(theme);
                    }
                }

                addMenuItems(menu, themes);
                break;
            }
            case MENU_THEME_LIGHT:
                addMenuItems(menu, filterThemes(LIGHT_PREFIX, null));
                break;
            case MENU_HOLO:
                addMenuItems(menu, filterThemes(HOLO_PREFIX, HOLO_LIGHT_PREFIX));
                break;
            case MENU_HOLO_LIGHT:
                addMenuItems(menu, filterThemes(HOLO_LIGHT_PREFIX, null));
                break;
            case MENU_DEVICE:
                addMenuItems(menu, filterThemes(DEVICE_PREFIX, DEVICE_LIGHT_PREFIX));
                break;
            case MENU_DEVICE_LIGHT:
                addMenuItems(menu, filterThemes(DEVICE_LIGHT_PREFIX, null));
                break;
        }
    }

    private List<String> filterThemes(String include, String exclude) {
        List<String> themes = new ArrayList<String>(mThemeList.size());
        for (String theme : mThemeList) {
            if (theme.startsWith(include) && (exclude == null || !theme.startsWith(exclude))) {
                themes.add(theme);
            }
        }

        return themes;
    }

    private void addMenuItems(Menu menu, List<String> themes) {
        String current = mConfigChooser.getConfiguration().getTheme();
        for (String theme : themes) {
            addMenuItem(menu, theme, theme.equals(current));
        }
    }

    private boolean isSelectedTheme(String theme) {
        return theme.equals(mConfigChooser.getConfiguration().getTheme());
    }

    private void addMenuItem(Menu menu, String theme, boolean selected) {
        String title = ResourceHelper.styleToTheme(theme);
        SelectThemeAction action = new SelectThemeAction(mConfigChooser, title, theme, selected);
        new ActionContributionItem(action).fill(menu, -1);
    }

    private static class OpenThemeAction extends Action {
        private final String mTheme;
        private final IFile mFile;

        private OpenThemeAction(String title, IFile file, String theme) {
            super(title, IAction.AS_PUSH_BUTTON);
            mFile = file;
            mTheme = theme;
        }

        @Override
        public void run() {
            IProject project = mFile.getProject();
            IHyperlink[] links = Hyperlinks.getResourceLinks(null, mTheme, project, null);
            if (links != null && links.length > 0) {
                IHyperlink link = links[0];
                link.open();
            }
        }
    }
}