1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.history;
|
---|
3 |
|
---|
4 | import java.awt.Color;
|
---|
5 | import java.util.ArrayList;
|
---|
6 | import java.util.Collections;
|
---|
7 | import java.util.List;
|
---|
8 |
|
---|
9 | import javax.swing.table.AbstractTableModel;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * The table model for the tags of the version
|
---|
15 | * at {@link PointInTimeType#REFERENCE_POINT_IN_TIME}
|
---|
16 | * or {@link PointInTimeType#CURRENT_POINT_IN_TIME}
|
---|
17 | * @since 11647 (extracted from HistoryBrowserModel)
|
---|
18 | */
|
---|
19 | public final class TagTableModel extends AbstractTableModel {
|
---|
20 |
|
---|
21 | private List<String> keys;
|
---|
22 | private final PointInTimeType pointInTimeType;
|
---|
23 | private final HistoryBrowserModel model;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Constructs a new {@code TagTableModel}.
|
---|
27 | * @param historyModel parent {@code HistoryBrowserModel}
|
---|
28 | * @param type type of point in time
|
---|
29 | */
|
---|
30 | public TagTableModel(HistoryBrowserModel historyModel, PointInTimeType type) {
|
---|
31 | model = historyModel;
|
---|
32 | pointInTimeType = type;
|
---|
33 | initKeyList();
|
---|
34 | }
|
---|
35 |
|
---|
36 | void initKeyList() {
|
---|
37 | keys = new ArrayList<>(model.getKeySet());
|
---|
38 | Collections.sort(keys);
|
---|
39 | fireTableDataChanged();
|
---|
40 | }
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | public int getRowCount() {
|
---|
44 | if (keys == null)
|
---|
45 | return 0;
|
---|
46 | return keys.size();
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | public Object getValueAt(int row, int column) {
|
---|
51 | return getKeyAt(row);
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Get the key for the given row.
|
---|
56 | * @param row The row
|
---|
57 | * @return The key in that row.
|
---|
58 | * @since 10637
|
---|
59 | */
|
---|
60 | public String getKeyAt(int row) {
|
---|
61 | return keys.get(row);
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Determines if a tag exists for the given key.
|
---|
66 | * @param key tag key
|
---|
67 | * @return {@code true} if a tag exists for the given key
|
---|
68 | */
|
---|
69 | public boolean hasTag(String key) {
|
---|
70 | HistoryOsmPrimitive primitive = model.getPointInTime(pointInTimeType);
|
---|
71 | return primitive != null && primitive.hasKey(key);
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Returns the tag value for the given key.
|
---|
76 | * @param key tag key
|
---|
77 | * @return tag value, or null
|
---|
78 | */
|
---|
79 | public String getValue(String key) {
|
---|
80 | HistoryOsmPrimitive primitive = model.getPointInTime(pointInTimeType);
|
---|
81 | if (primitive == null)
|
---|
82 | return null;
|
---|
83 | return primitive.get(key);
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Returns the history primitive which changed the given key.
|
---|
88 | * @param key the OSM key
|
---|
89 | * @return the history primitive which changed the given key
|
---|
90 | */
|
---|
91 | public HistoryOsmPrimitive getWhichChangedTag(String key) {
|
---|
92 | HistoryOsmPrimitive primitive = model.getPointInTime(pointInTimeType);
|
---|
93 | if (primitive == null)
|
---|
94 | return null;
|
---|
95 | return model.getHistory().getWhichChangedTag(primitive, key, model.isLatest(primitive));
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Returns a version string for the given primitive, {@code "*"} if it is {@linkplain HistoryBrowserModel#isLatest is latest}.
|
---|
100 | * @param primitive the history primitive
|
---|
101 | * @return a version string for the given primitive
|
---|
102 | */
|
---|
103 | public String getVersionString(HistoryOsmPrimitive primitive) {
|
---|
104 | return model.isLatest(primitive) ? "*" : "v" + primitive.getVersion();
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Returns the color for the given primitive timestamp
|
---|
109 | * @param primitive the history primitive
|
---|
110 | * @return the color for the given primitive timestamp
|
---|
111 | */
|
---|
112 | public Color getVersionColor(HistoryOsmPrimitive primitive) {
|
---|
113 | return model.getVersionColor(primitive);
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Determines if a tag exists in the opposite point in time for the given key.
|
---|
118 | * @param key tag key
|
---|
119 | * @return {@code true} if a tag exists for the given key
|
---|
120 | */
|
---|
121 | public boolean oppositeHasTag(String key) {
|
---|
122 | HistoryOsmPrimitive primitive = model.getPointInTime(pointInTimeType.opposite());
|
---|
123 | return primitive != null && primitive.hasKey(key);
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Returns the tag value in the opposite point in time for the given key.
|
---|
128 | * @param key tag key
|
---|
129 | * @return tag value, or null
|
---|
130 | */
|
---|
131 | public String getOppositeValue(String key) {
|
---|
132 | HistoryOsmPrimitive primitive = model.getPointInTime(pointInTimeType.opposite());
|
---|
133 | if (primitive == null)
|
---|
134 | return null;
|
---|
135 | return primitive.get(key);
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Determines if the tag value is the same in the opposite point in time for the given key.
|
---|
140 | * @param key tag key
|
---|
141 | * @return {@code true} if the tag value is the same in the opposite point in time for the given key
|
---|
142 | */
|
---|
143 | public boolean hasSameValueAsOpposite(String key) {
|
---|
144 | String value = getValue(key);
|
---|
145 | String oppositeValue = getOppositeValue(key);
|
---|
146 | return value != null && value.equals(oppositeValue);
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Returns the type of point in time.
|
---|
151 | * @return the type of point in time
|
---|
152 | */
|
---|
153 | public PointInTimeType getPointInTimeType() {
|
---|
154 | return pointInTimeType;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Determines if this is the current point in time.
|
---|
159 | * @return {@code true} if this is the current point in time
|
---|
160 | */
|
---|
161 | public boolean isCurrentPointInTime() {
|
---|
162 | return pointInTimeType == PointInTimeType.CURRENT_POINT_IN_TIME;
|
---|
163 | }
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Determines if this is the reference point in time.
|
---|
167 | * @return {@code true} if this is the reference point in time
|
---|
168 | */
|
---|
169 | public boolean isReferencePointInTime() {
|
---|
170 | return pointInTimeType == PointInTimeType.REFERENCE_POINT_IN_TIME;
|
---|
171 | }
|
---|
172 |
|
---|
173 | @Override
|
---|
174 | public int getColumnCount() {
|
---|
175 | return 3;
|
---|
176 | }
|
---|
177 |
|
---|
178 | TwoColumnDiff.Item.DiffItemType getDiffItemType(String key, boolean isValue) {
|
---|
179 | if ((!hasTag(key) && isCurrentPointInTime()) || (!oppositeHasTag(key) && isReferencePointInTime())) {
|
---|
180 | return TwoColumnDiff.Item.DiffItemType.DELETED;
|
---|
181 | } else if ((!oppositeHasTag(key) && isCurrentPointInTime()) || (!hasTag(key) && isReferencePointInTime())) {
|
---|
182 | return TwoColumnDiff.Item.DiffItemType.INSERTED;
|
---|
183 | } else if (isValue && hasTag(key) && oppositeHasTag(key) && !hasSameValueAsOpposite(key)) {
|
---|
184 | return TwoColumnDiff.Item.DiffItemType.CHANGED;
|
---|
185 | } else {
|
---|
186 | return TwoColumnDiff.Item.DiffItemType.EMPTY;
|
---|
187 | }
|
---|
188 | }
|
---|
189 | }
|
---|