AJJ
AJJ

Reputation: 3628

Get contents from eclipse viewpart

I am developing a plugin in eclipse using the eclipse ViewPart class. Inside the viewpart i have the styledtext. Consider i have 2 views view_1 and view_2 and both have styledText_1 and styledText_2. For some search function, i need to get the focused styled text content. I tried with below code, but was not successful.

IWorkbenchPage page = PlatformUI.getWorkbench()
                .getActiveWorkbenchWindow().getActivePage();
IWorkBenchPart activePart = page.getActivePart(); // will give the foucsed view part

Both the views are created by same class and has the static styledtext variable say "text".

I tried with

System.out.println(((StyledText)page.getActivePart().getClass().getDeclaredField("text").get(null)).getText());

But this prints the last opened view's text content how can i get the styled text of focused content.

Upvotes: 0

Views: 1168

Answers (1)

Alex K.
Alex K.

Reputation: 3304

You could try to retrieve your own view by id and, then get needed information directly from the view:

IViewPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
            .findView(MyView.ID);
        if (part instanceof MyView) {
            MyView view = (MyView) part;
            StyledText text = view.getStyledText();
        }

Or introduce an interface for both views, which would have a method getStyledText

IViewReference[] references = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences();
        for (IViewReference ref : references) {
            IViewPart view = ref.getView(false);
            if (view instanceof IStyledTextProvider) {
                StyledText text = ((IStyledTextProvider) view).getStyledText();
            }
        }

Upvotes: 2

Related Questions