Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7919017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:42:42+00:00 2026-06-03T15:42:42+00:00

For my plugin I’m trying to access the selected code in a CompilationUnitEditor. Therefore

  • 0

For my plugin I’m trying to access the selected code in a CompilationUnitEditor. Therefore I added a contribution to the context menu and use following code:

public class ContextMenuHandler implements IEditorActionDelegate {

    private IEditorPart editorPart;

    @Override
    public void setActiveEditor(IAction action, IEditorPart editorPart) {
        this.editorPart = editorPart;
    }

    @Override
    public void run(IAction action) {
        JavaUI.getEditorInputJavaElement(editorPart.getEditorInput());
    }

    @Override
    public void selectionChanged(IAction action, ISelection selection) {
        if (selection instanceof TextSelection) {
            TextSelection text = (TextSelection) selection;
            System.out.println("Text: " + text.getText());
        } else {
            System.out.println(selection);
        }
    }

}

Now the problem is that the method selectionChanged(…) is only called when i really select something so that I could copy/paste it. But I want to access the Code elements that are highlighted like this (here I would like to get the “IEditorPart”)

enter image description here

Unfortunately, I have no idea what I should look for.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T15:42:46+00:00Added an answer on June 3, 2026 at 3:42 pm

    Using the inputs from the other answers, I ended up with the following solution:

    @Override
    public void setActiveEditor(IAction action, IEditorPart editorPart) {
        ((CompilationUnitEditor) editorPart).getViewer().addTextListener(new ITextListener() {
    
            @Override
            public void textChanged(TextEvent event) {
                selectedText = event.getText();
            }
        });
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following plugin: https://github.com/Marak/jquery.emoticon.js How does the author expect you to use the
My plugin which adds a menu in admin page has two files. The code
A plugin I use on my site generates an array in this style: <script
I use jQuery Validation plugin to ensure what user entered positive cost_of_car in first
Has anybody written a plugin for outlook web access? Are there any resources to
I use maven release plugin. In my pom exists and Ant task that automatically
I have a plugin that I am trying to grab the the number of
Is there a VS2008 plugin for code metrics for C++? I have Team System
I added the filtering plugin to my Dojox Enhanced Grid. Now I would like
I am using a plugin named : jkey . Now i want to use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.