diff --git a/platform/platform-impl/src/com/intellij/openapi/diff/impl/CompositeDiffPanel.java b/platform/platform-impl/src/com/intellij/openapi/diff/impl/CompositeDiffPanel.java index 64b7ee6ffc6a..21c583219108 100644 --- a/platform/platform-impl/src/com/intellij/openapi/diff/impl/CompositeDiffPanel.java +++ b/platform/platform-impl/src/com/intellij/openapi/diff/impl/CompositeDiffPanel.java @@ -103,11 +103,10 @@ public class CompositeDiffPanel implements DiffViewer { } if (myMap.isEmpty()) { - final EmptyDiffViewer emptyDiffViewer = new EmptyDiffViewer(); - emptyDiffViewer.setDiffRequest(request); - myMap.put(FICTIVE_KEY, emptyDiffViewer); - final Content content = myUi.createContent(FICTIVE_KEY, emptyDiffViewer.getComponent(), FICTIVE_KEY, null, - emptyDiffViewer.getPreferredFocusedComponent()); + final ErrorDiffViewer errorDiffViewer = new ErrorDiffViewer(request); + myMap.put(FICTIVE_KEY, errorDiffViewer); + final Content content = myUi.createContent(FICTIVE_KEY, errorDiffViewer.getComponent(), FICTIVE_KEY, null, + errorDiffViewer.getPreferredFocusedComponent()); content.setCloseable(false); content.setPinned(true); content.setDisposer(myParentDisposable); diff --git a/platform/platform-impl/src/com/intellij/openapi/diff/impl/DiffPanelImpl.java b/platform/platform-impl/src/com/intellij/openapi/diff/impl/DiffPanelImpl.java index 62c73e2c76de..e421492bf272 100644 --- a/platform/platform-impl/src/com/intellij/openapi/diff/impl/DiffPanelImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/diff/impl/DiffPanelImpl.java @@ -140,7 +140,7 @@ public class DiffPanelImpl implements DiffPanelEx, ContentChangeListener, TwoSid private final Project myProject; private final boolean myIsHorizontal; private final DiffTool myParentTool; - private CanNotCalculateDiffPanel myNotCalculateDiffPanel; + private EditorNotificationPanel myTopMessageDiffPanel; private final VisibleAreaListener myVisibleAreaListener; public DiffPanelImpl(final Window owner, @@ -286,6 +286,7 @@ public class DiffPanelImpl implements DiffPanelEx, ContentChangeListener, TwoSid } public void setContents(DiffContent content1, DiffContent content2) { + LOG.assertTrue(content1 != null && content2 != null); LOG.assertTrue(!myDisposed); myData.setContents(content1, content2); Project project = myData.getProject(); @@ -342,10 +343,16 @@ public class DiffPanelImpl implements DiffPanelEx, ContentChangeListener, TwoSid void rediff() { try { - if (myNotCalculateDiffPanel != null) { - myPanel.removeTopComponent(myNotCalculateDiffPanel); + if (myTopMessageDiffPanel != null) { + myPanel.removeTopComponent(myTopMessageDiffPanel); + } + LineBlocks blocks = myData.updateEditors(); + setLineBlocks(blocks); + if (blocks.getCount() == 0) { + if (myData.isContentsEqual()) { + setFileContentsAreIdentical(); + } } - setLineBlocks(myData.updateEditors()); } catch (FilesTooBigForDiffException e) { setTooBigFileErrorContents(); @@ -354,13 +361,29 @@ public class DiffPanelImpl implements DiffPanelEx, ContentChangeListener, TwoSid public void setTooBigFileErrorContents() { setLineBlocks(LineBlocks.EMPTY); - myNotCalculateDiffPanel = new CanNotCalculateDiffPanel(); - myPanel.insertTopComponent(myNotCalculateDiffPanel); + myTopMessageDiffPanel = new CanNotCalculateDiffPanel(); + myPanel.insertTopComponent(myTopMessageDiffPanel); } public void setPatchAppliedApproximately() { - if (myNotCalculateDiffPanel == null) { - myPanel.insertTopComponent(new DiffIsApproximate()); + if (!(myTopMessageDiffPanel instanceof CanNotCalculateDiffPanel)) { + myTopMessageDiffPanel = new DiffIsApproximate(); + myPanel.insertTopComponent(myTopMessageDiffPanel); + } + } + + public void setFileContentsAreIdentical() { + if (myTopMessageDiffPanel == null || myTopMessageDiffPanel instanceof FileContentsAreIdenticalDiffPanel) { + LineSeparator sep1 = myData.getContent1() == null ? null : myData.getContent1().getLineSeparator(); + LineSeparator sep2 = myData.getContent2() == null ? null : myData.getContent2().getLineSeparator(); + + if (LineSeparator.knownAndDifferent(sep1, sep2)) { + myTopMessageDiffPanel = new LineSeparatorsOnlyDiffPanel(); + } + else { + myTopMessageDiffPanel = new FileContentsAreIdenticalDiffPanel(); + } + myPanel.insertTopComponent(myTopMessageDiffPanel); } } @@ -448,7 +471,7 @@ public class DiffPanelImpl implements DiffPanelEx, ContentChangeListener, TwoSid } public boolean hasDifferences() { - return getLineBlocks().getCount() > 0 || myNotCalculateDiffPanel != null; + return getLineBlocks().getCount() > 0 || myTopMessageDiffPanel != null; } @Nullable @@ -599,16 +622,25 @@ public class DiffPanelImpl implements DiffPanelEx, ContentChangeListener, TwoSid public LineBlocks getLineBlocks() { return myLineBlocks; } - static JComponent createComponentForTitle(@Nullable String title, @NotNull final LineSeparator separator, boolean left) { - JPanel bottomPanel = new JPanel(new BorderLayout()); - JLabel sepLabel = new JLabel(separator.name()); - sepLabel.setForeground(separator.equals(LineSeparator.CRLF) ? JBColor.RED : PlatformColors.BLUE); - bottomPanel.add(sepLabel, left ? BorderLayout.EAST : BorderLayout.WEST); + static JComponent createComponentForTitle(@Nullable String title, + @Nullable final LineSeparator sep1, + @Nullable final LineSeparator sep2, + boolean left) { + if (sep1 != null && sep2 != null && !sep1.equals(sep2)) { + LineSeparator separator = left ? sep1 : sep2; + JPanel bottomPanel = new JPanel(new BorderLayout()); + JLabel sepLabel = new JLabel(separator.name()); + sepLabel.setForeground(separator.equals(LineSeparator.CRLF) ? JBColor.RED : PlatformColors.BLUE); + bottomPanel.add(sepLabel, left ? BorderLayout.EAST : BorderLayout.WEST); - JPanel panel = new JPanel(new BorderLayout()); - panel.add(new JLabel(title == null ? "" : title)); - panel.add(bottomPanel, BorderLayout.SOUTH); - return panel; + JPanel panel = new JPanel(new BorderLayout()); + panel.add(new JLabel(title == null ? "" : title)); + panel.add(bottomPanel, BorderLayout.SOUTH); + return panel; + } + else { + return new JBLabel(title == null ? "" : title); + } } @Override @@ -683,14 +715,8 @@ public class DiffPanelImpl implements DiffPanelEx, ContentChangeListener, TwoSid String title1 = addReadOnly(data.getContentTitles()[0], myLeftSide.getEditor()); String title2 = addReadOnly(data.getContentTitles()[1], myRightSide.getEditor()); - if (title1 != null && title2 != null && sep1 != null && sep2 != null && !sep1.equals(sep2)) { - setTitle1(createComponentForTitle(title1, sep1, true)); - setTitle2(createComponentForTitle(title2, sep2, false)); - } - else { - setTitle1(new JBLabel(title1 == null ? "" : title1)); - setTitle2(new JBLabel(title2 == null ? "" : title2)); - } + setTitle1(createComponentForTitle(title1, sep1, sep2, true)); + setTitle2(createComponentForTitle(title2, sep1, sep2, false)); } private void setTitle1(JComponent title) { @@ -928,6 +954,18 @@ public class DiffPanelImpl implements DiffPanelEx, ContentChangeListener, TwoSid } } + public static class FileContentsAreIdenticalDiffPanel extends EditorNotificationPanel { + public FileContentsAreIdenticalDiffPanel() { + myLabel.setText(DiffBundle.message("diff.contents.are.identical.message.text")); + } + } + + public static class LineSeparatorsOnlyDiffPanel extends FileContentsAreIdenticalDiffPanel { + public LineSeparatorsOnlyDiffPanel() { + myLabel.setText(DiffBundle.message("diff.contents.have.differences.only.in.line.separators.message.text")); + } + } + public static class CanNotCalculateDiffPanel extends EditorNotificationPanel { public CanNotCalculateDiffPanel() { myLabel.setText("Can not calculate diff. File is too big and there are too many changes."); diff --git a/platform/platform-impl/src/com/intellij/openapi/diff/impl/EmptyDiffViewer.java b/platform/platform-impl/src/com/intellij/openapi/diff/impl/ErrorDiffViewer.java similarity index 61% rename from platform/platform-impl/src/com/intellij/openapi/diff/impl/EmptyDiffViewer.java rename to platform/platform-impl/src/com/intellij/openapi/diff/impl/ErrorDiffViewer.java index dcc7120f605d..f03e05d68df2 100644 --- a/platform/platform-impl/src/com/intellij/openapi/diff/impl/EmptyDiffViewer.java +++ b/platform/platform-impl/src/com/intellij/openapi/diff/impl/ErrorDiffViewer.java @@ -19,9 +19,7 @@ import com.intellij.openapi.actionSystem.ActionManager; import com.intellij.openapi.actionSystem.DataKey; import com.intellij.openapi.actionSystem.DataSink; import com.intellij.openapi.diff.*; -import com.intellij.openapi.ui.Splitter; import com.intellij.openapi.vcs.AbstractDataProviderPanel; -import com.intellij.util.LineSeparator; import com.intellij.util.ui.UIUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -35,17 +33,21 @@ import java.awt.*; * @author Irina.Chernushina * @author Kirill Likhodedov */ -public class EmptyDiffViewer implements DiffViewer { - private DiffRequest myRequest; +public class ErrorDiffViewer implements DiffViewer { + @NotNull private final DiffRequest myRequest; + + protected ErrorDiffViewer(@NotNull DiffRequest request) { + myRequest = request; + } @Override public boolean canShowRequest(DiffRequest request) { - return true; + return false; } @Override public void setDiffRequest(DiffRequest request) { - myRequest = request; + throw new IllegalStateException(); } @Override @@ -81,59 +83,29 @@ public class EmptyDiffViewer implements DiffViewer { DiffContent content1 = myRequest.getContents()[0]; DiffContent content2 = myRequest.getContents()[1]; - LineSeparator sep1 = content1.getLineSeparator(); - LineSeparator sep2 = content2.getLineSeparator(); - - final JPanel messagePanel = createMessagePanel(sep1, sep2); - - if (LineSeparator.knownAndDifferent(sep1, sep2)) { - assert sep1 != null && sep2 != null: "Separators should have been checked for nullity. sep1: " + sep1 + ", sep2: " + sep2; - - JComponent title1 = createTitleComponent(myRequest.getContentTitles()[0], sep1, true); - JComponent title2 = createTitleComponent(myRequest.getContentTitles()[1], sep2, false); - - Splitter titlePanel = new Splitter(false, 0.5F, 0.5F, 0.5F); - titlePanel.setFirstComponent(title1); - titlePanel.setSecondComponent(title2); - titlePanel.setDividerWidth(1); - - JPanel rootPanel = new JPanel(new BorderLayout()); - rootPanel.add(titlePanel, BorderLayout.NORTH); - rootPanel.add(messagePanel); - result.add(rootPanel, BorderLayout.CENTER); + String message; + if (DiffUtil.oneIsUnknown(content1, content2)) { + message = DiffBundle.message("diff.can.not.show.unknown"); } else { - result.add(messagePanel, BorderLayout.CENTER); + message = DiffBundle.message("diff.can.not.show"); } + + final JPanel messagePanel = createMessagePanel(message); + result.add(messagePanel, BorderLayout.CENTER); return result; } @NotNull - private JPanel createMessagePanel(@Nullable LineSeparator sep1, @Nullable LineSeparator sep2) { - String message; - if (DiffUtil.oneIsUnknown(myRequest.getContents()[0], myRequest.getContents()[1])) { - message = DiffBundle.message("diff.can.not.show.unknown"); - } - else if (LineSeparator.knownAndDifferent(sep1, sep2)) { - message = DiffBundle.message("diff.contents.have.differences.only.in.line.separators.message.text"); - } - else { - message = DiffBundle.message("diff.contents.are.identical.message.text"); - } - + private static JPanel createMessagePanel(@NotNull String message) { final JLabel label = new JLabel(message); label.setForeground(UIUtil.getInactiveTextColor()); final JPanel wrapper = new JPanel(new GridBagLayout()); - wrapper.add(label, new GridBagConstraints(0,0,1,1,0,0,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(1,1,1,1), 0,0)); + wrapper.add(label, + new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(1, 1, 1, 1), 0, 0)); return wrapper; } - private static JComponent createTitleComponent(String title, LineSeparator sep1, boolean left) { - JComponent panel = DiffPanelImpl.createComponentForTitle(title, sep1, left); - panel.setBorder(BorderFactory.createEmptyBorder(UIUtil.DEFAULT_VGAP, UIUtil.DEFAULT_HGAP, UIUtil.DEFAULT_VGAP, UIUtil.DEFAULT_HGAP)); - return panel; - } - @Nullable @Override public JComponent getPreferredFocusedComponent() { diff --git a/platform/platform-impl/src/com/intellij/openapi/diff/impl/external/FrameDiffTool.java b/platform/platform-impl/src/com/intellij/openapi/diff/impl/external/FrameDiffTool.java index 477835c682a2..4cf5574fab9b 100644 --- a/platform/platform-impl/src/com/intellij/openapi/diff/impl/external/FrameDiffTool.java +++ b/platform/platform-impl/src/com/intellij/openapi/diff/impl/external/FrameDiffTool.java @@ -159,6 +159,7 @@ public class FrameDiffTool implements DiffTool { return KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow() instanceof JDialog; } + // TODO remove check? private boolean checkNoDifferenceAndNotify(DiffPanel diffPanel, DiffRequest data, final Window window, final boolean showMessage) { if (!diffPanel.hasDifferences() && !data.getHints().contains(HINT_ALLOW_NO_DIFFERENCES)) { DiffManagerImpl manager = (DiffManagerImpl) DiffManager.getInstance(); @@ -218,6 +219,6 @@ public class FrameDiffTool implements DiffTool { @Override public DiffViewer createComponent(String title, DiffRequest request, Window window, @NotNull Disposable parentDisposable) { - return createDiffPanelIfShouldShow(request, window, parentDisposable, false); + return createDiffPanelImpl(request, window, parentDisposable); } } diff --git a/platform/platform-impl/src/com/intellij/openapi/diff/impl/highlighting/DiffPanelState.java b/platform/platform-impl/src/com/intellij/openapi/diff/impl/highlighting/DiffPanelState.java index d74ddfcfd7cd..9e4411120361 100644 --- a/platform/platform-impl/src/com/intellij/openapi/diff/impl/highlighting/DiffPanelState.java +++ b/platform/platform-impl/src/com/intellij/openapi/diff/impl/highlighting/DiffPanelState.java @@ -20,6 +20,8 @@ import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.diff.DiffContent; import com.intellij.openapi.diff.impl.ContentChangeListener; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.Comparing; +import com.intellij.openapi.util.text.StringUtil; import org.jetbrains.annotations.NotNull; import javax.swing.*; @@ -39,6 +41,10 @@ public class DiffPanelState extends SimpleDiffPanelState { }); } + public DiffContent getContent1() { + return myAppender1.getContent(); + } + public DiffContent getContent2() { return myAppender2.getContent(); } @@ -50,5 +56,9 @@ public class DiffPanelState extends SimpleDiffPanelState { public void drawOnDivider(final Graphics g, final JComponent component) { } + + public boolean isContentsEqual() { + return Comparing.equal(myAppender1.getText(), myAppender2.getText()); + } } diff --git a/platform/platform-resources-en/src/messages/DiffBundle.properties b/platform/platform-resources-en/src/messages/DiffBundle.properties index 096fa117e2c8..598cc9714697 100644 --- a/platform/platform-resources-en/src/messages/DiffBundle.properties +++ b/platform/platform-resources-en/src/messages/DiffBundle.properties @@ -85,6 +85,7 @@ diff.content.selected.value=Selected Value diff.clipboard.vs.value.dialog.title=Clipboard vs Selected Value diff.can.not.show.unknown=Can not show diff for unknown file type +diff.can.not.show=Can not show diff diff.acton.highlight.mode.action.by.word=By word diff.acton.highlight.mode.action.by.line=By line diff.acton.highlight.mode.action.no.highlighting=Do not highlight