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

Last change on this file since 13076 was 12636, checked in by Don-vip, 7 years ago

see #15182 - deprecate Main.getLayerManager(). Replacement: gui.MainApplication.getLayerManager()

  • Property svn:eol-style set to native
File size: 5.6 KB
Line 
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
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.data.osm.OsmPrimitive;
14import org.openstreetmap.josm.gui.MainApplication;
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;
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;
24import org.openstreetmap.josm.gui.layer.OsmDataLayer;
25import org.openstreetmap.josm.gui.widgets.OsmPrimitivesTable;
26import org.openstreetmap.josm.tools.CheckParameterUtil;
27
28/**
29 * An action that zooms to the selected OSM primitive in a table of primitives.
30 */
31public class ZoomToAction extends AbstractAction implements LayerChangeListener, ActiveLayerChangeListener, ListSelectionListener {
32
33 private final OsmPrimitivesTable table;
34
35 private final String descriptionNominal;
36 private final String descriptionInactiveLayer;
37 private final String descriptionNoSelection;
38
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 */
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 }
56
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 */
61 public ZoomToAction(MemberTable table) {
62 this(table,
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 }
67
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 */
72 public ZoomToAction(RelationMemberTable table) {
73 this(table,
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 }
78
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 */
83 public ZoomToAction(NodeListTable table) {
84 this(table,
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) {
92 if (!isEnabled())
93 return;
94 int[] rows = this.table.getSelectedRows();
95 if (rows.length == 0)
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);
102 AutoScaleAction.autoScale("selection");
103 }
104 }
105
106 protected final void updateEnabledState() {
107 if (Main.main == null || MainApplication.getLayerManager().getEditLayer() != this.table.getLayer()) {
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
127 public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
128 updateEnabledState();
129 }
130
131 @Override
132 public void layerAdded(LayerAddEvent e) {
133 updateEnabledState();
134 }
135
136 @Override
137 public void layerRemoving(LayerRemoveEvent e) {
138 updateEnabledState();
139 }
140
141 @Override
142 public void layerOrderChanged(LayerOrderChangeEvent e) {
143 // Do nothing
144 }
145}
Note: See TracBrowser for help on using the repository browser.