aboutsummaryrefslogtreecommitdiffstats
path: root/swtmenubar/src/com/android/menubar/MenuBarEnhancer37.java
blob: 8560bfab10c7a69a602fe9f83324767ebf97c3be (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
/*
 * 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.
 *
 * References:
 * Based on the SWT snippet example at
 * http://dev.eclipse.org/viewcvs/viewvc.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet354.java?view=co
 */

package com.android.menubar;


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

import java.lang.reflect.Method;

public class MenuBarEnhancer37 implements IMenuBarEnhancer {

    private static final int kAboutMenuItem = -1;           // SWT.ID_ABOUT       in SWT 3.7
    private static final int kPreferencesMenuItem = -2;     // SWT.ID_PREFERENCES in SWT 3.7
    private static final int kQuitMenuItem = -6;            // SWT.ID_QUIT        in SWT 3.7

    public MenuBarEnhancer37() {
    }

    public MenuBarMode getMenuBarMode() {
        return MenuBarMode.MAC_OS;
    }

    /**
     * Setup the About and Preferences native menut items with the
     * given application name and links them to the callback.
     *
     * @param appName The application name.
     * @param display The SWT display. Must not be null.
     * @param callbacks The callbacks invoked by the menus.
     */
    public void setupMenu(
            String appName,
            Display display,
            IMenuBarCallback callbacks) {

        try {
            // Initialize the menuItems.
            initialize(display, appName, callbacks);
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }

        // Schedule disposal of callback object
        display.disposeExec(new Runnable() {
            public void run() {
            }
        });
    }

    /**
     * Checks whether the required SWT 3.7 APIs are available.
     * <br/>
     * Calling this will load the class, which is OK since this class doesn't
     * directly use any SWT 3.7 API -- instead it uses reflection so that the
     * code can be loaded under SWT 3.6.
     *
     * @param display The current SWT display.
     * @return True if the SWT 3.7 API are available and this enhancer can be used.
     */
    public static boolean isSupported(Display display) {
        try {
            Object sysMenu = call0(display, "getSystemMenu");
            if (sysMenu instanceof Menu) {
                return findMenuById((Menu)sysMenu, kPreferencesMenuItem) != null &&
                       findMenuById((Menu)sysMenu, kAboutMenuItem) != null;
            }
        } catch (Exception ignore) {}
        return false;
    }

    private void initialize(
            Display display,
            String appName,
            final IMenuBarCallback callbacks)
                    throws Exception {
        Object sysMenu = call0(display, "getSystemMenu");
        if (sysMenu instanceof Menu) {
            MenuItem menu = findMenuById((Menu)sysMenu, kPreferencesMenuItem);
            if (menu != null) {
                menu.addSelectionListener(new SelectionAdapter() {
                    @Override
                    public void widgetSelected(SelectionEvent event) {
                        callbacks.onPreferencesMenuSelected();
                    }
                });
            }

            menu = findMenuById((Menu)sysMenu, kAboutMenuItem);
            if (menu != null) {
                menu.addSelectionListener(new SelectionAdapter() {
                    @Override
                    public void widgetSelected(SelectionEvent event) {
                        callbacks.onAboutMenuSelected();
                    }
                });
                menu.setText("About " + appName);
            }

            menu = findMenuById((Menu)sysMenu, kQuitMenuItem);
            if (menu != null) {
                // We already support the "quit" operation, no need for an extra handler here.
                menu.setText("Quit " + appName);
            }

        }
    }

    private static Object call0(Object obj, String method) {
        try {
            Method m = obj.getClass().getMethod(method, (Class<?>[])null);
            if (m != null) {
                return m.invoke(obj, (Object[])null);
            }
        } catch (Exception ignore) {}
        return null;
    }

    private static MenuItem findMenuById(Menu menu, int id) {
        MenuItem[] items = menu.getItems();
        for (int i = items.length - 1; i >= 0; i--) {
            MenuItem item = items[i];
            Object menuId = call0(item, "getID");
            if (menuId instanceof Integer) {
                if (((Integer) menuId).intValue() == id) {
                    return item;
                }
            }
        }
        return null;
    }
}