/** Draw the map. */
  @Override
  public void paint(Graphics g) {
    try {
      super.paint(g);

      // draw selection rectangle
      if (iSelectionRectStart != null && iSelectionRectEnd != null) {

        int zoomDiff = MAX_ZOOM - zoom;
        Point tlc = getTopLeftCoordinates();
        int x_min = (iSelectionRectStart.x >> zoomDiff) - tlc.x;
        int y_min = (iSelectionRectStart.y >> zoomDiff) - tlc.y;
        int x_max = (iSelectionRectEnd.x >> zoomDiff) - tlc.x;
        int y_max = (iSelectionRectEnd.y >> zoomDiff) - tlc.y;

        int w = x_max - x_min;
        int h = y_max - y_min;
        g.setColor(new Color(0.9f, 0.7f, 0.7f, 0.6f));
        g.fillRect(x_min, y_min, w, h);

        g.setColor(Color.BLACK);
        g.drawRect(x_min, y_min, w, h);
      }

      iSizeButton.paint(g);
      iSourceButton.paint(g);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
    /**
     * Constructs a new {@code ImageryProvidersPanel}.
     *
     * @param gui The parent preference tab pane
     * @param layerInfoArg The list of imagery entries to display
     */
    public ImageryProvidersPanel(final PreferenceTabbedPane gui, ImageryLayerInfo layerInfoArg) {
      super(new GridBagLayout());
      this.gui = gui;
      this.layerInfo = layerInfoArg;
      this.activeModel = new ImageryLayerTableModel();

      activeTable =
          new JTable(activeModel) {
            @Override
            public String getToolTipText(MouseEvent e) {
              java.awt.Point p = e.getPoint();
              return activeModel.getValueAt(rowAtPoint(p), columnAtPoint(p)).toString();
            }
          };
      activeTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

      defaultModel = new ImageryDefaultLayerTableModel();
      defaultTable = new JTable(defaultModel);

      defaultModel.addTableModelListener(e -> activeTable.repaint());
      activeModel.addTableModelListener(e -> defaultTable.repaint());

      TableColumnModel mod = defaultTable.getColumnModel();
      mod.getColumn(2).setPreferredWidth(800);
      mod.getColumn(2).setCellRenderer(new ImageryURLTableCellRenderer(layerInfo.getLayers()));
      mod.getColumn(1).setPreferredWidth(400);
      mod.getColumn(1).setCellRenderer(new ImageryNameTableCellRenderer());
      mod.getColumn(0).setPreferredWidth(50);

      mod = activeTable.getColumnModel();
      mod.getColumn(1).setPreferredWidth(800);
      mod.getColumn(1)
          .setCellRenderer(new ImageryURLTableCellRenderer(layerInfo.getDefaultLayers()));
      mod.getColumn(0).setPreferredWidth(200);

      RemoveEntryAction remove = new RemoveEntryAction();
      activeTable.getSelectionModel().addListSelectionListener(remove);

      add(new JLabel(tr("Available default entries:")), GBC.eol().insets(5, 5, 0, 0));
      // Add default item list
      JScrollPane scrolldef = new JScrollPane(defaultTable);
      scrolldef.setPreferredSize(new Dimension(200, 200));
      add(
          scrolldef,
          GBC.std()
              .insets(0, 5, 0, 0)
              .fill(GridBagConstraints.BOTH)
              .weight(1.0, 0.6)
              .insets(5, 0, 0, 0));

      // Add default item map
      defaultMap = new JMapViewer();
      defaultMap.setTileSource(new OsmTileSource.Mapnik()); // for attribution
      defaultMap.addMouseListener(
          new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
              if (e.getButton() == MouseEvent.BUTTON1) {
                defaultMap.getAttribution().handleAttribution(e.getPoint(), true);
              }
            }
          });
      defaultMap.setZoomContolsVisible(false);
      defaultMap.setMinimumSize(new Dimension(100, 200));
      add(
          defaultMap,
          GBC.std()
              .insets(5, 5, 0, 0)
              .fill(GridBagConstraints.BOTH)
              .weight(0.33, 0.6)
              .insets(5, 0, 0, 0));

      defaultTableListener = new DefListSelectionListener();
      defaultTable.getSelectionModel().addListSelectionListener(defaultTableListener);

      defaultToolbar = new JToolBar(JToolBar.VERTICAL);
      defaultToolbar.setFloatable(false);
      defaultToolbar.setBorderPainted(false);
      defaultToolbar.setOpaque(false);
      defaultToolbar.add(new ReloadAction());
      add(defaultToolbar, GBC.eol().anchor(GBC.SOUTH).insets(0, 0, 5, 0));

      ActivateAction activate = new ActivateAction();
      defaultTable.getSelectionModel().addListSelectionListener(activate);
      JButton btnActivate = new JButton(activate);

      middleToolbar = new JToolBar(JToolBar.HORIZONTAL);
      middleToolbar.setFloatable(false);
      middleToolbar.setBorderPainted(false);
      middleToolbar.setOpaque(false);
      middleToolbar.add(btnActivate);
      add(middleToolbar, GBC.eol().anchor(GBC.CENTER).insets(5, 15, 5, 0));

      add(Box.createHorizontalGlue(), GBC.eol().fill(GridBagConstraints.HORIZONTAL));

      add(new JLabel(tr("Selected entries:")), GBC.eol().insets(5, 0, 0, 0));
      JScrollPane scroll = new JScrollPane(activeTable);
      add(
          scroll,
          GBC.std()
              .fill(GridBagConstraints.BOTH)
              .span(GridBagConstraints.RELATIVE)
              .weight(1.0, 0.4)
              .insets(5, 0, 0, 5));
      scroll.setPreferredSize(new Dimension(200, 200));

      activeToolbar = new JToolBar(JToolBar.VERTICAL);
      activeToolbar.setFloatable(false);
      activeToolbar.setBorderPainted(false);
      activeToolbar.setOpaque(false);
      activeToolbar.add(new NewEntryAction(ImageryInfo.ImageryType.WMS));
      activeToolbar.add(new NewEntryAction(ImageryInfo.ImageryType.TMS));
      activeToolbar.add(new NewEntryAction(ImageryInfo.ImageryType.WMTS));
      // activeToolbar.add(edit); TODO
      activeToolbar.add(remove);
      add(activeToolbar, GBC.eol().anchor(GBC.NORTH).insets(0, 0, 5, 5));
    }