0
votes

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.

1
In what way is your code now working? 'activePart' should be your ViewPart if it is the active part. - greg-449
i tried with this System.out.println(((StyledText)page.getActivePart().getClass().getDeclaredField("text").get(null)).getText()); but his prints text of second view - AJJ
getActivePart definitely returns the current active part - greg-449
thats rite. It returns the active part. from the active part how can i get the content inside the active part??? - AJJ

1 Answers

2
votes

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