source: josm/trunk/src/org/openstreetmap/josm/gui/history/TwoColumnDiff.java@ 6246

Last change on this file since 6246 was 6246, checked in by Don-vip, 11 years ago

Sonar/FindBugs - various bugfixes / violation fixes

File size: 3.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.history;
3/// Feel free to move me somewhere else. Maybe a bit specific for josm.tools?
4
5import java.awt.Color;
6import java.util.ArrayList;
7
8import org.openstreetmap.josm.gui.history.TwoColumnDiff.Item.DiffItemType;
9import org.openstreetmap.josm.tools.Diff;
10import org.openstreetmap.josm.tools.Utils;
11
12/**
13 * Produces a "two column diff" of two lists. (same as diff -y)
14 *
15 * Each list is annotated with the changes relative to the other, and "empty" cells are inserted so the lists are comparable item by item.
16 *
17 * diff on [1 2 3 4] [1 a 4 5] yields:
18 *
19 * item(SAME, 1) item(SAME, 1)
20 * item(CHANGED, 2) item(CHANGED, 2)
21 * item(DELETED, 3) item(EMPTY)
22 * item(SAME, 4) item(SAME, 4)
23 * item(EMPTY) item(INSERTED, 5)
24 *
25 * @author olejorgenb
26 */
27class TwoColumnDiff {
28 public static class Item {
29
30 public enum DiffItemType {
31 INSERTED(new Color(0xDD, 0xFF, 0xDD)), DELETED(new Color(255,197,197)), CHANGED(new Color(255,234,213)),
32 SAME(new Color(234,234,234)), EMPTY(new Color(234,234,234));
33
34 private final Color color;
35 private DiffItemType(Color color) {
36 this.color = color;
37 }
38 public Color getColor() {
39 return color;
40 }
41 }
42
43 public Item(DiffItemType state, Object value) {
44 this.state = state;
45 this.value = state == DiffItemType.EMPTY ? null : value;
46 }
47
48 public final Object value;
49 public final DiffItemType state;
50 }
51
52 public ArrayList<Item> referenceDiff;
53 public ArrayList<Item> currentDiff;
54 Object[] reference;
55 Object[] current;
56
57 public TwoColumnDiff(Object[] reference, Object[] current) {
58 this.reference = Utils.copyArray(reference);
59 this.current = Utils.copyArray(current);
60 referenceDiff = new ArrayList<Item>();
61 currentDiff = new ArrayList<Item>();
62 diff();
63 }
64
65 private void diff() {
66 Diff diff = new Diff(reference, current);
67 Diff.Change script = diff.diff_2(false);
68 twoColumnDiffFromScript(script, reference, current);
69 }
70
71 /**
72 * The result from the diff algorithm is a "script" (a compressed description of the changes)
73 * This method expands this script into a full two column description.
74 */
75 private void twoColumnDiffFromScript(Diff.Change script, Object[] a, Object[] b) {
76 int ia = 0;
77 int ib = 0;
78
79 while(script != null) {
80 int deleted = script.deleted;
81 int inserted = script.inserted;
82 while(ia < script.line0 && ib < script.line1){
83 Item cell = new Item(DiffItemType.SAME, a[ia]);
84 referenceDiff.add(cell);
85 currentDiff.add(cell);
86 ia++;
87 ib++;
88 }
89
90 while(inserted > 0 || deleted > 0) {
91 if(inserted > 0 && deleted > 0) {
92 referenceDiff.add(new Item(DiffItemType.CHANGED, a[ia++]));
93 currentDiff.add(new Item(DiffItemType.CHANGED, b[ib++]));
94 } else if(inserted > 0) {
95 referenceDiff.add(new Item(DiffItemType.EMPTY, null));
96 currentDiff.add(new Item(DiffItemType.INSERTED, b[ib++]));
97 } else if(deleted > 0) {
98 referenceDiff.add(new Item(DiffItemType.DELETED, a[ia++]));
99 currentDiff.add(new Item(DiffItemType.EMPTY, null));
100 }
101 inserted--;
102 deleted--;
103 }
104 script = script.link;
105 }
106 while(ia < a.length && ib < b.length) {
107 referenceDiff.add(new Item(DiffItemType.SAME, a[ia++]));
108 currentDiff.add(new Item(DiffItemType.SAME, b[ib++]));
109 }
110 }
111}
Note: See TracBrowser for help on using the repository browser.