| | 139 | //gpx.convert-tags: all, list, *ask, no |
| | 140 | //gpx.convert-tags.last: *all, list, no |
| | 141 | //gpx.convert-tags.list.yes |
| | 142 | //gpx.convert-tags.list.no |
| | 143 | List<String> listPos = Config.getPref().getList("gpx.convert-tags.list.yes"); |
| | 144 | List<String> listNeg = Config.getPref().getList("gpx.convert-tags.list.no"); |
| | 145 | if (check && !keys.isEmpty()) { |
| | 146 | // Either "list" or "ask" was stored in the settings, so the nodes have to be filtered after all tags have been processed |
| | 147 | List<String> allTags = new ArrayList<>(listPos); |
| | 148 | allTags.addAll(listNeg); |
| | 149 | if (!allTags.containsAll(keys) || convertTags.equals("ask")) { |
| | 150 | // not all keys are in positive/negative list, so we have to ask the user |
| | 151 | TagConversionDialogResponse res = showTagConversionDialog(keys, listPos, listNeg); |
| | 152 | if (res.sel == null) { |
| | 153 | return null; |
| | 154 | } |
| | 155 | listPos = res.listPos; |
| | 156 | |
| | 157 | if (res.sel.equals("no")) { |
| | 158 | // User just chose not to convert any tags, but that was unknown before the initial conversion |
| | 159 | return filterDataSet(ds, null); |
| | 160 | } else if (res.sel.equals("all")) { |
| | 161 | return ds; |
| | 162 | } |
| | 163 | } |
| | 164 | if (!listPos.containsAll(keys)) { |
| | 165 | ds = filterDataSet(ds, listPos); |
| | 166 | } |
| | 167 | } |
| | 170 | |
| | 171 | /** |
| | 172 | * Filters the tags of the given {@link DataSet} |
| | 173 | * @param ds The {@link DataSet} |
| | 174 | * @param listPos A {@code List<String>} containing the tags to be kept, can be {@code null} if all tags are to be removed |
| | 175 | * @return The {@link DataSet} |
| | 176 | * @since xxx |
| | 177 | */ |
| | 178 | public DataSet filterDataSet(DataSet ds, List<String> listPos) { |
| | 179 | Collection<Node> nodes = ds.getNodes(); |
| | 180 | for (Node n : nodes) { |
| | 181 | for (String key : n.keySet()) { |
| | 182 | if (listPos == null || !listPos.contains(key)) { |
| | 183 | n.put(key, null); |
| | 184 | } |
| | 185 | } |
| | 186 | } |
| | 187 | return ds; |
| | 188 | } |
| | 189 | |
| | 190 | /** |
| | 191 | * Shows the TagConversionDialog asking the user whether to keep all, some or no tags |
| | 192 | * @param keys The keys present during the current conversion |
| | 193 | * @param listPos The keys that were previously selected |
| | 194 | * @param listNeg The keys that were previously unselected |
| | 195 | * @return {@link TagConversionDialogResponse} containing the selection |
| | 196 | */ |
| | 197 | private TagConversionDialogResponse showTagConversionDialog(List<String> keys, List<String> listPos, |
| | 198 | List<String> listNeg) { |
| | 199 | TagConversionDialogResponse res = new TagConversionDialogResponse(listPos, listNeg); |
| | 200 | String lSel = Config.getPref().get("gpx.convert-tags.last", "all"); |
| | 201 | |
| | 202 | JPanel p = new JPanel(new GridBagLayout()); |
| | 203 | ButtonGroup r = new ButtonGroup(); |
| | 204 | |
| | 205 | p.add(new JLabel(tr( |
| | 206 | "The GPX layer contains fields that can be converted to OSM tags. How would you like to proceed?")), |
| | 207 | GBC.eol()); |
| | 208 | JRadioButton rAll = new JRadioButton(tr("Convert all fields"), lSel.equals("all")); |
| | 209 | r.add(rAll); |
| | 210 | p.add(rAll, GBC.eol()); |
| | 211 | |
| | 212 | JRadioButton rList = new JRadioButton(tr("Only convert the following fields:"), lSel.equals("list")); |
| | 213 | r.add(rList); |
| | 214 | p.add(rList, GBC.eol()); |
| | 215 | |
| | 216 | JPanel q = new JPanel(); |
| | 217 | |
| | 218 | List<JCheckBox> checkList = new ArrayList<>(); |
| | 219 | for (String key : keys) { |
| | 220 | JCheckBox cTmp = new JCheckBox(key, !listNeg.contains(key)); |
| | 221 | checkList.add(cTmp); |
| | 222 | q.add(cTmp); |
| | 223 | } |
| | 224 | |
| | 225 | q.setBorder(BorderFactory.createEmptyBorder(0, 20, 5, 0)); |
| | 226 | p.add(q, GBC.eol()); |
| | 227 | |
| | 228 | JRadioButton rNone = new JRadioButton(tr("Do not convert any fields"), lSel.equals("no")); |
| | 229 | r.add(rNone); |
| | 230 | p.add(rNone, GBC.eol()); |
| | 231 | |
| | 232 | ActionListener enabler = new TagConversionDialogRadioButtonActionListener(checkList, true); |
| | 233 | ActionListener disabler = new TagConversionDialogRadioButtonActionListener(checkList, false); |
| | 234 | |
| | 235 | if (!lSel.equals("list")) { |
| | 236 | disabler.actionPerformed(null); |
| | 237 | } |
| | 238 | |
| | 239 | rAll.addActionListener(disabler); |
| | 240 | rList.addActionListener(enabler); |
| | 241 | rNone.addActionListener(disabler); |
| | 242 | |
| | 243 | ExtendedDialog ed = new ExtendedDialog(Main.parent, tr("Options"), tr("Convert"), |
| | 244 | tr("Convert and remember selection"), tr("Cancel")) |
| | 245 | .setButtonIcons("exportgpx", "exportgpx", "cancel").setContent(p); |
| | 246 | int ret = ed.showDialog().getValue(); |
| | 247 | |
| | 248 | if (ret == 1 || ret == 2) { |
| | 249 | for (JCheckBox cItem : checkList) { |
| | 250 | String key = cItem.getText(); |
| | 251 | if (cItem.isSelected()) { |
| | 252 | if (!listPos.contains(key)) { |
| | 253 | res.listPos.add(key); |
| | 254 | } |
| | 255 | res.listNeg.remove(key); |
| | 256 | } else { |
| | 257 | if (!listNeg.contains(key)) { |
| | 258 | res.listNeg.add(key); |
| | 259 | } |
| | 260 | res.listPos.remove(key); |
| | 261 | } |
| | 262 | } |
| | 263 | if (rAll.isSelected()) { |
| | 264 | res.sel = "all"; |
| | 265 | } else if (rNone.isSelected()) { |
| | 266 | res.sel = "no"; |
| | 267 | } |
| | 268 | Config.getPref().put("gpx.convert-tags.last", res.sel); |
| | 269 | if (ret == 2) { |
| | 270 | Config.getPref().put("gpx.convert-tags", res.sel); |
| | 271 | } else { |
| | 272 | Config.getPref().put("gpx.convert-tags", "ask"); |
| | 273 | } |
| | 274 | Config.getPref().putList("gpx.convert-tags.list.yes", res.listPos); |
| | 275 | Config.getPref().putList("gpx.convert-tags.list.no", res.listNeg); |
| | 276 | } else { |
| | 277 | res.sel = null; |
| | 278 | } |
| | 279 | return res; |
| | 280 | } |
| | 281 | |
| | 282 | private class TagConversionDialogResponse { |
| | 283 | public TagConversionDialogResponse(List<String> p, List<String> n) { |
| | 284 | listPos = new ArrayList<>(p); |
| | 285 | listNeg = new ArrayList<>(n); |
| | 286 | } |
| | 287 | |
| | 288 | List<String> listPos; |
| | 289 | List<String> listNeg; |
| | 290 | String sel = "list"; |
| | 291 | } |
| | 292 | |
| | 293 | private class TagConversionDialogRadioButtonActionListener implements ActionListener { |
| | 294 | |
| | 295 | private boolean enable; |
| | 296 | private List<JCheckBox> checkList; |
| | 297 | |
| | 298 | public TagConversionDialogRadioButtonActionListener(List<JCheckBox> chks, boolean en) { |
| | 299 | enable = en; |
| | 300 | checkList = chks; |
| | 301 | } |
| | 302 | |
| | 303 | @Override |
| | 304 | public void actionPerformed(ActionEvent arg0) { |
| | 305 | for (JCheckBox ch : checkList) { |
| | 306 | ch.setEnabled(enable); |
| | 307 | } |
| | 308 | } |
| | 309 | } |
| 184 | | final OsmDataLayer osmLayer = new OsmDataLayer(ds, tr("Converted from: {0}", layer.getName()), null); |
| 185 | | if (layer.getAssociatedFile() != null) { |
| 186 | | osmLayer.setAssociatedFile(new File(layer.getAssociatedFile().getParentFile(), layer.getAssociatedFile().getName() + ".osm")); |
| | 373 | if (ds != null) { |
| | 374 | final OsmDataLayer osmLayer = new OsmDataLayer(ds, tr("Converted from: {0}", layer.getName()), null); |
| | 375 | if (layer.getAssociatedFile() != null) { |
| | 376 | osmLayer.setAssociatedFile(new File(layer.getAssociatedFile().getParentFile(), |
| | 377 | layer.getAssociatedFile().getName() + ".osm")); |
| | 378 | } |
| | 379 | osmLayer.setUploadDiscouraged(true); |
| | 380 | MainApplication.getLayerManager().addLayer(osmLayer, false); |
| | 381 | MainApplication.getLayerManager().removeLayer(layer); |