/plugins/JDiffPlugin/trunk/jdiff/component/DiffTextAreaModel.java

# · Java · 127 lines · 77 code · 20 blank · 30 comment · 7 complexity · 5150dc179ffa754481aa4feba1b263db MD5 · raw file

  1. /*
  2. * Copyright (c) 2008, Dale Anson
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. package jdiff.component;
  19. import java.util.*;
  20. import jdiff.*;
  21. import jdiff.text.FileLine;
  22. import jdiff.util.Diff;
  23. import jdiff.util.DualDiffUtil;
  24. import jdiff.util.JDiffDiff;
  25. import org.gjt.sp.jedit.Buffer;
  26. import org.gjt.sp.jedit.EditPane;
  27. import org.gjt.sp.jedit.textarea.JEditTextArea;
  28. public class DiffTextAreaModel {
  29. private DualDiff dualDiff;
  30. private Diff.Change edits = null;
  31. private HashMap<Integer, Diff.Change> leftHunkMap = null;
  32. private HashMap<Integer, Diff.Change> rightHunkMap = null;
  33. private int leftLineCount;
  34. private int rightLineCount;
  35. private JEditTextArea leftTextArea;
  36. private JEditTextArea rightTextArea;
  37. public DiffTextAreaModel(DualDiff dualDiff) {
  38. this.dualDiff = dualDiff;
  39. prepData();
  40. }
  41. public Diff.Change getEdits() {
  42. return edits;
  43. }
  44. public int getLeftLineCount() {
  45. return leftLineCount;
  46. }
  47. public int getRightLineCount() {
  48. return rightLineCount;
  49. }
  50. public JEditTextArea getLeftTextArea() {
  51. return leftTextArea;
  52. }
  53. public JEditTextArea getRightTextArea() {
  54. return rightTextArea;
  55. }
  56. public HashMap<Integer, Diff.Change> getLeftHunkMap() {
  57. return leftHunkMap;
  58. }
  59. public HashMap<Integer, Diff.Change> getRightHunkMap() {
  60. return rightHunkMap;
  61. }
  62. private void prepData() {
  63. EditPane editPane0 = dualDiff.getEditPane0();
  64. EditPane editPane1 = dualDiff.getEditPane1();
  65. Buffer buf0 = editPane0.getBuffer();
  66. Buffer buf1 = editPane1.getBuffer();
  67. leftTextArea = editPane0.getTextArea();
  68. rightTextArea = editPane1.getTextArea();
  69. if ( !buf0.isLoaded() || !buf1.isLoaded() ) {
  70. edits = null;
  71. leftLineCount = ( ( buf0.isLoaded() ) ? buf0.getLineCount() : 1 );
  72. rightLineCount = ( ( buf1.isLoaded() ) ? buf1.getLineCount() : 1 );
  73. }
  74. else {
  75. FileLine[] fileLines0 = DualDiffUtil.getFileLines( dualDiff, buf0 );
  76. FileLine[] fileLines1 = DualDiffUtil.getFileLines( dualDiff, buf1 );
  77. Diff d = new JDiffDiff( fileLines0, fileLines1 );
  78. edits = d.diff_2();
  79. leftHunkMap = new HashMap<Integer, Diff.Change>();
  80. rightHunkMap = new HashMap<Integer, Diff.Change>();
  81. Diff.Change hunk = edits;
  82. for ( ; hunk != null; hunk = hunk.next ) {
  83. for (int i = 0; i < Math.max(1, hunk.lines0); i++) {
  84. leftHunkMap.put(hunk.first0 + i, hunk);
  85. }
  86. for (int i = 0; i < Math.max(1, hunk.lines1); i++) {
  87. rightHunkMap.put(hunk.first1 + i, hunk);
  88. }
  89. }
  90. /*
  91. System.out.println("++++++++++++++++++++++++++++++++++");
  92. Set<Map.Entry<Integer, Diff.Change>> entries = leftHunkMap.entrySet();
  93. for (Map.Entry<Integer, Diff.Change> entry : entries) {
  94. System.out.println(entry.getKey() + " = " + entry.getValue());
  95. }
  96. System.out.println("++++++++++++++++++++++++++++++++++");
  97. entries = rightHunkMap.entrySet();
  98. for (Map.Entry<Integer, Diff.Change> entry : entries) {
  99. System.out.println(entry.getKey() + " = " + entry.getValue());
  100. }
  101. System.out.println("++++++++++++++++++++++++++++++++++");
  102. */
  103. leftLineCount = fileLines0.length;
  104. rightLineCount = fileLines1.length;
  105. }
  106. }
  107. }