e4 dynamic menu contributions - eclipse

This feature has been realized as of Kepler M4, for a detailed information on the usage see my blog
I want to realize a fully dynamic menu contribution to the menu of a handler located in a view toolbar. In Eclipse 3 it was possible to add "dynamic" as org.eclipse.ui.menus contribution to a Menu!
I already found out about www.vogella.com/blog/2010/10/26/processors-e4-model explaining on how to dynamically contribute to menus by means of processor model extensions but I am talking about a completely dynamic menu implementation which changes on every call of the resp. submenu. As mentioned this was no problem to realize in Eclipse 3.x via the dynamic menu contribution and the set of isDynamic() to true.
I already tried several approaches:
Registering a processor hooking to a menu => no dynamic add possible (new elements are simply not shown, also discussed in Eclipse Forum - Cannot replace menu items at runtime)
Listening to the UIEventTopic for the event of creating the menu, getting the widget for Modification => modifications to the swt.Menu gathered are simply ignored (every listener, element etc.) (for info RCP Event Model
Open, untried solutions
Inserting a ToolControl to try an SWT approach -> quite complicated but may work
I've been banging my head for some time now, but can't seem to understand the correct implementation of this problem within E4.
-- This question was also asked in Eclipse Forum - Dynamic menu contributions
----- UPDATE
I tried a different approach up to now:
I added a HandledToolItem to the Menu (please see the following image)
and with the following code I am trying to interfere with the menus way to build, where the code is called by the resp. command handleer as referenced in the image.
#CanExecute
public boolean canExecute(#Optional MApplication application) {
System.out.println("CanExecute Counter="+counter);
// --- 1 ---
// Find the required MMenu Entry in the Application Model
if(application == null) return true;
EModelService modelService = (EModelService) application.getContext().get(EModelService.class.getName());
MPart part = (MPart) modelService.find("at.medevit.emr.contacts.ui.contactselector", application);
List<MToolBarElement> lmte = part.getToolbar().getChildren();
HandledToolItemImpl htil = null;
for (MToolBarElement mToolBarElement : lmte) {
if(mToolBarElement.getElementId().equals("at.medevit.emr.contacts.ui.contactselector.toolbar.handledtoolitem.filter")) htil = (HandledToolItemImpl) mToolBarElement;
}
if(htil != null) {
MMenu elemMenu = htil.getMenu();
// --- 2 ---
// Found it hopefully, let's start the real work, simply add a new item
MDirectMenuItem mdi = MMenuFactory.INSTANCE.createDirectMenuItem();
mdi.setLabel("Counter "+counter);
counter++;
// --- 3 ---
elemMenu.getChildren().add(mdi); // ConcurrentModificationException
}
As one can see, this code is queried once the menu is created, to determine whether the command is executable or not. All the code from 1 - 2 is to find the correct MMenu element to work on. The code from 2 - 3 creates a MenuItem and increments a counter in the field.
BUT at 3 I face a java.util.ConcurrentModificationException the first time the menu is opened! I assume that at this very point the menu is iterating over elemMenu.getChildren() and I am not allowed to enable!
So whats all the fuzz about the entire e4 model being changeable all the time ;) (just kiddin' I know this is a baaaad hack!!!)
Thing is: I really think that the possibility to add fully dynamic menu parts is one of the best usability tools, and if it is not possible to realize it in E4 as it was in E3 this is a very serious degradation of possibilities!!!
-- UPDATE
An Eclipse Bug has been filed for this https://bugs.eclipse.org/bugs/show_bug.cgi?id=389063

Proper dynamic model updates should be handled in the bug you've opened. As a workaround in Eclipse4 in Juno, a MRenderedMenuItem can be created in Eclipse4 to provide the equivalent functionality to the dynamic element (although if you are using 4.2, you would just use org.eclipse.ui.menus).
ex:
ContextFunction generator = new ContextFunction() {
#Override
public Object compute(IEclipseContext context) {
return new MyCompoundContributionItem(context);
}
};
MRenderedMenuItem menuItem = MenuFactoryImpl.eINSTANCE.createRenderedMenuItem();
menuItem.setElementId(id);
menuItem.setContributionItem(generator);
container.getChildren().add(menuItem);
This effectively provides a CompoundContributionItem directly to the Eclipse4 menu renderer.

Related

Visual Studio Code: How to add checkbox in view container

I've been searching non-stop for this on the documentation but haven't been able to find any sort of information. I would like to know how to add checkboxes in my custom view container, similar to the breakpoints' checkboxes.
You can simulate the checkbox by playing with the icon, create a new TreeItem with a different icon when clicked.
Somehow they have knowledge on where on the TreeItem you click.
Looking with the dev tools, it is an <input type="checkbox"/>.
This means that you can do more with TreeItems than the docs explain.
Looking at the source code the BreakpointView is not implemented with a TreeItemProvider, it extends the ViewPane class and uses some kind of templates to render an item. Beside a checkbox it is also possible to have a combobox (SelectBox class).
I have added a feature request (101175) to extend the vscode API so extension developers can write Views with ViewItems that have additional UI-Elements like the Breakpoint view.
You can do this in the proposed api: treeItemCheckbox in Insiders v1.72 now and since it is a fairly simple new api I suspect it will be released with Stable 1.72.
You can play with this now, see using the proposed apis.
Instead of extending TreeItem you will extend TreeItem2 (which extends TreeItem) if you want to use checkboxes. Here is some sample code I put together:
export class TreeTab extends vscode.TreeItem2 {
...
if (tab.isActive) {
this.iconPath = new vscode.ThemeIcon("arrow-small-right", new vscode.ThemeColor("tab.unfocusedActiveBackground"));
this.checkboxState = vscode.TreeItemCheckboxState.Checked;
// this.checkboxState = {state: vscode.TreeItemCheckboxState.Checked, tooltip: "my nifty checkbox tooltip"};
}
...
and elsewhere in your code if you want to detect when that checkbox is clicked/unclicked:
// use your TreeView variable instead of 'tabView'
// from this.tabView = vscode.window.createTreeView(...);
const checkBoxListener = this.tabView.onDidChangeCheckboxState(async event => {
// event = {item: Array(n)}, which TreeItem's checkbox was clicked and its state after clicking:0/1 = on/off
console.log(event);
});

Use of RCP e3-plugins in pure e4-application

I have an existing RCP-project that consists mostly of e3.x plugins. The target-platform is already migrated to Eclipse 4.5 and the compatibility layer is also in use. Currently it is possible to use the new POJO e4view in plugins. That "mixed mode" is working fine. But when I need to extend the existing menu via extensions (org.eclipse.ui.main.menu), I have to use the extension in plugin.xml.
My goal is to create an application-model (Application.e4xmi) and migrate the definitions from the current main-plugin.xml to the new application-model (i.e. handler, menu definitions etc.). New plugins should use the new model-fragment (fragment.e4xmi). It is very important, that I wont modify the existing e3-plugins. Is it possible to migrate the existing main-plugin to pure e4 plugin and add the old e3-plugins without having to change them.
The main goal is to write new e4-plugins (with DI, POJO handler, views etc.) while keeping the e3-plugins.
For example:
Plugin Main(e4):
Application.e3xmi (contains menu definition, handler, commands etc.)
plugin.xml (contains application and product target)
Plugin A(e4):
e4views (POJO)
fragment.e4xmi (contains extentions for new menu-entry)
plugin.xml
Plugin B(legacy e3):
e3 (extends from ViewPart)
plugin.xml (contains extentions for new menu-entry, old handler)
I think at the moment there are only 2 ways to contribute to the application model.
Model Fragment
Model Processor
The second one would be an option for you to contribute your menu entries. The problem is afaik it's only possible with the new model elements and you would need to implement a processor in the e3 plug-ins. My conclusion is that a pure e4 application with e3 contributions is not possible right now but I could be wrong.
Example taken from:
Eclipse4Modularity vogella
// the menu is injected based on the parameter
// defined in the extension point
#Inject
#Named("org.eclipse.ui.file.menu")
private MMenu menu;
MDirectMenuItem menuItem = modelService.createModelElement(MDirectMenuItem.class);
menuItem.setLabel("Another Exit");
menuItem.setContributionURI("bundleclass://"
+ "com.example.e4.rcp.todo.contribute/"
+ ExitHandlerWithCheck.class.getName());
menu.getChildren().add(menuItem);

Use eclipse GMF to create read only diagram

I followed the file system example http://gmfsamples.tuxfamily.org/wiki/doku.php?id=gmf_tutorial1
what I wanted to do is not using the generated editor with its palette.
I created a new plugin with one view and I wanted to create a diagram programatically inside this view to show for instance 2 objects connected with link
I came across this answer GMF display diagram example
but it didn't help me a lot.
in createPartControl of my view I did
#Override
public void createPartControl(Composite parent) {
DiagramGraphicalViewer viewer = new DiagramGraphicalViewer();
viewer.createControl(parent);
RootEditPart root = EditPartService.getInstance().createRootEditPart(diagram);
viewer.setRootEditPart(root);
viewer.setEditPartFactory(new EcoreEditPartProvider());
viewer.getControl().setBackground(ColorConstants.listBackground);
viewer.setContents(diagram);
}
as in the answer but I didn't know how to get that "diagram" variable
The easiest would be use the same GraphicalViewer for your view and the same diagram as well. Just get your DiagramEditPart from the viewer and call disableEditMode() on it. (Do the appropriate type casting if necessary).

Eclipse: Perform an action any time an editor receives input

I'm wondering if it is possible to determine what input was just entered inside of an editor in Eclipse - I'm currently working off of the example JDT editor - and then perform an action based on that input.
e.g.: I have a file example.jav open in my editor window. I push the 'a' key. 'a' would appear in the editor window per normal, but 'a' would also print out to the console.
Obviously the operation I'll be performing will be more complicated than a System.out.println() statement, but if someone could help show me where the change gets detected by the editor itself, I can take it from there.
A few notes:
I'm working in Eclipse 3.7.2 with Java 1.7
If you cannot find the JDT example editor, go to Help > Welcome > Samples and click on "Java Editor".
Thanks in advance!
Figured it out!
As the Editor API is so vast in eclipse that it is difficult to know where to start, I focused on adding a KeyListener to my Shell. Turns out that is slightly problematic in SWT, as when an item inside the Shell gains focus, the Shell itself looses focus. After a bit of searching though, I stumbled across someone else who had the same problem. By adding a filter to the Shell's display, you can add a Listener object which works for the entire application. Such as:
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
Shell shell = window.getShell();
shell.getDisplay().addFilter(SWT.KeyDown, new Listener()
{
public void handleEvent(Event event)
{
System.out.println("" + event.character);
}
});
To further this and only worry about keys pressed in a specific non-widget part (otherwise you could just add a KeyListener to that part) you can add a check to make sure that the currently active part is the same as whatever part you wish to perform the actions for by using a simple if check.
final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
Shell shell = window.getShell();
shell.getDisplay().addFilter(SWT.KeyDown, new Listener()
{
public void handleEvent(Event event)
{
IWorkbenchPage page = window.getActivePage();
IWorkbenchPart part = page.getActivePart();
IEditorPart editor = page.getActiveEditor();
if(part.equals(editor))
{
System.out.println("" + event.character);
}
}
});
Here is hoping that this helps someone else have an easier time than I had finding the solution!

Looking for a Combo(Viewer) in SWT/JFace which supports autocomplete

I'm looking for a Combo(Viewer) in SWT/JFace which supports autocomplete / type-ahead, i.e. the user can enter a couple of characters and the drop down list should show all matching elements.
You can also check out the org.eclipse.jface.fieldassist.AutoCompleteField class. It's not a combo, just a text field, but it adds auto complete functionality as if it were a combo very easily. You can do something as simple as this:
Text textField = new Text(parentComposite, SWT.BORDER);
new AutoCompleteField(textField, new TextContentAdapter(), new String[]
{"autocomplete option 1", "autocomplete option 2"});
I don't think there is anything like this built into either Combo or ComboViewer.
As thehiatus suggests org.eclipse.jface.fieldassist.AutoCompleteField is probably the best place to look for this, however, there is support for Combos:
new AutoCompleteField(combo, new ComboContentAdapter(), new String[]
{"item0", "item1"});
You may be interested in Eclipse's "Content Assist" feature. You can see it in action when using the Eclipse IDE's Java editor. As you edit source code, you will sometimes see a drop-down menu with phrases that complete what you were typing. (Note that you can press Ctrl+Space to force the drop-down menu to be displayed.)
You can implement this in your own SWT/JFace application as well. The "Java Developer's Guide to Eclipse" has an sample application that implements Content Assist. The sample application is a SQL editor, and it is described in Chapter 26, "Building a Custom Text Editor with JFace Text." There's actually an online overview of the chapter here. The sample SQL editor project, com.ibm.jdg2e.editor.jfacetext.sql, can be found here.
On the other hand, if you want to create your own Combo widget and auto-populate it based on input that is being entered, then this might not be very applicable. I'm thinking the org.eclipse.jface.viewers.ComboViewer might be helpful (though I'm not positive).
Check out: http://sourceforge.net/projects/swtaddons/
I use it in my project (with a little tweak).
It's really dead easy to set this up.
As thanks to paz117's comment, thought I'd share the code to make this work:
String[] proposals = new String[controller.model().size()];
for (int i = 0; i < controller.model().size(); i++)
proposals[i] = controller.model().get(i).getAppropriateName();
comboViewer = new ComboViewer(parent, SWT.NONE);
comboViewer.setContentProvider(new ArrayContentProvider());
comboViewer.setLabelProvider(new AppropriateLabelProvider());
comboViewer.setInput(_controller.model());
// additionally, configure the comboViewer arbitrary
new AutoCompleteField(comboViewer.getCombo(), new ComboContentAdapter(), proposals);
The only minor nuisance is that you have to separately populate the model of ComboViewer and AutoCompleteField separately, but that can be at least automated via a static utility method or something similar.
As reference for future visitors, the AutocompleteComboInput (SWT Add-on), can also be a way to achieve this.
Code snippet for screenshot (refer to documentation link above for the code template):
import net.sf.swtaddons.autocomplete.combo.AutocompleteComboInput;
...
subjectCodeCombo = new Combo(tab3Composite, SWT.DROP_DOWN);
// other code modifying Combo appearance here...
// returns a String[] of items retrieved from database
String[] subjectCodeArray = dbQuery.subjectsToArray();
subjectCodeCombo.setItems(subjectCodeArray);
subjectCodeCombo.setText("- SELECT -");
new AutocompleteComboInput(subjectCodeCombo);
The add-on requires all JARs below to be added to the Library: (more info)
eclipse-equinox-common-3.5.0.jar
net.sf.swtaddons_0.1.1_bin_src.jar (sourceforge)
org.eclipse.core.commands.jar
org.eclipse.jface-3.6.0.jar
Click here for JAR pack.