Search in sources :

Example 1 with GraphicURI

use of com.biglybt.pif.ui.GraphicURI in project BiglyBT by BiglySoftware.

the class MenuBuildUtils method addPluginMenuItems.

/**
 * Creates menu items inside the given menu based on the plugin API MenuItem
 * instances provided. This method is provided mainly as a utility method to
 * make it easier for menus to incorporate menu components specified by
 * plugins.
 *
 * Usually, the list of array items will be extracted from a class like
 * MenuItemManager or TableContextMenuManager, where plugins will usually
 * register menu items they have created.
 *
 * @param items The list of plugin MenuItem to add
 * @param parent The SWT Menu to add to.
 * @param prev_was_separator Indicates if the previous item in the menu is
 *            a separator or not
 * @param enable_items Indicates whether you want generated items to be
 *            enabled or not. If false, all items will be disabled. If true,
 *            then the items *may* be enabled, depending on how each MenuItem
 *            is configured.
 * @param controller The callback object used by this method when creating the
 *            SWT menus (used for invoking fill and selection listeners).
 */
public static void addPluginMenuItems(MenuItem[] items, Menu parent, boolean prev_was_separator, final boolean enable_items, final PluginMenuController controller) {
    for (int i = 0; i < items.length; i++) {
        final MenuItemImpl az_menuitem = (MenuItemImpl) items[i];
        controller.notifyFillListeners(az_menuitem);
        if (!az_menuitem.isVisible()) {
            continue;
        }
        final int style = az_menuitem.getStyle();
        final int swt_style;
        boolean this_is_separator = false;
        // Do we have any children? If so, we override any manually defined
        // style.
        boolean is_container = false;
        if (style == TableContextMenuItem.STYLE_MENU) {
            swt_style = SWT.CASCADE;
            is_container = true;
        } else if (style == TableContextMenuItem.STYLE_PUSH) {
            swt_style = SWT.PUSH;
        } else if (style == TableContextMenuItem.STYLE_CHECK) {
            swt_style = SWT.CHECK;
        } else if (style == TableContextMenuItem.STYLE_RADIO) {
            swt_style = SWT.RADIO;
        } else if (style == TableContextMenuItem.STYLE_SEPARATOR) {
            this_is_separator = true;
            swt_style = SWT.SEPARATOR;
        } else {
            swt_style = SWT.PUSH;
        }
        // Skip contiguous separators
        if (prev_was_separator && this_is_separator) {
            continue;
        }
        // Skip trailing separator
        if (this_is_separator && i == items.length - 1) {
            continue;
        }
        prev_was_separator = this_is_separator;
        final org.eclipse.swt.widgets.MenuItem menuItem = new org.eclipse.swt.widgets.MenuItem(parent, swt_style);
        if (swt_style == SWT.SEPARATOR) {
            continue;
        }
        if (enable_items) {
            if (style == TableContextMenuItem.STYLE_CHECK || style == TableContextMenuItem.STYLE_RADIO) {
                Boolean selection_value = (Boolean) az_menuitem.getData();
                if (selection_value == null) {
                    throw new RuntimeException("MenuItem with resource name \"" + az_menuitem.getResourceKey() + "\" needs to have a boolean value entered via setData before being used!");
                }
                menuItem.setSelection(selection_value.booleanValue());
            }
        }
        final Listener main_listener = controller.makeSelectionListener(az_menuitem);
        menuItem.addListener(SWT.Selection, new Listener() {

            @Override
            public void handleEvent(Event e) {
                if (az_menuitem.getStyle() == MenuItem.STYLE_CHECK || az_menuitem.getStyle() == MenuItem.STYLE_RADIO) {
                    if (!menuItem.isDisposed()) {
                        az_menuitem.setData(Boolean.valueOf(menuItem.getSelection()));
                    }
                }
                main_listener.handleEvent(e);
            }
        });
        if (is_container) {
            Menu this_menu = new Menu(parent);
            menuItem.setMenu(this_menu);
            addMaintenanceListenerForMenu(this_menu, new MenuBuilder() {

                @Override
                public void buildMenu(Menu root_menu, MenuEvent menuEvent) {
                    controller.buildSubmenu(az_menuitem);
                    addPluginMenuItems(az_menuitem.getItems(), root_menu, false, enable_items, controller);
                }
            });
        }
        String custom_title = az_menuitem.getText();
        menuItem.setText(custom_title);
        Graphic g = az_menuitem.getGraphic();
        if (g instanceof UISWTGraphic) {
            Utils.setMenuItemImage(menuItem, ((UISWTGraphic) g).getImage());
        } else if (g instanceof GraphicURI) {
            Utils.setMenuItemImage(menuItem, ((GraphicURI) g).getURI().toString());
        }
        menuItem.setEnabled(enable_items && az_menuitem.isEnabled());
    }
}
Also used : MenuItemListener(com.biglybt.pif.ui.menus.MenuItemListener) Listener(org.eclipse.swt.widgets.Listener) MenuListener(org.eclipse.swt.events.MenuListener) MenuItemFillListener(com.biglybt.pif.ui.menus.MenuItemFillListener) Graphic(com.biglybt.pif.ui.Graphic) UISWTGraphic(com.biglybt.ui.swt.pif.UISWTGraphic) TableContextMenuItem(com.biglybt.pif.ui.tables.TableContextMenuItem) MenuItem(com.biglybt.pif.ui.menus.MenuItem) MenuItemImpl(com.biglybt.pifimpl.local.ui.menus.MenuItemImpl) GraphicURI(com.biglybt.pif.ui.GraphicURI) Event(org.eclipse.swt.widgets.Event) MenuEvent(org.eclipse.swt.events.MenuEvent) UISWTGraphic(com.biglybt.ui.swt.pif.UISWTGraphic) Menu(org.eclipse.swt.widgets.Menu) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MenuEvent(org.eclipse.swt.events.MenuEvent)

