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