Skip to main content

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

Greatly appreciate your help :-)

Sten-Erik Bergner wrote:

You might want to try something like

1    ICompilationUnit originalUnit = type.getCompilationUnit();
2    WorkingCopyOwner owner = DefaultWorkingCopyOwner.PRIMARY;

If you're wondering about the 'type' variable, I've solved it like this in a
view that I implemented:

IType type = findInitialInputByJavaEditor();

where

protected IType findInitialInputByJavaEditor () {
	IEditorPart editor =
		(IEditorPart)getSite().getPage().getActiveEditor();
	if (! (editor instanceof JavaEditor)) return null;
	IType answer = null;
	try {
		IType[] types =
((ICompilationUnit)((JavaEditor)editor).getViewPartInput()).getTypes();
		answer = types.length == 1 ? types[0] : null;}
	catch (Exception e) {/* Never mind */}
	return answer;}

Another thing is that I'm not sure about the getWorkingCopy(...) part in
your sample code. As of 3.0 I have to use findWorkingCopy and I get the
impression that the documentation is not up to date.
Please note that I'm not an inside JDT developer and it would be great if
someone who is would care to comment and perhaps set us straight.

Sten-Erik Bergner
se.bergner@xxxxxxxxxx


-----Original Message-----
Date: Thu, 09 Sep 2004 15:13:03 -0400 (EDT)
From: Nima Bahrami Farid <nbfarid@xxxxxxxxxxxxxxxxxxx>
To: jdt-ui-dev@xxxxxxxxxxx
Subject: [jdt-ui-dev] Urgent: Java Source Code Manipulation
Reply-To: jdt-ui-dev@xxxxxxxxxxx

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



_______________________________________________
jdt-ui-dev mailing list
jdt-ui-dev@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/jdt-ui-dev



Back to the top