Пример #1
0
  private static BufferedImage createWorldMapImage(final Product product) {
    final GeneralPath[] geoBoundaryPaths = ProductUtils.createGeoBoundaryPaths(product);
    final Image wmImage = WorldMapImageLoader.getWorldMapImage(false);
    final WorldMapPainter worldMapPainter =
        new WorldMapPainter(
            wmImage.getScaledInstance(wmImage.getHeight(null) / 2, -1, Image.SCALE_SMOOTH));

    return worldMapPainter.createWorldMapImage(geoBoundaryPaths);
  }
Пример #2
0
  public static class WorldMapCellEditor extends AbstractCellEditor implements TableCellEditor {

    private static final Image worldMap = WorldMapImageLoader.getWorldMapImage(false);

    private final WorldMapPainter wmPainter;
    private final JScrollPane scrollPane;
    private Image scaledImage;

    public WorldMapCellEditor() {
      scrollPane = new JScrollPane();
      scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
      scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
      scrollPane.getViewport().setOpaque(false);
      wmPainter = new WorldMapPainter(worldMap);
      scaledImage = worldMap.getScaledInstance(worldMap.getWidth(null) / 2, -1, Image.SCALE_SMOOTH);
    }

    public Object getCellEditorValue() {
      return null;
    }

    public Component getTableCellEditorComponent(
        final JTable table,
        final Object value,
        final boolean isSelected,
        final int row,
        final int column) {

      final String boundaryString;
      if (value instanceof PropertyMap) {
        final PropertyMap map = (PropertyMap) value;
        boundaryString = map.getPropertyString(KEY_BOUNDARY_PATH, null);
      } else {
        boundaryString = null;
      }
      final GeneralPath[] geoBoundaryPathes = createGeoBoundaryPathes(boundaryString);
      if (geoBoundaryPathes.length > 0) {
        wmPainter.setWorldMapImage(scaledImage);
        final BufferedImage worldMapImage = wmPainter.createWorldMapImage(geoBoundaryPathes);
        scrollPane.setViewportView(new JLabel(new ImageIcon(worldMapImage)));

        final Color backgroundColor = table.getSelectionBackground();
        scrollPane.setBorder(BorderFactory.createLineBorder(backgroundColor, 3));
        scrollPane.setBackground(backgroundColor);

        // todo: first time scrolling is not good, cause the viewRect has no width and height at
        // this time
        // hack: this is not good, there might be a better solution
        final JViewport viewport = scrollPane.getViewport();
        if (viewport.getWidth() == 0 && viewport.getHeight() == 0) {
          viewport.setSize(
              table.getColumnModel().getColumn(column).getWidth(), table.getRowHeight(row));
        }

        scrollToCenterPath(geoBoundaryPathes[0], viewport);
        return scrollPane;
      } else {
        return null;
      }
    }

    private void scrollToCenterPath(final GeneralPath geoBoundaryPath, final JViewport viewport) {
      final GeneralPath pixelPath =
          ProductUtils.convertToPixelPath(geoBoundaryPath, wmPainter.getGeoCoding());
      final Rectangle viewRect = viewport.getViewRect();
      final Rectangle pathBounds = pixelPath.getBounds();
      setCenter(viewRect, new Point((int) pathBounds.getCenterX(), (int) pathBounds.getCenterY()));
      final Dimension bounds =
          new Dimension(scaledImage.getWidth(null), scaledImage.getHeight(null));
      ensureRectIsInBounds(viewRect, bounds);
      viewport.scrollRectToVisible(viewRect);
    }

    private void ensureRectIsInBounds(final Rectangle rectangle, final Dimension bounds) {
      if (rectangle.x < 0) {
        rectangle.x = 0;
      }
      if (rectangle.x + rectangle.width > bounds.width) {
        final JScrollBar verticalScrollBar = scrollPane.getVerticalScrollBar();
        final int scrollBarWidth;
        if (verticalScrollBar != null) {
          scrollBarWidth = verticalScrollBar.getWidth();
        } else {
          scrollBarWidth = 0;
        }
        rectangle.x = bounds.width - rectangle.width - scrollBarWidth;
      }
      if (rectangle.y < 0) {
        rectangle.y = 0;
      }
      if (rectangle.y + rectangle.height > bounds.height) {
        final JScrollBar horizontalScrollBar = scrollPane.getHorizontalScrollBar();
        final int scrollBarHeight;
        if (horizontalScrollBar != null) {
          scrollBarHeight = horizontalScrollBar.getHeight();
        } else {
          scrollBarHeight = 0;
        }
        rectangle.y = bounds.height - rectangle.height - scrollBarHeight;
      }
    }

    private static void setCenter(final Rectangle rectangle, final Point center) {
      final int diffX = center.x - (int) rectangle.getCenterX();
      final int diffY = center.y - (int) rectangle.getCenterY();
      final int x = ((int) rectangle.getX()) + diffX;
      final int y = (int) (rectangle.getY() + diffY);
      rectangle.setLocation(x, y);
    }
  }
