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