How can I only display projects inside a treeviewer eclipse plugin? - eclipse

I am making a tree viewer in Eclipse which would be used to pick a project and then I would find out the location of the project and zip it up.
I can currently display a tree which shows all the projects but it also allows you to expand the tree.
I am doing this in a wizard so I am unable to any dialogs.
I think I would need a filter but after using Google for a while I was unable to figure out how I could do this.
This is how I am currently making the viewer.
TreeViewer view = new TreeViewer(composite,new WorkbenchLabelProvider(),new BaseWorkbenchContentProvider());
view.setInput(ResourcesPlugin.getWorkspace().getRoot());

It's showing you exactly what that content provider is written to show.
The short answer is to setInput(ResourcesPlugin.getWorkspace().getRoot().getProjects()) and use the ArrayContentProvider with a ListViewer rather than the TableViewer.
The long answer is that the content provider you were using returns both the top level elements for the tree control using getElements() and any resource's children via getChildren(), and your case is not interested in the results of getChildren().

Projects are never nested so you only really need a TableViewer to show them. You can get the list of projects using:
IProject [] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
The same label provider will work.

Related

Filtering contents in Eclipse Common Navigator Framework view

I am developing a 3.x based Eclipse RCP application. In the part of application, I am implementing Common-navigator plugin of Eclipse itself, in order to display resources in the workspace. I'have created the navigator view shown below:
But I would like display only one tree child element. More specifically, I only want clause folder and its elements to be shown.
What is the accurate way to do it?
Add dependecy of org.eclipse.ui.navigator if not exists in plugin.xml.
Add extension point org.eclipse.ui.navigator.navigatorContent in extension tab.
Create CommonFilter under that and provide your values to the properties on the right.
Create a class which extends 'org.eclipse.jface.viewers.ViewerFilter' and implement you logic in overridden public boolean select(Viewer viewer, Object parentElement, Object element) (Note : return true would retain the resource in Navigator otherwise it will be hidden).
Configure this extended class in extension for class property in CommonFilter.
And you are good to go for testing.
BTW, this way is adding common filter to across all the Navigator. If you need to configure for particular navigator then you need to get its view and then get viewer out of it and attach your filter to viewer. To achieve this you may need a trigger point e.g., a menu/button/startup extension!

How to add an Eclipse CompareEditor inside a View?

How can I open an Eclipse CompareEditor inside a View?
More specifically, I want the view to contain a SashForm. In the left sash I put some information about the commit, and in the right sash I want to put a compare editor that compares the commit to its parent. Is there such a thing even possible?
I've succeeded in using a GitCompareEditorInput from eGit and the CompareUI class to open a separate compare editor or a dialog with the compare editor (that compares a certain commit to its parent). But now I am interested in putting that compare editor inside another view, as detailed above.
Thanks
Update:
I have tried CompareEditorInput.createContents(compositeParent) but I only get two empty rectangles.
The key to hosting a CompareEditorInput class appears to be providing a class which implements org.eclipse.compare.ICompareContainer. Once you have this you connect it to the input using CompareEditorInput.setContainer(container).
org.eclipse.compare.internal.CompareContainer provides a partial implementation of the container but this is internal so should only be used as a guide rather than used directly.

Use hyperlink for tree objects

Can i use hyperlink to my treeobject. I have four nodes in my tree. i want each each node to be hyperlinked. Is this possible in Eclipse Rcp? Help is needed. Sample code will be useful. I have used the rcp mail Template.
You can make your custom tree node and store the link as a data object in the tree node. Then add a selection listener to the tree. When any of the tree node is selected you will get an event from which you can extract the data object and use the information to open a new view or anything elese that you want to do.

How to get the screen dimension of a eclipse view and IJavaElement

