[11647] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui.history;
|
---|
| 3 |
|
---|
| 4 | import java.util.ArrayList;
|
---|
| 5 | import java.util.Collections;
|
---|
| 6 | import java.util.List;
|
---|
| 7 |
|
---|
| 8 | import javax.swing.table.AbstractTableModel;
|
---|
| 9 |
|
---|
| 10 | import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
|
---|
| 11 |
|
---|
| 12 | /**
|
---|
| 13 | * The table model for the tags of the version
|
---|
| 14 | * at {@link PointInTimeType#REFERENCE_POINT_IN_TIME}
|
---|
| 15 | * or {@link PointInTimeType#CURRENT_POINT_IN_TIME}
|
---|
| 16 | * @since 11647 (extracted from HistoryBrowserModel)
|
---|
| 17 | */
|
---|
| 18 | public final class TagTableModel extends AbstractTableModel {
|
---|
| 19 |
|
---|
| 20 | private List<String> keys;
|
---|
| 21 | private final PointInTimeType pointInTimeType;
|
---|
| 22 | private final HistoryBrowserModel model;
|
---|
| 23 |
|
---|
| 24 | /**
|
---|
| 25 | * Constructs a new {@code TagTableModel}.
|
---|
| 26 | * @param historyModel parent {@code HistoryBrowserModel}
|
---|
| 27 | * @param type type of point in time
|
---|
| 28 | */
|
---|
| 29 | public TagTableModel(HistoryBrowserModel historyModel, PointInTimeType type) {
|
---|
| 30 | model = historyModel;
|
---|
| 31 | pointInTimeType = type;
|
---|
| 32 | initKeyList();
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | void initKeyList() {
|
---|
| 36 | keys = new ArrayList<>(model.getKeySet());
|
---|
| 37 | Collections.sort(keys);
|
---|
| 38 | fireTableDataChanged();
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | @Override
|
---|
| 42 | public int getRowCount() {
|
---|
| 43 | if (keys == null)
|
---|
| 44 | return 0;
|
---|
| 45 | return keys.size();
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | @Override
|
---|
| 49 | public Object getValueAt(int row, int column) {
|
---|
| 50 | return getKeyAt(row);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | /**
|
---|
| 54 | * Get the key for the given row.
|
---|
| 55 | * @param row The row
|
---|
| 56 | * @return The key in that row.
|
---|
| 57 | * @since 10637
|
---|
| 58 | */
|
---|
| 59 | public String getKeyAt(int row) {
|
---|
| 60 | return keys.get(row);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /**
|
---|
| 64 | * Determines if a tag exists for the given key.
|
---|
| 65 | * @param key tag key
|
---|
| 66 | * @return {@code true} if a tag exists for the given key
|
---|
| 67 | */
|
---|
| 68 | public boolean hasTag(String key) {
|
---|
| 69 | HistoryOsmPrimitive primitive = model.getPointInTime(pointInTimeType);
|
---|
| 70 | return primitive != null && primitive.hasKey(key);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | /**
|
---|
| 74 | * Returns the tag value for the given key.
|
---|
| 75 | * @param key tag key
|
---|
| 76 | * @return tag value, or null
|
---|
| 77 | */
|
---|
| 78 | public String getValue(String key) {
|
---|
| 79 | HistoryOsmPrimitive primitive = model.getPointInTime(pointInTimeType);
|
---|
| 80 | if (primitive == null)
|
---|
| 81 | return null;
|
---|
| 82 | return primitive.get(key);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | /**
|
---|
| 86 | * Determines if a tag exists in the opposite point in time for the given key.
|
---|
| 87 | * @param key tag key
|
---|
| 88 | * @return {@code true} if a tag exists for the given key
|
---|
| 89 | */
|
---|
| 90 | public boolean oppositeHasTag(String key) {
|
---|
| 91 | HistoryOsmPrimitive primitive = model.getPointInTime(pointInTimeType.opposite());
|
---|
| 92 | return primitive != null && primitive.hasKey(key);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | /**
|
---|
| 96 | * Returns the tag value in the opposite point in time for the given key.
|
---|
| 97 | * @param key tag key
|
---|
| 98 | * @return tag value, or null
|
---|
| 99 | */
|
---|
| 100 | public String getOppositeValue(String key) {
|
---|
| 101 | HistoryOsmPrimitive primitive = model.getPointInTime(pointInTimeType.opposite());
|
---|
| 102 | if (primitive == null)
|
---|
| 103 | return null;
|
---|
| 104 | return primitive.get(key);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | /**
|
---|
| 108 | * Determines if the tag value is the same in the opposite point in time for the given key.
|
---|
| 109 | * @param key tag key
|
---|
| 110 | * @return {@code true} if the tag value is the same in the opposite point in time for the given key
|
---|
| 111 | */
|
---|
| 112 | public boolean hasSameValueAsOpposite(String key) {
|
---|
| 113 | String value = getValue(key);
|
---|
| 114 | String oppositeValue = getOppositeValue(key);
|
---|
| 115 | return value != null && value.equals(oppositeValue);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | /**
|
---|
| 119 | * Returns the type of point in time.
|
---|
| 120 | * @return the type of point in time
|
---|
| 121 | */
|
---|
| 122 | public PointInTimeType getPointInTimeType() {
|
---|
| 123 | return pointInTimeType;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | /**
|
---|
| 127 | * Determines if this is the current point in time.
|
---|
| 128 | * @return {@code true} if this is the current point in time
|
---|
| 129 | */
|
---|
| 130 | public boolean isCurrentPointInTime() {
|
---|
| 131 | return pointInTimeType.equals(PointInTimeType.CURRENT_POINT_IN_TIME);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | /**
|
---|
| 135 | * Determines if this is the reference point in time.
|
---|
| 136 | * @return {@code true} if this is the reference point in time
|
---|
| 137 | */
|
---|
| 138 | public boolean isReferencePointInTime() {
|
---|
| 139 | return pointInTimeType.equals(PointInTimeType.REFERENCE_POINT_IN_TIME);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | @Override
|
---|
| 143 | public int getColumnCount() {
|
---|
| 144 | return 2;
|
---|
| 145 | }
|
---|
| 146 | }
|
---|