aboutsummaryrefslogtreecommitdiffstats
path: root/swtmenubar/src/com/android/menubar/MenuBarEnhancer.java
blob: dba4f32e8f571d7f9e1082ee630859d38b5b5b19 (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
/*
 * Copyright (C) 2011 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.menubar;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;


/**
 * On Mac, {@link MenuBarEnhancer#setupMenu} plugs a listener on the About and the
 * Preferences menu items of the standard "application" menu in the menu bar.
 * On Windows or Linux, it adds relevant items to a given {@link Menu} linked to
 * the same listeners.
 */
public final class MenuBarEnhancer {

    private MenuBarEnhancer() {
    }

    /**
     * Creates an instance of {@link IMenuBarEnhancer} specific to the current platform
     * and invoke its {@link IMenuBarEnhancer#setupMenu} to updates the menu bar.
     * <p/>
     * Depending on the platform, this will either hook into the existing About menu item
     * and a Preferences or Options menu item or add new ones to the given {@code swtMenu}.
     * Depending on the platform, the menu items might be decorated with the
     * given {@code appName}.
     * <p/>
     * Potential errors are reported through {@link IMenuBarCallback}.
     *
     * @param appName Name used for the About menu item and similar. Must not be null.
     * @param swtMenu For non-mac platform this is the menu where the "About" and
     *          the "Options" menu items are created. Typically the menu might be
     *          called "Tools". Must not be null.
     * @param callbacks Callbacks called when "About" and "Preferences" menu items are invoked.
     *          Must not be null.
     * @return A actual {@link IMenuBarEnhancer} implementation. Never null.
     *          This is currently not of any use for the caller but is left in case
     *          we want to expand the functionality later.
     */
    public static IMenuBarEnhancer setupMenu(
            String appName,
            Menu swtMenu,
            IMenuBarCallback callbacks) {

        IMenuBarEnhancer enhancer = null;
        String p = SWT.getPlatform();
        String className = null;
        if ("carbon".equals(p)) {                                                 //$NON-NLS-1$
            className = "com.android.menubar.internal.MenuBarEnhancerCarbon";     //$NON-NLS-1$
        } else if ("cocoa".equals(p)) {                                           //$NON-NLS-1$
            className = "com.android.menubar.internal.MenuBarEnhancerCocoa";      //$NON-NLS-1$
        }

        if (System.getenv("DEBUG_SWTMENUBAR") != null) {
            callbacks.printError("DEBUG SwtMenuBar: SWT=%1$s, class=%2$s", p, className);
        }

        if (className != null) {
            try {
                Class<?> clazz = Class.forName(className);
                enhancer = (IMenuBarEnhancer) clazz.newInstance();
            } catch (Exception e) {
                // Log an error and fallback on the default implementation.
                callbacks.printError(
                        "Failed to instantiate %1$s: %2$s",                       //$NON-NLS-1$
                        className,
                        e.toString());
            }
        }

        // Default implementation for other platforms
        if (enhancer == null) {
            enhancer = new IMenuBarEnhancer() {
                public void setupMenu(
                        String appName,
                        Menu menu,
                        final IMenuBarCallback callbacks) {
                    new MenuItem(menu, SWT.SEPARATOR);

                    // Note: we use "Preferences" on Mac and "Options" on Windows/Linux.
                    final MenuItem pref = new MenuItem(menu, SWT.NONE);
                    pref.setText("Options...");

                    final MenuItem about = new MenuItem(menu, SWT.NONE);
                    about.setText("About...");

                    pref.addSelectionListener(new SelectionAdapter() {
                        @Override
                        public void widgetSelected(SelectionEvent e) {
                            try {
                                pref.setEnabled(false);
                                callbacks.onPreferencesMenuSelected();
                                super.widgetSelected(e);
                            } finally {
                                pref.setEnabled(true);
                            }
                        }
                    });

                    about.addSelectionListener(new SelectionAdapter() {
                        @Override
                        public void widgetSelected(SelectionEvent e) {
                            try {
                                about.setEnabled(false);
                                callbacks.onAboutMenuSelected();
                                super.widgetSelected(e);
                            } finally {
                                about.setEnabled(true);
                            }
                        }
                    });
                }
            };
        }

        enhancer.setupMenu(appName, swtMenu, callbacks);
        return enhancer;
    }

}