0

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.

AJJ
  • 3,570
  • 7
  • 43
  • 76
  • In what way is your code now working? 'activePart' should be your `ViewPart` if it is the active part. – greg-449 Nov 25 '13 at 15:51
  • i tried with this System.out.println(((StyledText)page.getActivePart().getClass().getDeclaredField("text").get(null)).getText()); but his prints text of second view – AJJ Nov 26 '13 at 05:10
  • getActivePart definitely returns the current active part – greg-449 Nov 26 '13 at 07:45
  • thats rite. It returns the active part. from the active part how can i get the content inside the active part??? – AJJ Nov 26 '13 at 07:52

1 Answers1

2

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();
            }
        }
Alex K.
  • 3,294
  • 4
  • 29
  • 41
  • If both the views are drawn from same view class then how can I get the contents??!!! – AJJ Nov 26 '13 at 08:07
  • What do you mean "views are drawn from the same view"? – Alex K. Nov 26 '13 at 08:13
  • I have one view class named TestView.java created by extending ViewPart. In this i will have a styledText to display the contents and I have a right click menu to create a duplicate view of TestView.java. This duplicate view will have different contents in styled text. In short, one view class with two different contents in styled text of the view class. How do i get the desired styled text from a view. – AJJ Nov 26 '13 at 08:21
  • 1
    So, why can't you use PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart() to get an active part, cast it to your view and then retrieve styledText, using getter? – Alex K. Nov 26 '13 at 08:30