I need to crate a dynamic list from a String List and use the List-items as a MenuItem Entry. Is this possible like with a loop over the complete list and then
new MenuItem(list-entry, new Command(){} )
After that i want to select it like a checkbox (just the usage not an actual checkbox).
Is my thinking progress reasonable or do i need to rethink everything? grateful for any help, thanks :)
Yes you can loop on it
MenuBar mymenubar = new MenuBar(true);
for(final String string : myListOfStrings){
MenuItem menuItem = new MenuItem(string , new Command() {
#Override
public void execute() {
//Do some thing on each menu
}
});
mymenubar .addItem(menuItem);
}
Selecting like a checkbox(on menu items) is not a good idea.In menu you can select one menu item only at a time.
Related
There is a good thread on how to correctly hook up a right-click menu to a Jface TreeViewer depending on the selected item.
I would like to show the right click menu depending on: if the right-click was on a node or into "empty space". The problem is that TreeViewer does not automatically clear the selection if you click into empty space. Is there any clean way how to achieve this?
My current approach would be to simply hook up a MouseListener to the tree with the following mouseDown method:
#Override
public void mouseDown(MouseEvent e) {
TreeItem item = treeViewer.getTree().getItem(new Point(e.x, e.y));
if (item == null) {
treeViewer.getTree().deselectAll();
}
}
This seems to work quite well. What do you think of this?
Ok, I found a dirty workaround. So if you really want to do it, here is a possible solution:
final Tree tree = viewer.getTree();
final Menu menu = new Menu(tree);
tree.setMenu(menu);
menu.addMenuListener(new MenuAdapter()
{
#Override
public void menuShown(MenuEvent e)
{
Point point = tree.toControl(Display.getDefault().getCursorLocation());
boolean found = false;
for (TreeItem item : tree.getItems())
{
for (int i = 0; i < tree.getColumnCount(); i++)
if (item.getBounds(i).contains(point))
found = true;
}
System.out.println(found);
}
});
How to add popup menu to your SWT/JFace TreeViewer
Hi, in your applications main class (that extends ApplicationWindow) in protected Control createContents(Composite parent) method you should add code like this:
//Author: Darius Kucinskas (c) 2008-2009
//Email: d[dot]kucinskas[eta]gmail[dot]com
//Blog: http://blog-of-darius.blogspot.com/
//License: GPL
// Create the popup menu
MenuManager menuMgr = new MenuManager();
Menu menu = menuMgr.createContextMenu(mTreeViewer.getControl());
menuMgr.addMenuListener(new IMenuListener() {
#Override
public void menuAboutToShow(IMenuManager manager) {
if(mTreeViewer.getSelection().isEmpty()) {
return;
}
if(mTreeViewer.getSelection() instanceof IStructuredSelection) {
IStructuredSelection selection = (IStructuredSelection)mTreeViewer.getSelection();
DatabaseModelObject object = (DatabaseModelObject)selection.getFirstElement();
if (object.getType() == DATABASE_OBJECT_TYPE.TABLE){
manager.add(new ShowTableDataAction(SWTApp.this));
}
}
}
});
menuMgr.setRemoveAllWhenShown(true);
mTreeViewer.getControl().setMenu(menu);
DatabaseModelObject - is class from my problem domain (specific to my program). mTreeViewer - is object of TreeViewer class (JFace). Thanks, have a nice day!
I'm looking for solution for a little task.
I am using SWT.
I have a Combo class:
public class ComboBoxComponent<T> extends Combo {
private ComboViewer comboListViewer;
public ComboBoxComponent(Composite composite, int i) {
super(composite, i);
comboListViewer = new ComboViewer(this);
setVisibleItemCount(15);
comboListViewer.setContentProvider(new org.eclipse.jface.viewers.ArrayContentProvider());
comboListViewer.setLabelProvider(new LabelProvider());
}
public void setDataModelList(T defaultObject, Collection<T> obj) {
Collection<T> defaultCollection = new LinkedHashSet<T>();
if (defaultObject != null)
defaultCollection.add(defaultObject);
defaultCollection.addAll(obj);
comboListViewer.setInput(defaultCollection);
select(0);
notifySelection();
}
public void notifySelection() {
Event event = new Event();
event.type = SWT.Selection;
event.widget = this;
event.display = getDisplay();
event.time = (int) new Date().getTime();
this.notifyListeners(SWT.Selection, event);
}
#Override
protected void checkSubclass() {
}
}
I want to disable for selecting some items in combo, how could I do it?
With SWT, disable items it is not possible for a ComboBox, you can remove them. You could use JComboBox from Swing to disable items.
You can bridge Swing components creating a SWT_AWT frame and a AWT panel like this
java.awt.Frame frame = SWT_AWT.new_Frame();
java.awt.Panel panel = new java.awt.Panel(new java.awt.BorderLayout());
and then add the Swing JComboBox to the panel.
panel.add(jComboBox);
Here's a tutorial for embedding Swing into SWT
http://www.java2s.com/Tutorial/Java/0280__SWT/EmbededSwingAWTcomponentstoSWT.htm
Disable items by not putting them into the combo list in the first place. Why should they be there if they are not selectable?
Use a storage class, like for example ArrayList to hold your data, then via a loop, input elements into the combo list while excluding the ones that you don't need there.
You can always use combo.remove(int start, in end); to remove your items in a specific location after they have already been added to the combo list.
Additionally, you can prevent that item from being used after it's been selected. You can do this by checking it's name or index number and then prevent it from being used somewhere, also prompting the user in general (if that is the logic you are using) is a good idea.
I have a MenuManager that is filled with Actions, is it possible to access the corresponding MenuItem for the action (ContributionItem) ?
MenuManager menuManager = new MenuManager("#PopupMenu", "contextMenu");
menuManager.add(IAction1...);
menuManager.add(IAction2...);
Menu menu = menuManager.createContextMenu(myTreeVvewer.getControl());
myTreeVvewer.getControl().setMenu(menu);
myTreeVvewer.getTree().addSelectionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent e) {
//Iterate menu items of menu and set something...
}
}
It would probably be easier to use the option MenuManager#.setRemoveAllWhenShown(true); and then dynamically add the menu items in a IMenuListener.
I need to create a context menu for a TreeViewer in an Eclipse plugin project. But, the menu should not contain constant items, they should vary depending on the type of the node that is selected. For example, my treeViewer has the following hierarchy:
Node A
|
--Node B
|
--Node C
For node A - I want to show a menu with an action, but for nodes B and C I don't want to show anything (no menu).
I managed to create the menu for node A, but then I can't get rid of it when some other type of node is selected. My code looks like:
treeViewer.addSelectionChangedListener(
new ISelectionChangedListener(){
public void selectionChanged(SelectionChangedEvent event) {
if(event.getSelection() instanceof IStructuredSelection) {
IStructuredSelection selection = (IStructuredSelection)event.getSelection();
Object o = selection.getFirstElement();
MenuManager menuMgr = new MenuManager();
if (o instanceof NodeA){
Menu menu = menuMgr.createContextMenu(treeViewer.getControl());
treeViewer.getControl().setMenu(menu);
getSite().registerContextMenu(menuMgr, treeViewer);
menuMgr.add(new SomeAction());
}else {
//what ?
}
}
}
}
);
On the else branch I tried to call dispose(),removeAll() on the MenuManager...nothing works!
Any help is appreciated, thanks.
As #jeeeyul mentioned, you should only create one MenuManager to use within your view.
You can use New>Plug-in Project and the view template to get an example of a context menu in a view using a viewer, but basically in your createPartControl(Composite) method you would hook up your context manager.
MenuManager menuMgr = new MenuManager();
menuMgr.setRemoveAllWhenShown(true);
menuMgr.addMenuListener(new IMenuListener() {
public void menuAboutToShow(IMenuManager manager) {
SampleView.this.fillContextMenu(manager);
}
});
Menu menu = menuMgr.createContextMenu(viewer.getControl());
viewer.getControl().setMenu(menu);
getSite().registerContextMenu(menuMgr, viewer);
Your fillContextMenu(MenuManager) method will have access to your viewer, so you can get the current selection from that. You can add whatever actions you want, even re-add actions after updating them with the current selection.
The registerContextMenu(*) call allows extension points like org.eclipse.ui.popupMenus and org.eclipse.ui.menus to contribute items to your context menu.
Just use single Menu Manager.
Do not make Menu Manager dynamically.
in theory, It's possible you tried, but it's inefficient and it's not general way.
Just make a Menu Manager and add all actions which you needs.
when selection has been changed, call Action#setVisible(true|false)to hide or show menu items.
You can also use Action#setEnable to enable/disable menu item.
ps.
Menu Manager is not a menu GUI(likes TreeViewer is a not tree)
It contributes Actions(business logic) to Menu(SWT). And It also manage visibility and enablement. We call this Contribution Manager. We can create a SWT menu very easy with this. (even we don't know about SWT, we just have to know only our business logic:Action) It's fundamental idea in JFace.
When you add an action into manu manager, action will be wrapped with ActionContributionItem. It hooks action's state to update UI(visibility, enablement for menu, button, toolbar and so on). It also hooks UI to launch action when it pressed.
If you are new to eclipse, It is easy to confuse role of SWT and JFace.
Thats the way I do it:
MenuManager menuMgr = new MenuManager();
Menu menu = menuMgr.createContextMenu(viewer.getControl());
menuMgr.addMenuListener(new IMenuListener() {
#Override
public void menuAboutToShow(IMenuManager manager) {
// IWorkbench wb = PlatformUI.getWorkbench();
// IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
if (viewer.getSelection().isEmpty()) {
return;
}
if (viewer.getSelection() instanceof IStructuredSelection) {
IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
Node object = (Node)selection.getFirstElement();
if (object.getModel() instanceof NodeA) {
manager.add(new Action();
} else if (object.getModel() instanceof NodeB) {
manager.add(new OtherAction());
}
}
}
});
menuMgr.setRemoveAllWhenShown(true);
viewer.getControl().setMenu(menu);
I hope this helps ;)
It is important to set removeAllWhenShown property of menu manager to false, in order to hide all the other nodes actions ;)
Suppose that you know how to create Action and you are only interested in context menu following example worked for me hope this bunch of code will help you
private void hookContextMenu() {
MenuManager contextMenu = new MenuManager();
contextMenu.setRemoveAllWhenShown(true);
contextMenu.addMenuListener(new IMenuListener() {
#Override
public void menuAboutToShow(IMenuManager manager) {
IStructuredSelection sSelection = (IStructuredSelection) treeViewer.getSelection();
}
if(selectedObject instanceof A){
manager.add(action);
}
}
});
I'm having a slight problem with an Eclipse Plug-In in development.
There is a view which is comparabe to a roster. There is a list of users in there. My problem is, that I'd like to add an context menu.
The idea is to perform a right-click on a user and the menu should pop up. So far so good... but the problem is that I don't want a single menu. I'd like to have an entry "set status" to that context menu and when one hovers over this entry the menu should be extended to show stuff like "away" "busy" "invisible" and so on...
Could anyone help me out to achieve this?
I have already implemented the corresponding action and made the addition to the MenuManager.
public SessionViewContextMenu(ViewPart sessionView, TableViewer viewer,
final Action action) {
MenuManager manager = new MenuManager("#PopupMenu");
manager.setRemoveAllWhenShown(true);
manager.addMenuListener(new IMenuListener() {
public void menuAboutToShow(IMenuManager manager) {
manager.add(action);
}
});
The correspodning action looks like this:
public Action(...) {
super(provider, "Bla Bla");
// some fancy picture
setImageDescriptor(...);
// setId(ACTION_ID);
setToolTipText("Bla Bla");
update();
}
Everything is working fine (at least the context menu shows the entry). Now I'd like to extend the menu when one hovers over / selects the corresponding action. So the menu should extend and show some more possibilites here...
Any help on how to create a recursive context menu is highly appreciated!
Hope, you understand the problem and don't hesitate to ask dor clarification!
Just create a sub-menu and add the actions to this sub-menu.
Here is a quick snippet which should clarify the usage:
// submenu for a specific user
MenuManager subMenu = new MenuManager("Change Status", null);
// Actions for the sub menu
subMenu.add(someAction);
// add the action to the submenu
manager.add(subMenu);
Hope that helps!
Put together:
public SessionViewContextMenu(ViewPart sessionView, TableViewer viewer,
final Action action) {
MenuManager manager = new MenuManager("#PopupMenu");
manager.setRemoveAllWhenShown(true);
manager.addMenuListener(new IMenuListener() {
public void menuAboutToShow(IMenuManager manager) {
manager.add(action);
// submenu for a specific user
MenuManager subMenu = new MenuManager("Change Status", null);
// Actions for the sub menu
subMenu.add(someAction);
// add the action to the submenu
manager.add(subMenu);
}
});