Java swt treeview popup menu - java

Hiho,
currently I have a working popup menu which appears when I click on a treeview item.
But I want to show different popups for different tree view entries. I don't get a idea how to do so...
Here is my code for creating the menu:
MenuManager menuMgr = new MenuManager("#PopupMenu");
menuMgr.setRemoveAllWhenShown(true);
menuMgr.addMenuListener(new IMenuListener() {
#Override
public void menuAboutToShow(IMenuManager manager) {
Action action = new Action() {
public void run() {
// So something
}
};
action.setText("Set as working file");
manager.add(action);
}
});
Menu menu = menuMgr.createContextMenu(getTree());
getTree().setMenu(menu);

You should propably use a MouseListener on the tree:
final Tree tree = new Tree(parent, ...);
tree.addMouseListener(new MouseAdapter() {
#override
public void mouseDown(MouseEvent me) {
if(tree.getSelection() instanceof MySpecificTreeNode) {
// create menu...
}
}
});

Two ideas. For both you need to listen to selections on the TreeView, because that's the only way to determine which Menu (or special content) you want to show.
Then you could either set the correct menu to the the tree right after you know which one to use or contribute the needed items to the existing menu (that's how it's done in the eclipse framework).

Related

Java SWT get bounds of custom element in TreeView

I´ve created a TreeView with a ContentProvider and custom tree elements.
I also have a ISelectionChangedListener added to the TreeView.
I want to add an MouseListener, do detect if an element of the tree is right-clicked and show a popup menu.
If the white area around the tree is clicked, i don´t want to display the popup menu.
The menu is added via Extensions in the plugin.xml.
How can I now evaluate if a tree element is right-clicked, so I can show the popup menu (maybe with visibleWhen in the plugin.xml) ?
I also want to clear the selection, if the right-click is detected in the white area of the TreeView.
Ok, i did not realized that i can still use tree.getItem(...).
So here is my full MouseListener:
treeOPCUA.addMouseListener(new MouseListener()
{
#Override
public void mouseUp(MouseEvent e)
{
if(e.button == 3 && rightMouseClicked == true)
rightMouseClicked = false;
}
#Override
public void mouseDown(MouseEvent e)
{
if(e.button == 3 && rightMouseClicked == false)
rightMouseClicked = true;
if(treeOPCUA.getItem(new Point(e.x, e.y)) == null)
viewer.setSelection(null);
}
#Override
public void mouseDoubleClick(MouseEvent e)
{
viewer.setExpandedState(e.getSource(), true);
}
});
With the boolean variable "rightMouseClicked" I detect in my ISelectionChangedListener if the right mouse is clicked:
if(event.getSelection() instanceof IStructuredSelection && !rightMouseClicked)
I hope this answer helps anyone.

Single selection and fire selecion of AbstractGraphicalEditPart

I Have an AbstractDecoratedTextEditor and it has a tab with components of AbstractGraphicalEditPart.
I want to
1) Select single components. If I select one component, other componentes was unselected
2) When I select one component, I wan to to fire the selection listenner of eclipse. Because this listenner will change the properties view of Eclipse.
I tried this code for number 2, but not work.
((IFigure) componentFigure).addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent arg0) {
ComponentEditPart.this.setSelected(SELECTED);
fireSelectionChanged();
super.mousePressed(arg0);
}
}
There is a selection listener in GEF, but it's on the EditPartViewer. Add a ISelectionChangedListener to your Graphical viewer. Each editpart has a method #getViewer() (i.e. AbstractGraphicalEditPart#getViewer()).
graphicalEditPart.getViewer().addSelectionChangedListener(new ISelectionChangedListener() {
public void selectionChanged(SelectionChangedEvent event) {
// TODO: implement it to handle selection change
}
}};

Editable JComboBox with dynamic entries

