001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.event.ActionEvent;
007
008import javax.swing.AbstractAction;
009import javax.swing.event.ListSelectionEvent;
010import javax.swing.event.ListSelectionListener;
011
012import org.openstreetmap.josm.Main;
013import org.openstreetmap.josm.data.osm.OsmPrimitive;
014import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
015import org.openstreetmap.josm.gui.conflict.pair.nodes.NodeListTable;
016import org.openstreetmap.josm.gui.conflict.pair.relation.RelationMemberTable;
017import org.openstreetmap.josm.gui.dialogs.relation.MemberTable;
018import org.openstreetmap.josm.gui.layer.Layer;
019import org.openstreetmap.josm.gui.layer.OsmDataLayer;
020import org.openstreetmap.josm.gui.widgets.OsmPrimitivesTable;
021import org.openstreetmap.josm.tools.CheckParameterUtil;
022
023public class ZoomToAction extends AbstractAction implements LayerChangeListener, ListSelectionListener {
024
025    private final OsmPrimitivesTable table;
026
027    private final String descriptionNominal;
028    private final String descriptionInactiveLayer;
029    private final String descriptionNoSelection;
030
031    public ZoomToAction(OsmPrimitivesTable table, String descriptionNominal, String descriptionInactiveLayer, String descriptionNoSelection) {
032        CheckParameterUtil.ensureParameterNotNull(table);
033        this.table = table;
034        this.descriptionNominal = descriptionNominal;
035        this.descriptionInactiveLayer = descriptionInactiveLayer;
036        this.descriptionNoSelection = descriptionNoSelection;
037        putValue(NAME, tr("Zoom to"));
038        putValue(SHORT_DESCRIPTION, descriptionNominal);
039        updateEnabledState();
040    }
041
042    public ZoomToAction(MemberTable table) {
043        this(table,
044                tr("Zoom to the object the first selected member refers to"),
045                tr("Zooming disabled because layer of this relation is not active"),
046                tr("Zooming disabled because there is no selected member"));
047    }
048
049    public ZoomToAction(RelationMemberTable table) {
050        this(table,
051                tr("Zoom to the object the first selected member refers to"),
052                tr("Zooming disabled because layer of this relation is not active"),
053                tr("Zooming disabled because there is no selected member"));
054    }
055
056    public ZoomToAction(NodeListTable table) {
057        this(table,
058                tr("Zoom to the first selected node"),
059                tr("Zooming disabled because layer of this way is not active"),
060                tr("Zooming disabled because there is no selected node"));
061    }
062
063    @Override
064    public void actionPerformed(ActionEvent e) {
065        if (!isEnabled())
066            return;
067        int[] rows = this.table.getSelectedRows();
068        if (rows == null || rows.length == 0)
069            return;
070        int row = rows[0];
071        OsmDataLayer layer = this.table.getLayer();
072        OsmPrimitive primitive = this.table.getPrimitiveInLayer(row, layer);
073        if (layer != null && primitive != null) {
074            layer.data.setSelected(primitive);
075            AutoScaleAction.autoScale("selection");
076        }
077    }
078
079    protected final void updateEnabledState() {
080        if (Main.main == null || Main.main.getEditLayer() != this.table.getLayer()) {
081            setEnabled(false);
082            putValue(SHORT_DESCRIPTION, descriptionInactiveLayer);
083            return;
084        }
085        if (this.table.getSelectedRowCount() == 0) {
086            setEnabled(false);
087            putValue(SHORT_DESCRIPTION, descriptionNoSelection);
088            return;
089        }
090        setEnabled(true);
091        putValue(SHORT_DESCRIPTION, descriptionNominal);
092    }
093
094    @Override
095    public void valueChanged(ListSelectionEvent e) {
096        updateEnabledState();
097    }
098
099    @Override
100    public void activeLayerChange(Layer oldLayer, Layer newLayer) {
101        updateEnabledState();
102    }
103
104    @Override
105    public void layerAdded(Layer newLayer) {
106        updateEnabledState();
107    }
108
109    @Override
110    public void layerRemoved(Layer oldLayer) {
111        updateEnabledState();
112    }
113}