[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[news.eclipse.tools] Re: plugin needs to send events to the workbench?

Your code looks fine, although some parts are not necessary. This should be sufficient:

ByteArrayOutputStream ostr = new ByteArrayOutputStream();
parser.prettify( currentFile.getContents(), ostr );
InputStream newStream = new ByteArrayInputStream( ostr.toByteArray());
currentFile.setContents( newStream, false, true, null);

I.e., you don't need to close the newStream, since the contract of setContents says it will always be closed. The refreshLocal is definitely not need here, since you're only using the IFile API to modify the file. A couple more questions:

- You didn't show the exception handling, but I assume get/setContents aren't throwing any exceptions?

- What does the deletion exception look like? If you catch the exception in the debugger, can you see any more details?

- if you replace parser.prettify with a simple method that copies from one stream to the other, do you still get the exception? I wonder if there's still a possible code path in prettify that does not close the stream. It might be better to do it like this:

InputStream in = currentFile.getContents();
try {
    parser.prettify(in, ostr);
} finally {
    in.close();
}

--


Eric Cifreo wrote:
"John Arthorne" <John_Arthorne@xxxxxxxx> wrote in message

news:<3E9DCD1A.4090201@xxxxxxxx>...


If you're using IFile.getContents and IFile.setContents, then no other


notification is required. If it's reporting that resources are out of


sync, this suggests that you've modified the file on disk without using


Eclipse API (for example, using java.io.FileOutputStream). If you've


done this, you can let Eclipse know by caling IFile.refreshLocal().


Another possibility is that you're not properly cleaning up somewhere.


Note that it is your responsibility to close the stream returned by


IFile.getContents(), otherwise you're leaving a file descriptor open.


--


Thanks for the response John. I was not closing that InputStream before,

but I am now and still getting the "Unable to delete" problem. It's a minor

annoyance since most often I won't be deleting files from my projects, but

I'd still like to see it work cleanly.

Here's the body of the run method in case anyone else can spot what the

problem is (I'm sure the formatting will be horrible):



/**

* @see IActionDelegate#run(IAction)

*

* @param action

*/

public void run( IAction action )

{

EclipsePlugin plugin = EclipsePlugin.getDefault();

IDialogSettings settings = plugin.getDialogSettings();

String styleLocation = null;

IndenterParser parser = null;

try

{

styleLocation = settings.get( STYLE_FILE );

IndenterProperties props = new IndenterProperties( styleLocation );

parser = new IndenterParser( props );

IFileEditorInput input = ( IFileEditorInput )editor_.getEditorInput();

IFile currentFile = input.getFile();


// If the file needs it, save it before formatting.

if ( editor_.isDirty() )

{

editor_.doSave( new NullProgressMonitor() );

}


ByteArrayOutputStream ostr = new ByteArrayOutputStream();

parser.prettify( currentFile.getContents(), ostr );

InputStream newStream = new ByteArrayInputStream( ostr.toByteArray()

);

ostr.flush();


currentFile.setContents( newStream, false, true, new

NullProgressMonitor() );


// clean up

currentFile.refreshLocal( IResource.DEPTH_ONE, new

NullProgressMonitor() );

newStream.close();

ostr.close();

}

catch( CoreException core )

{

handleException( core );


return;

}

catch( IndenterException ie )

{

handleException( ie );

// Don't blow away settings, the file may just be badly formatted.

// settings.put( STYLE_FILE, "" );

return;

}

catch( FileNotFoundException fnfe )

{

handleException( fnfe );

settings.put( STYLE_FILE, "" );

return;

}

catch( IOException ioe )

{

handleException( ioe );


return;

}

}

--Eric