Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2010 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 static org.eclipse.ltk.core.refactoring.RefactoringStatus.*;
 
 
 import  org.eclipse.core.resources.IFile;
 import  org.eclipse.core.runtime.IPath;
 import  org.eclipse.jface.text.IDocument;
 import  org.eclipse.ltk.core.refactoring.Change;
 import  org.eclipse.ltk.core.refactoring.TextFileChange;
 import  org.eclipse.text.edits.TextEdit;
 import  org.eclipse.ui.IEditorPart;
 import  org.eclipse.ui.IFileEditorInput;
 import  org.eclipse.ui.IWorkbench;
 import  org.eclipse.ui.IWorkbenchPage;
 import  org.eclipse.ui.IWorkbenchWindow;
 import  org.eclipse.ui.part.FileEditorInput;
 import  org.eclipse.ui.texteditor.ITextEditor;
 import  org.eclipse.ui.texteditor.ITextEditorExtension;
 
Unifies the creation of Changes from TextEdits on IFiles and IDocuments.

Author(s):
Jan Koehnlein - Initial contribution and API
 
 
 	@Inject(optional = true)
 	private IWorkbench workbench;
 
 
 
 	
 	
 	protected IFileEditorInput getEditorInput(URI resourceURIStatusWrapper status) {
 		IFile file = .findFileStorage(resourceURItrue);
 		if (file == null) {
 			status.add(ERROR, "No suitable storage found for resource {0}."resourceURI);
 			return null;
 		}
 		return new FileEditorInput(file);
 	}
 
 	public IRefactoringDocument get(URI urifinal StatusWrapper status) {
 		URI resourceURI = uri.trimFragment();
 		final IFileEditorInput fileEditorInput = getEditorInput(resourceURIstatus);
 		if (fileEditorInput != null) {
 			IFile file = fileEditorInput.getFile();
 			IPath redirectedPath = .getRedirectedPath(file.getFullPath());
 			IFile redirectedFile = file.getWorkspace().getRoot().getFile(redirectedPath);
 			if(redirectedFile.equals(file)) {
 				ITextEditor editor = new DisplayRunnableWithResult<ITextEditor>() {
 					protected ITextEditor run() throws Exception {
 						IWorkbenchWindow activeWorkbenchWindow = .getActiveWorkbenchWindow();
 						IWorkbenchPage activePage = activeWorkbenchWindow.getActivePage();
 						IEditorPart editor = activePage.findEditor(fileEditorInput);
 						if (editor instanceof ITextEditor) {
 							if (editor instanceof ITextEditorExtension
 									&& ((ITextEditorExtension) editor).isEditorInputReadOnly())
 								status.add(ERROR, "Editor for {0} is read only"fileEditorInput.getName());
 							return ((ITextEditor) editor);
 						}
 						return null;
 					}
 				}.syncExec();
 				if (editor != null) {
 					IDocument document = editor.getDocumentProvider().getDocument(fileEditorInput);
 					if (document != null)
 						return new EditorDocument(resourceURIeditordocument
 							.isSaveAllBeforeRefactoring() || !editor.isDirty());
 				}
 				return new FileDocument(resourceURIfilegetEncodingProvider(resourceURI));
else {
				return new RedirectedFileDocument(resourceURIfileredirectedFilegetEncodingProvider(resourceURI));
			}
		}
		return null;
	}
	protected IEncodingProvider getEncodingProvider(URI resourceURI) {
				resourceURIIEncodingProvider.class);
	}
	}
	public void setChangeRedirector(IChangeRedirector changeRedirector) {
		this.  = changeRedirector;
	}
	public static abstract class AbstractRefactoringDocument implements IRefactoringDocument {
		private URI resourceURI;
		public AbstractRefactoringDocument(URI resourceURI) {
			this. = resourceURI;
		}
		public abstract Change createChange(String name, TextEdit textEdit);
		public URI getURI() {
			return ;
		}
		public boolean equals(Object obj) {
		}
		public int hashCode() {
		}
	}
	public static class EditorDocument extends AbstractRefactoringDocument {
		private IDocument document;
		private ITextEditor editor;
		private boolean doSave;
		public EditorDocument(URI resourceURI, ITextEditor editor, IDocument documentboolean doSave) {
			super(resourceURI);
			this. = editor;
			this. = document;
			this. = doSave;
		}
		public ITextEditor getEditor() {
			return ;
		}
		public IDocument getDocument() {
			return ;
		}
		public boolean isDoSave() {
			return ;
		}
		public Change createChange(String name, TextEdit textEdit) {
			documentChange.setEdit(textEdit);
			documentChange.setTextType(getURI().fileExtension());
			return documentChange;
		}
		protected String getName() {
			buffer.append(" - ");
			buffer.append(getURI().segment(1));
			for (int i = 2; i < getURI().segmentCount() - 1; ++i) {
				buffer.append("/");
				buffer.append(getURI().segment(i));
			}
			return buffer.toString();
		}
			return .get();
		}
	}
	public static class FileDocument extends AbstractRefactoringDocument {
		private IFile file;
		public FileDocument(URI resourceURI, IFile fileIEncodingProvider encodingProvider) {
			super(resourceURI);
			this. = file;
			this. = encodingProvider;
		}
		public IFile getFile() {
			return ;
		}
		public Change createChange(String name, TextEdit textEdit) {
			TextFileChange textFileChange = new TextFileChange(name);
			textFileChange.setSaveMode(TextFileChange.FORCE_SAVE);
			textFileChange.setEdit(textEdit);
			textFileChange.setTextType(getURI().fileExtension());
			return textFileChange;
		}
			try {
				InputStream inputStream = .getContents();
				try {
					String charset = ( != null) ? .getEncoding(getURI()) : 
							.getCharset();
					InputStreamReader input = new InputStreamReader(inputStreamcharset);
					final char[] buffer = new char[4096];
					StringBuilder output = new StringBuilder(4096);
					int read;
					while ((read = input.read(buffer, 0, buffer.length)) != -1) {
						output.append(buffer, 0, read);
					}
					return output.toString();
finally {
					inputStream.close();
				}
catch (Exception e) {
				throw new WrappedException(e);
			}
		}
	}
	public static class RedirectedFileDocument extends FileDocument {
		private IFile redirectedFile;
		public RedirectedFileDocument(URI resourceURI, IFile file, IFile redirectedFile
				IEncodingProvider encodingProvider) {
			super(resourceURIfileencodingProvider);
			this. = redirectedFile;
		}
		public Change createChange(String name, TextEdit textEdit) {
			TextFileChange textFileChange = new TextFileChange(name);
			textFileChange.setSaveMode(TextFileChange.FORCE_SAVE);
			textFileChange.setEdit(textEdit);
			textFileChange.setTextType(getURI().fileExtension());
			return textFileChange;
		}
	}
New to GrepCode? Check out our FAQ X