| | 66 | |
| | 67 | static class MemberListPopupMenu extends JPopupMenu { |
| | 68 | private final ZoomToMemberAction zoomToMemberAction; |
| | 69 | private final ShowHistoryAction showHistoryAction; |
| | 70 | |
| | 71 | MemberListPopupMenu() { |
| | 72 | zoomToMemberAction = new ZoomToMemberAction(); |
| | 73 | add(zoomToMemberAction); |
| | 74 | showHistoryAction = new ShowHistoryAction(); |
| | 75 | add(showHistoryAction); |
| | 76 | } |
| | 77 | |
| | 78 | void prepare(PrimitiveId pid) { |
| | 79 | zoomToMemberAction.setPrimitiveId(pid); |
| | 80 | zoomToMemberAction.updateEnabledState(); |
| | 81 | |
| | 82 | showHistoryAction.setPrimitiveId(pid); |
| | 83 | showHistoryAction.updateEnabledState(); |
| | 84 | } |
| | 85 | } |
| | 86 | |
| | 87 | static class ZoomToMemberAction extends AbstractAction { |
| | 88 | private transient PrimitiveId primitiveId; |
| | 89 | |
| | 90 | /** |
| | 91 | * Constructs a new {@code ZoomToMemberAction}. |
| | 92 | */ |
| | 93 | ZoomToMemberAction() { |
| | 94 | putValue(NAME, tr("Zoom to member")); |
| | 95 | putValue(SHORT_DESCRIPTION, tr("Zoom to this member in the current data layer")); |
| | 96 | new ImageProvider("dialogs", "zoomin").getResource().attachImageIcon(this, true); |
| | 97 | } |
| | 98 | |
| | 99 | @Override |
| | 100 | public void actionPerformed(ActionEvent e) { |
| | 101 | if (!isEnabled()) |
| | 102 | return; |
| | 103 | IPrimitive p = getPrimitiveToZoom(); |
| | 104 | if (p != null && p.isSelectable()) { |
| | 105 | p.getDataSet().setSelected(p); |
| | 106 | AutoScaleAction.autoScale(AutoScaleMode.SELECTION); |
| | 107 | } |
| | 108 | } |
| | 109 | |
| | 110 | public void setPrimitiveId(PrimitiveId pid) { |
| | 111 | this.primitiveId = pid; |
| | 112 | updateEnabledState(); |
| | 113 | } |
| | 114 | |
| | 115 | protected IPrimitive getPrimitiveToZoom() { |
| | 116 | if (primitiveId == null) |
| | 117 | return null; |
| | 118 | OsmData<?, ?, ?, ?> ds = MainApplication.getLayerManager().getActiveData(); |
| | 119 | if (ds == null) |
| | 120 | return null; |
| | 121 | return ds.getPrimitiveById(primitiveId); |
| | 122 | } |
| | 123 | |
| | 124 | void updateEnabledState() { |
| | 125 | IPrimitive p = getPrimitiveToZoom(); |
| | 126 | setEnabled(p != null && p.isSelectable()); |
| | 127 | } |
| | 128 | } |
| | 129 | |
| | 130 | private static PrimitiveId primitiveIdAtRow(DiffTableModel model, int row) { |
| | 131 | if (row < 0) |
| | 132 | return null; |
| | 133 | RelationMemberData rm = (RelationMemberData) model.getValueAt(row, 0).value; |
| | 134 | if (rm == null) |
| | 135 | return null; |
| | 136 | return new SimplePrimitiveId(rm.getUniqueId(), rm.getType()); |
| | 137 | } |
| | 138 | |
| | 139 | static class InternalPopupMenuLauncher extends PopupMenuLauncher { |
| | 140 | InternalPopupMenuLauncher() { |
| | 141 | super(new MemberListPopupMenu()); |
| | 142 | } |
| | 143 | |
| | 144 | @Override |
| | 145 | protected int checkTableSelection(JTable table, Point p) { |
| | 146 | int row = super.checkTableSelection(table, p); |
| | 147 | ((MemberListPopupMenu) menu).prepare(primitiveIdAtRow((DiffTableModel) table.getModel(), row)); |
| | 148 | return row; |
| | 149 | } |
| | 150 | } |
| | 151 | |
| | 152 | |