Dynamic popup menu for all views - eclipse-plugin

I need to provide a dynamic popup menu for all views. I can create a dynamic popup menu contributibution, but I must set the URI and register it for certain view. Now I'm trying to register the menu dynamically, when the user selects another view:
public class GlobalSelectionListener implements ISelectionListener {
HashSet<IWorkbenchPart> extended = new HashSet<IWorkbenchPart>();
#Override
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
if (!extended.contains(part)) {
IWorkbenchPartSite wps = part.getSite();
if (wps == null)
return;
//creates popup menu for this part
MenuManager mgr = new MenuManager();
mgr.add(new DynamicMenu()); //DynamicMenu extends ContributionItem
wps.registerContextMenu("identifier." + mgr.hashCode(), mgr, wps.getSelectionProvider());
extended.add(part);
System.out.println(part + " menu extended");
}
}
}
But this does not work. No one menu item appears in popup menu. I don't know, whether is it ever possible to do it this way. Is there any method to add popup menu for arbitrary view dynamically? It seems, that the registerContextMenu() method does something else.

The problem was not solved, nevertheless there is a workaround. It is possible to register the pop-up menu in plugin.xml file for all views and editors needed. Usualy the number of plugin usecases is limited. If you're writting a plugin, you know what you need the plugin for. Use the Spy plug-in (ALT+SHIFT+F1) to see the active menu contribution identifiers and register your contribution to the pop-up menu of all views and editors you need.

Related

How to access components inside a custom ToolWindow from an action?

I have registered an action in the EditorPopupMenu (this is right click menu). I also have a bunch of components inside a ToolWindow (that I designed using the GUI Designer plugin) that I want to update the values of.
There have been some posts on the IntelliJ forums about this, and the typical answer seems to advice using the ToolWindow's ContentManager, and obtain the JPanel containing all your components. E.g. the following:
Project p = e.getProject();
ToolWindow toolWindow;
toolWindow = ToolWindowManager.getInstance(p).getToolWindow("My ToolWindow ID");
ContentManager contentManager = toolWindow.getContentManager();
JPanel jp = (JPanel) contentManager.getContent(0).getComponent();
This feels counterintuitive... Having to navigate inside JPanel's to find a bunch of components. What if I decided to put my components inside a different container? Suddenly the way I navigate to my components would break down.
Is it really the most practical way to constrain myself to the way my GUI is built? Can't I access these components in a different way?
I found a way to access my custom myToolWindow. This should help quite some people.
Make sure that your custom MyToolWindow extends the class SimpleToolWindowPanel.
In your custom myToolWindowFactory class, pass your custom MyToolWindow to ContentFactory.createContent() as the first argument. NOT one of the JPanel's inside MyToolWindow as is done in the ToolWindow examples given in the official IntelliJ documentation...
In your MyToolWindow constructor, call the method setContent(<YourJPanelContainingYourComponents>).
I found the answer by experimenting on example 5 from this link:
public JBTabbedTerminalWidget getTerminalWidget(ToolWindow window) {
window.show(null);
if (myTerminalWidget == null) {
JComponent parentPanel = window.getContentManager().getContents()[0].getComponent();
if (parentPanel instanceof SimpleToolWindowPanel) {
SimpleToolWindowPanel panel = (SimpleToolWindowPanel) parentPanel;
JPanel jPanel = (JPanel) panel.getComponents()[0];
myTerminalWidget = (JBTabbedTerminalWidget) jPanel.getComponents()[0];
} else {
NotificationUtils.infoNotification("Wait for Freeline to initialize");
}
}
return myTerminalWidget;
}

Cannot get the selected item in a ListViewer in a JFace Dialog

I created a dialog class that inherited from JFace Dialog using Windows Builder. In that, I added some controls included a button and a JFace ListViewer. In widgetSelected() function of the button, I can get out the selected item in the ListViewer. But in `okPressed(), I cannot get this. I don't know why. Can you help me?
Thanks!
If you want to access UI elements in okPressed you must do so before calling super.okPressed() because that will close the dialog and dispose of the controls. So something like:
#Override
protected void okPressed()
{
IStructuredSelection sel = viewer.getStructuredSelection();
// TODO deal with selection
// Call super.okPressed() last
super.okPressed();
}
Alternatively save the selection when your widgetSelected is called.

DirectoryDialog in custom Wizard with SWT and JFace

I have to create a custom wizard to develop a Eclipse Plug-in. I wish to use a DirectoryDialog but I can't get work with the other elements. I'm seeing that the DirectoyDialog is used in a "extends composite" class, but, is there any way to use in a "wizardPage"?
Thanks at all!
org.eclipse.swt.widgets.DirectoryDialog extends Dialog and can only be used as a pop-up dialog. It cannot be embedded in a wizard.
You can put a Button on the wizard page that displays the directory dialog when clicked.
Use the following code in your WizardPage
Button btnBrowse = new Button(container, SWT.NONE);
btnBrowse.setText("Browse..");
btnBrowse.addListener(SWT.Selection, new Listener() {
#Override
public void handleEvent(Event e) {
DirectoryDialog dirDialog = new DirectoryDialog(getShell());
dirDialog.setText("Select the parent directory for tools");
String location = dirDialog.open();
}
});
getShell() api used in 4th line is from WizardPage class.

Get the visibility status of a toolbar according to Customize perspective dialog in Eclipse

I'm working on a plug-in, which contributes to the toolbar. The toolbar is listed in the Customize perstpective dialog. When I uncheck the check-box, the toolbar disappears as expected. But when the toolbar is updated from my code (using the toolbar manager), it gets displayed again. I need to get somehow the visibility status from code in order to prevent the toolbar from beeing updated when disabled.
Do you anybody have any idea, where the Customize perspective dialog stores the visibility status of menus and toolbars and how to get it from code, please?
You can get information by item ID this way:
private static boolean isToolbarItemVisible(String id){
IWorkbench workbench = PlatformUI.getWorkbench();
IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
return !((WorkbenchPage) workbenchWindow.getActivePage()).getHiddenItems().contains((ModeledPageLayout.HIDDEN_TOOLBAR_PREFIX + id + ","));
}

how to click on action bar menu items in test app that having only apk using robotium

I am testing APK file. I dont have source code. I need to click on the action bar menu item
I have tried solo.clickonActionbaritems option and it did not work. Please help me. Many thanks!
I guess you want to click on Android Menu button:
Find your keycode here
solo.sendKey(KeyEvent.KEYCODE_MENU);
Also I highly recommend to use Hierarchy viewer in DDMS to find id of required button.
View requiredButton = getViewById(id.reqired_button_id);
solo.clickOnView(requiredButton);
// Method returns view
protected View getViewById(int id) {
View view = solo.getView(id);
assertNotNull("View is null", view);
assertTrue("View is not shown", view.isShown());
return view;
}
Also you can use clickOnText.