Changeset 11647 in josm for trunk


Ignore:
Timestamp:
2017-03-02T00:32:19+01:00 (8 years ago)
Author:
Don-vip
Message:

see #14175 - refactor HistoryBrowserModel

Location:
trunk
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/history/HistoryBrowserModel.java

    r11646 r11647  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.util.ArrayList;
    7 import java.util.Collections;
    86import java.util.HashSet;
    9 import java.util.List;
    107import java.util.Set;
    118
    129import javax.swing.JTable;
    13 import javax.swing.table.AbstractTableModel;
    1410import javax.swing.table.TableModel;
    1511
     
    9692    public HistoryBrowserModel() {
    9793        versionTableModel = new VersionTableModel(this);
    98         currentTagTableModel = new TagTableModel(PointInTimeType.CURRENT_POINT_IN_TIME);
    99         referenceTagTableModel = new TagTableModel(PointInTimeType.REFERENCE_POINT_IN_TIME);
     94        currentTagTableModel = new TagTableModel(this, PointInTimeType.CURRENT_POINT_IN_TIME);
     95        referenceTagTableModel = new TagTableModel(this, PointInTimeType.REFERENCE_POINT_IN_TIME);
    10096        referenceNodeListTableModel = new DiffTableModel();
    10197        currentNodeListTableModel = new DiffTableModel();
     
    482478     * Returns the latest {@code HistoryOsmPrimitive}.
    483479     * @return the latest {@code HistoryOsmPrimitive}
     480     * @since 11646
    484481     */
    485482    public HistoryOsmPrimitive getLatest() {
     
    488485
    489486    /**
    490      * The table model for the tags of the version at {@link PointInTimeType#REFERENCE_POINT_IN_TIME}
    491      * or {@link PointInTimeType#CURRENT_POINT_IN_TIME}
    492      *
    493      */
    494     public final class TagTableModel extends AbstractTableModel {
    495 
    496         private List<String> keys;
    497         private final PointInTimeType pointInTimeType;
    498 
    499         private TagTableModel(PointInTimeType type) {
    500             pointInTimeType = type;
    501             initKeyList();
    502         }
    503 
    504         private void initKeyList() {
    505             Set<String> keySet = new HashSet<>();
    506             if (current != null) {
    507                 keySet.addAll(current.getTags().keySet());
    508             }
    509             if (reference != null) {
    510                 keySet.addAll(reference.getTags().keySet());
    511             }
    512             keys = new ArrayList<>(keySet);
    513             Collections.sort(keys);
    514             fireTableDataChanged();
    515         }
    516 
    517         @Override
    518         public int getRowCount() {
    519             if (keys == null)
    520                 return 0;
    521             return keys.size();
    522         }
    523 
    524         @Override
    525         public Object getValueAt(int row, int column) {
    526             return getKeyAt(row);
    527         }
    528 
    529         /**
    530          * Get the key for the given row.
    531          * @param row The row
    532          * @return The key in that row.
    533          * @since 10637
    534          */
    535         public String getKeyAt(int row) {
    536             return keys.get(row);
    537         }
    538 
    539         /**
    540          * Determines if a tag exists for the given key.
    541          * @param key tag key
    542          * @return {@code true} if a tag exists for the given key
    543          */
    544         public boolean hasTag(String key) {
    545             HistoryOsmPrimitive primitive = getPointInTime(pointInTimeType);
    546             return primitive != null && primitive.hasKey(key);
    547         }
    548 
    549         /**
    550          * Returns the tag value for the given key.
    551          * @param key tag key
    552          * @return tag value, or null
    553          */
    554         public String getValue(String key) {
    555             HistoryOsmPrimitive primitive = getPointInTime(pointInTimeType);
    556             if (primitive == null)
    557                 return null;
    558             return primitive.get(key);
    559         }
    560 
    561         /**
    562          * Determines if a tag exists in the opposite point in time for the given key.
    563          * @param key tag key
    564          * @return {@code true} if a tag exists for the given key
    565          */
    566         public boolean oppositeHasTag(String key) {
    567             PointInTimeType opposite = pointInTimeType.opposite();
    568             HistoryOsmPrimitive primitive = getPointInTime(opposite);
    569             return primitive != null && primitive.hasKey(key);
    570         }
    571 
    572         /**
    573          * Returns the tag value in the opposite point in time for the given key.
    574          * @param key tag key
    575          * @return tag value, or null
    576          */
    577         public String getOppositeValue(String key) {
    578             PointInTimeType opposite = pointInTimeType.opposite();
    579             HistoryOsmPrimitive primitive = getPointInTime(opposite);
    580             if (primitive == null)
    581                 return null;
    582             return primitive.get(key);
    583         }
    584 
    585         /**
    586          * Determines if the tag value is the same in the opposite point in time for the given key.
    587          * @param key tag key
    588          * @return {@code true} if the tag value is the same in the opposite point in time for the given key
    589          */
    590         public boolean hasSameValueAsOpposite(String key) {
    591             String value = getValue(key);
    592             String oppositeValue = getOppositeValue(key);
    593             return value != null && value.equals(oppositeValue);
    594         }
    595 
    596         /**
    597          * Returns the type of point in time.
    598          * @return the type of point in time
    599          */
    600         public PointInTimeType getPointInTimeType() {
    601             return pointInTimeType;
    602         }
    603 
    604         /**
    605          * Determines if this is the current point in time.
    606          * @return {@code true} if this is the current point in time
    607          */
    608         public boolean isCurrentPointInTime() {
    609             return pointInTimeType.equals(PointInTimeType.CURRENT_POINT_IN_TIME);
    610         }
    611 
    612         /**
    613          * Determines if this is the reference point in time.
    614          * @return {@code true} if this is the reference point in time
    615          */
    616         public boolean isReferencePointInTime() {
    617             return pointInTimeType.equals(PointInTimeType.REFERENCE_POINT_IN_TIME);
    618         }
    619 
    620         @Override
    621         public int getColumnCount() {
    622             return 2;
    623         }
     487     * Returns the key set (union of current and reference point in type key sets).
     488     * @return the key set (union of current and reference point in type key sets)
     489     * @since 11647
     490     */
     491    public Set<String> getKeySet() {
     492        Set<String> keySet = new HashSet<>();
     493        if (current != null) {
     494            keySet.addAll(current.getTags().keySet());
     495        }
     496        if (reference != null) {
     497            keySet.addAll(reference.getTags().keySet());
     498        }
     499        return keySet;
    624500    }
    625501
  • trunk/src/org/openstreetmap/josm/gui/history/TagInfoTransferHandler.java

    r10755 r11647  
    1313import org.openstreetmap.josm.gui.datatransfer.TagTransferable;
    1414import org.openstreetmap.josm.gui.datatransfer.data.TagTransferData;
    15 import org.openstreetmap.josm.gui.history.HistoryBrowserModel.TagTableModel;
    1615
    1716/**
  • trunk/src/org/openstreetmap/josm/gui/history/TagTableCellRenderer.java

    r10670 r11647  
    3333    }
    3434
    35     protected void setBackgroundReadable(String key, HistoryBrowserModel.TagTableModel model, boolean isSelected, boolean hasFocus,
    36             boolean isValue) {
     35    protected void setBackgroundReadable(String key, TagTableModel model, boolean isSelected, boolean hasFocus, boolean isValue) {
    3736        Color bgColor = UIManager.getColor("Table.background");
    3837        if ((!model.hasTag(key) && model.isCurrentPointInTime())
     
    6463
    6564        String key = (String) value;
    66         HistoryBrowserModel.TagTableModel model = getTagTableModel(table);
     65        TagTableModel model = getTagTableModel(table);
    6766
    6867        String text = "";
     
    8786    }
    8887
    89     protected HistoryBrowserModel.TagTableModel getTagTableModel(JTable table) {
    90         return (HistoryBrowserModel.TagTableModel) table.getModel();
     88    protected TagTableModel getTagTableModel(JTable table) {
     89        return (TagTableModel) table.getModel();
    9190    }
    9291}
  • trunk/test/unit/org/openstreetmap/josm/gui/history/HistoryBrowserModelTest.java

    r11646 r11647  
    1717import org.openstreetmap.josm.data.osm.history.HistoryDataSet;
    1818import org.openstreetmap.josm.data.osm.history.HistoryNode;
    19 import org.openstreetmap.josm.gui.history.HistoryBrowserModel.TagTableModel;
    2019import org.openstreetmap.josm.testutils.JOSMTestRules;
    2120
Note: See TracChangeset for help on using the changeset viewer.