How to get element by ID in PDE? - java

for example my View has an id
public static final String ID = "examplePlugin.views.SampleView"
How do I get an instance of this View (or any other thing in eclipse ) by id ?

For views you use methods on the current workbench page IWorkbenchPage. You can find the page with:
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
(this must be called from a plugin with the workbench running).
You can find an existing view with:
page.findView(id);
and create and show a view with:
page.showView(id);

Related

Get TreeViewer in RCP from another Plugin

I'm working at a Plugin for a Eclipse-RCP. There is another Plugin with a TreeViewer and I want to select an Item from my Plugin. I don't know how to get access it, is this even possible?
I think can get the correct view with:
IViewReference home;
IViewReference [] viewRefs = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences();
for (int i = 0; i < viewRefs.length; i++) {
if(viewRefs[i].getId()==myid){
home = viewRefs[i];
break;
}
}
But home is not a TreeViewer and I cant cast it. How can I get the TreeViewer?
home.getTreeViewer() //Doesn't work cause of casting issues
I am a newbie to rcp, so I would be nice for some explanation.
You need to find your view using:
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IViewPart viewPart = page.findView("your view id");
You can then cast the view part to your view class and call a method that you write on that class:
if (viewPart != null) {
MyViewClass myViewPart = (MyViewClass)viewPart;
myViewPart.getTreeViewer();
}
where MyViewClass is your ViewPart class. You will have to write the getTreeViewer method.
If the view is not currently open you can use showView:
viewPart = page.showView("your view id");
You have to cast home to the type of your other view. Then you can get the TreeViewer.
You can find the ViewPart directly from your active IWorkbenchPage using IWorkbenchPage#findView(java.lang.String).
If you have the object you want to be selected, get the View's Site, get the Site's selection provider, and then tell the selection provider what should be selected (with a StructuredSelection instance containing the object). This only works if the tree, or whatever is in the part (you shouldn't have to care or know that it's a tree), actually contains the object that you're telling it to select.

How to get a debug view in eclipse?

If I want to get a view by entering the ID using this code,so from where I can get the MyView.ID?
IViewPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
.findView(MyView.ID);
if (part instanceof MyView) {
MyView view = (MyView) part;
You can either find the plugin that contributes the view and look in its 'plugin.xml' to find the org.eclipse.ui.views extension point that defines the view.
Or you can use the Eclipse plugin spy tool to identify the view.
Debug has many views so I'm not sure which one you are asking about. The one labelled 'Debug' has id org.eclipse.debug.ui.DebugView

Liferay - change portlet column programmatically

I'm doing a Liferay portal with different portlets on it in different columns created in a Liferay Layout.
My question is: How can I change programmatically (in java) the column to which a portlet belongs?
I've tried with this:
long userId = themeDisplay.getUserId();
long groupId = themeDisplay.getLayout().getGroupId();
Layout layout = LayoutLocalServiceUtil.getFriendlyURLLayout(groupId, true, currentFriendlyURL);
LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();
layoutTypePortlet.removePortletId(userId, iniPortletName);
String portletInstanceId = layoutTypePortlet.addPortletId(userId, iniPortletName, newColumn, position, true);
LayoutLocalServiceUtil.updateLayout(layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(), layout.getTypeSettings());
It works, but with this code, I delete the portlet and then, I create a new instance, and I don't want that.
How can I update the position of same instance of the portlet?
Thank you.
Ok, I found it.
If you want to keep the same instance, you can use the movePortletId(userId, portletId, newColumn, position) method.
String portletId = (String) request.getAttribute(WebKeys.PORTLET_ID);
layoutTypePortlet.movePortletId(userId, portletId, finColumn, position);
You need to use the portletId instead of portletName, that identifies all the portlets with the same name.
https://docs.liferay.com/portal/6.2/javadocs/src-html/com/liferay/portal/model/LayoutTypePortlet.html
Thank you.

Eclipse (XText) SelectionListener registration

I have implemented a view which shall register itself as listner for changes in the XText editor and related outline. To this end I am adding this line
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().addSelectionListener(this);
in the createPartControl method of the view (which implements the ISelectionListener interface). In the selectionChanged method I therefore check whether the selection is a ITextSelection, the case in which it comes from the XTextEditor, or an IStructuredSelection, the case in which it come sfrom the Outline.
The problem is that, doing so, when I start Eclipse the outline is said to be "unavailable". If I click on the outline it is refreshed, the contents are shown and the listening view correctly updated.
What am I doing wrong and what should I do to avoid the initial "unavailability" of the outline?
I have been recently faced with this exact problem and have solved it by implementing IPartListener2 in the class that extends ViewPart then adding a part listener in the createPartcontrol method like this :
getSite().getWorkbenchWindow().getPartService().addPartListener(this);
Now by using something like this in your partOpened method (that has to be implemented before you can actually use the part listener you will get the view contents to be avaiable initially :
public void partOpened(IWorkbenchPartReference partRef) {
if(partRef.getPage().getActiveEditor() instanceof XtextEditor) {
somepart=partRef.getPage().getActiveEditor();
final XtextEditor editor = (XtextEditor)somepart;
final IXtextDocument document = editor.getDocument();
document.readOnly(new IUnitOfWork.Void<XtextResource>(){
public void process (XtextResource resource) throws Exception {
IParseResult parseResult = resource.getParseResult();
if(parseResult ==null)
return;
CompositeNode rootNode=(CompositeNode) parseResult.getRootNode();
LeafNode node = (LeafNode)NodeModelUtils.findLeafNodeAtOffset(rootNode, 0);
EObject object =NodeModelUtils.findActualSemanticObjectFor(node);
view.setInput(object);
}
});
}
}
this will make the view you're implementing get it's contents when you activate the XtextEditor (that is specific to your DSL).
In order to make the view change contents in real time as you change anything in the file active in the editor you should implement an IDocumentListener and override the DocumentChanged method. If you do this you won't be dependant on the SelectionListener anymore because the view should update automatically when something changes in your document
Hope this helps!

How to get the selected node in the package explorer from an Eclipse plugin

I'm writing an Eclipse command plugin and want to retrieve the currently selected node in the package explorer view. I want to be able to get the absolute filepath, where the selected node resides on the filesystem (i.e. c:\eclipse\test.html), from the returned result.
How do I do this ?
The first step is to get a selection service, e.g. from any view or editor like this:
ISelectionService service = getSite().getWorkbenchWindow()
.getSelectionService();
Or, as VonC wrote, you could get it via the PlatformUI, if you are neither in a view or an editor.
Then, get the selection for the Package Explorer and cast it to an IStructuredSelection:
IStructuredSelection structured = (IStructuredSelection) service
.getSelection("org.eclipse.jdt.ui.PackageExplorer");
From that, you can get your selected IFile:
IFile file = (IFile) structured.getFirstElement();
Now to get the full path, you will have to get the location for the IFile:
IPath path = file.getLocation();
Which you then can finally use to get the real full path to your file (among other things):
System.out.println(path.toPortableString());
You can find more information on the selection service here: Using the Selection Service.
The code would be like:
IWorkbenchWindow window =
PlatformUI.getWorkbench().getActiveWorkbenchWindow();
ISelection selection = window.getSelectionService().getSelection("org.eclipse.jdt.ui.PackageExplorer");
You view an example in an Action like this LuaFileWizardAction class.

Categories