I writed an eclipse plugin that visualize some Java classes as UML in a view part. You choose a package in the Package Explorer and after rigth click on the package you can visualized it by clicking on a self created command menuitem in the menu. The visualization is created with the prefuse library.
My next goal was to make it possible to use it for multitouch. I use the Multitouch Library from PQLabs multitouch SDK. Now, on a multitouch screen, I can move my UML model, resize it etc.
But I was not able to get the informations from the Package Explorer. I tried to find out the location of the Package Explorer and IJavaElements on the screen but failed. My idea was to compare the x and y points from my finger (which I get from the PQL Labs SDK) with the x and y coordinates from the IJavaElements. I failed from the start, I could not be able that a touch point recognizes if it is in the border of the Package Explorer view or not. The only thing I got was the bounds of the whole Display. I searched the internet and stackoverflow but did not find something that was useful.
I do not know if it is possible or not but I will appreciate when somebody can give me information or directed me to good links.
I don't know much about the Package Explorer, but with the Project Explorer (Common Navigator) (which you should also address), there is a TreeViewer associated with it (in this case the class is CommonViewer). You can get that by doing CommonNavigator.getCommonViewer(). Once you have the TreeViewer, you can then get to the underlying SWT Tree and from there use the standard SWT methods to get the position of the tree relative to the enclosing window. There are also methods where you can get the bounds of a tree cell. Have a look at the SWT Snippets (Google it) to help you work with the Tree.
For the Package Explorer it will be similar; you will have to look at the source code.
As described in Francis' answer you should be able to get the TreeViewer, and TreeViewer#getControl() would provide the underlying control.
When you fetch the position and size of a control via control.getBounds(), those would be relative to the coordinates of the shell/window. If you need the absolute display/screen coordinates, see control.toDisplay(some_x, some_y)
I want to share my steps on how I solved my problem. After the advices I digged deeper into the jdt Java infrastructure of the Java IDE. To get the TreeViewer of the Package Explorer I had to cast the "PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView("org.eclipse.jdt.ui.PackageExplorer")" into a "PackageExplorerPart" which is in the "org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart". The further steps I did were:
treeViewer = packageExplorerPart.getTreeViewer();
tree = (Tree) treeViewer.getControl();
After that I created a "Point" with the coordinates x and y, which were passed by touching the screen. But, the point location were representing the whole screen and to get the correct coordinates for the Package Explorer I converted the point with the method tree.toControl(...). At last, I checked if an item was given with method tree.getItem(....).

Toolbar items dynamically

I need to create dynamically buttons in main toolbar. I found a solution, but I can create just one button (dynamic contribution item - class extending ContributionItem). But I need to create more than one button, but I cannot find the solution.
I'm fighting with task to create plugin, which parses a XML file containing structure of menu and toolbars. We've already done this plugin for Visual Studio. Its quite easy in principle, but I found swiftly, that not for Eclipse. There is one small but critical otherness. Plugins are implemented declaratively in Eclipse. The file plugin.xml is the gist of plugin's infrastructure, Java code is just ancillary.
The customer wants to refresh the menu and toolbar whenever the selected project is changed. Eclipse lacks several features needed to get the task done. Main menu and main toolbar are cteated at Eclipse's start-up and then they can be hardly rebuilt.
In the most cases the conditions defined at enabledWhen/visibleWhen elements are sufficient to filter contributions according to the context (active part, selected object, whatever else).
If you need to have more freedom, please try E4 ToolControl that allows you to implement your own UI elements:
#PostConstruct
public void createControls(Composite parent) {
//your custom code here
}
More details here https://www.vogella.com/tutorials/EclipseRCP/article.html#toolcontrols
From my understanding you want to have different buttons on the main toolbar depending on the selection of the project explorer (eg. 1 project is java project, the other is javascript etc.). First you will have to contribute to the main toolbar. I think there are some tutorial available so google will help.
The main steps are:
1. create a command (org.eclipse.ui.commmands)
2. create a handler (org.eclipse.ui.handlers) with the previously declared command id
3. contribute to the main toolbar (org.eclipse.ui.menus) with menucontribution and commandId with the following locationURI: toolbar:org.eclipse.ui.main.toolbar?after=misc
showing/hiding, enabling/disabling a menu item/button also can be done declaratively or "mixed". Declaratively means eg. using enabledWhen/visibleWhen...
Mixed means using property tester (org.eclipse.core.expressions.propertyTester). With this you can define your "enablement logic" in Java code.
In Eclipse e4 the UI is generated from a, EMF based, model. The Application.e4xmi serves as a base for that model. Contributions to the model can be done via fragments, which are again XML, or via processors. Processors are written in Java and use e4 services, like the part service, to modify the model at runtime.
I think you want to write a processor that parses your custom XML and modifies the eclipse e4 model accordingly.