1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.relation;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Dimension;
|
---|
7 | import java.awt.GraphicsEnvironment;
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.util.Arrays;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.HashSet;
|
---|
12 | import java.util.Set;
|
---|
13 |
|
---|
14 | import javax.swing.AbstractAction;
|
---|
15 | import javax.swing.DropMode;
|
---|
16 | import javax.swing.JPopupMenu;
|
---|
17 | import javax.swing.JTable;
|
---|
18 | import javax.swing.ListSelectionModel;
|
---|
19 | import javax.swing.SwingUtilities;
|
---|
20 | import javax.swing.event.ListSelectionEvent;
|
---|
21 | import javax.swing.event.ListSelectionListener;
|
---|
22 |
|
---|
23 | import org.openstreetmap.josm.Main;
|
---|
24 | import org.openstreetmap.josm.actions.AutoScaleAction;
|
---|
25 | import org.openstreetmap.josm.actions.ZoomToAction;
|
---|
26 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
27 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
28 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
29 | import org.openstreetmap.josm.data.osm.Way;
|
---|
30 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
31 | import org.openstreetmap.josm.gui.dialogs.relation.sort.WayConnectionType;
|
---|
32 | import org.openstreetmap.josm.gui.dialogs.relation.sort.WayConnectionType.Direction;
|
---|
33 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
|
---|
34 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener;
|
---|
35 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent;
|
---|
36 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
|
---|
37 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
|
---|
38 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
|
---|
39 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
40 | import org.openstreetmap.josm.gui.util.HighlightHelper;
|
---|
41 | import org.openstreetmap.josm.gui.widgets.OsmPrimitivesTable;
|
---|
42 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * The table of members a selected relation has.
|
---|
46 | */
|
---|
47 | public class MemberTable extends OsmPrimitivesTable implements IMemberModelListener {
|
---|
48 |
|
---|
49 | /** the additional actions in popup menu */
|
---|
50 | private ZoomToGapAction zoomToGap;
|
---|
51 | private final transient HighlightHelper highlightHelper = new HighlightHelper();
|
---|
52 | private boolean highlightEnabled;
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * constructor for relation member table
|
---|
56 | *
|
---|
57 | * @param layer the data layer of the relation. Must not be null
|
---|
58 | * @param relation the relation. Can be null
|
---|
59 | * @param model the table model
|
---|
60 | */
|
---|
61 | public MemberTable(OsmDataLayer layer, Relation relation, MemberTableModel model) {
|
---|
62 | super(model, new MemberTableColumnModel(layer.data, relation), model.getSelectionModel());
|
---|
63 | setLayer(layer);
|
---|
64 | model.addMemberModelListener(this);
|
---|
65 |
|
---|
66 | MemberRoleCellEditor ce = (MemberRoleCellEditor) getColumnModel().getColumn(0).getCellEditor();
|
---|
67 | setRowHeight(ce.getEditor().getPreferredSize().height);
|
---|
68 | setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
|
---|
69 | setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
---|
70 | putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
|
---|
71 |
|
---|
72 | installCustomNavigation(0);
|
---|
73 | initHighlighting();
|
---|
74 |
|
---|
75 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
76 | setTransferHandler(new MemberTransferHandler());
|
---|
77 | setFillsViewportHeight(true); // allow drop on empty table
|
---|
78 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
79 | setDragEnabled(true);
|
---|
80 | }
|
---|
81 | setDropMode(DropMode.INSERT_ROWS);
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | @Override
|
---|
86 | protected ZoomToAction buildZoomToAction() {
|
---|
87 | return new ZoomToAction(this);
|
---|
88 | }
|
---|
89 |
|
---|
90 | @Override
|
---|
91 | protected JPopupMenu buildPopupMenu() {
|
---|
92 | JPopupMenu menu = super.buildPopupMenu();
|
---|
93 | zoomToGap = new ZoomToGapAction();
|
---|
94 | registerListeners();
|
---|
95 | menu.addSeparator();
|
---|
96 | getSelectionModel().addListSelectionListener(zoomToGap);
|
---|
97 | menu.add(zoomToGap);
|
---|
98 | menu.addSeparator();
|
---|
99 | menu.add(new SelectPreviousGapAction());
|
---|
100 | menu.add(new SelectNextGapAction());
|
---|
101 | return menu;
|
---|
102 | }
|
---|
103 |
|
---|
104 | @Override
|
---|
105 | public Dimension getPreferredSize() {
|
---|
106 | return getPreferredFullWidthSize();
|
---|
107 | }
|
---|
108 |
|
---|
109 | @Override
|
---|
110 | public void makeMemberVisible(int index) {
|
---|
111 | scrollRectToVisible(getCellRect(index, 0, true));
|
---|
112 | }
|
---|
113 |
|
---|
114 | private transient ListSelectionListener highlighterListener = lse -> {
|
---|
115 | if (MainApplication.isDisplayingMapView()) {
|
---|
116 | Collection<RelationMember> sel = getMemberTableModel().getSelectedMembers();
|
---|
117 | final Set<OsmPrimitive> toHighlight = new HashSet<>();
|
---|
118 | for (RelationMember r: sel) {
|
---|
119 | if (r.getMember().isUsable()) {
|
---|
120 | toHighlight.add(r.getMember());
|
---|
121 | }
|
---|
122 | }
|
---|
123 | SwingUtilities.invokeLater(() -> {
|
---|
124 | if (MainApplication.isDisplayingMapView() && highlightHelper.highlightOnly(toHighlight)) {
|
---|
125 | MainApplication.getMap().mapView.repaint();
|
---|
126 | }
|
---|
127 | });
|
---|
128 | }
|
---|
129 | };
|
---|
130 |
|
---|
131 | private void initHighlighting() {
|
---|
132 | highlightEnabled = Config.getPref().getBoolean("draw.target-highlight", true);
|
---|
133 | if (!highlightEnabled) return;
|
---|
134 | getMemberTableModel().getSelectionModel().addListSelectionListener(highlighterListener);
|
---|
135 | if (MainApplication.isDisplayingMapView()) {
|
---|
136 | HighlightHelper.clearAllHighlighted();
|
---|
137 | MainApplication.getMap().mapView.repaint();
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | @Override
|
---|
142 | public void registerListeners() {
|
---|
143 | MainApplication.getLayerManager().addLayerChangeListener(zoomToGap);
|
---|
144 | MainApplication.getLayerManager().addActiveLayerChangeListener(zoomToGap);
|
---|
145 | super.registerListeners();
|
---|
146 | }
|
---|
147 |
|
---|
148 | @Override
|
---|
149 | public void unregisterListeners() {
|
---|
150 | super.unregisterListeners();
|
---|
151 | MainApplication.getLayerManager().removeLayerChangeListener(zoomToGap);
|
---|
152 | MainApplication.getLayerManager().removeActiveLayerChangeListener(zoomToGap);
|
---|
153 | }
|
---|
154 |
|
---|
155 | public void stopHighlighting() {
|
---|
156 | if (highlighterListener == null) return;
|
---|
157 | if (!highlightEnabled) return;
|
---|
158 | getMemberTableModel().getSelectionModel().removeListSelectionListener(highlighterListener);
|
---|
159 | highlighterListener = null;
|
---|
160 | if (MainApplication.isDisplayingMapView()) {
|
---|
161 | HighlightHelper.clearAllHighlighted();
|
---|
162 | MainApplication.getMap().mapView.repaint();
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | private class SelectPreviousGapAction extends AbstractAction {
|
---|
167 |
|
---|
168 | SelectPreviousGapAction() {
|
---|
169 | putValue(NAME, tr("Select previous Gap"));
|
---|
170 | putValue(SHORT_DESCRIPTION, tr("Select the previous relation member which gives rise to a gap"));
|
---|
171 | }
|
---|
172 |
|
---|
173 | @Override
|
---|
174 | public void actionPerformed(ActionEvent e) {
|
---|
175 | int i = getSelectedRow() - 1;
|
---|
176 | while (i >= 0 && getMemberTableModel().getWayConnection(i).linkPrev) {
|
---|
177 | i--;
|
---|
178 | }
|
---|
179 | if (i >= 0) {
|
---|
180 | getSelectionModel().setSelectionInterval(i, i);
|
---|
181 | }
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | private class SelectNextGapAction extends AbstractAction {
|
---|
186 |
|
---|
187 | SelectNextGapAction() {
|
---|
188 | putValue(NAME, tr("Select next Gap"));
|
---|
189 | putValue(SHORT_DESCRIPTION, tr("Select the next relation member which gives rise to a gap"));
|
---|
190 | }
|
---|
191 |
|
---|
192 | @Override
|
---|
193 | public void actionPerformed(ActionEvent e) {
|
---|
194 | int i = getSelectedRow() + 1;
|
---|
195 | while (i < getRowCount() && getMemberTableModel().getWayConnection(i).linkNext) {
|
---|
196 | i++;
|
---|
197 | }
|
---|
198 | if (i < getRowCount()) {
|
---|
199 | getSelectionModel().setSelectionInterval(i, i);
|
---|
200 | }
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | private class ZoomToGapAction extends AbstractAction implements LayerChangeListener, ActiveLayerChangeListener, ListSelectionListener {
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Constructs a new {@code ZoomToGapAction}.
|
---|
208 | */
|
---|
209 | ZoomToGapAction() {
|
---|
210 | putValue(NAME, tr("Zoom to Gap"));
|
---|
211 | putValue(SHORT_DESCRIPTION, tr("Zoom to the gap in the way sequence"));
|
---|
212 | updateEnabledState();
|
---|
213 | }
|
---|
214 |
|
---|
215 | private WayConnectionType getConnectionType() {
|
---|
216 | return getMemberTableModel().getWayConnection(getSelectedRows()[0]);
|
---|
217 | }
|
---|
218 |
|
---|
219 | private final Collection<Direction> connectionTypesOfInterest = Arrays.asList(
|
---|
220 | WayConnectionType.Direction.FORWARD, WayConnectionType.Direction.BACKWARD);
|
---|
221 |
|
---|
222 | private boolean hasGap() {
|
---|
223 | WayConnectionType connectionType = getConnectionType();
|
---|
224 | return connectionTypesOfInterest.contains(connectionType.direction)
|
---|
225 | && !(connectionType.linkNext && connectionType.linkPrev);
|
---|
226 | }
|
---|
227 |
|
---|
228 | @Override
|
---|
229 | public void actionPerformed(ActionEvent e) {
|
---|
230 | WayConnectionType connectionType = getConnectionType();
|
---|
231 | Way way = (Way) getMemberTableModel().getReferredPrimitive(getSelectedRows()[0]);
|
---|
232 | if (!connectionType.linkPrev) {
|
---|
233 | getLayer().data.setSelected(WayConnectionType.Direction.FORWARD.equals(connectionType.direction)
|
---|
234 | ? way.firstNode() : way.lastNode());
|
---|
235 | AutoScaleAction.autoScale("selection");
|
---|
236 | } else if (!connectionType.linkNext) {
|
---|
237 | getLayer().data.setSelected(WayConnectionType.Direction.FORWARD.equals(connectionType.direction)
|
---|
238 | ? way.lastNode() : way.firstNode());
|
---|
239 | AutoScaleAction.autoScale("selection");
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | private void updateEnabledState() {
|
---|
244 | setEnabled(Main.main != null
|
---|
245 | && MainApplication.getLayerManager().getEditLayer() == getLayer()
|
---|
246 | && getSelectedRowCount() == 1
|
---|
247 | && hasGap());
|
---|
248 | }
|
---|
249 |
|
---|
250 | @Override
|
---|
251 | public void valueChanged(ListSelectionEvent e) {
|
---|
252 | updateEnabledState();
|
---|
253 | }
|
---|
254 |
|
---|
255 | @Override
|
---|
256 | public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
|
---|
257 | updateEnabledState();
|
---|
258 | }
|
---|
259 |
|
---|
260 | @Override
|
---|
261 | public void layerAdded(LayerAddEvent e) {
|
---|
262 | updateEnabledState();
|
---|
263 | }
|
---|
264 |
|
---|
265 | @Override
|
---|
266 | public void layerRemoving(LayerRemoveEvent e) {
|
---|
267 | updateEnabledState();
|
---|
268 | }
|
---|
269 |
|
---|
270 | @Override
|
---|
271 | public void layerOrderChanged(LayerOrderChangeEvent e) {
|
---|
272 | // Do nothing
|
---|
273 | }
|
---|
274 | }
|
---|
275 |
|
---|
276 | protected MemberTableModel getMemberTableModel() {
|
---|
277 | return (MemberTableModel) getModel();
|
---|
278 | }
|
---|
279 | }
|
---|