use of com.biglybt.pifimpl.local.ui.menus.MenuItemImpl 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());
}
}
use of com.biglybt.pifimpl.local.ui.menus.MenuItemImpl 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());
}
}
Aggregations