Changeset 16473 in josm


Ignore:
Timestamp:
2020-05-23T08:08:11+02:00 (2 weeks ago)
Author:
GerdP
Message:

fix #19260: Show menu on right-click in History of relation members
Common code for NodeListViewer and RelationMemberListViewer was moved to HistoryViewerPanel

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

Legend:

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

    r15772 r16473  
    44import java.awt.GridBagConstraints;
    55import java.awt.Insets;
     6import java.awt.event.ActionEvent;
    67
     8import javax.swing.AbstractAction;
     9import javax.swing.JPopupMenu;
    710import javax.swing.JScrollPane;
    811import javax.swing.JTable;
    912
     13import org.openstreetmap.josm.actions.AutoScaleAction;
     14import org.openstreetmap.josm.actions.AutoScaleAction.AutoScaleMode;
     15import org.openstreetmap.josm.data.osm.IPrimitive;
     16import org.openstreetmap.josm.data.osm.OsmData;
     17import org.openstreetmap.josm.data.osm.PrimitiveId;
     18import org.openstreetmap.josm.gui.MainApplication;
    1019import org.openstreetmap.josm.gui.util.AdjustmentSynchronizer;
     20import org.openstreetmap.josm.tools.ImageProvider;
    1121
    1222/**
     
    8393        add(embedInScrollPane(buildTable(PointInTimeType.CURRENT_POINT_IN_TIME)), gc);
    8494    }
     95
     96    static class ListPopupMenu extends JPopupMenu {
     97        private final ZoomToObjectAction zoomToObjectAction;
     98        private final ShowHistoryAction showHistoryAction;
     99
     100        ListPopupMenu(String name, String shortDescription) {
     101            zoomToObjectAction = new ZoomToObjectAction(name, shortDescription);
     102            add(zoomToObjectAction);
     103            showHistoryAction = new ShowHistoryAction();
     104            add(showHistoryAction);
     105        }
     106
     107        void prepare(PrimitiveId pid) {
     108            zoomToObjectAction.setPrimitiveId(pid);
     109            zoomToObjectAction.updateEnabledState();
     110
     111            showHistoryAction.setPrimitiveId(pid);
     112            showHistoryAction.updateEnabledState();
     113        }
     114    }
     115
     116    static class ZoomToObjectAction extends AbstractAction {
     117        private transient PrimitiveId primitiveId;
     118
     119        /**
     120         * Constructs a new {@code ZoomToObjectAction}.
     121         * @param name  name for the action
     122         * @param shortDescription The key used for storing a short <code>String</code> description for the action, used for tooltip text.
     123         */
     124        ZoomToObjectAction(String name, String shortDescription) {
     125            putValue(NAME, name);
     126            putValue(SHORT_DESCRIPTION, shortDescription);
     127            new ImageProvider("dialogs", "zoomin").getResource().attachImageIcon(this, true);
     128        }
     129
     130        @Override
     131        public void actionPerformed(ActionEvent e) {
     132            if (!isEnabled())
     133                return;
     134            IPrimitive p = getPrimitiveToZoom();
     135            if (p != null && p.isSelectable()) {
     136                p.getDataSet().setSelected(p);
     137                AutoScaleAction.autoScale(AutoScaleMode.SELECTION);
     138            }
     139        }
     140
     141        public void setPrimitiveId(PrimitiveId pid) {
     142            this.primitiveId = pid;
     143            updateEnabledState();
     144        }
     145
     146        protected IPrimitive getPrimitiveToZoom() {
     147            if (primitiveId == null)
     148                return null;
     149            OsmData<?, ?, ?, ?> ds = MainApplication.getLayerManager().getActiveData();
     150            if (ds == null)
     151                return null;
     152            return ds.getPrimitiveById(primitiveId);
     153        }
     154
     155        public void updateEnabledState() {
     156            setEnabled(MainApplication.getLayerManager().getActiveData() != null && getPrimitiveToZoom() != null);
     157        }
     158    }
    85159}
  • trunk/src/org/openstreetmap/josm/gui/history/NodeListViewer.java

    r16465 r16473  
    55
    66import java.awt.Point;
    7 import java.awt.event.ActionEvent;
    87
    9 import javax.swing.AbstractAction;
    10 import javax.swing.JPopupMenu;
    118import javax.swing.JTable;
    129import javax.swing.ListSelectionModel;
    1310
    14 import org.openstreetmap.josm.actions.AutoScaleAction;
    15 import org.openstreetmap.josm.actions.AutoScaleAction.AutoScaleMode;
    16 import org.openstreetmap.josm.data.osm.IPrimitive;
    17 import org.openstreetmap.josm.data.osm.OsmData;
    1811import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1912import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
     
    2114import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
    2215import org.openstreetmap.josm.data.osm.history.History;
    23 import org.openstreetmap.josm.gui.MainApplication;
    2416import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
    25 import org.openstreetmap.josm.tools.ImageProvider;
    2617
    2718/**
     
    5647        table.addMouseListener(new ShowHistoryAction.DoubleClickAdapter(e -> {
    5748            int row = table.rowAtPoint(e.getPoint());
    58             return row < 0 ? null : primitiveIdAtRow(tableModel, row);
     49            return primitiveIdAtRow(tableModel, row);
    5950        }));
    6051        return table;
    6152    }
    6253
    63     static class NodeListPopupMenu extends JPopupMenu {
    64         private final ZoomToNodeAction zoomToNodeAction;
    65         private final ShowHistoryAction showHistoryAction;
    66 
    67         NodeListPopupMenu() {
    68             zoomToNodeAction = new ZoomToNodeAction();
    69             add(zoomToNodeAction);
    70             showHistoryAction = new ShowHistoryAction();
    71             add(showHistoryAction);
    72         }
    73 
    74         void prepare(PrimitiveId pid) {
    75             zoomToNodeAction.setPrimitiveId(pid);
    76             zoomToNodeAction.updateEnabledState();
    77 
    78             showHistoryAction.setPrimitiveId(pid);
    79             showHistoryAction.updateEnabledState();
    80         }
    81     }
    82 
    83     static class ZoomToNodeAction extends AbstractAction {
    84         private transient PrimitiveId primitiveId;
    85 
    86         /**
    87          * Constructs a new {@code ZoomToNodeAction}.
    88          */
    89         ZoomToNodeAction() {
    90             putValue(NAME, tr("Zoom to node"));
    91             putValue(SHORT_DESCRIPTION, tr("Zoom to this node in the current data layer"));
    92             new ImageProvider("dialogs", "zoomin").getResource().attachImageIcon(this, true);
    93         }
    94 
    95         @Override
    96         public void actionPerformed(ActionEvent e) {
    97             if (!isEnabled())
    98                 return;
    99             IPrimitive p = getPrimitiveToZoom();
    100             if (p != null && p.isSelectable()) {
    101                 p.getDataSet().setSelected(p);
    102                 AutoScaleAction.autoScale(AutoScaleMode.SELECTION);
    103             }
    104         }
    105 
    106         public void setPrimitiveId(PrimitiveId pid) {
    107             this.primitiveId = pid;
    108             updateEnabledState();
    109         }
    110 
    111         protected IPrimitive getPrimitiveToZoom() {
    112             if (primitiveId == null)
    113                 return null;
    114             OsmData<?, ?, ?, ?> ds = MainApplication.getLayerManager().getActiveData();
    115             if (ds == null)
    116                 return null;
    117             return ds.getPrimitiveById(primitiveId);
    118         }
    119 
    120         public void updateEnabledState() {
    121             setEnabled(MainApplication.getLayerManager().getActiveData() != null && getPrimitiveToZoom() != null);
    122         }
    123     }
    124 
    12554    private static PrimitiveId primitiveIdAtRow(DiffTableModel model, int row) {
     55        if (row < 0)
     56            return null;
    12657        Long id = (Long) model.getValueAt(row, 0).value;
    12758        return id == null ? null : new SimplePrimitiveId(id, OsmPrimitiveType.NODE);
     
    13061    static class InternalPopupMenuLauncher extends PopupMenuLauncher {
    13162        InternalPopupMenuLauncher() {
    132             super(new NodeListPopupMenu());
     63            super(new ListPopupMenu(tr("Zoom to node"), tr("Zoom to this node in the current data layer")));
    13364        }
    13465
     
    13667        protected int checkTableSelection(JTable table, Point p) {
    13768            int row = super.checkTableSelection(table, p);
    138             ((NodeListPopupMenu) menu).prepare(primitiveIdAtRow((DiffTableModel) table.getModel(), row));
     69            ((ListPopupMenu) menu).prepare(primitiveIdAtRow((DiffTableModel) table.getModel(), row));
    13970            return row;
    14071        }
  • trunk/src/org/openstreetmap/josm/gui/history/RelationMemberListViewer.java

    r16460 r16473  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.awt.Point;
    67import java.awt.Rectangle;
    78
     
    910import javax.swing.ListSelectionModel;
    1011
     12import org.openstreetmap.josm.data.osm.PrimitiveId;
    1113import org.openstreetmap.josm.data.osm.RelationMemberData;
     14import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
     15import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
    1216
    1317/**
     
    2529    @Override
    2630    protected JTable buildTable(PointInTimeType pointInTimeType) {
    27         DiffTableModel tableModel = model.getRelationMemberTableModel(pointInTimeType);
    28         RelationMemberTableColumnModel columnModel = new RelationMemberTableColumnModel();
    29         JTable table = new JTable(tableModel, columnModel);
     31        final DiffTableModel tableModel = model.getRelationMemberTableModel(pointInTimeType);
     32        final RelationMemberTableColumnModel columnModel = new RelationMemberTableColumnModel();
     33        final JTable table = new JTable(tableModel, columnModel);
    3034        tableModel.addTableModelListener(new ReversedChangeListener(
    3135                table, columnModel, tr("The members of this relation are in reverse order")));
    3236        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    3337        selectionSynchronizer.participateInSynchronizedSelection(table.getSelectionModel());
     38        table.addMouseListener(new InternalPopupMenuLauncher());
    3439        table.getModel().addTableModelListener(e -> {
    3540            Rectangle rect = table.getCellRect(((DiffTableModel) e.getSource()).getFirstChange(), 0, true);
     
    3843        table.addMouseListener(new ShowHistoryAction.DoubleClickAdapter(e -> {
    3944            int row = table.rowAtPoint(e.getPoint());
    40             return row < 0 ? null : (RelationMemberData) tableModel.getValueAt(row, 0).value;
     45            return primitiveIdAtRow(tableModel, row);
    4146        }));
    4247        return table;
     
    5055        super(model);
    5156    }
     57
     58    private static PrimitiveId primitiveIdAtRow(DiffTableModel model, int row) {
     59        if (row < 0)
     60            return null;
     61        RelationMemberData rm = (RelationMemberData) model.getValueAt(row, 0).value;
     62        if (rm == null)
     63            return null;
     64        return new SimplePrimitiveId(rm.getUniqueId(), rm.getType());
     65    }
     66
     67    static class InternalPopupMenuLauncher extends PopupMenuLauncher {
     68        InternalPopupMenuLauncher() {
     69            super(new ListPopupMenu(tr("Zoom to member"), tr("Zoom to this member in the current data layer")));
     70
     71        }
     72
     73        @Override
     74        protected int checkTableSelection(JTable table, Point p) {
     75            int row = super.checkTableSelection(table, p);
     76            ((ListPopupMenu) menu).prepare(primitiveIdAtRow((DiffTableModel) table.getModel(), row));
     77            return row;
     78        }
     79    }
     80
    5281}
Note: See TracChangeset for help on using the changeset viewer.