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

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

findbugs - RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE

  • Property svn:eol-style set to native
File size: 4.5 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.conflict.pair.nodes.NodeListTable;
15import org.openstreetmap.josm.gui.conflict.pair.relation.RelationMemberTable;
16import org.openstreetmap.josm.gui.dialogs.relation.MemberTable;
17import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
18import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener;
19import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent;
20import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
21import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
22import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
23import org.openstreetmap.josm.gui.layer.OsmDataLayer;
24import org.openstreetmap.josm.gui.widgets.OsmPrimitivesTable;
25import org.openstreetmap.josm.tools.CheckParameterUtil;
26
27public class ZoomToAction extends AbstractAction implements LayerChangeListener, ActiveLayerChangeListener, ListSelectionListener {
28
29 private final OsmPrimitivesTable table;
30
31 private final String descriptionNominal;
32 private final String descriptionInactiveLayer;
33 private final String descriptionNoSelection;
34
35 public ZoomToAction(OsmPrimitivesTable table, String descriptionNominal, String descriptionInactiveLayer, String descriptionNoSelection) {
36 CheckParameterUtil.ensureParameterNotNull(table);
37 this.table = table;
38 this.descriptionNominal = descriptionNominal;
39 this.descriptionInactiveLayer = descriptionInactiveLayer;
40 this.descriptionNoSelection = descriptionNoSelection;
41 putValue(NAME, tr("Zoom to"));
42 putValue(SHORT_DESCRIPTION, descriptionNominal);
43 updateEnabledState();
44 }
45
46 public ZoomToAction(MemberTable table) {
47 this(table,
48 tr("Zoom to the object the first selected member refers to"),
49 tr("Zooming disabled because layer of this relation is not active"),
50 tr("Zooming disabled because there is no selected member"));
51 }
52
53 public ZoomToAction(RelationMemberTable table) {
54 this(table,
55 tr("Zoom to the object the first selected member refers to"),
56 tr("Zooming disabled because layer of this relation is not active"),
57 tr("Zooming disabled because there is no selected member"));
58 }
59
60 public ZoomToAction(NodeListTable table) {
61 this(table,
62 tr("Zoom to the first selected node"),
63 tr("Zooming disabled because layer of this way is not active"),
64 tr("Zooming disabled because there is no selected node"));
65 }
66
67 @Override
68 public void actionPerformed(ActionEvent e) {
69 if (!isEnabled())
70 return;
71 int[] rows = this.table.getSelectedRows();
72 if (rows.length == 0)
73 return;
74 int row = rows[0];
75 OsmDataLayer layer = this.table.getLayer();
76 OsmPrimitive primitive = this.table.getPrimitiveInLayer(row, layer);
77 if (layer != null && primitive != null) {
78 layer.data.setSelected(primitive);
79 AutoScaleAction.autoScale("selection");
80 }
81 }
82
83 protected final void updateEnabledState() {
84 if (Main.main == null || Main.getLayerManager().getEditLayer() != this.table.getLayer()) {
85 setEnabled(false);
86 putValue(SHORT_DESCRIPTION, descriptionInactiveLayer);
87 return;
88 }
89 if (this.table.getSelectedRowCount() == 0) {
90 setEnabled(false);
91 putValue(SHORT_DESCRIPTION, descriptionNoSelection);
92 return;
93 }
94 setEnabled(true);
95 putValue(SHORT_DESCRIPTION, descriptionNominal);
96 }
97
98 @Override
99 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}
Note: See TracBrowser for help on using the repository browser.