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.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.LayerManager.LayerAddEvent;
|
---|
18 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener;
|
---|
19 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent;
|
---|
20 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
|
---|
21 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
|
---|
22 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
|
---|
23 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
24 | import org.openstreetmap.josm.gui.widgets.OsmPrimitivesTable;
|
---|
25 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * An action that zooms to the selected OSM primitive in a table of primitives
|
---|
29 | */
|
---|
30 | public class ZoomToAction extends AbstractAction implements LayerChangeListener, ActiveLayerChangeListener, ListSelectionListener {
|
---|
31 |
|
---|
32 | private final OsmPrimitivesTable table;
|
---|
33 |
|
---|
34 | private final String descriptionNominal;
|
---|
35 | private final String descriptionInactiveLayer;
|
---|
36 | private final String descriptionNoSelection;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Creates a new, generic zoom to action
|
---|
40 | * @param table The table to get the selected element from
|
---|
41 | * @param descriptionNominal The description to display if zooming is possible
|
---|
42 | * @param descriptionInactiveLayer The description to display if zooming is impossible because the layer is not active
|
---|
43 | * @param descriptionNoSelection The description to display if zooming is impossible because the table selection is empty
|
---|
44 | */
|
---|
45 | public ZoomToAction(OsmPrimitivesTable table, String descriptionNominal, String descriptionInactiveLayer, String descriptionNoSelection) {
|
---|
46 | CheckParameterUtil.ensureParameterNotNull(table);
|
---|
47 | this.table = table;
|
---|
48 | this.descriptionNominal = descriptionNominal;
|
---|
49 | this.descriptionInactiveLayer = descriptionInactiveLayer;
|
---|
50 | this.descriptionNoSelection = descriptionNoSelection;
|
---|
51 | putValue(NAME, tr("Zoom to"));
|
---|
52 | putValue(SHORT_DESCRIPTION, descriptionNominal);
|
---|
53 | updateEnabledState();
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Creates a new zoom to action for a {@link MemberTable} using the matching description strings
|
---|
58 | * @param table The table to get the selected element from
|
---|
59 | */
|
---|
60 | public ZoomToAction(MemberTable table) {
|
---|
61 | this(table,
|
---|
62 | tr("Zoom to the object the first selected member refers to"),
|
---|
63 | tr("Zooming disabled because layer of this relation is not active"),
|
---|
64 | tr("Zooming disabled because there is no selected member"));
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Creates a new zoom to action for a {@link RelationMemberTable} using the matching description strings
|
---|
69 | * @param table The table to get the selected element from
|
---|
70 | */
|
---|
71 | public ZoomToAction(RelationMemberTable table) {
|
---|
72 | this(table,
|
---|
73 | tr("Zoom to the object the first selected member refers to"),
|
---|
74 | tr("Zooming disabled because layer of this relation is not active"),
|
---|
75 | tr("Zooming disabled because there is no selected member"));
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Creates a new zoom to action for a {@link NodeListTable} using the matching description strings
|
---|
80 | * @param table The table to get the selected element from
|
---|
81 | */
|
---|
82 | public ZoomToAction(NodeListTable table) {
|
---|
83 | this(table,
|
---|
84 | tr("Zoom to the first selected node"),
|
---|
85 | tr("Zooming disabled because layer of this way is not active"),
|
---|
86 | tr("Zooming disabled because there is no selected node"));
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Override
|
---|
90 | public void actionPerformed(ActionEvent e) {
|
---|
91 | if (!isEnabled())
|
---|
92 | return;
|
---|
93 | int[] rows = this.table.getSelectedRows();
|
---|
94 | if (rows.length == 0)
|
---|
95 | return;
|
---|
96 | int row = rows[0];
|
---|
97 | OsmDataLayer layer = this.table.getLayer();
|
---|
98 | OsmPrimitive primitive = this.table.getPrimitiveInLayer(row, layer);
|
---|
99 | if (layer != null && primitive != null) {
|
---|
100 | layer.data.setSelected(primitive);
|
---|
101 | AutoScaleAction.autoScale("selection");
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | protected final void updateEnabledState() {
|
---|
106 | if (Main.main == null || Main.getLayerManager().getEditLayer() != this.table.getLayer()) {
|
---|
107 | setEnabled(false);
|
---|
108 | putValue(SHORT_DESCRIPTION, descriptionInactiveLayer);
|
---|
109 | return;
|
---|
110 | }
|
---|
111 | if (this.table.getSelectedRowCount() == 0) {
|
---|
112 | setEnabled(false);
|
---|
113 | putValue(SHORT_DESCRIPTION, descriptionNoSelection);
|
---|
114 | return;
|
---|
115 | }
|
---|
116 | setEnabled(true);
|
---|
117 | putValue(SHORT_DESCRIPTION, descriptionNominal);
|
---|
118 | }
|
---|
119 |
|
---|
120 | @Override
|
---|
121 | public void valueChanged(ListSelectionEvent e) {
|
---|
122 | updateEnabledState();
|
---|
123 | }
|
---|
124 |
|
---|
125 | @Override
|
---|
126 | public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
|
---|
127 | updateEnabledState();
|
---|
128 | }
|
---|
129 |
|
---|
130 | @Override
|
---|
131 | public void layerAdded(LayerAddEvent e) {
|
---|
132 | updateEnabledState();
|
---|
133 | }
|
---|
134 |
|
---|
135 | @Override
|
---|
136 | public void layerRemoving(LayerRemoveEvent e) {
|
---|
137 | updateEnabledState();
|
---|
138 | }
|
---|
139 |
|
---|
140 | @Override
|
---|
141 | public void layerOrderChanged(LayerOrderChangeEvent e) {
|
---|
142 | // Do nothing
|
---|
143 | }
|
---|
144 | }
|
---|