Changeset 11646 in josm for trunk/src/org/openstreetmap


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

see #14175 - refactor HistoryBrowserModel

Location:
trunk/src/org/openstreetmap/josm/gui/history
Files:
1 added
3 edited

Legend:

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

    r10791 r11646  
    104104
    105105        btn = new JButton(closeAction);
    106         final String closeHistoryBrowserDialogKey = "CloseHistoryBrowserDialog";
    107106        btn.setName("btn.close");
    108107        pnl.add(btn);
     
    158157        }
    159158
    160         public void run() {
     159        void run() {
    161160            getHistoryBrowser().getModel().unlinkAsListener();
    162161            HistoryDataSet.getInstance().removeHistoryDataSetListener(HistoryBrowserDialog.this);
  • trunk/src/org/openstreetmap/josm/gui/history/HistoryBrowserModel.java

    r11608 r11646  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.text.DateFormat;
    76import java.util.ArrayList;
    87import java.util.Collections;
     
    1615
    1716import org.openstreetmap.josm.Main;
    18 import org.openstreetmap.josm.data.osm.Changeset;
    1917import org.openstreetmap.josm.data.osm.Node;
    2018import org.openstreetmap.josm.data.osm.OsmPrimitive;
     
    4846import org.openstreetmap.josm.gui.util.ChangeNotifier;
    4947import org.openstreetmap.josm.tools.CheckParameterUtil;
    50 import org.openstreetmap.josm.tools.date.DateUtils;
    5148
    5249/**
     
    9895     */
    9996    public HistoryBrowserModel() {
    100         versionTableModel = new VersionTableModel();
     97        versionTableModel = new VersionTableModel(this);
    10198        currentTagTableModel = new TagTableModel(PointInTimeType.CURRENT_POINT_IN_TIME);
    10299        referenceTagTableModel = new TagTableModel(PointInTimeType.REFERENCE_POINT_IN_TIME);
     
    135132    }
    136133
    137     protected boolean canShowAsLatest(OsmPrimitive primitive) {
     134    private boolean canShowAsLatest(OsmPrimitive primitive) {
    138135        if (primitive == null)
    139136            return false;
     
    197194    }
    198195
    199     protected void fireModelChange() {
     196    private void fireModelChange() {
    200197        initNodeListTableModels();
    201198        initMemberListTableModels();
     
    214211    }
    215212
    216     protected void initTagTableModels() {
     213    private void initTagTableModels() {
    217214        currentTagTableModel.initKeyList();
    218215        referenceTagTableModel.initKeyList();
     
    223220     * TODO: Maybe rename to reflect this? eg. updateNodeListTableModels
    224221     */
    225     protected void initNodeListTableModels() {
     222    private void initNodeListTableModels() {
    226223        if (current == null || current.getType() != OsmPrimitiveType.WAY
    227224         || reference == null || reference.getType() != OsmPrimitiveType.WAY)
     
    234231    }
    235232
    236     protected void initMemberListTableModels() {
     233    private void initMemberListTableModels() {
    237234        if (current == null || current.getType() != OsmPrimitiveType.RELATION
    238235         || reference == null || reference.getType() != OsmPrimitiveType.RELATION)
     
    397394
    398395    /**
    399      * The table model for the list of versions in the current history
    400      *
    401      */
    402     public final class VersionTableModel extends AbstractTableModel {
    403 
    404         private VersionTableModel() {
    405         }
    406 
    407         @Override
    408         public int getRowCount() {
    409             if (history == null)
    410                 return 0;
    411             int ret = history.getNumVersions();
     396     * Sets the reference point in time to the given row.
     397     * @param row row number
     398     */
     399    public void setReferencePointInTime(int row) {
     400        if (history == null)
     401            return;
     402        if (row == history.getNumVersions()) {
    412403            if (latest != null) {
    413                 ret++;
    414             }
    415             return ret;
    416         }
    417 
    418         @Override
    419         public Object getValueAt(int row, int column) {
    420             switch (column) {
    421             case VersionTableColumnModel.COL_VERSION:
    422                 HistoryOsmPrimitive p1 = getPrimitive(row);
    423                 if (p1 != null)
    424                     return Long.toString(p1.getVersion());
    425                 return null;
    426             case VersionTableColumnModel.COL_REFERENCE:
    427                 return isReferencePointInTime(row);
    428             case VersionTableColumnModel.COL_CURRENT:
    429                 return isCurrentPointInTime(row);
    430             case VersionTableColumnModel.COL_DATE:
    431                 HistoryOsmPrimitive p3 = getPrimitive(row);
    432                 if (p3 != null && p3.getTimestamp() != null)
    433                     return DateUtils.formatDateTime(p3.getTimestamp(), DateFormat.SHORT, DateFormat.SHORT);
    434                 return null;
    435             case VersionTableColumnModel.COL_USER:
    436                 HistoryOsmPrimitive p4 = getPrimitive(row);
    437                 if (p4 != null) {
    438                     User user = p4.getUser();
    439                     if (user != null)
    440                         return user.getName();
    441                 }
    442                 return null;
    443             case VersionTableColumnModel.COL_EDITOR:
    444                 HistoryOsmPrimitive p5 = getPrimitive(row);
    445                 if (p5 != null) {
    446                     Changeset cs = p5.getChangeset();
    447                     if (cs != null) {
    448                         return cs.get("created_by");
    449                     }
    450                 }
    451                 return null;
    452             }
     404                setReferencePointInTime(latest);
     405            }
     406            return;
     407        }
     408        if (row < 0 || row > history.getNumVersions())
     409            return;
     410        setReferencePointInTime(history.get(row));
     411    }
     412
     413    /**
     414     * Sets the current point in time to the given row.
     415     * @param row row number
     416     */
     417    public void setCurrentPointInTime(int row) {
     418        if (history == null)
     419            return;
     420        if (row == history.getNumVersions()) {
     421            if (latest != null) {
     422                setCurrentPointInTime(latest);
     423            }
     424            return;
     425        }
     426        if (row < 0 || row > history.getNumVersions())
     427            return;
     428        setCurrentPointInTime(history.get(row));
     429    }
     430
     431    /**
     432     * Determines if the given row is the reference point in time.
     433     * @param row row number
     434     * @return {@code true} if the given row is the reference point in time
     435     */
     436    public boolean isReferencePointInTime(int row) {
     437        if (history == null)
     438            return false;
     439        if (row == history.getNumVersions())
     440            return latest == reference;
     441        if (row < 0 || row > history.getNumVersions())
     442            return false;
     443        return history.get(row) == reference;
     444    }
     445
     446    /**
     447     * Determines if the given row is the current point in time.
     448     * @param row row number
     449     * @return {@code true} if the given row is the current point in time
     450     */
     451    public boolean isCurrentPointInTime(int row) {
     452        if (history == null)
     453            return false;
     454        if (row == history.getNumVersions())
     455            return latest == current;
     456        if (row < 0 || row > history.getNumVersions())
     457            return false;
     458        return history.get(row) == current;
     459    }
     460
     461    /**
     462     * Returns the {@code HistoryPrimitive} at the given row.
     463     * @param row row number
     464     * @return the {@code HistoryPrimitive} at the given row
     465     */
     466    public HistoryOsmPrimitive getPrimitive(int row) {
     467        if (history == null)
    453468            return null;
    454         }
    455 
    456         @Override
    457         public void setValueAt(Object aValue, int row, int column) {
    458             if (!((Boolean) aValue))
    459                 return;
    460             switch (column) {
    461             case 1:
    462                 setReferencePointInTime(row);
    463                 break;
    464             case 2:
    465                 setCurrentPointInTime(row);
    466                 break;
    467             default:
    468                 return;
    469             }
    470             fireTableDataChanged();
    471         }
    472 
    473         @Override
    474         public boolean isCellEditable(int row, int column) {
    475             return column >= 1 && column <= 2;
    476         }
    477 
    478         public void setReferencePointInTime(int row) {
    479             if (history == null)
    480                 return;
    481             if (row == history.getNumVersions()) {
    482                 if (latest != null) {
    483                     HistoryBrowserModel.this.setReferencePointInTime(latest);
    484                 }
    485                 return;
    486             }
    487             if (row < 0 || row > history.getNumVersions())
    488                 return;
    489             HistoryOsmPrimitive reference = history.get(row);
    490             HistoryBrowserModel.this.setReferencePointInTime(reference);
    491         }
    492 
    493         public void setCurrentPointInTime(int row) {
    494             if (history == null)
    495                 return;
    496             if (row == history.getNumVersions()) {
    497                 if (latest != null) {
    498                     HistoryBrowserModel.this.setCurrentPointInTime(latest);
    499                 }
    500                 return;
    501             }
    502             if (row < 0 || row > history.getNumVersions())
    503                 return;
    504             HistoryOsmPrimitive current = history.get(row);
    505             HistoryBrowserModel.this.setCurrentPointInTime(current);
    506         }
    507 
    508         public boolean isReferencePointInTime(int row) {
    509             if (history == null)
    510                 return false;
    511             if (row == history.getNumVersions())
    512                 return latest == reference;
    513             if (row < 0 || row > history.getNumVersions())
    514                 return false;
    515             HistoryOsmPrimitive p = history.get(row);
    516             return p == reference;
    517         }
    518 
    519         public boolean isCurrentPointInTime(int row) {
    520             if (history == null)
    521                 return false;
    522             if (row == history.getNumVersions())
    523                 return latest == current;
    524             if (row < 0 || row > history.getNumVersions())
    525                 return false;
    526             HistoryOsmPrimitive p = history.get(row);
    527             return p == current;
    528         }
    529 
    530         public HistoryOsmPrimitive getPrimitive(int row) {
    531             if (history == null)
    532                 return null;
    533             return isLatest(row) ? latest : history.get(row);
    534         }
    535 
    536         public boolean isLatest(int row) {
    537             return row >= history.getNumVersions();
    538         }
    539 
    540         public OsmPrimitive getLatest() {
    541             if (latest == null)
    542                 return null;
    543             OsmDataLayer editLayer = Main.getLayerManager().getEditLayer();
    544             if (editLayer == null)
    545                 return null;
    546             return editLayer.data.getPrimitiveById(latest.getId(), latest.getType());
    547         }
    548 
    549         @Override
    550         public int getColumnCount() {
    551             return 6;
    552         }
     469        return isLatest(row) ? latest : history.get(row);
     470    }
     471
     472    /**
     473     * Determines if the given row is the latest.
     474     * @param row row number
     475     * @return {@code true} if the given row is the latest
     476     */
     477    public boolean isLatest(int row) {
     478        return row >= history.getNumVersions();
     479    }
     480
     481    /**
     482     * Returns the latest {@code HistoryOsmPrimitive}.
     483     * @return the latest {@code HistoryOsmPrimitive}
     484     */
     485    public HistoryOsmPrimitive getLatest() {
     486        return latest;
    553487    }
    554488
     
    558492     *
    559493     */
    560     public class TagTableModel extends AbstractTableModel {
     494    public final class TagTableModel extends AbstractTableModel {
    561495
    562496        private List<String> keys;
    563497        private final PointInTimeType pointInTimeType;
    564498
    565         protected TagTableModel(PointInTimeType type) {
     499        private TagTableModel(PointInTimeType type) {
    566500            pointInTimeType = type;
    567501            initKeyList();
    568502        }
    569503
    570         protected void initKeyList() {
     504        private void initKeyList() {
    571505            Set<String> keySet = new HashSet<>();
    572506            if (current != null) {
     
    603537        }
    604538
     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         */
    605544        public boolean hasTag(String key) {
    606545            HistoryOsmPrimitive primitive = getPointInTime(pointInTimeType);
     
    608547        }
    609548
     549        /**
     550         * Returns the tag value for the given key.
     551         * @param key tag key
     552         * @return tag value, or null
     553         */
    610554        public String getValue(String key) {
    611555            HistoryOsmPrimitive primitive = getPointInTime(pointInTimeType);
     
    615559        }
    616560
     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         */
    617566        public boolean oppositeHasTag(String key) {
    618567            PointInTimeType opposite = pointInTimeType.opposite();
     
    621570        }
    622571
     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         */
    623577        public String getOppositeValue(String key) {
    624578            PointInTimeType opposite = pointInTimeType.opposite();
     
    629583        }
    630584
     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         */
    631590        public boolean hasSameValueAsOpposite(String key) {
    632591            String value = getValue(key);
     
    635594        }
    636595
     596        /**
     597         * Returns the type of point in time.
     598         * @return the type of point in time
     599         */
    637600        public PointInTimeType getPointInTimeType() {
    638601            return pointInTimeType;
    639602        }
    640603
     604        /**
     605         * Determines if this is the current point in time.
     606         * @return {@code true} if this is the current point in time
     607         */
    641608        public boolean isCurrentPointInTime() {
    642609            return pointInTimeType.equals(PointInTimeType.CURRENT_POINT_IN_TIME);
    643610        }
    644611
     612        /**
     613         * Determines if this is the reference point in time.
     614         * @return {@code true} if this is the reference point in time
     615         */
    645616        public boolean isReferencePointInTime() {
    646617            return pointInTimeType.equals(PointInTimeType.REFERENCE_POINT_IN_TIME);
     
    653624    }
    654625
     626    /**
     627     * Sets the latest {@code HistoryOsmPrimitive}.
     628     * @param latest the latest {@code HistoryOsmPrimitive}
     629     */
    655630    protected void setLatest(HistoryOsmPrimitive latest) {
    656631        if (latest == null) {
     
    744719            return;
    745720        OsmPrimitive primitive = event.getDataset().getPrimitiveById(history.getId(), history.getType());
    746         HistoryOsmPrimitive latest;
     721        HistoryOsmPrimitive newLatest;
    747722        if (canShowAsLatest(primitive)) {
    748             latest = new HistoryPrimitiveBuilder().build(primitive);
     723            newLatest = new HistoryPrimitiveBuilder().build(primitive);
    749724        } else {
    750             latest = null;
    751         }
    752         setLatest(latest);
     725            newLatest = null;
     726        }
     727        setLatest(newLatest);
    753728        fireModelChange();
    754729    }
     
    825800        }
    826801
    827         public HistoryOsmPrimitive build(OsmPrimitive primitive) {
     802        HistoryOsmPrimitive build(OsmPrimitive primitive) {
    828803            primitive.accept(this);
    829804            return clone;
  • trunk/src/org/openstreetmap/josm/gui/history/VersionTable.java

    r11322 r11646  
    5858    }
    5959
     60    /**
     61     * Builds the table.
     62     */
    6063    protected void build() {
    6164        getTableHeader().setFont(getTableHeader().getFont().deriveFont(9f));
     
    108111    }
    109112
    110     protected HistoryBrowserModel.VersionTableModel getVersionTableModel() {
    111         return (HistoryBrowserModel.VersionTableModel) getModel();
    112     }
    113 
    114113    @Override
    115114    public void stateChanged(ChangeEvent e) {
     
    129128                int col = columnAtPoint(e.getPoint());
    130129                if (row >= 0 && (col == VersionTableColumnModel.COL_DATE || col == VersionTableColumnModel.COL_USER)) {
    131                     model.getVersionTableModel().setCurrentPointInTime(row);
    132                     model.getVersionTableModel().setReferencePointInTime(Math.max(0, row - 1));
     130                    model.setCurrentPointInTime(row);
     131                    model.setReferencePointInTime(Math.max(0, row - 1));
    133132                }
    134133            }
     
    137136        @Override
    138137        protected int checkTableSelection(JTable table, Point p) {
    139             HistoryBrowserModel.VersionTableModel tableModel = getVersionTableModel();
    140138            int row = rowAtPoint(p);
    141             if (row > -1 && !tableModel.isLatest(row)) {
    142                 popupMenu.prepare(tableModel.getPrimitive(row));
     139            if (row > -1 && !model.isLatest(row)) {
     140                popupMenu.prepare(model.getPrimitive(row));
    143141            }
    144142            return row;
     
    315313        }
    316314
    317         // for unit tests
    318         private AlignedRenderer() {
     315        AlignedRenderer() {
    319316            this(SwingConstants.LEFT);
    320317        }
Note: See TracChangeset for help on using the changeset viewer.