1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer.gpx;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.GridBagLayout;
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.ActionListener;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.Date;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.Map.Entry;
|
---|
14 |
|
---|
15 | import javax.swing.BorderFactory;
|
---|
16 | import javax.swing.ButtonGroup;
|
---|
17 | import javax.swing.JCheckBox;
|
---|
18 | import javax.swing.JLabel;
|
---|
19 | import javax.swing.JPanel;
|
---|
20 | import javax.swing.JRadioButton;
|
---|
21 |
|
---|
22 | import org.openstreetmap.josm.data.gpx.GpxConstants;
|
---|
23 | import org.openstreetmap.josm.data.gpx.GpxTrack;
|
---|
24 | import org.openstreetmap.josm.data.gpx.GpxTrackSegment;
|
---|
25 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
26 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
27 | import org.openstreetmap.josm.data.osm.Node;
|
---|
28 | import org.openstreetmap.josm.data.osm.Way;
|
---|
29 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
30 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
31 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
32 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
33 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
34 | import org.openstreetmap.josm.tools.GBC;
|
---|
35 | import org.openstreetmap.josm.tools.date.DateUtils;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Converts a {@link GpxLayer} to a {@link OsmDataLayer}.
|
---|
39 | * @since 14129 (extracted from {@link ConvertToDataLayerAction})
|
---|
40 | */
|
---|
41 | public class ConvertFromGpxLayerAction extends ConvertToDataLayerAction<GpxLayer> {
|
---|
42 |
|
---|
43 | private static final String GPX_SETTING = "gpx.convert-tags";
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Creates a new {@code FromGpxLayer}.
|
---|
47 | * @param layer the source layer
|
---|
48 | */
|
---|
49 | public ConvertFromGpxLayerAction(GpxLayer layer) {
|
---|
50 | super(layer);
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | public DataSet convert() {
|
---|
55 | final DataSet ds = new DataSet();
|
---|
56 |
|
---|
57 | List<String> keys = new ArrayList<>();
|
---|
58 | String convertTags = Config.getPref().get(GPX_SETTING, "ask");
|
---|
59 | boolean check = "list".equals(convertTags) || "ask".equals(convertTags);
|
---|
60 | boolean none = "no".equals(convertTags); // no need to convert tags when no dialog will be shown anyways
|
---|
61 |
|
---|
62 | for (GpxTrack trk : layer.data.getTracks()) {
|
---|
63 | for (GpxTrackSegment segment : trk.getSegments()) {
|
---|
64 | List<Node> nodes = new ArrayList<>();
|
---|
65 | for (WayPoint p : segment.getWayPoints()) {
|
---|
66 | Node n = new Node(p.getCoor());
|
---|
67 | for (Entry<String, Object> entry : p.attr.entrySet()) {
|
---|
68 | String key = entry.getKey();
|
---|
69 | Object obj = p.get(key);
|
---|
70 | if (check && !keys.contains(key) && (obj instanceof String || obj instanceof Number || obj instanceof Date)) {
|
---|
71 | keys.add(key);
|
---|
72 | }
|
---|
73 | if (!none && (obj instanceof String || obj instanceof Number)) {
|
---|
74 | // only convert when required
|
---|
75 | n.put(key, obj.toString());
|
---|
76 | } else if (obj instanceof Date && GpxConstants.PT_TIME.equals(key)) {
|
---|
77 | // timestamps should always be converted
|
---|
78 | Date date = (Date) obj;
|
---|
79 | if (!none) { //... but the tag will only be set when required
|
---|
80 | n.put(key, DateUtils.fromDate(date));
|
---|
81 | }
|
---|
82 | n.setTimestamp(date);
|
---|
83 | }
|
---|
84 | }
|
---|
85 | ds.addPrimitive(n);
|
---|
86 | nodes.add(n);
|
---|
87 | }
|
---|
88 | Way w = new Way();
|
---|
89 | w.setNodes(nodes);
|
---|
90 | ds.addPrimitive(w);
|
---|
91 | }
|
---|
92 | }
|
---|
93 | //gpx.convert-tags: all, list, *ask, no
|
---|
94 | //gpx.convert-tags.last: *all, list, no
|
---|
95 | //gpx.convert-tags.list.yes
|
---|
96 | //gpx.convert-tags.list.no
|
---|
97 | List<String> listPos = Config.getPref().getList(GPX_SETTING + ".list.yes");
|
---|
98 | List<String> listNeg = Config.getPref().getList(GPX_SETTING + ".list.no");
|
---|
99 | if (check && !keys.isEmpty()) {
|
---|
100 | // Either "list" or "ask" was stored in the settings, so the Nodes have to be filtered after all tags have been processed
|
---|
101 | List<String> allTags = new ArrayList<>(listPos);
|
---|
102 | allTags.addAll(listNeg);
|
---|
103 | if (!allTags.containsAll(keys) || "ask".equals(convertTags)) {
|
---|
104 | // not all keys are in positive/negative list, so we have to ask the user
|
---|
105 | TagConversionDialogResponse res = showTagConversionDialog(keys, listPos, listNeg);
|
---|
106 | if (res.sel == null) {
|
---|
107 | return null;
|
---|
108 | }
|
---|
109 | listPos = res.listPos;
|
---|
110 |
|
---|
111 | if ("no".equals(res.sel)) {
|
---|
112 | // User just chose not to convert any tags, but that was unknown before the initial conversion
|
---|
113 | return filterDataSet(ds, null);
|
---|
114 | } else if ("all".equals(res.sel)) {
|
---|
115 | return ds;
|
---|
116 | }
|
---|
117 | }
|
---|
118 | if (!listPos.containsAll(keys)) {
|
---|
119 | return filterDataSet(ds, listPos);
|
---|
120 | }
|
---|
121 | }
|
---|
122 | return ds;
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Filters the tags of the given {@link DataSet}
|
---|
127 | * @param ds The {@link DataSet}
|
---|
128 | * @param listPos A {@code List<String>} containing the tags to be kept, can be {@code null} if all tags are to be removed
|
---|
129 | * @return The {@link DataSet}
|
---|
130 | * @since 14103
|
---|
131 | */
|
---|
132 | public DataSet filterDataSet(DataSet ds, List<String> listPos) {
|
---|
133 | Collection<Node> nodes = ds.getNodes();
|
---|
134 | for (Node n : nodes) {
|
---|
135 | for (String key : n.keySet()) {
|
---|
136 | if (listPos == null || !listPos.contains(key)) {
|
---|
137 | n.put(key, null);
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 | return ds;
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Shows the TagConversionDialog asking the user whether to keep all, some or no tags
|
---|
146 | * @param keys The keys present during the current conversion
|
---|
147 | * @param listPos The keys that were previously selected
|
---|
148 | * @param listNeg The keys that were previously unselected
|
---|
149 | * @return {@link TagConversionDialogResponse} containing the selection
|
---|
150 | */
|
---|
151 | private static TagConversionDialogResponse showTagConversionDialog(List<String> keys, List<String> listPos, List<String> listNeg) {
|
---|
152 | TagConversionDialogResponse res = new TagConversionDialogResponse(listPos, listNeg);
|
---|
153 | String lSel = Config.getPref().get(GPX_SETTING + ".last", "all");
|
---|
154 |
|
---|
155 | JPanel p = new JPanel(new GridBagLayout());
|
---|
156 | ButtonGroup r = new ButtonGroup();
|
---|
157 |
|
---|
158 | p.add(new JLabel(
|
---|
159 | tr("The GPX layer contains fields that can be converted to OSM tags. How would you like to proceed?")),
|
---|
160 | GBC.eol());
|
---|
161 | JRadioButton rAll = new JRadioButton(tr("Convert all fields"), "all".equals(lSel));
|
---|
162 | r.add(rAll);
|
---|
163 | p.add(rAll, GBC.eol());
|
---|
164 |
|
---|
165 | JRadioButton rList = new JRadioButton(tr("Only convert the following fields:"), "list".equals(lSel));
|
---|
166 | r.add(rList);
|
---|
167 | p.add(rList, GBC.eol());
|
---|
168 |
|
---|
169 | JPanel q = new JPanel();
|
---|
170 |
|
---|
171 | List<JCheckBox> checkList = new ArrayList<>();
|
---|
172 | for (String key : keys) {
|
---|
173 | JCheckBox cTmp = new JCheckBox(key, !listNeg.contains(key));
|
---|
174 | checkList.add(cTmp);
|
---|
175 | q.add(cTmp);
|
---|
176 | }
|
---|
177 |
|
---|
178 | q.setBorder(BorderFactory.createEmptyBorder(0, 20, 5, 0));
|
---|
179 | p.add(q, GBC.eol());
|
---|
180 |
|
---|
181 | JRadioButton rNone = new JRadioButton(tr("Do not convert any fields"), "no".equals(lSel));
|
---|
182 | r.add(rNone);
|
---|
183 | p.add(rNone, GBC.eol());
|
---|
184 |
|
---|
185 | ActionListener enabler = new TagConversionDialogRadioButtonActionListener(checkList, true);
|
---|
186 | ActionListener disabler = new TagConversionDialogRadioButtonActionListener(checkList, false);
|
---|
187 |
|
---|
188 | if (!"list".equals(lSel)) {
|
---|
189 | disabler.actionPerformed(null);
|
---|
190 | }
|
---|
191 |
|
---|
192 | rAll.addActionListener(disabler);
|
---|
193 | rList.addActionListener(enabler);
|
---|
194 | rNone.addActionListener(disabler);
|
---|
195 |
|
---|
196 | ExtendedDialog ed = new ExtendedDialog(MainApplication.getMainFrame(), tr("Options"),
|
---|
197 | tr("Convert"), tr("Convert and remember selection"), tr("Cancel"))
|
---|
198 | .setButtonIcons("exportgpx", "exportgpx", "cancel").setContent(p);
|
---|
199 | int ret = ed.showDialog().getValue();
|
---|
200 |
|
---|
201 | if (ret == 1 || ret == 2) {
|
---|
202 | for (JCheckBox cItem : checkList) {
|
---|
203 | String key = cItem.getText();
|
---|
204 | if (cItem.isSelected()) {
|
---|
205 | if (!res.listPos.contains(key)) {
|
---|
206 | res.listPos.add(key);
|
---|
207 | }
|
---|
208 | res.listNeg.remove(key);
|
---|
209 | } else {
|
---|
210 | if (!res.listNeg.contains(key)) {
|
---|
211 | res.listNeg.add(key);
|
---|
212 | }
|
---|
213 | res.listPos.remove(key);
|
---|
214 | }
|
---|
215 | }
|
---|
216 | if (rAll.isSelected()) {
|
---|
217 | res.sel = "all";
|
---|
218 | } else if (rNone.isSelected()) {
|
---|
219 | res.sel = "no";
|
---|
220 | }
|
---|
221 | Config.getPref().put(GPX_SETTING + ".last", res.sel);
|
---|
222 | if (ret == 2) {
|
---|
223 | Config.getPref().put(GPX_SETTING, res.sel);
|
---|
224 | } else {
|
---|
225 | Config.getPref().put(GPX_SETTING, "ask");
|
---|
226 | }
|
---|
227 | Config.getPref().putList(GPX_SETTING + ".list.yes", res.listPos);
|
---|
228 | Config.getPref().putList(GPX_SETTING + ".list.no", res.listNeg);
|
---|
229 | } else {
|
---|
230 | res.sel = null;
|
---|
231 | }
|
---|
232 | return res;
|
---|
233 | }
|
---|
234 |
|
---|
235 | private static class TagConversionDialogResponse {
|
---|
236 |
|
---|
237 | final List<String> listPos;
|
---|
238 | final List<String> listNeg;
|
---|
239 | String sel = "list";
|
---|
240 |
|
---|
241 | TagConversionDialogResponse(List<String> p, List<String> n) {
|
---|
242 | listPos = new ArrayList<>(p);
|
---|
243 | listNeg = new ArrayList<>(n);
|
---|
244 | }
|
---|
245 | }
|
---|
246 |
|
---|
247 | private static class TagConversionDialogRadioButtonActionListener implements ActionListener {
|
---|
248 |
|
---|
249 | private final boolean enable;
|
---|
250 | private final List<JCheckBox> checkList;
|
---|
251 |
|
---|
252 | TagConversionDialogRadioButtonActionListener(List<JCheckBox> chks, boolean en) {
|
---|
253 | enable = en;
|
---|
254 | checkList = chks;
|
---|
255 | }
|
---|
256 |
|
---|
257 | @Override
|
---|
258 | public void actionPerformed(ActionEvent arg0) {
|
---|
259 | for (JCheckBox ch : checkList) {
|
---|
260 | ch.setEnabled(enable);
|
---|
261 | }
|
---|
262 | }
|
---|
263 | }
|
---|
264 | }
|
---|