Add icons to elements in ElementListSelectionDialog - eclipse

I have the following code to create a dialog in a RCP Eclipse application using the ElementListSelectionDialog class:
ElementListSelectionDialog dialog = new ElementListSelectionDialog(shell, new LabelProvider());
dialog.setTitle("test");
dialog.setMessage("test");
dialog.setMultipleSelection(false);
dialog.setElements(new String[]{"test1", "test2", "test3"});
dialog.open();
The previous code generates this dialog:
This is fine but I also want to add icons to the elements in the list, similar to how it looks the web.xml editor:

You need to extend the LabelProvider you are passing to the ElementListSelectionDialog constructor and override the
public Image getImage(Object element)
method. This will be called for each of the objects you add to the dialog with the setElements method.

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);
});

Attaching Properties view to a custom XML Editor

I have created a custom editor in eclipse plugin which shows a XML in Tree-Table(TreeViewer) format with couple of its attributes. For showing remaining attributes I am trying to tie it up with "Properties View", but not really able to make progress on it.
I went through similar question on SO like
How to handle property sheet from customized editor in eclipse plugin development? where it talk about make your viewer contribute to workbench selection and implementing an IPropertySource on object which is selected in editor.
In my case I am directly setting an document object in treeviewer input like below.
IFileEditorInput editorInput = (IFileEditorInput) getEditorInput();
IFile inputIFile = editorInput.getFile();
File f = new File(inputIFile.getLocation().toString());
try {
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(f);
}
catch (SAXException | IOException | ParserConfigurationException e) {
e.printStackTrace();
}
//setting root element of doc as input
treeViewer.setInput(doc.getDocumentElement());
Now on what object should I implement an IPropertySource interface to contribute properties?
Let me know if I am going in right direction or missing something or doing it completely wrong.
Hope this make sense !!
When your selection provider fires a selection changed event the properties page will look at the new selection. If you are using a tree viewer as the provider the selection will be the current object from your tree content provider.
The properties view will try to get an IPropertySourceProvider from the selection. Your object can implement IPropertySourceProvider directly, or provide it view the IAdaptable interface or by using IAdapterFactory.
Once the view has the IPropertySourceProvier it will call the getPropertySource method. Your code must return an IPropertySource object - it is up to you to write this class.

How to add action item to the coolbar of e4 eclipse rcp application?

I am currently trying to port my eclipse 3 rcp application to e4.The major hurdle I am facing is to use action item which i was using in e3.In eclipse 3 application i was creating action item of coolbar by extending action.The code was looking like below spinets.
public class Testaction extends Action {
private IWorkbenchWindow window;
public Testaction (IWorkbenchWindow window, String string) {
setText(string);
setToolTipText(string);
setId("ID");
setImageDescriptor(Activator.getImageDescriptor("/icons/some.png"));
this.window = window;
}
#override
public void run() {
/**
Do something
**/
super.run();
}
was adding it to coolbar through
toolbar.add(demoaction);
But with e4 this part seems to be changed and I understand that there we need to have annotation #Execute which will excute the contribution which we will be giving through setcontribuitionuri as below snippet
part.setContributionURI(
"bundleclass://bundle/bundle.contribuitionclass");
I just want to know whether I can use my old action class here or i need to port everything to newer style .
Any help on this will be appreciated.Thanks in advance...
e4 does not support Actions for model elements in the Application.e4xmi.
The simplest conversion is to use a Direct ToolItem in the tool bar. However using a Handled ToolItem with a Command and Handler is more flexible.
In either case the Image, Label and Tooltip are specified in the Application.e4xmi.

Update property view on click of project explorer, eclipse plugin

I have created a custom project in project explorer. Whenever I click on custom project folder currently it shows default property sheet but I want to customize this property sheet.
I have gone through the tabbed property example but I am not able customize it.
Please can anyone provide me some sample examples or code for same.
Thanks.
how to connect that property view to an editor or project explorer
model classes for you custom project and its folders should implement IAdaptable interface and return an object implementing IPropertySource that describes given element. it will be passed to properties view automatically when you click on the element.
alternativelly, you can avoid implementing IAdaptable and create an IAdapterFactory that converts an instance of you project/folder element into corresponding IPropertySoure but then you have to make Eclipse framework aware of your IAdapterFactory implementation.
public class MyProjectAdapterFactory implements IAdapterFactory {
#Override
public Object getAdapter(Object adaptableObject, Class adapterType) {
if (adapterType== IPropertySource.class && adaptableObject instanceof MyProject){
return new MyProjectPropertySource((MyProject) adaptableObject);
}
return null;
}
#Override
public Class[] getAdapterList() {
return new Class[] { IPropertySource.class };
}
}
register it in you plugin.xml file:
<extension point="org.eclipse.core.runtime.adapters">
<factory adaptableType="my.example.MyProject" class="my.example.MyProjectAdapterFactory">
<adapter type="org.eclipse.ui.views.properties.IPropertySource"/>
</factory>
</extension>
look at the full tutorial: http://www.vogella.de/articles/EclipsePlugIn/article.html

Eclipse plugin RCP question

I am creating new Eclipse RCP application. I used "Hello RCP" as template project to create a new project. It works fine. Then I tried adding new menu. I used two extension points "org.eclipse.ui.commands" and "org.eclipse.ui.menu". I created handler class and also defined the menucontribution location uri(). But my menu is not showing up. I strongly suspect my location uri is wrong. But I dont know how to correct it. I have pasted my plugin.xml contents here. Let me know if anyone has a solution. I am following the steps given here http://zenit.senecac.on.ca/wiki/index.php/Add_Menu_to_RCP_Application
You can use ApplicationActionbarAdvisor class to define menus and commands for toolbar as well as menubar.
Firstly you have to declare the commands you want to add in your app like below:
private IWorkbenchAction newAction
Then with the help of ActionFactory class, you have to define the commands in makeActions() method like below:
newAction = ActionFactory.NEW_WIZARD_DROP_DOWN.create(window);
register(newAction);
newAction.setText("New");
Now, after declaration you have to add the commands in menus by using fillMenuBar(IMenuManager menuBar) method :
MenuManager filemenu = new MenuManager("&File", "file");
filemenu.add(newAction);
and if you want to add this command in toolbar, then you have to use following method:
protected void fillCoolBar(ICoolBarManager coolBar) {
IToolBarManager toolbar = new ToolBarManager(coolBar.getStyle());
coolBar.add(toolbar);
toolbar.add(newAction);
Sometimes, you have to use IContributionIem class for declaring commands because all the commands are not in IWorkbenchAction class.
Note: If you are using iContributionItem class then declaration and defination code will be replaced as follws:
IContributionItem show_view;
show_view = ContributionItemFactory.VIEWS_SHORTLIST.create(window);
and rest will be same.