Пример #3
0
  public static class WorldMapCellRenderer extends DefaultTableCellRenderer {

    private static final Image worldMap = WorldMapImageLoader.getWorldMapImage(false);

    private final int cellWidth;
    private final int cellHeight;

    private WorldMapPainter wmPainter;
    private JLabel cellComponent;

    public WorldMapCellRenderer(final int cellWidth) {
      this.cellWidth = cellWidth;
      cellHeight = cellWidth / 2;
      wmPainter =
          new WorldMapPainter(worldMap.getScaledInstance(this.cellWidth, -1, Image.SCALE_DEFAULT));
    }

    @Override
    public Component getTableCellRendererComponent(
        final JTable table,
        final Object value,
        final boolean isSelected,
        final boolean hasFocus,
        final int row,
        final int column) {

      if (cellComponent == null) {
        cellComponent =
            (JLabel)
                super.getTableCellRendererComponent(
                    table, value,
                    isSelected, hasFocus,
                    row, column);
        cellComponent.setText("");
        cellComponent.setHorizontalAlignment(SwingConstants.CENTER);
        cellComponent.setVerticalAlignment(SwingConstants.CENTER);
      }

      setBackground(table, isSelected);

      final String boundaryString;

      if (value instanceof PropertyMap) {
        final PropertyMap propertyMap = (PropertyMap) value;
        boundaryString = propertyMap.getPropertyString(KEY_BOUNDARY_PATH, null);
      } else {
        boundaryString = null;
      }

      final GeneralPath[] geoBoundaryPathes = createGeoBoundaryPathes(boundaryString);
      if (geoBoundaryPathes.length > 0) {
        final BufferedImage worldMapImage = wmPainter.createWorldMapImage(geoBoundaryPathes);
        final Image scaledWorldMap =
            worldMapImage.getScaledInstance(cellWidth, -1, Image.SCALE_DEFAULT);

        cellComponent.setIcon(new ImageIcon(scaledWorldMap));
        cellComponent.setText("");

        adjustCellHeight(table, row);
      } else {
        cellComponent.setIcon(null);
        if (value == null) {
          cellComponent.setText("");
        } else {
          cellComponent.setText("Location not available!");
        }
      }

      return cellComponent;
    }

    private void setBackground(final JTable table, final boolean isSelected) {
      Color backgroundColor = table.getBackground();
      if (isSelected) {
        backgroundColor = table.getSelectionBackground();
      }
      cellComponent.setBorder(BorderFactory.createLineBorder(backgroundColor, 3));
      cellComponent.setBackground(backgroundColor);
    }

    private void adjustCellHeight(final JTable table, final int row) {
      if (table.getRowHeight(row) < cellHeight) {
        table.setRowHeight(row, cellHeight);
      }
    }
  }