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.conflict.pair.nodes.NodeListTable;
015import org.openstreetmap.josm.gui.conflict.pair.relation.RelationMemberTable;
016import org.openstreetmap.josm.gui.dialogs.relation.MemberTable;
017import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
018import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener;
019import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent;
020import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
021import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
022import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
023import org.openstreetmap.josm.gui.layer.OsmDataLayer;
024import org.openstreetmap.josm.gui.widgets.OsmPrimitivesTable;
025import org.openstreetmap.josm.tools.CheckParameterUtil;
026
027public class ZoomToAction extends AbstractAction implements LayerChangeListener, ActiveLayerChangeListener, ListSelectionListener {
028
029    private final OsmPrimitivesTable table;
030
031    private final String descriptionNominal;
032    private final String descriptionInactiveLayer;
033    private final String descriptionNoSelection;
034
035    public ZoomToAction(OsmPrimitivesTable table, String descriptionNominal, String descriptionInactiveLayer, String descriptionNoSelection) {
036        CheckParameterUtil.ensureParameterNotNull(table);
037        this.table = table;
038        this.descriptionNominal = descriptionNominal;
039        this.descriptionInactiveLayer = descriptionInactiveLayer;
040        this.descriptionNoSelection = descriptionNoSelection;
041        putValue(NAME, tr("Zoom to"));
042        putValue(SHORT_DESCRIPTION, descriptionNominal);
043        updateEnabledState();
044    }
045
046    public ZoomToAction(MemberTable table) {
047        this(table,
048                tr("Zoom to the object the first selected member refers to"),
049                tr("Zooming disabled because layer of this relation is not active"),
050                tr("Zooming disabled because there is no selected member"));
051    }
052
053    public ZoomToAction(RelationMemberTable table) {
054        this(table,
055                tr("Zoom to the object the first selected member refers to"),
056                tr("Zooming disabled because layer of this relation is not active"),
057                tr("Zooming disabled because there is no selected member"));
058    }
059
060    public ZoomToAction(NodeListTable table) {
061        this(table,
062                tr("Zoom to the first selected node"),
063                tr("Zooming disabled because layer of this way is not active"),
064                tr("Zooming disabled because there is no selected node"));
065    }
066
067    @Override
068    public void actionPerformed(ActionEvent e) {
069        if (!isEnabled())
070            return;
071        int[] rows = this.table.getSelectedRows();
072        if (rows == null || rows.length == 0)
073            return;
074        int row = rows[0];
075        OsmDataLayer layer = this.table.getLayer();
076        OsmPrimitive primitive = this.table.getPrimitiveInLayer(row, layer);
077        if (layer != null && primitive != null) {
078            layer.data.setSelected(primitive);
079            AutoScaleAction.autoScale("selection");
080        }
081    }
082
083    protected final void updateEnabledState() {
084        if (Main.main == null || Main.getLayerManager().getEditLayer() != this.table.getLayer()) {
085            setEnabled(false);
086            putValue(SHORT_DESCRIPTION, descriptionInactiveLayer);
087            return;
088        }
089        if (this.table.getSelectedRowCount() == 0) {
090            setEnabled(false);
091            putValue(SHORT_DESCRIPTION, descriptionNoSelection);
092            return;
093        }
094        setEnabled(true);
095        putValue(SHORT_DESCRIPTION, descriptionNominal);
096    }
097
098    @Override
099    public void valueChanged(ListSelectionEvent e) {
100        updateEnabledState();
101    }
102
103    @Override
104    public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
105        updateEnabledState();
106    }
107
108    @Override
109    public void layerAdded(LayerAddEvent e) {
110        updateEnabledState();
111    }
112
113    @Override
114    public void layerRemoving(LayerRemoveEvent e) {
115        updateEnabledState();
116    }
117
118    @Override
119    public void layerOrderChanged(LayerOrderChangeEvent e) {
120        // Do nothing
121    }
122}