Search in sources :

Example 1 with Menu

use of dorkbox.systemTray.Menu in project runwar by cfmlprojects.

the class Tray method instantiateMenu.

private static void instantiateMenu(String trayConfigJSON, String statusText, String iconImage, HashMap<String, String> variableMap, Server server) {
    JSONObject menu;
    setVariableMap(variableMap);
    menu = getTrayConfig(trayConfigJSON, statusText, variableMap);
    if (menu == null) {
        RunwarLogger.LOG.error("Could not load tray config json, using default");
        menu = getTrayConfig(defaultMenu, statusText, variableMap);
    }
    systemTray.setStatus(getString(menu, "title", ""));
    systemTray.setTooltip(getString(menu, "tooltip", ""));
    setIconImage(iconImage);
    Menu mainMenu = systemTray.getMenu();
    addMenuItems((JSONArray) menu.get("items"), mainMenu, server);
}
Also used : JSONObject(net.minidev.json.JSONObject) Menu(dorkbox.systemTray.Menu)

Example 2 with Menu

use of dorkbox.systemTray.Menu in project runwar by cfmlprojects.

the class Tray method addMenuItems.

public static void addMenuItems(JSONArray items, Menu menu, Server server) {
    for (Object ob : items) {
        JSONObject itemInfo = (JSONObject) ob;
        InputStream is = null;
        String label = getString(itemInfo, "label", "");
        String hotkey = getString(itemInfo, "hotkey", "");
        boolean isDisabled = Boolean.parseBoolean(getString(itemInfo, "disabled", "false"));
        if (itemInfo.get("image") != null) {
            is = getImageInputStream(itemInfo.get("image").toString());
        }
        MenuItem menuItem = null;
        if (itemInfo.get("items") != null) {
            Menu submenu = new Menu(label, is);
            submenu.setShortcut(label.charAt(0));
            menu.add(submenu);
            addMenuItems((JSONArray) itemInfo.get("items"), submenu, server);
        } else if (itemInfo.get("separator") != null) {
            menu.add(new Separator());
        } else if (itemInfo.get("checkbox") != null) {
            Checkbox checkbox = new Checkbox(label, null);
            checkbox.setShortcut(label.charAt(0));
            checkbox.setEnabled(!isDisabled);
            menu.add(checkbox);
        } else if (itemInfo.get("action") != null) {
            String action = getString(itemInfo, "action", "");
            if (action.toLowerCase().equals("stopserver")) {
                menuItem = new MenuItem(label, is, new ExitAction(server));
                menuItem.setShortcut('s');
            } else if (action.toLowerCase().equals("restartserver")) {
                menuItem = new MenuItem(label, is, new RestartAction(server));
                menuItem.setShortcut('r');
            } else if (action.toLowerCase().equals("getversion")) {
                menuItem = new MenuItem("Version: " + Server.getVersion(), is, new GetVersionAction());
                menuItem.setShortcut('v');
            } else if (action.toLowerCase().equals("openbrowser")) {
                String url = itemInfo.get("url").toString();
                menuItem = new MenuItem(label, is, new OpenBrowserAction(url));
                menuItem.setShortcut('o');
            } else if (action.toLowerCase().equals("openfilesystem")) {
                File path = new File(getString(itemInfo, "path", Server.getServerOptions().getWarPath()));
                menuItem = new MenuItem(label, is, new BrowseFilesystemAction(path.getAbsolutePath()));
                menuItem.setShortcut('b');
            } else {
                RunwarLogger.LOG.error("Unknown menu item action \"" + action + "\" for \"" + label + "\"");
            }
        } else {
            menuItem = new MenuItem(label, is);
        }
        if (menuItem != null) {
            if (hotkey.length() > 0) {
                menuItem.setShortcut(hotkey.charAt(0));
            }
            menuItem.setEnabled(!isDisabled);
            menu.add(menuItem);
        }
        try {
            if (is != null)
                is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) MenuItem(dorkbox.systemTray.MenuItem) LaunchUtil.getResourceAsString(runwar.LaunchUtil.getResourceAsString) IOException(java.io.IOException) JSONObject(net.minidev.json.JSONObject) Checkbox(dorkbox.systemTray.Checkbox) JSONObject(net.minidev.json.JSONObject) Menu(dorkbox.systemTray.Menu) ZipFile(java.util.zip.ZipFile) File(java.io.File) LaunchUtil.readFile(runwar.LaunchUtil.readFile) Separator(dorkbox.systemTray.Separator)

Aggregations

Menu (dorkbox.systemTray.Menu)2 JSONObject (net.minidev.json.JSONObject)2 Checkbox (dorkbox.systemTray.Checkbox)1 MenuItem (dorkbox.systemTray.MenuItem)1 Separator (dorkbox.systemTray.Separator)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ZipFile (java.util.zip.ZipFile)1 LaunchUtil.getResourceAsString (runwar.LaunchUtil.getResourceAsString)1 LaunchUtil.readFile (runwar.LaunchUtil.readFile)1