/plugins/JDiffPlugin/tags/jdiffplugin-1_2_1/DiffGlobalPhysicalOverview.java

# · Java · 153 lines · 104 code · 30 blank · 19 comment · 12 complexity · 9c27b2d97a5e9bd454ba42939116cef3 MD5 · raw file

  1. /*
  2. * DiffGlobalPhysicalOverview.java
  3. * Copyright (C) 2000 Andre Kaplan
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. import java.awt.Color;
  20. import java.awt.Graphics;
  21. import java.awt.Rectangle;
  22. import jdiff.util.Diff;
  23. import org.gjt.sp.jedit.Buffer;
  24. import org.gjt.sp.jedit.textarea.JEditTextArea;
  25. import org.gjt.sp.util.Log;
  26. public class DiffGlobalPhysicalOverview extends DiffOverview
  27. {
  28. public DiffGlobalPhysicalOverview(
  29. Diff.change edits,
  30. int lineCount0,
  31. int lineCount1,
  32. JEditTextArea textArea0,
  33. JEditTextArea textArea1
  34. ) {
  35. super(edits, lineCount0, lineCount1, textArea0, textArea1);
  36. }
  37. public void paint(Graphics gfx) {
  38. Rectangle size = getBounds();
  39. gfx.setColor(getBackground());
  40. gfx.fillRect(0, 0, size.width, size.height);
  41. Rectangle inner = new Rectangle(4, 4, size.width - 8, size.height - 8);
  42. int lines = Math.max(this.lineCount0, this.lineCount1);
  43. double pxlPerLine = ((double) inner.height) / lines;
  44. Rectangle left = new Rectangle(
  45. inner.x,
  46. inner.y,
  47. inner.width / 3,
  48. Math.max(1, (int) Math.round(pxlPerLine * this.lineCount0))
  49. );
  50. Rectangle right = new Rectangle(
  51. inner.x + (inner.width - left.width),
  52. inner.y,
  53. left.width,
  54. Math.max(1, (int) Math.round(pxlPerLine * this.lineCount1))
  55. );
  56. Rectangle cursor = new Rectangle(inner.x + inner.width / 2 - 1, inner.y,
  57. 2, 0);
  58. Color leftColor = JDiffPlugin.overviewInvalidColor;
  59. Color rightColor = JDiffPlugin.overviewInvalidColor;
  60. gfx.setColor(Color.black);
  61. gfx.drawRect(left.x - 1, left.y - 1, left.width + 1, left.height + 1);
  62. gfx.drawRect(right.x - 1, right.y - 1, right.width + 1, right.height + 1);
  63. gfx.setColor(Color.white);
  64. gfx.fillRect(left.x, left.y, left.width, left.height);
  65. gfx.fillRect(right.x, right.y, right.width, right.height);
  66. Diff.change hunk = this.edits;
  67. int leftOffset = 0;
  68. int rightOffset = 0;
  69. for (; hunk != null; hunk = hunk.link) {
  70. leftOffset = hunk.line0;
  71. rightOffset = hunk.line1;
  72. if (hunk.inserted == 0 && hunk.deleted != 0) { // DELETE
  73. leftColor = JDiffPlugin.overviewDeletedColor;
  74. rightColor = JDiffPlugin.overviewInvalidColor;
  75. } else if (hunk.inserted != 0 && hunk.deleted == 0) { // INSERT
  76. leftColor = JDiffPlugin.overviewInvalidColor;
  77. rightColor = JDiffPlugin.overviewInsertedColor;
  78. } else { // CHANGE
  79. leftColor = JDiffPlugin.overviewChangedColor;
  80. rightColor = JDiffPlugin.overviewChangedColor;
  81. }
  82. left.y = inner.y + (int) Math.round(leftOffset * pxlPerLine);
  83. right.y = inner.y + (int) Math.round(rightOffset * pxlPerLine);
  84. left.height = Math.max(1, (int) Math.round(hunk.deleted * pxlPerLine));
  85. right.height = Math.max(1, (int) Math.round(hunk.inserted * pxlPerLine));
  86. gfx.setColor(leftColor);
  87. gfx.fillRect(left.x, left.y, left.width, left.height);
  88. gfx.setColor(rightColor);
  89. gfx.fillRect(right.x, right.y, right.width, right.height);
  90. gfx.setColor(Color.black);
  91. gfx.drawLine(left.x + left.width + 1, left.y, right.x - 1, right.y);
  92. }
  93. // Display the textArea cursor
  94. this.paintCursor(gfx);
  95. }
  96. public void paintCursor(Graphics gfx) {
  97. Rectangle size = getBounds();
  98. Rectangle inner = new Rectangle(4, 4, size.width - 8, size.height - 8);
  99. int lines = Math.max(this.lineCount0, this.lineCount1);
  100. double pxlPerLine = ((double) inner.height) / lines;
  101. Buffer buffer0 = this.textArea0.getBuffer();
  102. int physicalFirstLine0 = buffer0.virtualToPhysical(this.textArea0.getFirstLine());
  103. int physicalLastLine0 = buffer0.virtualToPhysical(this.textArea0.getFirstLine() + this.textArea0.getVisibleLines() - 1);
  104. Rectangle leftCursor = new Rectangle(
  105. inner.x, inner.y + ((int) Math.round(pxlPerLine * physicalFirstLine0)),
  106. inner.width / 3,
  107. Math.max(1, (int) Math.round(pxlPerLine * Math.min(this.lineCount0, physicalLastLine0 - physicalFirstLine0 + 1)))
  108. );
  109. Buffer buffer1 = this.textArea1.getBuffer();
  110. int physicalFirstLine1 = buffer1.virtualToPhysical(this.textArea1.getFirstLine());
  111. int physicalLastLine1 = buffer1.virtualToPhysical(this.textArea1.getFirstLine() + this.textArea1.getVisibleLines() - 1);
  112. Rectangle rightCursor = new Rectangle(
  113. inner.x + (inner.width - leftCursor.width),
  114. inner.y + ((int) Math.round(pxlPerLine * physicalFirstLine1)),
  115. leftCursor.width,
  116. Math.max(1, (int) Math.round(pxlPerLine * Math.min(this.lineCount1, physicalLastLine1 - physicalFirstLine1 + 1)))
  117. );
  118. gfx.setColor(JDiffPlugin.leftCursorColor);
  119. gfx.drawRect(leftCursor.x, leftCursor.y, leftCursor.width - 1, leftCursor.height - 1);
  120. gfx.setColor(JDiffPlugin.rightCursorColor);
  121. gfx.drawRect(rightCursor.x, rightCursor.y, rightCursor.width - 1, rightCursor.height - 1);
  122. }
  123. }