Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/layer/AbstractTileSourceLayer.java
r13861 r13884 50 50 import javax.swing.Action; 51 51 import javax.swing.JLabel; 52 import javax.swing.JMenu; 52 53 import javax.swing.JMenuItem; 53 54 import javax.swing.JOptionPane; … … 163 164 164 165 private final AttributionSupport attribution = new AttributionSupport(); 165 private final TileHolder clickedTileHolder = new TileHolder();166 166 167 167 /** … … 190 190 if (!isVisible()) return; 191 191 if (e.getButton() == MouseEvent.BUTTON3) { 192 clickedTileHolder.setTile(getTileForPixelpos(e.getX(), e.getY())); 193 new TileSourceLayerPopup().show(e.getComponent(), e.getX(), e.getY()); 192 new TileSourceLayerPopup(e.getX(), e.getY()).show(e.getComponent(), e.getX(), e.getY()); 194 193 } else if (e.getButton() == MouseEvent.BUTTON1) { 195 194 attribution.handleAttribution(e.getPoint(), true); … … 419 418 } 420 419 421 private final class ShowTileInfoAction extends AbstractAction { 422 423 private ShowTileInfoAction() { 424 super(tr("Show tile info")); 425 setEnabled(clickedTileHolder.getTile() != null); 420 private abstract static class AbstractTileAction extends AbstractAction { 421 422 protected final AbstractTileSourceLayer<?> layer; 423 protected final Tile tile; 424 425 private AbstractTileAction(String name, AbstractTileSourceLayer<?> layer, Tile tile) { 426 super(name); 427 this.layer = layer; 428 this.tile = tile; 429 } 430 @Override 431 public abstract void actionPerformed(ActionEvent arg0); 432 433 } 434 private final static class ShowTileInfoAction extends AbstractTileAction { 435 436 private ShowTileInfoAction(AbstractTileSourceLayer<?> layer, Tile tile) { 437 super(tr("Show tile info"), layer, tile); 438 setEnabled(tile != null); 426 439 } 427 440 … … 432 445 @Override 433 446 public void actionPerformed(ActionEvent ae) { 434 Tile clickedTile = clickedTileHolder.getTile(); 435 if (clickedTile != null) { 447 if (tile != null) { 436 448 ExtendedDialog ed = new ExtendedDialog(Main.parent, tr("Tile Info"), tr("OK")); 437 449 JPanel panel = new JPanel(new GridBagLayout()); 438 Rectangle2D displaySize = coordinateConverter.getRectangleForTile(clickedTile);450 Rectangle2D displaySize = layer.coordinateConverter.getRectangleForTile(tile); 439 451 String url = ""; 440 452 try { 441 url = clickedTile.getUrl();453 url = tile.getUrl(); 442 454 } catch (IOException e) { 443 455 // silence exceptions … … 446 458 447 459 List<List<String>> content = new ArrayList<>(); 448 content.add(Arrays.asList(tr("Tile name"), clickedTile.getKey()));460 content.add(Arrays.asList(tr("Tile name"), tile.getKey())); 449 461 content.add(Arrays.asList(tr("Tile URL"), url)); 450 462 content.add(Arrays.asList(tr("Tile size"), 451 getSizeString( clickedTile.getTileSource().getTileSize())));463 getSizeString(tile.getTileSource().getTileSize()))); 452 464 content.add(Arrays.asList(tr("Tile display size"), 453 465 new StringBuilder().append(displaySize.getWidth()) 454 466 .append('x') 455 467 .append(displaySize.getHeight()).toString())); 456 if ( coordinateConverter.requiresReprojection()) {468 if (layer.coordinateConverter.requiresReprojection()) { 457 469 content.add(Arrays.asList(tr("Reprojection"), 458 clickedTile.getTileSource().getServerCRS() +470 tile.getTileSource().getServerCRS() + 459 471 " -> " + Main.getProjection().toCode())); 460 BufferedImage img = clickedTile.getImage();472 BufferedImage img = tile.getImage(); 461 473 if (img != null) { 462 474 content.add(Arrays.asList(tr("Reprojected tile size"), … … 468 480 panel.add(new JLabel(entry.get(0) + ':'), GBC.std()); 469 481 panel.add(GBC.glue(5, 0), GBC.std()); 470 panel.add( createTextField(entry.get(1)), GBC.eol().fill(GBC.HORIZONTAL));471 } 472 473 for (Entry<String, String> e: clickedTile.getMetadata().entrySet()) {482 panel.add(layer.createTextField(entry.get(1)), GBC.eol().fill(GBC.HORIZONTAL)); 483 } 484 485 for (Entry<String, String> e: tile.getMetadata().entrySet()) { 474 486 panel.add(new JLabel(tr("Metadata ") + tr(e.getKey()) + ':'), GBC.std()); 475 487 panel.add(GBC.glue(5, 0), GBC.std()); … … 478 490 value = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(Long.parseLong(value))); 479 491 } 480 panel.add( createTextField(value), GBC.eol().fill(GBC.HORIZONTAL));492 panel.add(layer.createTextField(value), GBC.eol().fill(GBC.HORIZONTAL)); 481 493 482 494 } … … 488 500 } 489 501 490 private final class LoadTileAction extends AbstractAction {491 492 private LoadTileAction( ) {493 super(tr("Load tile") );494 setEnabled( clickedTileHolder.getTile()!= null);502 private final static class LoadTileAction extends AbstractTileAction { 503 504 private LoadTileAction(AbstractTileSourceLayer<?> layer, Tile tile) { 505 super(tr("Load tile"), layer, tile); 506 setEnabled(tile != null); 495 507 } 496 508 497 509 @Override 498 510 public void actionPerformed(ActionEvent ae) { 499 Tile clickedTile = clickedTileHolder.getTile(); 500 if (clickedTile != null) { 501 loadTile(clickedTile, true); 502 invalidate(); 503 } 504 } 505 } 506 507 private void sendOsmTileRequest(String request) { 508 Tile clickedTile = clickedTileHolder.getTile(); 509 if (clickedTile != null) { 511 if (tile != null) { 512 layer.loadTile(tile, true); 513 layer.invalidate(); 514 } 515 } 516 } 517 518 private void sendOsmTileRequest(Tile tile, String request) { 519 if (tile != null) { 510 520 try { 511 new Notification(HttpClient.create(new URL( clickedTile.getUrl() + '/' + request))521 new Notification(HttpClient.create(new URL(tile.getUrl() + '/' + request)) 512 522 .connect().fetchContent()).show(); 513 523 } catch (IOException ex) { … … 517 527 } 518 528 519 private final class GetOsmTileStatusAction extends AbstractAction {520 private GetOsmTileStatusAction( ) {521 super(tr("Get tile status") );522 setEnabled( clickedTileHolder.getTile()!= null);529 private final static class GetOsmTileStatusAction extends AbstractTileAction { 530 private GetOsmTileStatusAction(AbstractTileSourceLayer<?> layer, Tile tile) { 531 super(tr("Get tile status"), layer, tile); 532 setEnabled(tile != null); 523 533 } 524 534 525 535 @Override 526 536 public void actionPerformed(ActionEvent e) { 527 sendOsmTileRequest("status");528 } 529 } 530 531 private final class MarkOsmTileDirtyAction extends AbstractAction {532 private MarkOsmTileDirtyAction( ) {533 super(tr("Force tile rendering") );534 setEnabled( clickedTileHolder.getTile()!= null);537 layer.sendOsmTileRequest(tile, "status"); 538 } 539 } 540 541 private final static class MarkOsmTileDirtyAction extends AbstractTileAction { 542 private MarkOsmTileDirtyAction(AbstractTileSourceLayer<?> layer, Tile tile) { 543 super(tr("Force tile rendering"), layer, tile); 544 setEnabled(tile != null); 535 545 } 536 546 537 547 @Override 538 548 public void actionPerformed(ActionEvent e) { 539 sendOsmTileRequest("dirty"); 540 } 541 } 542 543 /** 544 * Simple class to keep clickedTile within hookUpMapView 545 */ 546 private static final class TileHolder { 547 private Tile t; 548 549 public Tile getTile() { 550 return t; 551 } 552 553 public void setTile(Tile t) { 554 this.t = t; 549 layer.sendOsmTileRequest(tile, "dirty"); 555 550 } 556 551 } … … 607 602 /** 608 603 * Constructs a new {@code TileSourceLayerPopup}. 604 * @param x horizontal dimension where user clicked 605 * @param y vertical dimension where user clicked 609 606 */ 610 public TileSourceLayerPopup() { 611 for (Action a : getCommonEntries()) { 612 if (a instanceof LayerAction) { 613 add(((LayerAction) a).createMenuComponent()); 614 } else { 615 add(new JMenuItem(a)); 616 } 617 } 618 add(new JSeparator()); 619 add(new JMenuItem(new LoadTileAction())); 620 add(new JMenuItem(new ShowTileInfoAction())); 621 if (ExpertToggleAction.isExpert() && tileSource != null && tileSource.isModTileFeatures()) { 622 add(new JMenuItem(new GetOsmTileStatusAction())); 623 add(new JMenuItem(new MarkOsmTileDirtyAction())); 607 public TileSourceLayerPopup(int x, int y) { 608 List<JMenu> submenus = new ArrayList<>(); 609 MainApplication.getLayerManager().getVisibleLayersInZOrder().stream() 610 .filter(AbstractTileSourceLayer.class::isInstance) 611 .map(AbstractTileSourceLayer.class::cast) 612 .forEachOrdered(layer -> { 613 JMenu submenu = new JMenu(layer.getName()); 614 for (Action a : layer.getCommonEntries()) { 615 if (a instanceof LayerAction) { 616 submenu.add(((LayerAction) a).createMenuComponent()); 617 } else { 618 submenu.add(new JMenuItem(a)); 619 } 620 } 621 submenu.add(new JSeparator()); 622 Tile tile = layer.getTileForPixelpos(x, y); 623 submenu.add(new JMenuItem(new LoadTileAction(layer, tile))); 624 submenu.add(new JMenuItem(new ShowTileInfoAction(layer, tile))); 625 if (ExpertToggleAction.isExpert() && tileSource != null && tileSource.isModTileFeatures()) { 626 submenu.add(new JMenuItem(new GetOsmTileStatusAction(layer, tile))); 627 submenu.add(new JMenuItem(new MarkOsmTileDirtyAction(layer, tile))); 628 } 629 submenus.add(submenu); 630 }); 631 632 if (submenus.size() == 1) { 633 JMenu menu = submenus.get(0); 634 Arrays.stream(menu.getMenuComponents()).forEachOrdered(this::add); 635 } else if (submenus.size() > 1) { 636 submenus.stream().forEachOrdered(this::add); 624 637 } 625 638 } -
trunk/test/unit/org/openstreetmap/josm/gui/layer/AbstractTileSourceLayerTest.java
r13733 r13884 203 203 @Test 204 204 public void testTileSourceLayerPopup() { 205 assertNotNull(testLayer.new TileSourceLayerPopup( ));205 assertNotNull(testLayer.new TileSourceLayerPopup(100, 100)); 206 206 } 207 207 }
Note:
See TracChangeset
for help on using the changeset viewer.