I made my JComboBox editable using:
myCombo.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
I encounter multible problems with the following task:
When writing into the combobox, the new content of the box should be taken and compared with a list and all entries starting with this text should be shown in the popupmenu.
So if i have a list with: "Aban" "Aben" "Aber" "Acen" "Aden"
and enter "Ab" into the box, then the pop-up should display the first 3 entries.
When clicking one of the entries (Either by keyboard selecting and pressing enter/tab or by clicking with a mouse) The ComboBox should get that value and the Popup should hide. I need to find this action as some of the elements have a note at the end (In backets which I require) but only when one of the entries is selected
Here are the most imporant parts of my code:
final JTextComponent tcA = (JTextComponent) myCombo.getEditor().getEditorComponent();
tcA.getDocument().addDocumentListener(new DocumentListener() {
public void methodUsedByinsertUpdateAndremoveUpdate(DocumentEvent e) {
String item = ((JTextComponent) myCombo.getEditor().getEditorComponent()).getText();
//Routine to get the new list in a vector, not pasted for readability
DefaultComboBoxModel newMyComboModel = new DefaultComboBoxModel(myVectorList);
myCombo.setModel(newMyComboModel);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
myCombo.showPopup();
}
});
}
}
myCombo.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(myCombo.getModel().getSize() == 1) {
//Special logic to find out if the selected item has a note
SwingUtilities.invokeLater(new Runnable() {
public void run() {
myCombo.hidePopup();
}
});
}
}
With this, i have:
Trouble with the first character (Caret position not working correctly)
Popup not automatically shown and hides when entering new character into the field
Problems with Swing GUI not being actualised
If you require more information just ask
The Glazed List recommended by peeskillet did exactly what I wanted

How to save selected view highlighted in rcp application?

I have an Eclipse RCP application. In a perspective there are four views and I want to highlight respective views whenever I click on them. Is it possible to do it?
i have tried following code:
private void addFocusBackgroundOnSelectingView() {
viewer.getControl().addListener(SWT.MouseEnter, new Listener() {
#Override
public void handleEvent(Event event) {
viewer.getControl().setBackground(
PlatformUI.getWorkbench().getDisplay()
.getSystemColor(SWT.COLOR_GRAY));
}
});
viewer.getControl().addListener(SWT.MouseExit, new Listener() {
#Override
public void handleEvent(Event event) {
viewer.getControl().setBackground(
PlatformUI.getWorkbench().getDisplay()
.getSystemColor(SWT.COLOR_WHITE));
}
});
}
I want to save the selection even i mouse hover out if that view is already had selected.
The Eclipse PartService keeps track of which part (Editors, Views etc...) is currently active. You can add a listener to the service via the PlatfomUI class:
IPartListener partListener = ...;
IPartService partService = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService();
partService.addPartListener(listener);
The IPartListener interface has partActivated and partDeactivated methods where you can do your highlighting.

How would i program a dynamic menubar in Swing?

Basically i want to be able to allow the user to save bookmarks which are then put into a list on a submenu on a menubar. How would i go about programming a general function for any number of bookmarks that may be added, i basically want the items to put the URL into a textbox when clicked. Would i need to create a new class for this, or is there an inbuilt function?
My program is a simple RSS reader written in Java using Swing.
You need to add a MenuListener to the menu item that you want to be dynamic.
In the void menuSelected(MenuEvent e) method, implement the construction of the submenus.
In a first implementation, you can first reset the content of your menu and then rebuid it from scratch instead of updating it :
JMenu menu = new JMenu("Bookmarks");
menu.addMenuListener(new MyMenuListener());
private class MyMenuListener implements MenuListener {
public void menuCanceled(MenuEvent e) { }
public void menuDeselected(MenuEvent e) { }
public void menuSelected(MenuEvent e) {
JMenu menu = (JMenu) e.getSource();
populateWindowMenu(menu);
}
}
void populateWindowMenu(JMenu windowMenu) {
windowMenu.removeAll();
// Populate the menu here
}

Categories