Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » needs code snippet for ICompilationUnit javadoc promise
needs code snippet for ICompilationUnit javadoc promise [message #188187] Thu, 02 December 2004 10:48 Go to next message
Eclipse UserFriend
Originally posted by: edoardoATenigmatecDOTnet.no.spam

Hi
I previously posted on the same problem but can't find an answer.
I'd need to see a code snippet that does what the following javadox
promises : creating an ICompilationUnit on an not-yet-existing one

my attempts so far fail - to me it seems I need to create the resource
BEFORE I commit.

TIA,
Edo

--------------
package org.eclipse.jdt.core;

public interface ICompilationUnit ...

/**
....
* Since 2.1, a working copy can be created on a not-yet existing
compilation
* unit. In particular, such a working copy can then be committed in
order to create
* the corresponding compilation unit.
*/
void commitWorkingCopy(boolean force, IProgressMonitor monitor) throws
JavaModelException;
Re: needs code snippet for ICompilationUnit javadoc promise [message #188392 is a reply to message #188187] Fri, 03 December 2004 07:12 Go to previous messageGo to next message
Eclipse UserFriend
You can have a look at the Java model test:
org.eclipse.jdt.core.tests.model.WorkingCopyTests#testNonExi stingCu().

HTH,
Jerome

"Edoardo Comar" <edoardoATenigmatecDOTnet@no.spam> wrote in message
news:condg0$n3m$1@www.eclipse.org...
> Hi
> I previously posted on the same problem but can't find an answer.
> I'd need to see a code snippet that does what the following javadox
> promises : creating an ICompilationUnit on an not-yet-existing one
>
> my attempts so far fail - to me it seems I need to create the resource
> BEFORE I commit.
>
> TIA,
> Edo
>
> --------------
> package org.eclipse.jdt.core;
>
> public interface ICompilationUnit ...
>
> /**
> ...
> * Since 2.1, a working copy can be created on a not-yet existing
> compilation
> * unit. In particular, such a working copy can then be committed in
> order to create
> * the corresponding compilation unit.
> */
> void commitWorkingCopy(boolean force, IProgressMonitor monitor) throws
> JavaModelException;
Re: needs code snippet for ICompilationUnit javadoc promise [message #188400 is a reply to message #188392] Fri, 03 December 2004 07:52 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: edoardoATenigmatecDOTnet.no.spam

Thanks Jerome!

can you confirm that there is currently no way to open an editor on such
a compilation unit ?
if you think it would be reasonable to implement such a request, I'll
enter it into bugzilla

My rationale is that my plugin provides an editor for a 'domain object'
which has some details implemented by java classes.
As long as the editor is dirty, ie manipulating a domain object in
memory, I would be happy to keep the ICompilationUnits in memory as well,
and commit (or discard them) all when the editor is saved (or closed
without saving, respectively).

Edo



Jerome Lanneluc wrote:
> You can have a look at the Java model test:
> org.eclipse.jdt.core.tests.model.WorkingCopyTests#testNonExi stingCu().
>
> HTH,
> Jerome
>
> "Edoardo Comar" <edoardoATenigmatecDOTnet@no.spam> wrote in message
> news:condg0$n3m$1@www.eclipse.org...
>
>>Hi
>>I previously posted on the same problem but can't find an answer.
>>I'd need to see a code snippet that does what the following javadox
>>promises : creating an ICompilationUnit on an not-yet-existing one
>>
>>my attempts so far fail - to me it seems I need to create the resource
>>BEFORE I commit.
>>
>>TIA,
>>Edo
>>
>>--------------
>>package org.eclipse.jdt.core;
>>
>>public interface ICompilationUnit ...
>>
>>/**
>>...
>> * Since 2.1, a working copy can be created on a not-yet existing
>>compilation
>> * unit. In particular, such a working copy can then be committed in
>>order to create
>> * the corresponding compilation unit.
>> */
>>void commitWorkingCopy(boolean force, IProgressMonitor monitor) throws
>>JavaModelException;
>
>
>
Re: needs code snippet for ICompilationUnit javadoc promise [message #188417 is a reply to message #188400] Fri, 03 December 2004 08:46 Go to previous messageGo to next message
Eclipse UserFriend
I'm not in the editor business, but I would expect to be able to open an
editor on such a compilation unit.

What makes you say that it is not possible ? Do you get any exception when
attempting to open an editor on such a compilation unit ?

Jerome

"Edoardo Comar" <edoardoATenigmatecDOTnet@no.spam> wrote in message
news:copnhk$ne7$1@www.eclipse.org...
> Thanks Jerome!
>
> can you confirm that there is currently no way to open an editor on such
> a compilation unit ?
> if you think it would be reasonable to implement such a request, I'll
> enter it into bugzilla
>
> My rationale is that my plugin provides an editor for a 'domain object'
> which has some details implemented by java classes.
> As long as the editor is dirty, ie manipulating a domain object in
> memory, I would be happy to keep the ICompilationUnits in memory as well,
> and commit (or discard them) all when the editor is saved (or closed
> without saving, respectively).
>
> Edo
>
>
>
> Jerome Lanneluc wrote:
> > You can have a look at the Java model test:
> > org.eclipse.jdt.core.tests.model.WorkingCopyTests#testNonExi stingCu().
> >
> > HTH,
> > Jerome
> >
> > "Edoardo Comar" <edoardoATenigmatecDOTnet@no.spam> wrote in message
> > news:condg0$n3m$1@www.eclipse.org...
> >
> >>Hi
> >>I previously posted on the same problem but can't find an answer.
> >>I'd need to see a code snippet that does what the following javadox
> >>promises : creating an ICompilationUnit on an not-yet-existing one
> >>
> >>my attempts so far fail - to me it seems I need to create the resource
> >>BEFORE I commit.
> >>
> >>TIA,
> >>Edo
> >>
> >>--------------
> >>package org.eclipse.jdt.core;
> >>
> >>public interface ICompilationUnit ...
> >>
> >>/**
> >>...
> >> * Since 2.1, a working copy can be created on a not-yet existing
> >>compilation
> >> * unit. In particular, such a working copy can then be committed in
> >>order to create
> >> * the corresponding compilation unit.
> >> */
> >>void commitWorkingCopy(boolean force, IProgressMonitor monitor) throws
> >>JavaModelException;
> >
> >
> >
Re: needs code snippet for ICompilationUnit javadoc promise [message #188450 is a reply to message #188417] Fri, 03 December 2004 10:11 Go to previous message
Eclipse UserFriend
Originally posted by: edoardoATenigmatecDOTnet.no.spam

This is a multi-part message in MIME format.
--------------000605070300070901080803
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

The exception is ... "File does not exist" (surprise :-)

here is attached an action that you can declare in an action set and try

I'm submitting the issue to bugzilla
Edo


Jerome Lanneluc wrote:
> I'm not in the editor business, but I would expect to be able to open an
> editor on such a compilation unit.
>
> What makes you say that it is not possible ? Do you get any exception when
> attempting to open an editor on such a compilation unit ?
>
> Jerome
>
> "Edoardo Comar" <edoardoATenigmatecDOTnet@no.spam> wrote in message
> news:copnhk$ne7$1@www.eclipse.org...
>
>>Thanks Jerome!
>>
>>can you confirm that there is currently no way to open an editor on such
>>a compilation unit ?
>>if you think it would be reasonable to implement such a request, I'll
>>enter it into bugzilla
>>
>>My rationale is that my plugin provides an editor for a 'domain object'
>>which has some details implemented by java classes.
>>As long as the editor is dirty, ie manipulating a domain object in
>>memory, I would be happy to keep the ICompilationUnits in memory as well,
>>and commit (or discard them) all when the editor is saved (or closed
>>without saving, respectively).
>>
>>Edo
>>
>>
>>
>>Jerome Lanneluc wrote:
>>
>>>You can have a look at the Java model test:
>>> org.eclipse.jdt.core.tests.model.WorkingCopyTests#testNonExi stingCu().
>>>
>>>HTH,
>>>Jerome
>>>
>>>"Edoardo Comar" <edoardoATenigmatecDOTnet@no.spam> wrote in message
>>>news:condg0$n3m$1@www.eclipse.org...
>>>
>>>
>>>>Hi
>>>>I previously posted on the same problem but can't find an answer.
>>>>I'd need to see a code snippet that does what the following javadox
>>>>promises : creating an ICompilationUnit on an not-yet-existing one
>>>>
>>>>my attempts so far fail - to me it seems I need to create the resource
>>>>BEFORE I commit.
>>>>
>>>>TIA,
>>>>Edo
>>>>
>>>>--------------
>>>>package org.eclipse.jdt.core;
>>>>
>>>>public interface ICompilationUnit ...
>>>>
>>>>/**
>>>>...
>>>> * Since 2.1, a working copy can be created on a not-yet existing
>>>>compilation
>>>> * unit. In particular, such a working copy can then be committed in
>>>>order to create
>>>> * the corresponding compilation unit.
>>>> */
>>>>void commitWorkingCopy(boolean force, IProgressMonitor monitor) throws
>>>>JavaModelException;
>>>
>>>
>>>
>
>


--------------000605070300070901080803
Content-Type: text/x-java;
name="TestActionSetAction2.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="TestActionSetAction2.java"

/*
* @(#)$Id: TestActionSetAction2.java,v 1.2 2004/12/03 09:54:41 edoardo Exp $
* Confidential Licenced Materials of Enigmatec Corporation.
* Copyright Enigmatec Corporation @ 2004
*/
package com.enigmatec.emsj.test.visualinspection;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;

/**
* It creates a new non existing Java ICompilationUnit working copy in the same package as the currently
* open java editor and then attempts to open an editor on it.
* <p>
* This action can be contributed by an action set.
* </p>
* <pre>
* <extension
point="org.eclipse.ui.actionSets">
<actionSet
label="TESTactionSet"
visible="true"
id="visualinspection.actionSets.ID">
<action
label="TASA2"
class=" com.enigmatec.emsj.test.visualinspection.TestActionSetAction 2 "
tooltip="actionSets Visual inspection test2"
toolbarPath="testpath"
id="visualinspection.actionSets.actionTEST.ID2">
</action>
</actionSet>
</extension>

* </pre>
*
* @author Edoardo Comar <edoardo.NOSPAM@NOSPAM.enigmatec.net>
*/
public class TestActionSetAction2 implements IWorkbenchWindowActionDelegate {

private IWorkbenchWindow window;
private ISelection selection;

public TestActionSetAction2() {
super();
}

/**
* the guts of the action
*/
protected void runWithException(IAction action) throws Exception {
IEditorPart editorPart = getWorkbenchWindow().getActivePage().getActiveEditor();
if (editorPart == null) {
return;
}

IEditorInput editorInput = editorPart.getEditorInput();
IResource resource = (IResource) editorInput.getAdapter(IResource.class);
if (resource == null) {
return;
}

ICompilationUnit icu = JavaCore.createCompilationUnitFrom((IFile) resource);
if (icu == null) {
return;
}

IPath icuPath = icu.getPath();
IPath nonExistingCUPath = icuPath.removeLastSegments(1).addTrailingSeparator().append(
"Foo.java");
IFile nonExistingFile = resource.getWorkspace().getRoot().getFile(nonExistingCUPath) ;
System.out.println(nonExistingFile);

ICompilationUnit nonExistingCU = JavaCore.createCompilationUnitFrom(nonExistingFile);
ICompilationUnit workingCopy = nonExistingCU.getWorkingCopy(null);

workingCopy.getBuffer().setContents("public class Foo {\n }\n");

System.out.println(workingCopy);

JavaUI.openInEditor(workingCopy);
}

/*
* (non-Javadoc)
*
* @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action. IAction)
*/
public void run(IAction action) {
try {
runWithException(action);
} catch (Exception e) {

StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String smallStackTrace = sw.toString().substring(0, Math.min(sw.toString().length(), 500));

ErrorDialog.openError(window.getShell(), "ERROR", e.getMessage(), new Status(IStatus.ERROR,
"your-plugin-id", IStatus.ERROR, smallStackTrace, e));
}
}

/* (non-Javadoc)
* @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse. jface.action.IAction, org.eclipse.jface.viewers.ISelection)
*/
public void selectionChanged(IAction action, ISelection selection) {
this.selection = selection;
}


/* (non-Javadoc)
* @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
*/
public void dispose() {
window = null;
selection = null;
}

/* (non-Javadoc)
* @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclip se.ui.IWorkbenchWindow)
*/
public void init(IWorkbenchWindow window) {
this.window = window;
}

protected IWorkbenchWindow getWorkbenchWindow() {
return window;
}

protected ISelection getSelection() {
return selection;
}

}

--------------000605070300070901080803--
Previous Topic:Writing JNI with JDT+CDT
Next Topic:AST to source
Goto Forum:
  


Current Time: Mon Jun 02 16:08:32 EDT 2025

Powered by FUDForum. Page generated in 0.03056 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top