Lines 29-36
Link Here
|
29 |
import org.eclipse.jdt.internal.compiler.util.SuffixConstants; |
29 |
import org.eclipse.jdt.internal.compiler.util.SuffixConstants; |
30 |
import org.eclipse.jdt.internal.core.util.Messages; |
30 |
import org.eclipse.jdt.internal.core.util.Messages; |
31 |
import org.eclipse.jdt.internal.core.util.Util; |
31 |
import org.eclipse.jdt.internal.core.util.Util; |
32 |
import org.eclipse.jface.text.BadLocationException; |
|
|
33 |
import org.eclipse.jface.text.IDocument; |
34 |
import org.eclipse.text.edits.TextEdit; |
32 |
import org.eclipse.text.edits.TextEdit; |
35 |
|
33 |
|
36 |
/** |
34 |
/** |
Lines 228-234
Link Here
|
228 |
private void processCompilationUnitResource(ICompilationUnit source, PackageFragment dest) throws JavaModelException { |
226 |
private void processCompilationUnitResource(ICompilationUnit source, PackageFragment dest) throws JavaModelException { |
229 |
String newCUName = getNewNameFor(source); |
227 |
String newCUName = getNewNameFor(source); |
230 |
String destName = (newCUName != null) ? newCUName : source.getElementName(); |
228 |
String destName = (newCUName != null) ? newCUName : source.getElementName(); |
231 |
ASTRewrite rewrite = updateContent(source, dest, newCUName); // null if unchanged |
229 |
TextEdit edit = updateContent(source, dest, newCUName); // null if unchanged |
232 |
|
230 |
|
233 |
// TODO (frederic) remove when bug 67606 will be fixed (bug 67823) |
231 |
// TODO (frederic) remove when bug 67606 will be fixed (bug 67823) |
234 |
// store encoding (fix bug 66898) |
232 |
// store encoding (fix bug 66898) |
Lines 265-271
Link Here
|
265 |
flags |= IResource.KEEP_HISTORY; |
263 |
flags |= IResource.KEEP_HISTORY; |
266 |
sourceResource.move(destFile.getFullPath(), flags, getSubProgressMonitor(1)); |
264 |
sourceResource.move(destFile.getFullPath(), flags, getSubProgressMonitor(1)); |
267 |
} else { |
265 |
} else { |
268 |
if (rewrite != null) flags |= IResource.KEEP_HISTORY; |
266 |
if (edit != null) flags |= IResource.KEEP_HISTORY; |
269 |
sourceResource.copy(destFile.getFullPath(), flags, getSubProgressMonitor(1)); |
267 |
sourceResource.copy(destFile.getFullPath(), flags, getSubProgressMonitor(1)); |
270 |
} |
268 |
} |
271 |
setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE); |
269 |
setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE); |
Lines 279-288
Link Here
|
279 |
} |
277 |
} |
280 |
|
278 |
|
281 |
// update new resource content |
279 |
// update new resource content |
282 |
if (rewrite != null){ |
280 |
if (edit != null){ |
283 |
boolean wasReadOnly = destFile.isReadOnly(); |
281 |
boolean wasReadOnly = destFile.isReadOnly(); |
284 |
try { |
282 |
try { |
285 |
saveContent(dest, destName, rewrite, sourceEncoding, destFile); |
283 |
saveContent(dest, destName, edit, sourceEncoding, destFile); |
286 |
} catch (CoreException e) { |
284 |
} catch (CoreException e) { |
287 |
if (e instanceof JavaModelException) throw (JavaModelException) e; |
285 |
if (e instanceof JavaModelException) throw (JavaModelException) e; |
288 |
throw new JavaModelException(e); |
286 |
throw new JavaModelException(e); |
Lines 308-315
Link Here
|
308 |
// update new resource content |
306 |
// update new resource content |
309 |
// in case we do a saveas on the same resource we have to simply update the contents |
307 |
// in case we do a saveas on the same resource we have to simply update the contents |
310 |
// see http://dev.eclipse.org/bugs/show_bug.cgi?id=9351 |
308 |
// see http://dev.eclipse.org/bugs/show_bug.cgi?id=9351 |
311 |
if (rewrite != null){ |
309 |
if (edit != null){ |
312 |
saveContent(dest, destName, rewrite, sourceEncoding, destFile); |
310 |
saveContent(dest, destName, edit, sourceEncoding, destFile); |
313 |
} |
311 |
} |
314 |
} |
312 |
} |
315 |
} |
313 |
} |
Lines 462-474
Link Here
|
462 |
AST ast = astCU.getAST(); |
460 |
AST ast = astCU.getAST(); |
463 |
ASTRewrite rewrite = ASTRewrite.create(ast); |
461 |
ASTRewrite rewrite = ASTRewrite.create(ast); |
464 |
updatePackageStatement(astCU, newFragName, rewrite); |
462 |
updatePackageStatement(astCU, newFragName, rewrite); |
465 |
IDocument document = getDocument(cu); |
463 |
TextEdit edits = rewrite.rewriteAST(); |
466 |
TextEdit edits = rewrite.rewriteAST(document, null); |
464 |
cu.applyTextEdit(edits, this.progressMonitor); |
467 |
try { |
|
|
468 |
edits.apply(document); |
469 |
} catch (BadLocationException e) { |
470 |
throw new JavaModelException(e, IJavaModelStatusConstants.INVALID_CONTENTS); |
471 |
} |
472 |
cu.save(null, false); |
465 |
cu.save(null, false); |
473 |
} |
466 |
} |
474 |
} |
467 |
} |
Lines 523-529
Link Here
|
523 |
throw new JavaModelException(ce); |
516 |
throw new JavaModelException(ce); |
524 |
} |
517 |
} |
525 |
} |
518 |
} |
526 |
private void saveContent(PackageFragment dest, String destName, ASTRewrite rewrite, String sourceEncoding, IFile destFile) throws JavaModelException { |
519 |
private void saveContent(PackageFragment dest, String destName, TextEdit edits, String sourceEncoding, IFile destFile) throws JavaModelException { |
527 |
try { |
520 |
try { |
528 |
// TODO (frederic) remove when bug 67606 will be fixed (bug 67823) |
521 |
// TODO (frederic) remove when bug 67606 will be fixed (bug 67823) |
529 |
// fix bug 66898 |
522 |
// fix bug 66898 |
Lines 534-550
Link Here
|
534 |
// use no encoding |
527 |
// use no encoding |
535 |
} |
528 |
} |
536 |
// when the file was copied, its read-only flag was preserved -> temporary set it to false |
529 |
// when the file was copied, its read-only flag was preserved -> temporary set it to false |
537 |
// note this doesn't interfer with repository providers as this is a new resource that cannot be under |
530 |
// note this doesn't interfere with repository providers as this is a new resource that cannot be under |
538 |
// version control yet |
531 |
// version control yet |
539 |
Util.setReadOnly(destFile, false); |
532 |
Util.setReadOnly(destFile, false); |
540 |
ICompilationUnit destCU = dest.getCompilationUnit(destName); |
533 |
ICompilationUnit destCU = dest.getCompilationUnit(destName); |
541 |
IDocument document = getDocument(destCU); |
534 |
destCU.applyTextEdit(edits, this.progressMonitor); |
542 |
TextEdit edits = rewrite.rewriteAST(document, null); |
|
|
543 |
try { |
544 |
edits.apply(document); |
545 |
} catch (BadLocationException e) { |
546 |
throw new JavaModelException(e, IJavaModelStatusConstants.INVALID_CONTENTS); |
547 |
} |
548 |
destCU.save(getSubProgressMonitor(1), this.force); |
535 |
destCU.save(getSubProgressMonitor(1), this.force); |
549 |
} |
536 |
} |
550 |
/** |
537 |
/** |
Lines 553-559
Link Here
|
553 |
* |
540 |
* |
554 |
* @return an AST rewrite or null if no rewrite needed |
541 |
* @return an AST rewrite or null if no rewrite needed |
555 |
*/ |
542 |
*/ |
556 |
private ASTRewrite updateContent(ICompilationUnit cu, PackageFragment dest, String newName) throws JavaModelException { |
543 |
private TextEdit updateContent(ICompilationUnit cu, PackageFragment dest, String newName) throws JavaModelException { |
557 |
String[] currPackageName = ((PackageFragment) cu.getParent()).names; |
544 |
String[] currPackageName = ((PackageFragment) cu.getParent()).names; |
558 |
String[] destPackageName = dest.names; |
545 |
String[] destPackageName = dest.names; |
559 |
if (Util.equalArraysOrNull(currPackageName, destPackageName) && newName == null) { |
546 |
if (Util.equalArraysOrNull(currPackageName, destPackageName) && newName == null) { |
Lines 567-573
Link Here
|
567 |
ASTRewrite rewrite = ASTRewrite.create(ast); |
554 |
ASTRewrite rewrite = ASTRewrite.create(ast); |
568 |
updateTypeName(cu, astCU, cu.getElementName(), newName, rewrite); |
555 |
updateTypeName(cu, astCU, cu.getElementName(), newName, rewrite); |
569 |
updatePackageStatement(astCU, destPackageName, rewrite); |
556 |
updatePackageStatement(astCU, destPackageName, rewrite); |
570 |
return rewrite; |
557 |
return rewrite.rewriteAST(); |
571 |
} |
558 |
} |
572 |
} |
559 |
} |
573 |
private void updatePackageStatement(CompilationUnit astCU, String[] pkgName, ASTRewrite rewriter) throws JavaModelException { |
560 |
private void updatePackageStatement(CompilationUnit astCU, String[] pkgName, ASTRewrite rewriter) throws JavaModelException { |