Get TreeViewer in RCP from another Plugin - java

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.

Related

Espresso perform click on view in Recyclerview item

I tried everything to perform click on view in recyclerview item but not succesfull. Read every option on internet and tried it but still not working. Lately I used:
onView(ViewMatchers.withId(R.id.live_rw_liveMatchList))
.perform(RecyclerViewActions.actionOnItemAtPosition(2, MyViewAction.clickChildViewWithId(R.id.pick_pw_pickLeft)));
But all i get is:
android.support.test.espresso.AmbiguousViewMatcherException: 'with id:
hr.psk.android:id/ticket_list_list' matches multiple views in the
hierarchy. Problem views are marked with '****MATCHES****' below.
And it matches only one view (my recyclerview with 13 childs)
I know this should work but it is not working in my project. Tried to perform only click on recyclerviw item in other recyclerview to make it more simple like this:
onView(withId(R.id.ticket_list_list))
.perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
But it gives me the same message back -> Multiple matches problem
somebody help, I really tried everything
onView(withId(R.id.ticket_list_list))
.perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
The error you're getting means that there's another View somewhere that also uses the id ticket_list_list
Is ticket_list_list your actual list view? (the parent of the things that you want to click)
If it is then is it the only one available on the screen? if it is then use the following
onView(allOf(withId(R.id.ticket_list_list), isDisplayed()))
.perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
Adding the isDisplayed() Matcher makes the targeted View more specific by targeting only those that are displayed and ignoring those that are existing but not displayed
If there really is another matcher displayed (or the above doesn't work for you) that has the same id as the one you're trying to match then use the following
onView(allOf(withId(R.id.ticket_list_list), withParentIndex(index_of_matcher_here)))
.perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
Instead of isDisplayed() withParentIndex(some_number) should more specifically target and give you the view that has the id you're looking for along with the index of it
In the very messy instance that the above still doesn't work, maybe because all the returned Views have the same index possibly because they are the children of another View then you can get creative and do something like
onView(allOf(withId(R.id.ticket_list_list), withParent(allOf(withId(if_of_the_parent), withParentIndex(index_of_matcher_here)))))
.perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
to specify the index of the parent instead.
Just mix 'n match your way through it, just make sure to not go crazy on the Matchers and just keep things readable
Use as little as you can (make sure it's readable and maintainable too and not just made up of indexes), you're not trying to get the coordinates for a missile target- you just want the View
I also face the same problem before few days but finally got the solutions from StackOverflow.
If you want to click recycler custom item view click then implement the following code in your project
You need to write this code in java this will help you a lot and saved your time
ClickOnButtonView is control a view action happened in the list item and
UiController class helps you find custom row item
The code is written in Kotlin
#RunWith(AndroidJUnit4::class)
class CampaignFragmentTest {
#get: Rule
val activityTestRule = ActivityTestRule(TestActivity::class.java)
#Test
fun testCustomListClick() {
clickOnButtonAtRow(0)
}
}
private fun clickOnButtonAtRow(position: Int) {
Espresso.onView(ViewMatchers.withId(R.id.list)).perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>
(position, ClickOnButtonView()))
}
inner class ClickOnButtonView : ViewAction {
internal var click = ViewActions.click()
override fun getConstraints(): Matcher<View> {
return click.constraints
}
override fun getDescription(): String {
return " click on custom button view"
}
override fun perform(uiController: UiController, view: View) {
//btnClickMe -> Custom row item view button
click.perform(uiController, view.findViewById(R.id.btnClickMe))
}
}
That error means that it found multiple views with the id R.id.ticket_list_list. There should also be a view hierarchy log that follows the error message you provided, showing the views that Espresso found with that same id marked with ****MATCHES****.
So you can either change the id of the view you want to test or target the view by providing another ViewMatcher as below:
onView(allOf(withId(recyclerViewID), viewMatcher))
.perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
You Can also create your own custom click action like this :
fun customActionClickOnItemEvent(
#NonNull targetViewId: Int
): ViewAction {
return object : ViewAction {
val click = ViewActions.click()
override fun getDescription(): String = "Item clicked"
override fun getConstraints(): Matcher<View> = click.constraints
override fun perform(uiController: UiController?, view: View?) {
click.perform(uiController,view?.findViewById(targetViewId))
}
}
}
And Calling this function like this :
onView(withId(R.id.rv_products)).perform(
RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(
1,
customActionClickOnItemEvent(R.id.img_wish_list)
)
)

How to get element by ID in PDE?

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

Adding JFace wizard pages after addPages() was called?

i am trying to create a jface wizard.
In my wizard i have my "startpage". The options i choose in my "startpage" depending on how many pages will follow. But in my opinion its not possible to do that. Because the addPages() method getting called after the wizard was started. The addPage() method is private. But i need to add my pages there, because when i do it somewhere else, the createControl(Composite parent) don't getting called.
Is there any solution how to solve that problem?
I thought about writing a own method sth. like this:
public void addNewPage() {
Page page = new Page("pagename");
page.createControl(parent);
page.setDescription("");}
...
But it doesn't work.
Do you guys have any solution for my problem?
You could add all your pages in the wizard addPages and then override getNextPage to control which pages is displayed when Next is pressed.
If that is not enough you can always write your own implementation of the IWizard interface.
You can do so by overriding org.eclipse.jface.wizard.Wizard.getNextPage to return a new page if conditions are met (conditionForMorePages in the snippet below):
#Override
public IWizardPage getNextPage() {
IWizardPage nextPage = super.getNextPage(page);
if (nextPage == null) {
if (conditionForMorePages){
// we need an additional page.
IWizardPage nextPage = new MyAdditionalPage();
}
}
return nextPage;
}
If your wizard start with only one page, "back" and "next" buttons do not appear by default. If there is a chance you have more steps coming up dynamically, you want to display the navigation buttons. You can do so by setting the proper flag using the API
public void setForcePreviousAndNextButtons(boolean b)

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