/** Creates new form JMainFrame */
  public JMainFrame() {
    initComponents();

    splMain.setDividerLocation(0.5f);
    splMain.setResizeWeight(0.5f);

    _mapView = createMapView();
    layeredPane.setLayout(new OverlayLayout(layeredPane));
    layeredPane.add(_mapView, JLayeredPane.DEFAULT_LAYER);

    lblError = new JLabel();
    lblError.setForeground(Color.RED);
    lblError.setFont(lblError.getFont().deriveFont(Font.BOLD));
    layeredPane.add(lblError, JLayeredPane.POPUP_LAYER);
    lblError.setBorder(BorderFactory.createEmptyBorder(25, 25, 25, 25));
    lblError.setText("");

    _editorPane = createCodeTextPane();
    pnlCode.setLayout(new BorderLayout());
    pnlCode.add(_editorPane.getContainerWithLines(), BorderLayout.CENTER);

    refreshUI();

    // 50.636311, 5.570565
    MapFile mf = new MapFile(Constants.MAP_FILE);
    double lat = mf.boundingBox().getCenterPoint().latitude;
    double lng = mf.boundingBox().getCenterPoint().longitude;
    _mapView.getModel().mapViewPosition.setCenter(new LatLong(lat, lng));
    _mapView.getModel().mapViewPosition.setZoomLevel((byte) 15);
  }
  private void changeMap() {
    try {
      JFileChooser fileChooser = new JFileChooser();
      int result = fileChooser.showOpenDialog(this);
      if (result == JFileChooser.APPROVE_OPTION) {
        File f = fileChooser.getSelectedFile();
        if (!f.exists()) throw new Exception("File doesn't exists!");

        String path = f.getAbsolutePath();
        // String path = Constants.ROOT_MAPSFORGE_DIR + "\\styles\\default.xml";

        Constants.MAP_FILE = path;

        File fProperties = new File("properties.props");

        Properties props = new Properties();
        props.load(new FileInputStream(fProperties));
        props.put("mapFile", Constants.MAP_FILE);
        props.save(new FileOutputStream(fProperties), "");

        MapFile mf = new MapFile(f);
        double lat = mf.boundingBox().getCenterPoint().latitude;
        double lng = mf.boundingBox().getCenterPoint().longitude;
        _mapView.getModel().mapViewPosition.setCenter(new LatLong(lat, lng));

        refreshUI();
      }
    } catch (Exception e) {
      e.printStackTrace();
      JOptionPane.showMessageDialog(
          this, e.toString(), "Error while changing map file", JOptionPane.ERROR_MESSAGE);
    }
  }
  private MapView createMapView() {
    MapView mapView = new MapView();
    mapView.getMapScaleBar().setVisible(true);
    mapView.addComponentListener(
        new MapViewComponentListener(mapView, mapView.getModel().mapViewDimension));

    MouseEventListener mouseEventListener = new MouseEventListener(mapView.getModel());
    mapView.addMouseListener(mouseEventListener);
    mapView.addMouseMotionListener(mouseEventListener);
    mapView.addMouseWheelListener(mouseEventListener);

    return mapView;
  }
  private void refreshUI() {
    if (_openedDocument != null) {
      // _editorPane.setText(_openedDocument.getContent());

      String path = _openedDocument.getPath();
      Layers layers = _mapView.getLayerManager().getLayers();

      layers.clear();
      TileCache tc = createTileCache(0);

      System.out.println("RefreshUI()");

      String filePath = Constants.MAP_FILE;
      String rendererPath = _openedDocument.getPath();
      String fileLocation = "";
      if (rendererPath != null)
        fileLocation = new File(rendererPath).getParentFile().getAbsolutePath();

      TileRendererLayer layer =
          createTileRendererLayerWithString(
              tc,
              _mapView.getModel().mapViewPosition,
              false,
              true,
              filePath,
              _editorPane.getText(),
              fileLocation);
      layers.add(layer);

      _openedDocument.putContents(_editorPane.getText());

      // TODO
      final RenderThemeFuture rtf =
          new RenderThemeFuture(
              GRAPHIC_FACTORY,
              new MapsForgeStringXmlRenderTheme(_editorPane.getText(), fileLocation),
              new DisplayModel());
      Thread t =
          new Thread(
              new Runnable() {

                @Override
                public void run() {

                  rtf.run();
                  try {
                    rtf.get();

                    SwingUtilities.invokeLater(
                        new Runnable() {

                          @Override
                          public void run() {
                            System.out.println("Dismiss the error!");
                            // _mapView.setVisible(true);
                            // lblError.setVisible(false);
                            lblError.setText("");
                          }
                        });

                  } catch (final Exception e) {
                    SwingUtilities.invokeLater(
                        new Runnable() {

                          @Override
                          public void run() {
                            System.out.println("Showing an error!");
                            // _mapView.setVisible(false);
                            // lblError.setVisible(true);
                            lblError.setText("<html>" + e.toString() + "</html>");
                          }
                        });
                  }
                }
              });
      t.start();

    } else {
      Layers layers = _mapView.getLayerManager().getLayers();
      layers.clear();
    }
  }