This commit is contained in:
Alexey Kudravtsev
2013-10-04 15:43:43 +04:00
parent aaa74f2523
commit b28887a8e1
8 changed files with 96 additions and 43 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -37,6 +37,7 @@ import java.nio.charset.IllegalCharsetNameException;
* Represents bytes as content. May has text representaion.
*/
public class BinaryContent extends DiffContent {
@NotNull
private final FileType myFileType;
private final byte[] myBytes;
private final Charset myCharset;
@@ -44,8 +45,6 @@ public class BinaryContent extends DiffContent {
private String myFilePath;
/**
*
* @param bytes
* @param charset use to convert bytes to String. null means bytes can't be converted to text.
* Has no sense if fileType.isBinary()
* @param fileType type of content
@@ -66,6 +65,7 @@ public class BinaryContent extends DiffContent {
myFilePath = filePath;
}
@Override
@SuppressWarnings({"EmptyCatchBlock"})
@Nullable
public Document getDocument() {
@@ -98,10 +98,12 @@ public class BinaryContent extends DiffContent {
/**
* @return null
*/
@Override
public OpenFileDescriptor getOpenFileDescriptor(int offset) {
return null;
}
@Override
@Nullable
public VirtualFile getFile() {
if (myFileType instanceof UIBasedFileType) {
@@ -115,15 +117,18 @@ public class BinaryContent extends DiffContent {
return null;
}
@Nullable
@Override
@NotNull
public FileType getContentType() {
return myFileType;
}
@Override
public byte[] getBytes() throws IOException {
return myBytes;
}
@Override
public boolean isBinary() {
return myCharset == null;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -123,8 +123,6 @@ public abstract class DiffContent {
/**
* Creates DiffContent associated with given file.
*
* @param project
* @param file
* @return content associated with file
*/
public static FileContent fromFile(Project project, VirtualFile file) {
@@ -134,8 +132,6 @@ public abstract class DiffContent {
/**
* Creates DiffContent associated with given document
*
* @param project
* @param document
* @return content associated with document
*/
public static DocumentContent fromDocument(Project project, Document document) {
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -28,9 +28,9 @@ import org.jetbrains.annotations.Nullable;
public class DocumentContent extends DiffContent {
private final Document myDocument;
private final VirtualFile myFile;
private final FileType myOverridenType;
private Project myProject;
private FileDocumentManager myDocumentManager;
private final FileType myOverriddenType;
private final Project myProject;
private final FileDocumentManager myDocumentManager;
public DocumentContent(Project project, Document document) {
this(project, document, null);
@@ -41,21 +41,24 @@ public class DocumentContent extends DiffContent {
myDocument = document;
myDocumentManager = FileDocumentManager.getInstance();
myFile = myDocumentManager.getFile(document);
myOverridenType = type;
myOverriddenType = type;
}
public DocumentContent(Document document) {
public DocumentContent(@NotNull Document document) {
this(null, document, null);
}
public DocumentContent(Document document, FileType type) {
public DocumentContent(@NotNull Document document, @NotNull FileType type) {
this(null, document, type);
}
@Override
@NotNull
public Document getDocument() {
return myDocument;
}
@Override
public OpenFileDescriptor getOpenFileDescriptor(int offset) {
VirtualFile file = getFile();
if (file == null) return null;
@@ -63,15 +66,18 @@ public class DocumentContent extends DiffContent {
return new OpenFileDescriptor(myProject, file, offset);
}
@Override
public VirtualFile getFile() {
return myFile;
}
@Override
@Nullable
public FileType getContentType() {
return myOverridenType == null ? DiffContentUtil.getContentType(getFile()) : myOverridenType;
return myOverriddenType == null ? DiffContentUtil.getContentType(getFile()) : myOverriddenType;
}
@Override
public byte[] getBytes() {
return myDocument.getText().getBytes();
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -70,6 +70,7 @@ abstract class DocumentsSynchronizer {
protected abstract Document createOriginal();
@NotNull
protected abstract Document createCopy();
protected void replaceString(@NotNull final Document document, final int startOffset, final int endOffset, @NotNull final String newText) {
@@ -110,7 +111,7 @@ abstract class DocumentsSynchronizer {
private void startListen() {
final Document original = getOriginal();
final Document copy = getCopy();
if (original == null || copy == null) return;
if (original == null) return;
beforeListenersAttached(original, copy);
original.addDocumentListener(myOriginalListener);
@@ -138,6 +139,7 @@ abstract class DocumentsSynchronizer {
return myOriginal;
}
@NotNull
public Document getCopy() {
if (myCopy == null) myCopy = createCopy();
return myCopy;
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -44,6 +44,7 @@ public class FileContent extends DiffContent {
myType = detectType(file);
}
@Override
public Document getDocument() {
if (myDocument == null && DiffContentUtil.isTextFile(myFile)) {
myDocument = FileDocumentManager.getInstance().getDocument(myFile);
@@ -51,26 +52,31 @@ public class FileContent extends DiffContent {
return myDocument;
}
@Override
public OpenFileDescriptor getOpenFileDescriptor(int offset) {
return new OpenFileDescriptor(myProject, myFile, offset);
}
@Override
@NotNull
public VirtualFile getFile() {
return myFile;
}
@Override
@Nullable
public FileType getContentType() {
FileType type = myFile.getFileType();
return isUnknown(type) ? myType : type;
}
@Override
public byte[] getBytes() throws IOException {
if (myFile.isDirectory()) return null;
return myFile.contentsToByteArray();
}
@Override
public boolean isBinary() {
if (myFile.isDirectory()) return false;
if (myType != null && !myType.isBinary()) {
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -57,7 +57,7 @@ public class FragmentContent extends DiffContent {
this(original, range, project, (FileType)null);
}
private String subText(Document document, int startOffset, int length) {
private static String subText(Document document, int startOffset, int length) {
return document.getCharsSequence().subSequence(startOffset, startOffset + length).toString();
}
@@ -70,6 +70,7 @@ public class FragmentContent extends DiffContent {
}
@Override
@NotNull
public Document getDocument() {
return mySynchonizer.getCopy();
}
@@ -125,12 +126,12 @@ public class FragmentContent extends DiffContent {
@Override
protected void beforeListenersAttached(@NotNull Document original, @NotNull Document copy) {
boolean writable = copy.isWritable();
if (!writable) {
boolean readOnly = !copy.isWritable();
if (readOnly) {
copy.setReadOnly(false);
}
replaceString(copy, 0, copy.getTextLength(), subText(original, myRangeMarker.getStartOffset(), getLength()));
copy.setReadOnly(!writable);
copy.setReadOnly(readOnly);
}
private int getLength() {
@@ -142,6 +143,7 @@ public class FragmentContent extends DiffContent {
return myRangeMarker.getDocument();
}
@NotNull
@Override
protected Document createCopy() {
final Document originalDocument = myRangeMarker.getDocument();
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -89,6 +89,7 @@ public class SimpleContent extends DiffContent {
return myLineSeparators.restoreText(myDocument.getText());
}
@Override
public Document getDocument() {
return myDocument;
}
@@ -96,6 +97,7 @@ public class SimpleContent extends DiffContent {
/**
* @return null
*/
@Override
public OpenFileDescriptor getOpenFileDescriptor(int offset) {
return null;
}
@@ -103,10 +105,12 @@ public class SimpleContent extends DiffContent {
/**
* @return null
*/
@Override
public VirtualFile getFile() {
return null;
}
@Override
@Nullable
public FileType getContentType() {
return myType;
@@ -115,6 +119,7 @@ public class SimpleContent extends DiffContent {
/**
* @return Encodes using default encoding
*/
@Override
public byte[] getBytes() {
String currentText = getText();
if (myOriginalText.equals(myDocument.getText()) && myCharset == null) {
@@ -132,9 +137,7 @@ public class SimpleContent extends DiffContent {
buffer.get(result, bomLength, encodedLength);
return result;
}
else {
return currentText.getBytes();
}
return currentText.getBytes();
}
@NotNull
@@ -154,7 +157,6 @@ public class SimpleContent extends DiffContent {
/**
* @param text text of content
* @param fileName used to determine content type
* @return
*/
public static SimpleContent forFileContent(String text, String fileName) {
FileType fileType;
@@ -212,7 +214,7 @@ public class SimpleContent extends DiffContent {
LineTokenizer lineTokenizer = new LineTokenizer(text);
String[] lines = lineTokenizer.execute();
mySeparator = lineTokenizer.getLineSeparator();
LOG.assertTrue(mySeparator == null || mySeparator.length() > 0);
LOG.assertTrue(mySeparator == null || !mySeparator.isEmpty());
if (mySeparator == null) mySeparator = SystemProperties.getLineSeparator();
return LineTokenizer.concatLines(lines);
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -89,14 +89,31 @@ public class MergeRequestImpl extends MergeRequest {
myDiffContents[2] = right;
}
@Override
@NotNull
public DiffContent[] getContents() { return myDiffContents; }
public DiffContent[] getContents() {
return myDiffContents;
}
public String[] getContentTitles() { return myVersionTitles; }
public void setVersionTitles(String[] versionTitles) { myVersionTitles = versionTitles; }
@Override
public String[] getContentTitles() {
return myVersionTitles;
}
public String getWindowTitle() { return myWindowTitle; }
public void setWindowTitle(String windowTitle) { myWindowTitle = windowTitle; }
@Override
public void setVersionTitles(String[] versionTitles) {
myVersionTitles = versionTitles;
}
@Override
public String getWindowTitle() {
return myWindowTitle;
}
@Override
public void setWindowTitle(String windowTitle) {
myWindowTitle = windowTitle;
}
public void setResult(int result) {
if (result == DialogWrapper.OK_EXIT_CODE) applyChanges();
@@ -110,21 +127,26 @@ public class MergeRequestImpl extends MergeRequest {
}
}
public int getResult() { return myResult; }
@Override
public int getResult() {
return myResult;
}
@Nullable
private MergeContent getMergeContent() {
if (myDiffContents [1] instanceof MergeContent) {
if (myDiffContents[1] instanceof MergeContent) {
return (MergeContent)myDiffContents[1];
}
return null;
}
@Override
@Nullable
public DiffContent getResultContent() {
return getMergeContent();
}
@Override
public void restoreOriginalContent() {
final MergeContent mergeContent = getMergeContent();
if (mergeContent == null) return;
@@ -163,6 +185,7 @@ public class MergeRequestImpl extends MergeRequest {
configureAction(builder, builder.getOkAction(), myOkButtonPresentation);
builder.setOkOperation(new Runnable() {
@Override
public void run() {
myOkButtonPresentation.run(builder.getDialogWrapper());
}
@@ -176,6 +199,7 @@ public class MergeRequestImpl extends MergeRequest {
configureAction(builder, builder.getCancelAction(), myCancelButtonPresentation);
builder.setCancelOperation(new Runnable() {
@Override
public void run() {
myCancelButtonPresentation.run(builder.getDialogWrapper());
}
@@ -191,6 +215,7 @@ public class MergeRequestImpl extends MergeRequest {
return myHelpId;
}
@Override
public void setHelpId(@Nullable @NonNls String helpId) {
myHelpId = helpId;
}
@@ -211,23 +236,30 @@ public class MergeRequestImpl extends MergeRequest {
myTarget.applyText(myWorkingDocument.getText(), myProject);
}
public Document getDocument() { return myWorkingDocument; }
@Override
public Document getDocument() {
return myWorkingDocument;
}
@Override
public OpenFileDescriptor getOpenFileDescriptor(int offset) {
VirtualFile file = getFile();
if (file == null) return null;
return new OpenFileDescriptor(myProject, file, offset);
}
@Override
public VirtualFile getFile() {
return myTarget.getFile();
}
@Override
@Nullable
public FileType getContentType() {
return myTarget.getContentType();
}
@Override
public byte[] getBytes() throws IOException {
return myTarget.getBytes();
}
@@ -250,6 +282,7 @@ public class MergeRequestImpl extends MergeRequest {
changeCounter.addListener(this);
}
@Override
public void run() {
if (myWasInvoked) return;
if (!getWholePanel().isDisplayable()) return;
@@ -257,9 +290,9 @@ public class MergeRequestImpl extends MergeRequest {
ChangeCounter.getOrCreate(myMergePanel.getMergeList()).removeListener(this);
int doApply = Messages
.showOkCancelDialog(getWholePanel(), DiffBundle.message("merge.all.changes.have.processed.save.and.finish.confirmation.text"),
DiffBundle.message("all.changes.processed.dialog.title"),
DiffBundle.message("merge.save.and.finish.button"), DiffBundle.message("merge.continue.button"),
Messages.getQuestionIcon());
DiffBundle.message("all.changes.processed.dialog.title"),
DiffBundle.message("merge.save.and.finish.button"), DiffBundle.message("merge.continue.button"),
Messages.getQuestionIcon());
if (doApply != 0) return;
myDialogWrapper.close(DialogWrapper.OK_EXIT_CODE);
}
@@ -268,6 +301,7 @@ public class MergeRequestImpl extends MergeRequest {
return myMergePanel.getComponent();
}
@Override
public void onCountersChanged(ChangeCounter counter) {
if (myWasInvoked) return;
if (counter.getChangeCounter() != 0 || counter.getConflictCounter() != 0) return;