SWT: show popup menu below toolbar button after clicking on it - swt

I want to show a popup menu below a toolbar button when the user clicks this button. I've read about the SWT.DROP_DOWN style for a ToolItem but this seems very much limited to a simple list of items according to this sample. Instead, I want to show a popup menu with, e.g., checkbox and radio button menu items.

You can make MenuItem with styles SWT.CHECK, SWT.CASCADE, SWT.PUSH, SWT.RADIO, SWT.SEPARATOR
see javadoc..
So you can "hang" swt menu to selection of dropdown on toolbar item like this
public class Test {
private Shell shell;
public Test() {
Display display = new Display();
shell = new Shell(display, SWT.SHELL_TRIM);
shell.setLayout(new FillLayout(SWT.VERTICAL));
shell.setSize(50, 100);
ToolBar toolbar = new ToolBar(shell, SWT.FLAT);
ToolItem itemDrop = new ToolItem(toolbar, SWT.DROP_DOWN);
itemDrop.setText("drop menu");
itemDrop.addSelectionListener(new SelectionAdapter() {
Menu dropMenu = null;
#Override
public void widgetSelected(SelectionEvent e) {
if(dropMenu == null) {
dropMenu = new Menu(shell, SWT.POP_UP);
shell.setMenu(dropMenu);
MenuItem itemCheck = new MenuItem(dropMenu, SWT.CHECK);
itemCheck.setText("checkbox");
MenuItem itemRadio = new MenuItem(dropMenu, SWT.RADIO);
itemRadio.setText("radio1");
MenuItem itemRadio2 = new MenuItem(dropMenu, SWT.RADIO);
itemRadio2.setText("radio2");
}
if (e.detail == SWT.ARROW) {
// Position the menu below and vertically aligned with the the drop down tool button.
final ToolItem toolItem = (ToolItem) e.widget;
final ToolBar toolBar = toolItem.getParent();
Point point = toolBar.toDisplay(new Point(e.x, e.y));
dropMenu.setLocation(point.x, point.y);
dropMenu.setVisible(true);
}
}
});
shell.open();
while(!shell.isDisposed()) {
if(!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
public static void main(String[] args) {
new Test();
}
}

Related

How to get current row (element) on TreeEditor Button click event

TreeViewerColumn colEdit= new TreeViewerColumn (viewer, column);
colEdit.setLabelProvider(new ColumnLabelProvider(){
#Override
public void update(ViewerCell cell) {
TreeItem item = (TreeItem) cell.getItem();
Button btnEdit= new Button((Composite) cell.getViewerRow().getControl(),SWT.NONE);
btnEdit.setText("Edit");
TreeEditor editor = new TreeEditor(item.getParent());
editor.grabHorizontal = true;
editor.grabVertical = true;
editor.setEditor(btnEdit, item, cell.getColumnIndex());
editor.layout();
btnEdit.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
// How to get current row element.
TreeItem[] selection = treeViewer.getTree().getSelection(); // (Selection is empty, because click on button and selection not happened)
}
});
}
});
treeViewer.getTree().getSelection(); return empty because Tree Editor with button control added and while button click row selection not happening.
cell.getElement() returning null in handler method.
btnEdit.setData can help me but Is there any way to get current row (element) on which button click event it clicked ??

Solution :: Simplest way to setup Context Menu using Draw2d eclipse RCP

Menu menu = new Menu (editor.getSite().getShell(), SWT.POP_UP);
MenuItem itemA = new MenuItem(menu, SWT.PUSH);
itemA.setText("ItemA"); menu.setVisible(true);
Then you will have enent, On click.
itemA.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
}
});

How to set a title (not a caption)for MenuBar in Vaadin

