001: /**
002: *
003: */package core;
004:
005: import org.eclipse.core.filebuffers.FileBuffers;
006: import org.eclipse.core.filebuffers.ITextFileBuffer;
007: import org.eclipse.core.filebuffers.ITextFileBufferManager;
008: import org.eclipse.core.runtime.CoreException;
009: import org.eclipse.core.runtime.IPath;
010: import org.eclipse.jdt.core.dom.CompilationUnit;
011: import org.eclipse.jface.text.BadLocationException;
012: import org.eclipse.jface.text.IDocument;
013: import org.eclipse.text.edits.MalformedTreeException;
014: import org.eclipse.text.edits.TextEdit;
015:
016: /**
017: * Helper class used to write the performed changes back to Java source.
018: *
019: * @author sh
020: */
021: public class ManipulateHelper {
022:
023: /**
024: * To avoid writing thow save methods, this interface hast been created.
025: * The implementing classes provide a TextEdit object.
026: *
027: * @author sh
028: *
029: */
030: public interface TextEditProvider {
031: /**
032: * Provides a TextEdit document.
033: *
034: * @param document the document the TextEdit object will be applied to
035: * @return the TextEdit instance
036: */
037: TextEdit getTextEdit(IDocument document);
038: }
039:
040: /**
041: * Convenience method that saves changes that have been made directly to an AST.
042: *
043: * @param unit the unit that contains changes.
044: */
045: public static void saveDirectlyModifiedUnit(
046: final CompilationUnit unit) {
047: save(unit, new TextEditProvider() {
048: public TextEdit getTextEdit(IDocument document) {
049: return unit.rewrite(document, null);
050: }
051: });
052: }
053:
054: /**
055: * Writes changes to Java source. This method does not distinguish how the
056: * AST was changed.
057: *
058: * @param unit the AST node of the compilation unit that has been changed
059: * @param textEditProvider provides the change information
060: * @param textEditProvider
061: */
062: private static void save(CompilationUnit unit,
063: TextEditProvider textEditProvider) {
064: ITextFileBufferManager bufferManager = FileBuffers
065: .getTextFileBufferManager();
066: IPath path = unit.getJavaElement().getPath();
067: try {
068: // connect to path
069: bufferManager.connect(path, null);
070:
071: ITextFileBuffer textFileBuffer = bufferManager
072: .getTextFileBuffer(path);
073:
074: // retrieve the buffer
075: IDocument document = textFileBuffer.getDocument();
076:
077: // ask the textEditProvider for the change information
078: TextEdit edit = textEditProvider.getTextEdit(document);
079:
080: // apply the changes to the document
081: edit.apply(document);
082:
083: // write the changes from the buffer to the file
084: textFileBuffer
085: .commit(null /* ProgressMonitor */, false /* Overwrite */);
086: } catch (MalformedTreeException e) {
087: e.printStackTrace();
088: } catch (BadLocationException e) {
089: e.printStackTrace();
090: } catch (CoreException e) {
091: e.printStackTrace();
092: } finally {
093: try {
094: bufferManager.disconnect(path, null);
095: } catch (CoreException e) {
096: e.printStackTrace();
097: }
098: }
099: }
100: }
|