1 11 package org.eclipse.jdt.internal.ui.compare; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.Map ; 15 16 import org.eclipse.core.runtime.Assert; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IPath; 19 import org.eclipse.core.runtime.IStatus; 20 21 import org.eclipse.core.filebuffers.FileBuffers; 22 import org.eclipse.core.filebuffers.ITextFileBuffer; 23 import org.eclipse.core.filebuffers.ITextFileBufferManager; 24 import org.eclipse.core.filebuffers.LocationKind; 25 26 import org.eclipse.core.resources.IFile; 27 28 import org.eclipse.swt.widgets.Shell; 29 30 import org.eclipse.jface.dialogs.MessageDialog; 31 import org.eclipse.jface.viewers.ISelection; 32 33 import org.eclipse.jface.text.IDocument; 34 import org.eclipse.jface.text.TextUtilities; 35 36 import org.eclipse.compare.CompareConfiguration; 37 import org.eclipse.compare.CompareUI; 38 import org.eclipse.compare.IStreamContentAccessor; 39 import org.eclipse.compare.ITypedElement; 40 41 import org.eclipse.team.core.TeamException; 42 43 import org.eclipse.team.ui.history.ElementLocalHistoryPageSource; 44 import org.eclipse.team.ui.history.HistoryPageCompareEditorInput; 45 46 import org.eclipse.jdt.core.ICompilationUnit; 47 import org.eclipse.jdt.core.IJavaProject; 48 import org.eclipse.jdt.core.IMember; 49 import org.eclipse.jdt.core.ISourceRange; 50 import org.eclipse.jdt.core.JavaModelException; 51 import org.eclipse.jdt.core.dom.ASTNode; 52 import org.eclipse.jdt.core.dom.AnnotationTypeDeclaration; 53 import org.eclipse.jdt.core.dom.BodyDeclaration; 54 import org.eclipse.jdt.core.dom.CompilationUnit; 55 import org.eclipse.jdt.core.dom.EnumDeclaration; 56 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; 57 58 import org.eclipse.jdt.internal.corext.SourceRange; 59 import org.eclipse.jdt.internal.corext.dom.ASTNodes; 60 import org.eclipse.jdt.internal.corext.dom.NodeFinder; 61 import org.eclipse.jdt.internal.corext.util.Resources; 62 63 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 64 import org.eclipse.jdt.internal.ui.JavaPlugin; 65 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 66 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 67 68 69 72 class JavaReplaceWithEditionActionImpl extends JavaHistoryActionImpl { 73 74 protected boolean fPrevious= false; 75 76 JavaReplaceWithEditionActionImpl(boolean previous) { 77 super(true); 78 fPrevious= previous; 79 } 80 81 public void run(ISelection selection) { 82 83 Shell shell= getShell(); 84 85 final IMember input= getEditionElement(selection); 86 if (input == null) { 87 MessageDialog.openInformation(shell, CompareMessages.ReplaceFromHistory_title, CompareMessages.ReplaceFromHistory_invalidSelectionMessage); 88 return; 89 } 90 91 final IFile file= getFile(input); 92 if (file == null) { 93 showError(); 94 return; 95 } 96 97 IStatus status= Resources.makeCommittable(file, shell); 98 if (!status.isOK()) { 99 return; 100 } 101 102 if (fPrevious) { 103 String errorTitle= CompareMessages.ReplaceFromHistory_title; 104 String errorMessage= CompareMessages.ReplaceFromHistory_internalErrorMessage; 105 try { 106 ITypedElement ti = ElementLocalHistoryPageSource.getPreviousEdition(file, input); 107 if (ti == null) { 108 MessageDialog.openInformation(shell, errorTitle, CompareMessages.ReplaceFromHistory_parsingErrorMessage); 109 return; 110 } 111 replace(input, file, ti); 112 } catch (TeamException e) { 113 ExceptionHandler.handle(e, shell, errorTitle, errorMessage); 114 } 115 } else { 116 JavaElementHistoryPageSource pageSource = JavaElementHistoryPageSource.getInstance(); 117 CompareConfiguration cc = new CompareConfiguration(); 118 cc.setLeftEditable(false); 119 cc.setRightEditable(false); 120 HistoryPageCompareEditorInput ci = new HistoryPageCompareEditorInput(cc, pageSource, input) { 121 protected void performReplace(Object selectedElement) { 122 if (selectedElement instanceof ITypedElement) { 123 JavaReplaceWithEditionActionImpl.this.replace(input, file, (ITypedElement)selectedElement); 124 } 125 } 126 }; 127 ci.setReplace(true); 128 ci.setTitle(CompareMessages.JavaReplaceWithEditionActionImpl_0); 129 ci.setHelpContextId(IJavaHelpContextIds.REPLACE_ELEMENT_WITH_HISTORY_DIALOG); 130 CompareUI.openCompareDialog(ci); 131 } 132 } 133 134 public void replace(IMember input, IFile file, ITypedElement element) { 135 136 Shell shell= getShell(); 137 138 String errorTitle= CompareMessages.ReplaceFromHistory_title; 139 String errorMessage= CompareMessages.ReplaceFromHistory_internalErrorMessage; 140 141 IPath path= file.getFullPath(); 143 ITextFileBufferManager bufferManager= FileBuffers.getTextFileBufferManager(); 144 ITextFileBuffer textFileBuffer= null; 145 try { 146 bufferManager.connect(path, LocationKind.IFILE, null); 147 textFileBuffer= bufferManager.getTextFileBuffer(path, LocationKind.IFILE); 148 IDocument document= textFileBuffer.getDocument(); 149 performReplace(input, file, textFileBuffer, document, element); 150 } catch(InvocationTargetException ex) { 151 ExceptionHandler.handle(ex, shell, errorTitle, errorMessage); 152 153 } catch(InterruptedException ex) { 154 Assert.isTrue(false); 156 157 } catch(CoreException ex) { 158 ExceptionHandler.handle(ex, shell, errorTitle, errorMessage); 159 160 } finally { 161 try { 162 if (textFileBuffer != null) 163 bufferManager.disconnect(path, LocationKind.IFILE, null); 164 } catch (CoreException e) { 165 JavaPlugin.log(e); 166 } 167 } 168 } 169 170 private void performReplace(IMember input, IFile file, 171 ITextFileBuffer textFileBuffer, IDocument document, ITypedElement ti) 172 throws CoreException, JavaModelException, 173 InvocationTargetException , InterruptedException { 174 175 if (ti instanceof IStreamContentAccessor) { 176 177 boolean inEditor= beingEdited(file); 178 179 String content= JavaCompareUtilities.readString((IStreamContentAccessor)ti); 180 String newContent= trimTextBlock(content, TextUtilities.getDefaultLineDelimiter(document), input.getJavaProject()); 181 if (newContent == null) { 182 showError(); 183 return; 184 } 185 186 ICompilationUnit compilationUnit= input.getCompilationUnit(); 187 CompilationUnit root= parsePartialCompilationUnit(compilationUnit); 188 189 190 final ISourceRange nameRange= input.getNameRange(); 191 int length= nameRange.getLength(); 193 if (length < 0) 194 length= 1; 195 ASTNode node2= NodeFinder.perform(root, new SourceRange(nameRange.getOffset(), length)); 196 ASTNode node= ASTNodes.getParent(node2, BodyDeclaration.class); 197 if (node == null) 198 node= ASTNodes.getParent(node2, AnnotationTypeDeclaration.class); 199 if (node == null) 200 node= ASTNodes.getParent(node2, EnumDeclaration.class); 201 202 if (node == null) { 204 showError(); 205 return; 206 } 207 208 ASTRewrite rewriter= ASTRewrite.create(root.getAST()); 209 rewriter.replace(node, rewriter.createStringPlaceholder(newContent, node.getNodeType()), null); 210 211 if (inEditor) { 212 JavaEditor je= getEditor(file); 213 if (je != null) 214 je.setFocus(); 215 } 216 217 Map options= null; 218 IJavaProject javaProject= compilationUnit.getJavaProject(); 219 if (javaProject != null) 220 options= javaProject.getOptions(true); 221 applyChanges(rewriter, document, textFileBuffer, getShell(), inEditor, options); 222 223 } 224 } 225 226 private void showError() { 227 MessageDialog.openError(getShell(), CompareMessages.ReplaceFromHistory_title, CompareMessages.ReplaceFromHistory_internalErrorMessage); 228 } 229 } 230 | Popular Tags |