Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jdt-ui-dev] Urgent: Java Source Code Manipulation

Hi,
I have extended the JDT by adding a view to it. This view has a list. 
When a member of this list is double clicked, I want to add some source 
code to the currently open Java file in the editor. How do I do this?

As I understand it, I need to get a hold of the workingCopy of the 
ICompilationUnit. But I don't know how to grab it. I know the rest (ie. 
.getBuffer(), .append(), .commitWorkingCopy and .discardWorkingCopy()). 
but how do I get the ICompilationUnit in the first place. The following 
is the code from Eclipse documentation:

/////////////////////////
// Get original compilation unit
    ICompilationUnit originalUnit = ...;
    
    // Get working copy owner
    WorkingCopyOwner owner = ...;
    
    // Create working copy
    ICompilationUnit workingCopy = originalUnit.getWorkingCopy(owner, 
null,     null);
    
    // Modify buffer and reconcile
    IBuffer buffer = ((IOpenable)workingCopy).getBuffer();
    buffer.append("class X {}");
    workingCopy.reconcile(NO_AST, false, null, null);
    
    // Commit changes
    workingCopy.commitWorkingCopy(false, null);
    
    // Destroy working copy
    workingCopy.discardWorkingCopy();

/////////////////////////

What I can't figure out are the first two lines:

1    ICompilationUnit originalUnit = ...;
2    WorkingCopyOwner owner = ...;

What am I supposed to put in for the dots?

Thank you so much, 
I need the answer ASAP. thank you



Back to the top