Example 2 with GraphicURI

use of com.biglybt.pif.ui.GraphicURI in project BiglyBT by BiglySoftware.

the class TorrentMenuFancy method addPluginItem.

private void addPluginItem(Composite detailArea, final com.biglybt.pif.ui.menus.MenuItem item) {
    // menuWillBeShown listeners might change the visibility, so run before check
    MenuItemImpl menuImpl = ((MenuItemImpl) item);
    menuImpl.invokeMenuWillBeShownListeners(getTarget(item));
    if (!item.isVisible()) {
        if (DEBUG_MENU) {
            System.out.println("Menu Not Visible: " + item.getText() + ": " + item.getMenuID());
        }
        return;
    }
    Graphic graphic = item.getGraphic();
    FancyRowInfo row;
    if (DEBUG_MENU) {
        System.out.println("Menu " + item.getText() + ": " + item.getMenuID());
    }
    if (item.getStyle() == com.biglybt.pif.ui.menus.MenuItem.STYLE_MENU) {
        row = createMenuRow(detailArea, item.getResourceKey(), null, new FancyMenuRowInfoListener() {

            @Override
            public void buildMenu(Menu menu) {
                if (dms.length != 0) {
                    MenuBuilder submenuBuilder = ((MenuItemImpl) item).getSubmenuBuilder();
                    if (submenuBuilder != null) {
                        try {
                            item.removeAllChildItems();
                            submenuBuilder.buildSubmenu(item, getTarget(item));
                        } catch (Throwable t) {
                            Debug.out(t);
                        }
                    }
                    MenuBuildUtils.addPluginMenuItems(item.getItems(), menu, false, true, new MenuBuildUtils.PluginMenuController() {

                        @Override
                        public Listener makeSelectionListener(final com.biglybt.pif.ui.menus.MenuItem plugin_menu_item) {
                            return new TableSelectedRowsListener(tv, false) {

                                @Override
                                public boolean run(TableRowCore[] rows) {
                                    if (rows.length != 0) {
                                        ((MenuItemImpl) plugin_menu_item).invokeListenersMulti(getTarget(item));
                                    }
                                    return true;
                                }
                            };
                        }

                        @Override
                        public void notifyFillListeners(com.biglybt.pif.ui.menus.MenuItem menu_item) {
                            ((MenuItemImpl) menu_item).invokeMenuWillBeShownListeners(getTarget(item));
                        }

                        // @see com.biglybt.ui.swt.MenuBuildUtils.PluginMenuController#buildSubmenu(com.biglybt.pif.ui.menus.MenuItem)
                        @Override
                        public void buildSubmenu(com.biglybt.pif.ui.menus.MenuItem parent) {
                            com.biglybt.pif.ui.menus.MenuBuilder submenuBuilder = ((MenuItemImpl) parent).getSubmenuBuilder();
                            if (submenuBuilder != null) {
                                try {
                                    parent.removeAllChildItems();
                                    submenuBuilder.buildSubmenu(parent, getTarget(item));
                                } catch (Throwable t) {
                                    Debug.out(t);
                                }
                            }
                        }
                    });
                }
            }
        });
    } else {
        row = createRow(detailArea, item.getResourceKey(), null, new TableSelectedRowsListener(tv, false) {

            @Override
            public boolean run(TableRowCore[] rows) {
                if (rows.length != 0) {
                    ((MenuItemImpl) item).invokeListenersMulti(getTarget(item));
                }
                return true;
            }
        });
    }
    row.setEnabled(item.isEnabled());
    if (graphic instanceof UISWTGraphic) {
        row.getIconLabel().setImage(((UISWTGraphic) graphic).getImage());
    } else if (graphic instanceof GraphicURI) {
        ImageLoader.getInstance().setLabelImage(row.getIconLabel(), ((GraphicURI) graphic).getURI().toString());
    }
}
Also used : TableSelectedRowsListener(com.biglybt.ui.swt.views.table.TableSelectedRowsListener) Graphic(com.biglybt.pif.ui.Graphic) UISWTGraphic(com.biglybt.ui.swt.pif.UISWTGraphic) TableSelectedRowsListener(com.biglybt.ui.swt.views.table.TableSelectedRowsListener) MenuBuilder(com.biglybt.pif.ui.menus.MenuBuilder) MenuItemImpl(com.biglybt.pifimpl.local.ui.menus.MenuItemImpl) TableRowCore(com.biglybt.ui.common.table.TableRowCore) GraphicURI(com.biglybt.pif.ui.GraphicURI) MenuItem(com.biglybt.pif.ui.menus.MenuItem) UISWTGraphic(com.biglybt.ui.swt.pif.UISWTGraphic)

Aggregations

Graphic (com.biglybt.pif.ui.Graphic)2 GraphicURI (com.biglybt.pif.ui.GraphicURI)2 MenuItem (com.biglybt.pif.ui.menus.MenuItem)2 MenuItemImpl (com.biglybt.pifimpl.local.ui.menus.MenuItemImpl)2 UISWTGraphic (com.biglybt.ui.swt.pif.UISWTGraphic)2 MenuBuilder (com.biglybt.pif.ui.menus.MenuBuilder)1 MenuItemFillListener (com.biglybt.pif.ui.menus.MenuItemFillListener)1 MenuItemListener (com.biglybt.pif.ui.menus.MenuItemListener)1 TableContextMenuItem (com.biglybt.pif.ui.tables.TableContextMenuItem)1 TableRowCore (com.biglybt.ui.common.table.TableRowCore)1 TableSelectedRowsListener (com.biglybt.ui.swt.views.table.TableSelectedRowsListener)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 MenuEvent (org.eclipse.swt.events.MenuEvent)1 MenuListener (org.eclipse.swt.events.MenuListener)1 Event (org.eclipse.swt.widgets.Event)1 Listener (org.eclipse.swt.widgets.Listener)1 Menu (org.eclipse.swt.widgets.Menu)1