Changeset 3408 in josm for trunk/src/org/openstreetmap/josm/gui/layer/GpxLayer.java
- Timestamp:
- 2010-08-03T07:52:26+02:00 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/layer/GpxLayer.java
r3325 r3408 10 10 import java.awt.BasicStroke; 11 11 import java.awt.Color; 12 import java.awt.Component;13 12 import java.awt.Graphics2D; 14 13 import java.awt.GridBagLayout; 15 14 import java.awt.Point; 16 15 import java.awt.event.ActionEvent; 17 import java.awt.event.ActionListener;18 16 import java.awt.geom.Area; 19 17 import java.awt.geom.Rectangle2D; … … 31 29 32 30 import javax.swing.AbstractAction; 31 import javax.swing.Action; 33 32 import javax.swing.Box; 34 33 import javax.swing.ButtonGroup; … … 38 37 import javax.swing.JLabel; 39 38 import javax.swing.JList; 40 import javax.swing.JMenuItem;41 39 import javax.swing.JOptionPane; 42 40 import javax.swing.JPanel; 43 41 import javax.swing.JRadioButton; 44 import javax.swing.JSeparator;45 42 import javax.swing.filechooser.FileFilter; 46 43 … … 130 127 131 128 @Override 132 public Component[] getMenuEntries() { 133 JMenuItem line = new JMenuItem(tr("Customize line drawing"), ImageProvider.get("mapmode/addsegment")); 134 line.addActionListener(new ActionListener() { 135 public void actionPerformed(ActionEvent e) { 136 JRadioButton[] r = new JRadioButton[3]; 137 r[0] = new JRadioButton(tr("Use global settings.")); 138 r[1] = new JRadioButton(tr("Draw lines between points for this layer.")); 139 r[2] = new JRadioButton(tr("Do not draw lines between points for this layer.")); 140 ButtonGroup group = new ButtonGroup(); 141 Box panel = Box.createVerticalBox(); 142 for (JRadioButton b : r) { 143 group.add(b); 144 panel.add(b); 145 } 146 String propName = "draw.rawgps.lines.layer " + getName(); 147 if (Main.pref.hasKey(propName)) { 148 group.setSelected(r[Main.pref.getBoolean(propName) ? 1 : 2].getModel(), true); 149 } else { 150 group.setSelected(r[0].getModel(), true); 151 } 152 int answer = JOptionPane.showConfirmDialog(Main.parent, panel, 153 tr("Select line drawing options"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); 154 switch (answer) { 155 case JOptionPane.CANCEL_OPTION: 156 case JOptionPane.CLOSED_OPTION: 157 return; 158 default: 159 // continue 160 } 161 if (group.getSelection() == r[0].getModel()) { 162 Main.pref.put(propName, null); 163 } else { 164 Main.pref.put(propName, group.getSelection() == r[1].getModel()); 165 } 166 Main.map.repaint(); 167 } 168 }); 169 170 JMenuItem color = new JMenuItem(tr("Customize Color"), ImageProvider.get("colorchooser")); 171 color.putClientProperty("help", "Action/LayerCustomizeColor"); 172 color.addActionListener(new ActionListener() { 173 public void actionPerformed(ActionEvent e) { 174 JColorChooser c = new JColorChooser(getColor(getName())); 175 Object[] options = new Object[] { tr("OK"), tr("Cancel"), tr("Default") }; 176 int answer = JOptionPane.showOptionDialog( 177 Main.parent, 178 c, 179 tr("Choose a color"), 180 JOptionPane.OK_CANCEL_OPTION, 181 JOptionPane.PLAIN_MESSAGE, 182 null, 183 options, options[0] 184 ); 185 switch (answer) { 186 case 0: 187 Main.pref.putColor("layer " + getName(), c.getColor()); 188 break; 189 case 1: 190 return; 191 case 2: 192 Main.pref.putColor("layer " + getName(), null); 193 break; 194 } 195 Main.map.repaint(); 196 } 197 }); 198 199 JMenuItem markersFromNamedTrackpoints = new JMenuItem(tr("Markers From Named Points"), ImageProvider 200 .get("addmarkers")); 201 markersFromNamedTrackpoints.putClientProperty("help", "Action/MarkersFromNamedPoints"); 202 markersFromNamedTrackpoints.addActionListener(new ActionListener() { 203 public void actionPerformed(ActionEvent e) { 204 GpxData namedTrackPoints = new GpxData(); 205 for (GpxTrack track : data.tracks) { 206 for (GpxTrackSegment seg : track.getSegments()) { 207 for (WayPoint point : seg.getWayPoints()) 208 if (point.attr.containsKey("name") || point.attr.containsKey("desc")) { 209 namedTrackPoints.waypoints.add(point); 210 } 211 } 212 } 213 214 MarkerLayer ml = new MarkerLayer(namedTrackPoints, tr("Named Trackpoints from {0}", getName()), 215 getAssociatedFile(), GpxLayer.this); 216 if (ml.data.size() > 0) { 217 Main.main.addLayer(ml); 218 } 219 } 220 }); 221 222 JMenuItem importAudio = new JMenuItem(tr("Import Audio"), ImageProvider.get("importaudio")); 223 importAudio.putClientProperty("help", "ImportAudio"); 224 importAudio.addActionListener(new ActionListener() { 225 private void warnCantImportIntoServerLayer(GpxLayer layer) { 226 String msg = tr("<html>The data in the GPX layer ''{0}'' has been downloaded from the server.<br>" 227 + "Because its way points do not include a timestamp we cannot correlate them with audio data.</html>", 228 layer.getName() 229 ); 230 HelpAwareOptionPane.showOptionDialog( 231 Main.parent, 232 msg, 233 tr("Import not possible"), 234 JOptionPane.WARNING_MESSAGE, 235 ht("/Action/ImportImages#CantImportIntoGpxLayerFromServer") 236 ); 237 } 238 public void actionPerformed(ActionEvent e) { 239 if (GpxLayer.this.data.fromServer) { 240 warnCantImportIntoServerLayer(GpxLayer.this); 241 return; 242 } 243 String dir = Main.pref.get("markers.lastaudiodirectory"); 244 JFileChooser fc = new JFileChooser(dir); 245 fc.setFileSelectionMode(JFileChooser.FILES_ONLY); 246 fc.setAcceptAllFileFilterUsed(false); 247 fc.setFileFilter(new FileFilter() { 248 @Override 249 public boolean accept(File f) { 250 return f.isDirectory() || f.getName().toLowerCase().endsWith(".wav"); 251 } 252 253 @Override 254 public String getDescription() { 255 return tr("Wave Audio files (*.wav)"); 256 } 257 }); 258 fc.setMultiSelectionEnabled(true); 259 if (fc.showOpenDialog(Main.parent) == JFileChooser.APPROVE_OPTION) { 260 if (!fc.getCurrentDirectory().getAbsolutePath().equals(dir)) { 261 Main.pref.put("markers.lastaudiodirectory", fc.getCurrentDirectory().getAbsolutePath()); 262 } 263 264 File sel[] = fc.getSelectedFiles(); 265 // sort files in increasing order of timestamp (this is the end time, but so 266 // long as they don't overlap, that's fine) 267 if (sel.length > 1) { 268 Arrays.sort(sel, new Comparator<File>() { 269 public int compare(File a, File b) { 270 return a.lastModified() <= b.lastModified() ? -1 : 1; 271 } 272 }); 273 } 274 275 String names = null; 276 for (int i = 0; i < sel.length; i++) { 277 if (names == null) { 278 names = " ("; 279 } else { 280 names += ", "; 281 } 282 names += sel[i].getName(); 283 } 284 if (names != null) { 285 names += ")"; 286 } else { 287 names = ""; 288 } 289 MarkerLayer ml = new MarkerLayer(new GpxData(), tr("Audio markers from {0}", getName()) + names, 290 getAssociatedFile(), GpxLayer.this); 291 double firstStartTime = sel[0].lastModified() / 1000.0 /* ms -> seconds */ 292 - AudioUtil.getCalibratedDuration(sel[0]); 293 294 Markers m = new Markers(); 295 for (int i = 0; i < sel.length; i++) { 296 importAudio(sel[i], ml, firstStartTime, m); 297 } 298 Main.main.addLayer(ml); 299 Main.map.repaint(); 300 } 301 } 302 }); 303 304 JMenuItem tagimage = new JMenuItem(tr("Import images"), ImageProvider.get("dialogs/geoimage")); 305 tagimage.putClientProperty("help", ht("/Action/ImportImages")); 306 tagimage.addActionListener(new ActionListener() { 307 308 private void warnCantImportIntoServerLayer(GpxLayer layer) { 309 String msg = tr("<html>The data in the GPX layer ''{0}'' has been downloaded from the server.<br>" 310 + "Because its way points do not include a timestamp we cannot correlate them with images.</html>", 311 layer.getName() 312 ); 313 HelpAwareOptionPane.showOptionDialog( 314 Main.parent, 315 msg, 316 tr("Import not possible"), 317 JOptionPane.WARNING_MESSAGE, 318 ht("/Action/ImportImages#CantImportIntoGpxLayerFromServer") 319 ); 320 } 321 322 public void actionPerformed(ActionEvent e) { 323 if (GpxLayer.this.data.fromServer) { 324 warnCantImportIntoServerLayer(GpxLayer.this); 325 return; 326 } 327 String curDir = Main.pref.get("geoimage.lastdirectory", Main.pref.get("lastDirectory")); 328 if (curDir.equals("")) { 329 curDir = "."; 330 } 331 JFileChooser fc = new JFileChooser(new File(curDir)); 332 333 fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 334 fc.setMultiSelectionEnabled(true); 335 fc.setAcceptAllFileFilterUsed(false); 336 JpgImporter importer = new JpgImporter(GpxLayer.this); 337 fc.setFileFilter(importer.filter); 338 fc.showOpenDialog(Main.parent); 339 LinkedList<File> files = new LinkedList<File>(); 340 File[] sel = fc.getSelectedFiles(); 341 if (sel == null || sel.length == 0) 342 return; 343 if (!fc.getCurrentDirectory().getAbsolutePath().equals(curDir)) { 344 Main.pref.put("geoimage.lastdirectory", fc.getCurrentDirectory().getAbsolutePath()); 345 } 346 addRecursiveFiles(files, sel); 347 importer.importDataHandleExceptions(files, NullProgressMonitor.INSTANCE); 348 } 349 350 private void addRecursiveFiles(LinkedList<File> files, File[] sel) { 351 for (File f : sel) { 352 if (f.isDirectory()) { 353 addRecursiveFiles(files, f.listFiles()); 354 } else if (f.getName().toLowerCase().endsWith(".jpg")) { 355 files.add(f); 356 } 357 } 358 } 359 }); 360 129 public Action[] getMenuEntries() { 361 130 if (Main.applet) 362 return new Component[] { new JMenuItem(LayerListDialog.getInstance().createShowHideLayerAction(this)), 363 new JMenuItem(LayerListDialog.getInstance().createDeleteLayerAction(this)), new JSeparator(), color, line, 364 new JMenuItem(new ConvertToDataLayerAction()), new JSeparator(), 365 new JMenuItem(new RenameLayerAction(getAssociatedFile(), this)), new JSeparator(), 366 new JMenuItem(new LayerListPopup.InfoAction(this)) }; 367 return new Component[] { new JMenuItem(LayerListDialog.getInstance().createShowHideLayerAction(this)), 368 new JMenuItem(LayerListDialog.getInstance().createDeleteLayerAction(this)), new JSeparator(), 369 new JMenuItem(new LayerSaveAction(this)), new JMenuItem(new LayerSaveAsAction(this)), color, line, 370 tagimage, importAudio, markersFromNamedTrackpoints, new JMenuItem(new ConvertToDataLayerAction()), 371 new JMenuItem(new DownloadAlongTrackAction()), new JSeparator(), 372 new JMenuItem(new RenameLayerAction(getAssociatedFile(), this)), new JSeparator(), 373 new JMenuItem(new LayerListPopup.InfoAction(this)) }; 131 return new Action[] { 132 LayerListDialog.getInstance().createShowHideLayerAction(), 133 LayerListDialog.getInstance().createDeleteLayerAction(), 134 SeparatorLayerAction.INSTANCE, 135 new CustomizeColor(), 136 new CustomizeLineDrawing(), 137 new ConvertToDataLayerAction(), 138 SeparatorLayerAction.INSTANCE, 139 new RenameLayerAction(getAssociatedFile(), this), 140 SeparatorLayerAction.INSTANCE, 141 new LayerListPopup.InfoAction(this) }; 142 return new Action[] { 143 LayerListDialog.getInstance().createShowHideLayerAction(), 144 LayerListDialog.getInstance().createDeleteLayerAction(), 145 SeparatorLayerAction.INSTANCE, 146 new LayerSaveAction(this), 147 new LayerSaveAsAction(this), 148 new CustomizeColor(), 149 new CustomizeLineDrawing(), 150 new ImportImages(), 151 new ImportAudio(), 152 new MarkersFromNamedPoins(), 153 new ConvertToDataLayerAction(), 154 new DownloadAlongTrackAction(), 155 SeparatorLayerAction.INSTANCE, 156 new RenameLayerAction(getAssociatedFile(), this), 157 SeparatorLayerAction.INSTANCE, 158 new LayerListPopup.InfoAction(this) }; 374 159 } 375 160 … … 1417 1202 return best; 1418 1203 } 1204 1205 private class CustomizeLineDrawing extends AbstractAction { 1206 1207 CustomizeLineDrawing() { 1208 super(tr("Customize line drawing"), ImageProvider.get("mapmode/addsegment")); 1209 } 1210 1211 @Override 1212 public void actionPerformed(ActionEvent e) { 1213 JRadioButton[] r = new JRadioButton[3]; 1214 r[0] = new JRadioButton(tr("Use global settings.")); 1215 r[1] = new JRadioButton(tr("Draw lines between points for this layer.")); 1216 r[2] = new JRadioButton(tr("Do not draw lines between points for this layer.")); 1217 ButtonGroup group = new ButtonGroup(); 1218 Box panel = Box.createVerticalBox(); 1219 for (JRadioButton b : r) { 1220 group.add(b); 1221 panel.add(b); 1222 } 1223 String propName = "draw.rawgps.lines.layer " + getName(); 1224 if (Main.pref.hasKey(propName)) { 1225 group.setSelected(r[Main.pref.getBoolean(propName) ? 1 : 2].getModel(), true); 1226 } else { 1227 group.setSelected(r[0].getModel(), true); 1228 } 1229 int answer = JOptionPane.showConfirmDialog(Main.parent, panel, 1230 tr("Select line drawing options"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); 1231 switch (answer) { 1232 case JOptionPane.CANCEL_OPTION: 1233 case JOptionPane.CLOSED_OPTION: 1234 return; 1235 default: 1236 // continue 1237 } 1238 if (group.getSelection() == r[0].getModel()) { 1239 Main.pref.put(propName, null); 1240 } else { 1241 Main.pref.put(propName, group.getSelection() == r[1].getModel()); 1242 } 1243 Main.map.repaint(); 1244 } 1245 } 1246 1247 private class CustomizeColor extends AbstractAction { 1248 1249 public CustomizeColor() { 1250 super(tr("Customize Color"), ImageProvider.get("colorchooser")); 1251 putValue("help", "Action/LayerCustomizeColor"); 1252 } 1253 1254 @Override 1255 public void actionPerformed(ActionEvent e) { 1256 JColorChooser c = new JColorChooser(getColor(getName())); 1257 Object[] options = new Object[] { tr("OK"), tr("Cancel"), tr("Default") }; 1258 int answer = JOptionPane.showOptionDialog( 1259 Main.parent, 1260 c, 1261 tr("Choose a color"), 1262 JOptionPane.OK_CANCEL_OPTION, 1263 JOptionPane.PLAIN_MESSAGE, 1264 null, 1265 options, options[0] 1266 ); 1267 switch (answer) { 1268 case 0: 1269 Main.pref.putColor("layer " + getName(), c.getColor()); 1270 break; 1271 case 1: 1272 return; 1273 case 2: 1274 Main.pref.putColor("layer " + getName(), null); 1275 break; 1276 } 1277 Main.map.repaint(); 1278 } 1279 1280 } 1281 1282 private class MarkersFromNamedPoins extends AbstractAction { 1283 1284 public MarkersFromNamedPoins() { 1285 super(tr("Markers From Named Points"), ImageProvider.get("addmarkers")); 1286 putValue("help", "Action/MarkersFromNamedPoints"); 1287 } 1288 1289 @Override 1290 public void actionPerformed(ActionEvent e) { 1291 GpxData namedTrackPoints = new GpxData(); 1292 for (GpxTrack track : data.tracks) { 1293 for (GpxTrackSegment seg : track.getSegments()) { 1294 for (WayPoint point : seg.getWayPoints()) 1295 if (point.attr.containsKey("name") || point.attr.containsKey("desc")) { 1296 namedTrackPoints.waypoints.add(point); 1297 } 1298 } 1299 } 1300 1301 MarkerLayer ml = new MarkerLayer(namedTrackPoints, tr("Named Trackpoints from {0}", getName()), 1302 getAssociatedFile(), GpxLayer.this); 1303 if (ml.data.size() > 0) { 1304 Main.main.addLayer(ml); 1305 } 1306 1307 } 1308 } 1309 1310 private class ImportAudio extends AbstractAction { 1311 1312 public ImportAudio() { 1313 super(tr("Import Audio"), ImageProvider.get("importaudio")); 1314 putValue("help", "ImportAudio"); 1315 } 1316 1317 private void warnCantImportIntoServerLayer(GpxLayer layer) { 1318 String msg = tr("<html>The data in the GPX layer ''{0}'' has been downloaded from the server.<br>" 1319 + "Because its way points do not include a timestamp we cannot correlate them with audio data.</html>", 1320 layer.getName() 1321 ); 1322 HelpAwareOptionPane.showOptionDialog( 1323 Main.parent, 1324 msg, 1325 tr("Import not possible"), 1326 JOptionPane.WARNING_MESSAGE, 1327 ht("/Action/ImportImages#CantImportIntoGpxLayerFromServer") 1328 ); 1329 } 1330 1331 @Override 1332 public void actionPerformed(ActionEvent e) { 1333 if (GpxLayer.this.data.fromServer) { 1334 warnCantImportIntoServerLayer(GpxLayer.this); 1335 return; 1336 } 1337 String dir = Main.pref.get("markers.lastaudiodirectory"); 1338 JFileChooser fc = new JFileChooser(dir); 1339 fc.setFileSelectionMode(JFileChooser.FILES_ONLY); 1340 fc.setAcceptAllFileFilterUsed(false); 1341 fc.setFileFilter(new FileFilter() { 1342 @Override 1343 public boolean accept(File f) { 1344 return f.isDirectory() || f.getName().toLowerCase().endsWith(".wav"); 1345 } 1346 1347 @Override 1348 public String getDescription() { 1349 return tr("Wave Audio files (*.wav)"); 1350 } 1351 }); 1352 fc.setMultiSelectionEnabled(true); 1353 if (fc.showOpenDialog(Main.parent) == JFileChooser.APPROVE_OPTION) { 1354 if (!fc.getCurrentDirectory().getAbsolutePath().equals(dir)) { 1355 Main.pref.put("markers.lastaudiodirectory", fc.getCurrentDirectory().getAbsolutePath()); 1356 } 1357 1358 File sel[] = fc.getSelectedFiles(); 1359 // sort files in increasing order of timestamp (this is the end time, but so 1360 // long as they don't overlap, that's fine) 1361 if (sel.length > 1) { 1362 Arrays.sort(sel, new Comparator<File>() { 1363 public int compare(File a, File b) { 1364 return a.lastModified() <= b.lastModified() ? -1 : 1; 1365 } 1366 }); 1367 } 1368 1369 String names = null; 1370 for (int i = 0; i < sel.length; i++) { 1371 if (names == null) { 1372 names = " ("; 1373 } else { 1374 names += ", "; 1375 } 1376 names += sel[i].getName(); 1377 } 1378 if (names != null) { 1379 names += ")"; 1380 } else { 1381 names = ""; 1382 } 1383 MarkerLayer ml = new MarkerLayer(new GpxData(), tr("Audio markers from {0}", getName()) + names, 1384 getAssociatedFile(), GpxLayer.this); 1385 double firstStartTime = sel[0].lastModified() / 1000.0 /* ms -> seconds */ 1386 - AudioUtil.getCalibratedDuration(sel[0]); 1387 1388 Markers m = new Markers(); 1389 for (int i = 0; i < sel.length; i++) { 1390 importAudio(sel[i], ml, firstStartTime, m); 1391 } 1392 Main.main.addLayer(ml); 1393 Main.map.repaint(); 1394 } 1395 1396 } 1397 } 1398 1399 private class ImportImages extends AbstractAction { 1400 1401 public ImportImages() { 1402 super(tr("Import images"), ImageProvider.get("dialogs/geoimage")); 1403 putValue("help", ht("/Action/ImportImages")); 1404 } 1405 1406 private void warnCantImportIntoServerLayer(GpxLayer layer) { 1407 String msg = tr("<html>The data in the GPX layer ''{0}'' has been downloaded from the server.<br>" 1408 + "Because its way points do not include a timestamp we cannot correlate them with images.</html>", 1409 layer.getName() 1410 ); 1411 HelpAwareOptionPane.showOptionDialog( 1412 Main.parent, 1413 msg, 1414 tr("Import not possible"), 1415 JOptionPane.WARNING_MESSAGE, 1416 ht("/Action/ImportImages#CantImportIntoGpxLayerFromServer") 1417 ); 1418 } 1419 1420 private void addRecursiveFiles(LinkedList<File> files, File[] sel) { 1421 for (File f : sel) { 1422 if (f.isDirectory()) { 1423 addRecursiveFiles(files, f.listFiles()); 1424 } else if (f.getName().toLowerCase().endsWith(".jpg")) { 1425 files.add(f); 1426 } 1427 } 1428 } 1429 1430 @Override 1431 public void actionPerformed(ActionEvent e) { 1432 1433 if (GpxLayer.this.data.fromServer) { 1434 warnCantImportIntoServerLayer(GpxLayer.this); 1435 return; 1436 } 1437 String curDir = Main.pref.get("geoimage.lastdirectory", Main.pref.get("lastDirectory")); 1438 if (curDir.equals("")) { 1439 curDir = "."; 1440 } 1441 JFileChooser fc = new JFileChooser(new File(curDir)); 1442 1443 fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 1444 fc.setMultiSelectionEnabled(true); 1445 fc.setAcceptAllFileFilterUsed(false); 1446 JpgImporter importer = new JpgImporter(GpxLayer.this); 1447 fc.setFileFilter(importer.filter); 1448 fc.showOpenDialog(Main.parent); 1449 LinkedList<File> files = new LinkedList<File>(); 1450 File[] sel = fc.getSelectedFiles(); 1451 if (sel == null || sel.length == 0) 1452 return; 1453 if (!fc.getCurrentDirectory().getAbsolutePath().equals(curDir)) { 1454 Main.pref.put("geoimage.lastdirectory", fc.getCurrentDirectory().getAbsolutePath()); 1455 } 1456 addRecursiveFiles(files, sel); 1457 importer.importDataHandleExceptions(files, NullProgressMonitor.INSTANCE); 1458 } 1459 1460 } 1419 1461 }
Note:
See TracChangeset
for help on using the changeset viewer.