简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM