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