[5297] | 1 | package org.openstreetmap.josm.actions;
|
---|
| 2 |
|
---|
| 3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 4 |
|
---|
| 5 | import java.awt.event.ActionEvent;
|
---|
| 6 |
|
---|
| 7 | import javax.swing.AbstractAction;
|
---|
| 8 | import javax.swing.event.ListSelectionEvent;
|
---|
| 9 | import javax.swing.event.ListSelectionListener;
|
---|
| 10 |
|
---|
| 11 | import org.openstreetmap.josm.Main;
|
---|
[5301] | 12 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[5297] | 13 | import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
|
---|
| 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;
|
---|
| 17 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
| 18 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
| 19 | import org.openstreetmap.josm.gui.widgets.OsmPrimitivesTable;
|
---|
| 20 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
| 21 |
|
---|
| 22 | public class ZoomToAction extends AbstractAction implements LayerChangeListener, ListSelectionListener {
|
---|
| 23 |
|
---|
| 24 | private final OsmPrimitivesTable table;
|
---|
| 25 |
|
---|
| 26 | private final String descriptionNominal;
|
---|
| 27 | private final String descriptionInactiveLayer;
|
---|
| 28 | private final String descriptionNoSelection;
|
---|
| 29 |
|
---|
| 30 | public ZoomToAction(OsmPrimitivesTable table, String descriptionNominal, String descriptionInactiveLayer, String descriptionNoSelection) {
|
---|
| 31 | CheckParameterUtil.ensureParameterNotNull(table);
|
---|
| 32 | this.table = table;
|
---|
| 33 | this.descriptionNominal = descriptionNominal;
|
---|
| 34 | this.descriptionInactiveLayer = descriptionInactiveLayer;
|
---|
| 35 | this.descriptionNoSelection = descriptionNoSelection;
|
---|
| 36 | putValue(NAME, tr("Zoom to"));
|
---|
| 37 | putValue(SHORT_DESCRIPTION, descriptionNominal);
|
---|
| 38 | updateEnabledState();
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | public ZoomToAction(MemberTable table) {
|
---|
| 42 | this(table,
|
---|
| 43 | tr("Zoom to the object the first selected member refers to"),
|
---|
| 44 | tr("Zooming disabled because layer of this relation is not active"),
|
---|
| 45 | tr("Zooming disabled because there is no selected member"));
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | public ZoomToAction(RelationMemberTable table) {
|
---|
| 49 | this(table,
|
---|
| 50 | tr("Zoom to the object the first selected member refers to"),
|
---|
| 51 | tr("Zooming disabled because layer of this relation is not active"),
|
---|
| 52 | tr("Zooming disabled because there is no selected member"));
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | public ZoomToAction(NodeListTable table) {
|
---|
| 56 | this(table,
|
---|
| 57 | tr("Zoom to the first selected node"),
|
---|
| 58 | tr("Zooming disabled because layer of this way is not active"),
|
---|
| 59 | tr("Zooming disabled because there is no selected node"));
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | public void actionPerformed(ActionEvent e) {
|
---|
| 63 | if (! isEnabled())
|
---|
| 64 | return;
|
---|
| 65 | int rows[] = this.table.getSelectedRows();
|
---|
| 66 | if (rows == null || rows.length == 0)
|
---|
| 67 | return;
|
---|
| 68 | int row = rows[0];
|
---|
| 69 | OsmDataLayer layer = this.table.getLayer();
|
---|
[5301] | 70 | OsmPrimitive primitive = this.table.getPrimitiveToZoomTo(row, layer);
|
---|
[5297] | 71 | if (layer != null && primitive != null) {
|
---|
| 72 | layer.data.setSelected(primitive);
|
---|
| 73 | AutoScaleAction.autoScale("selection");
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | protected void updateEnabledState() {
|
---|
| 78 | if (Main.main == null || Main.main.getEditLayer() != this.table.getLayer()) {
|
---|
| 79 | setEnabled(false);
|
---|
| 80 | putValue(SHORT_DESCRIPTION, descriptionInactiveLayer);
|
---|
| 81 | return;
|
---|
| 82 | }
|
---|
| 83 | if (this.table.getSelectedRowCount() == 0) {
|
---|
| 84 | setEnabled(false);
|
---|
| 85 | putValue(SHORT_DESCRIPTION, descriptionNoSelection);
|
---|
| 86 | return;
|
---|
| 87 | }
|
---|
| 88 | setEnabled(true);
|
---|
| 89 | putValue(SHORT_DESCRIPTION, descriptionNominal);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | public void valueChanged(ListSelectionEvent e) {
|
---|
| 93 | updateEnabledState();
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | public void activeLayerChange(Layer oldLayer, Layer newLayer) {
|
---|
| 97 | updateEnabledState();
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | public void layerAdded(Layer newLayer) {
|
---|
| 101 | updateEnabledState();
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | public void layerRemoved(Layer oldLayer) {
|
---|
| 105 | updateEnabledState();
|
---|
| 106 | }
|
---|
| 107 | }
|
---|