source: josm/trunk/src/org/openstreetmap/josm/actions/ZoomToAction.java@ 14374

Last change on this file since 14374 was 14221, checked in by Don-vip, 6 years ago

see #16706 - use documented enum for AutoScaleMode instead of undocumented string constants

  • Property svn:eol-style set to native
File size: 5.7 KB
RevLine 
[7937]1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7
8import javax.swing.AbstractAction;
9import javax.swing.event.ListSelectionEvent;
10import javax.swing.event.ListSelectionListener;
11
[14221]12import org.openstreetmap.josm.actions.AutoScaleAction.AutoScaleMode;
[7937]13import org.openstreetmap.josm.data.osm.OsmPrimitive;
[12636]14import org.openstreetmap.josm.gui.MainApplication;
[7937]15import org.openstreetmap.josm.gui.conflict.pair.nodes.NodeListTable;
16import org.openstreetmap.josm.gui.conflict.pair.relation.RelationMemberTable;
17import org.openstreetmap.josm.gui.dialogs.relation.MemberTable;
[10386]18import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
19import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener;
20import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent;
21import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
22import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
23import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
[7937]24import org.openstreetmap.josm.gui.layer.OsmDataLayer;
25import org.openstreetmap.josm.gui.widgets.OsmPrimitivesTable;
26import org.openstreetmap.josm.tools.CheckParameterUtil;
27
[12366]28/**
[12581]29 * An action that zooms to the selected OSM primitive in a table of primitives.
[12366]30 */
[10386]31public class ZoomToAction extends AbstractAction implements LayerChangeListener, ActiveLayerChangeListener, ListSelectionListener {
[7937]32
33 private final OsmPrimitivesTable table;
[8443]34
[7937]35 private final String descriptionNominal;
36 private final String descriptionInactiveLayer;
37 private final String descriptionNoSelection;
[8443]38
[12366]39 /**
40 * Creates a new, generic zoom to action
41 * @param table The table to get the selected element from
42 * @param descriptionNominal The description to display if zooming is possible
43 * @param descriptionInactiveLayer The description to display if zooming is impossible because the layer is not active
44 * @param descriptionNoSelection The description to display if zooming is impossible because the table selection is empty
45 */
[7937]46 public ZoomToAction(OsmPrimitivesTable table, String descriptionNominal, String descriptionInactiveLayer, String descriptionNoSelection) {
47 CheckParameterUtil.ensureParameterNotNull(table);
48 this.table = table;
49 this.descriptionNominal = descriptionNominal;
50 this.descriptionInactiveLayer = descriptionInactiveLayer;
51 this.descriptionNoSelection = descriptionNoSelection;
52 putValue(NAME, tr("Zoom to"));
53 putValue(SHORT_DESCRIPTION, descriptionNominal);
54 updateEnabledState();
55 }
[8443]56
[12366]57 /**
58 * Creates a new zoom to action for a {@link MemberTable} using the matching description strings
59 * @param table The table to get the selected element from
60 */
[7937]61 public ZoomToAction(MemberTable table) {
[8443]62 this(table,
[7937]63 tr("Zoom to the object the first selected member refers to"),
64 tr("Zooming disabled because layer of this relation is not active"),
65 tr("Zooming disabled because there is no selected member"));
66 }
[8443]67
[12366]68 /**
69 * Creates a new zoom to action for a {@link RelationMemberTable} using the matching description strings
70 * @param table The table to get the selected element from
71 */
[7937]72 public ZoomToAction(RelationMemberTable table) {
[8443]73 this(table,
[7937]74 tr("Zoom to the object the first selected member refers to"),
75 tr("Zooming disabled because layer of this relation is not active"),
76 tr("Zooming disabled because there is no selected member"));
77 }
[8443]78
[12366]79 /**
80 * Creates a new zoom to action for a {@link NodeListTable} using the matching description strings
81 * @param table The table to get the selected element from
82 */
[7937]83 public ZoomToAction(NodeListTable table) {
[8443]84 this(table,
[7937]85 tr("Zoom to the first selected node"),
86 tr("Zooming disabled because layer of this way is not active"),
87 tr("Zooming disabled because there is no selected node"));
88 }
89
90 @Override
91 public void actionPerformed(ActionEvent e) {
[8443]92 if (!isEnabled())
[7937]93 return;
94 int[] rows = this.table.getSelectedRows();
[11381]95 if (rows.length == 0)
[7937]96 return;
97 int row = rows[0];
98 OsmDataLayer layer = this.table.getLayer();
99 OsmPrimitive primitive = this.table.getPrimitiveInLayer(row, layer);
100 if (layer != null && primitive != null) {
101 layer.data.setSelected(primitive);
[14221]102 AutoScaleAction.autoScale(AutoScaleMode.SELECTION);
[7937]103 }
104 }
105
106 protected final void updateEnabledState() {
[14143]107 if (MainApplication.getLayerManager().getActiveDataLayer() != this.table.getLayer()) {
[7937]108 setEnabled(false);
109 putValue(SHORT_DESCRIPTION, descriptionInactiveLayer);
110 return;
111 }
112 if (this.table.getSelectedRowCount() == 0) {
113 setEnabled(false);
114 putValue(SHORT_DESCRIPTION, descriptionNoSelection);
115 return;
116 }
117 setEnabled(true);
118 putValue(SHORT_DESCRIPTION, descriptionNominal);
119 }
120
121 @Override
122 public void valueChanged(ListSelectionEvent e) {
123 updateEnabledState();
124 }
125
126 @Override
[10386]127 public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
[7937]128 updateEnabledState();
129 }
130
131 @Override
[10386]132 public void layerAdded(LayerAddEvent e) {
[7937]133 updateEnabledState();
134 }
135
136 @Override
[10386]137 public void layerRemoving(LayerRemoveEvent e) {
[7937]138 updateEnabledState();
139 }
[10386]140
141 @Override
142 public void layerOrderChanged(LayerOrderChangeEvent e) {
143 // Do nothing
144 }
[7937]145}
Note: See TracBrowser for help on using the repository browser.