[7937] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.awt.event.ActionEvent;
|
---|
| 7 |
|
---|
| 8 | import javax.swing.AbstractAction;
|
---|
| 9 | import javax.swing.event.ListSelectionEvent;
|
---|
| 10 | import javax.swing.event.ListSelectionListener;
|
---|
| 11 |
|
---|
| 12 | import org.openstreetmap.josm.Main;
|
---|
| 13 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 14 | import org.openstreetmap.josm.gui.conflict.pair.nodes.NodeListTable;
|
---|
| 15 | import org.openstreetmap.josm.gui.conflict.pair.relation.RelationMemberTable;
|
---|
| 16 | import org.openstreetmap.josm.gui.dialogs.relation.MemberTable;
|
---|
[10386] | 17 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
|
---|
| 18 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener;
|
---|
| 19 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent;
|
---|
| 20 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
|
---|
| 21 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
|
---|
| 22 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
|
---|
[7937] | 23 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
| 24 | import org.openstreetmap.josm.gui.widgets.OsmPrimitivesTable;
|
---|
| 25 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
| 26 |
|
---|
[10386] | 27 | public class ZoomToAction extends AbstractAction implements LayerChangeListener, ActiveLayerChangeListener, ListSelectionListener {
|
---|
[7937] | 28 |
|
---|
| 29 | private final OsmPrimitivesTable table;
|
---|
[8443] | 30 |
|
---|
[7937] | 31 | private final String descriptionNominal;
|
---|
| 32 | private final String descriptionInactiveLayer;
|
---|
| 33 | private final String descriptionNoSelection;
|
---|
[8443] | 34 |
|
---|
[7937] | 35 | public ZoomToAction(OsmPrimitivesTable table, String descriptionNominal, String descriptionInactiveLayer, String descriptionNoSelection) {
|
---|
| 36 | CheckParameterUtil.ensureParameterNotNull(table);
|
---|
| 37 | this.table = table;
|
---|
| 38 | this.descriptionNominal = descriptionNominal;
|
---|
| 39 | this.descriptionInactiveLayer = descriptionInactiveLayer;
|
---|
| 40 | this.descriptionNoSelection = descriptionNoSelection;
|
---|
| 41 | putValue(NAME, tr("Zoom to"));
|
---|
| 42 | putValue(SHORT_DESCRIPTION, descriptionNominal);
|
---|
| 43 | updateEnabledState();
|
---|
| 44 | }
|
---|
[8443] | 45 |
|
---|
[7937] | 46 | public ZoomToAction(MemberTable table) {
|
---|
[8443] | 47 | this(table,
|
---|
[7937] | 48 | tr("Zoom to the object the first selected member refers to"),
|
---|
| 49 | tr("Zooming disabled because layer of this relation is not active"),
|
---|
| 50 | tr("Zooming disabled because there is no selected member"));
|
---|
| 51 | }
|
---|
[8443] | 52 |
|
---|
[7937] | 53 | public ZoomToAction(RelationMemberTable table) {
|
---|
[8443] | 54 | this(table,
|
---|
[7937] | 55 | tr("Zoom to the object the first selected member refers to"),
|
---|
| 56 | tr("Zooming disabled because layer of this relation is not active"),
|
---|
| 57 | tr("Zooming disabled because there is no selected member"));
|
---|
| 58 | }
|
---|
[8443] | 59 |
|
---|
[7937] | 60 | public ZoomToAction(NodeListTable table) {
|
---|
[8443] | 61 | this(table,
|
---|
[7937] | 62 | tr("Zoom to the first selected node"),
|
---|
| 63 | tr("Zooming disabled because layer of this way is not active"),
|
---|
| 64 | tr("Zooming disabled because there is no selected node"));
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | @Override
|
---|
| 68 | public void actionPerformed(ActionEvent e) {
|
---|
[8443] | 69 | if (!isEnabled())
|
---|
[7937] | 70 | return;
|
---|
| 71 | int[] rows = this.table.getSelectedRows();
|
---|
[11381] | 72 | if (rows.length == 0)
|
---|
[7937] | 73 | return;
|
---|
| 74 | int row = rows[0];
|
---|
| 75 | OsmDataLayer layer = this.table.getLayer();
|
---|
| 76 | OsmPrimitive primitive = this.table.getPrimitiveInLayer(row, layer);
|
---|
| 77 | if (layer != null && primitive != null) {
|
---|
| 78 | layer.data.setSelected(primitive);
|
---|
| 79 | AutoScaleAction.autoScale("selection");
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | protected final void updateEnabledState() {
|
---|
[10413] | 84 | if (Main.main == null || Main.getLayerManager().getEditLayer() != this.table.getLayer()) {
|
---|
[7937] | 85 | setEnabled(false);
|
---|
| 86 | putValue(SHORT_DESCRIPTION, descriptionInactiveLayer);
|
---|
| 87 | return;
|
---|
| 88 | }
|
---|
| 89 | if (this.table.getSelectedRowCount() == 0) {
|
---|
| 90 | setEnabled(false);
|
---|
| 91 | putValue(SHORT_DESCRIPTION, descriptionNoSelection);
|
---|
| 92 | return;
|
---|
| 93 | }
|
---|
| 94 | setEnabled(true);
|
---|
| 95 | putValue(SHORT_DESCRIPTION, descriptionNominal);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | @Override
|
---|
| 99 | public void valueChanged(ListSelectionEvent e) {
|
---|
| 100 | updateEnabledState();
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | @Override
|
---|
[10386] | 104 | public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
|
---|
[7937] | 105 | updateEnabledState();
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | @Override
|
---|
[10386] | 109 | public void layerAdded(LayerAddEvent e) {
|
---|
[7937] | 110 | updateEnabledState();
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | @Override
|
---|
[10386] | 114 | public void layerRemoving(LayerRemoveEvent e) {
|
---|
[7937] | 115 | updateEnabledState();
|
---|
| 116 | }
|
---|
[10386] | 117 |
|
---|
| 118 | @Override
|
---|
| 119 | public void layerOrderChanged(LayerOrderChangeEvent e) {
|
---|
| 120 | // Do nothing
|
---|
| 121 | }
|
---|
[7937] | 122 | }
|
---|