I'd like to have a title for my MenuBar, but I'm not finding any methods in the Vaadin API for that. Just a small text to appear on the button which triggers the dropdown (like the user's name), instead of the triangle it shows now. Thanks!
MenuBar menu = new MenuBar();
menu.setWidth("70px");
MenuItem home = menu.addItem("Home", FontAwesome.HOME, new Command() {
public void menuSelected(MenuItem selectedItem) {doSomething();}});
MenuItem settings = menu.addItem("Settings", FontAwesome.COGS, new Command() {
public void menuSelected(MenuItem selectedItem) {doSomething();}});
MenuItem logout = menu.addItem("Sign out", FontAwesome.SIGN_OUT, new Command() {
public void menuSelected(MenuItem selectedItem) {doSomething();}});
If you remove the setWidth you will notice that all three menus (Home, Settings, Sign out) are horizontally aligned which is, I believe, not what you want. What you want to do is more something like this:
MenuBar menu = new MenuBar();
MenuItem rootItem = menu.addItem("User name", null);
rootItem.addItem("Home", FontAwesome.HOME, this::doSomething);
rootItem.addItem("Settings", FontAwesome.COGS, this::doSomething);
rootItem.addItem("Sign out", FontAwesome.SIGN_OUT, this::doSomething);

GXT ToolBar is scrolled

I'm developing a simple GXT widget - it's a TreePanel with a ToolBar added using setTopComponent.
The problem is that as soon as the tree is large enough so that it can be scrolled, the scroll-bar doesn't scroll the tree only, but scrolls the ToolBar as well.
What should be change so that ToolBar remains on the top of page, and only the tree is scrolled.
public class TreePanelExample extends LayoutContainer {
#Override
protected void onRender(Element parent, int index) {
super.onRender(parent, index);
Folder model = getTreeModel();
TreeStore<ModelData> store = new TreeStore<ModelData>();
store.add(model.getChildren(), true);
final TreePanel<ModelData> tree = new TreePanel<ModelData>(store);
tree.setDisplayProperty("name");
tree.setAutoLoad(true);
ToolBar toolBar = new ToolBar();
toolBar.setBorders(true);
toolBar.add(new Button("Dummy button", new SelectionListener<ButtonEvent>() {
public void componentSelected(ButtonEvent ce) {
Info.display("Dummy button", "I'm so dumb!");
}
}));
ContentPanel panel = new ContentPanel();
panel.setHeaderVisible(false);
panel.setCollapsible(false);
panel.setFrame(false);
panel.setAutoWidth(true);
panel.setAutoHeight(true);
// setting fixed size doesn't make any difference
// panel.setHeight(100);
panel.setTopComponent(toolBar);
panel.add(tree);
add(panel);
}
The problem is that
TreePanelExample extends LayoutContainer
while instead it should extend Viewport.
Additionally I shouldn't have used
panel.setAutoWidth(true);
panel.setAutoHeight(true);
Plus it is necessary to add the main panel using
new BorderLayoutData(LayoutRegion.CENTER);
Here is the complete solution:
public class TreePanelExample extends Viewport {
public TreePanelExample() {
super();
setLayout(new BorderLayout());
Folder model = getTreeModel();
TreeStore<ModelData> store = new TreeStore<ModelData>();
store.add(model.getChildren(), true);
final TreePanel<ModelData> treePanel = new TreePanel<ModelData>(store);
treePanel.setDisplayProperty("name");
treePanel.setAutoLoad(true);
ToolBar toolBar = new ToolBar();
toolBar.setBorders(true);
toolBar.add(new Button("Dummy button", new SelectionListener<ButtonEvent>() {
public void componentSelected(ButtonEvent ce) {
Info.display("Dummy button", "I'm so dumb!");
}
}));
ContentPanel panel = new ContentPanel();
panel.setBodyBorder(false);
panel.setHeaderVisible(false);
panel.setTopComponent(toolBar);
panel.setLayout(new FitLayout());
panel.add(treePanel);
BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
centerData.setMargins(new Margins(5, 5, 5, 5));
centerData.setCollapsible(true);
panel.syncSize();
add(panel, centerData);
}

How to create a toolbar inside viewpart (Not use plugin.xml)

I Created a ToolItem "Save As" like image above, But it not display at toolbar position. So how to create a toolbar inside viewpart (Not use plugin.xml)
IMAGE EXAMPLE
This is my code Create Toolbar:
public void createToolbar(Composite parent) {
// Create composite Toolbar and set layout
toolBarComposite = new Composite(parent, SWT.NONE);
gridLayout = new GridLayout(1, false);
toolBarComposite.setLayout(gridLayout);
gridData = new GridData(SWT.RIGHT, SWT.NONE, true, false);
toolBarComposite.setLayoutData(gridData);
// Create Toolbar
gridData = new GridData(SWT.RIGHT, SWT.NONE, true, false);
toolBar = new ToolBar(toolBarComposite, SWT.FLAT);
toolBar.setLayoutData(gridData);
// Create Item
item = new ToolItem(toolBar, SWT.PUSH);
item.setImage(SAVE_IMAGE);
item.setToolTipText("Save (Ctrl + S)");
item.setEnabled(true);
item.addSelectionListener(new SelectionAdapter() {
private static final long serialVersionUID = -102212312093090431L;
#Override
public void widgetSelected(SelectionEvent e) {
}
#Override
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
Thank for your advance !
You're going to have to use contributions on the view site's action bar.
Example
// Copy-pasted from an existing project, so the code can be made nicer
private void createAdditionalToolbarActions()
{
getViewSite().getActionBars().getToolBarManager().add(new GroupMarker("additions")); //$NON-NLS-1$
getViewSite().getActionBars().getToolBarManager().prependToGroup("additions", new SaveAction()); //$NON-NLS-1$
getViewSite().getActionBars().updateActionBars();
}
The method getViewSite is part of ViewPart. Call this after the contents of the view have been created.
The SaveAction must implement IAction or IContributionItem. For convenience, just extend the SaveAction from org.eclipse.jface.action.Action and call methods such as setImageDescriptor and setToolTipText.
Do all your business login in the run override.