Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2013 itemis AG (http://www.itemis.eu) and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html /
  
  package org.eclipse.xtext.ui.refactoring.impl;
  
 import  org.eclipse.core.runtime.CoreException;
 import  org.eclipse.core.runtime.IProgressMonitor;
 import  org.eclipse.jface.text.BadLocationException;
 import  org.eclipse.jface.text.DocumentRewriteSession;
 import  org.eclipse.jface.text.DocumentRewriteSessionType;
 import  org.eclipse.jface.text.IDocument;
 import  org.eclipse.jface.text.IDocumentExtension4;
 import  org.eclipse.ltk.core.refactoring.Change;
 import  org.eclipse.ltk.core.refactoring.RefactoringStatus;
 import  org.eclipse.ltk.internal.core.refactoring.Changes;
 import  org.eclipse.text.edits.MalformedTreeException;
 import  org.eclipse.text.edits.UndoEdit;
 import  org.eclipse.ui.IEditorInput;
 import  org.eclipse.ui.IEditorPart;
 import  org.eclipse.ui.IWorkbenchPage;
 import  org.eclipse.ui.IWorkbenchPartSite;
 import  org.eclipse.ui.texteditor.ITextEditor;
 
The reverse of an EditorDocumentChange.

Author(s):
Jan Koehnlein - Initial contribution and API
 
 public class EditorDocumentUndoChange extends Change {
 
 	private static final Logger LOG = Logger.getLogger(EditorDocumentUndoChange.class);
 
 	private String name;
 	private UndoEdit undoEdit;
 	private String editorID;
 	private IEditorInput editorInput;
 	private IWorkbenchPage page;
 	private boolean doSave;
 
 	public EditorDocumentUndoChange(String name, ITextEditor editor, UndoEdit undoEditboolean doSave) {
 		this. = name;
 		IWorkbenchPartSite site = editor.getSite();
 		this. = site.getPage();
 		this. = site.getId();
 		this. = editor.getEditorInput();
 		this. = undoEdit;
 		this. = doSave;
 	}
 
 	public String getName() {
 		return ;
 	}
 
 	protected ITextEditor getEditor() {
 		try {
 			IEditorPart editor = .findEditor();
 			if (editor != null && Objects.equal(editor.getSite().getId(), ))
 				return (ITextEditor) editor;
 			else
 				return (ITextEditor) .openEditor();
 		} catch (Exception exc) {
 			.error("Error restoring editor"exc);
 			return null;
 		}
 	}
 
 	public Object getModifiedElement() {
 		return null;
 	}
 
 	public void initializeValidationData(IProgressMonitor pm) {
 	}
 
 	public RefactoringStatus isValid(IProgressMonitor pmthrows CoreException {
 		return new RefactoringStatus();
 	}
 
 	public Change perform(final IProgressMonitor pmthrows CoreException {
 		return new DisplayRunnableWithResult<Change>() {
 			protected Change run() throws Exception {
 				IDocument document = null;
 				try {
 					document = acquireDocument(pm);
 					UndoEdit undo = performEdits(document);
 					commit(documentpm);
					return createUndoChange(undo);
catch (BadLocationException e) {
					throw Changes.asCoreException(e);
catch (MalformedTreeException e) {
					throw Changes.asCoreException(e);
finally {
					releaseDocument(documentpm);
				}
			}
	}
	protected UndoEdit performEdits(IDocument documentthrows BadLocationException, MalformedTreeException {
		DocumentRewriteSession session = null;
		try {
			if (document instanceof IDocumentExtension4) {
				session = ((IDocumentExtension4) document).startRewriteSession(DocumentRewriteSessionType.UNRESTRICTED);
			}
			return .apply(document);
finally {
			if (session != null) {
				((IDocumentExtension4) document).stopRewriteSession(session);
			}
		}
	}
	protected IDocument acquireDocument(final IProgressMonitor pm) {
		return new DisplayRunnableWithResult<IDocument>() {
			protected IDocument run() throws Exception {
				ITextEditor editor = getEditor();
				return editor != null ? editor.getDocumentProvider().getDocument() : null;
			}
	}
	protected void commit(IDocument document, IProgressMonitor pmthrows CoreException {
		if ()
			getEditor().doSave(pm);
	}
	protected void releaseDocument(IDocument document, IProgressMonitor pmthrows CoreException {
	}
	protected Change createUndoChange(UndoEdit edit) {
		return new EditorDocumentUndoChange(getName(), getEditor(), edit);
	}
New to GrepCode? Check out our FAQ X