This tutorial series show you how to build your first 2D LuciadLightspeed application with a GXY view:

Goal

In this tutorial, we add a UI widget for layer management.

Starting point

We take the code written in the Navigate the map article as our starting point, and expand from there.

Adding a UI widget with available layers

To keep the user of our application informed about the layers that are available on the view, we add a widget showing the available layers: a TLcdLayerTree. The widget also includes check boxes to toggle the visibility of the individual layers.

Program: Creating the TLcdLayerTree widget
private JComponent createLayerControl(ILcdGXYView aView) {
  return new TLcdLayerTree(aView);
}

Once the widget is created, we can add it to our JFrame:

Program: Creating the TLcdLayerTree widget
JComponent layerControl = createLayerControl(view);
frame.add(layerControl, BorderLayout.EAST);

The full code

Program: The full code
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.IOException;

import javax.swing.JComponent;
import javax.swing.JFrame;

import com.luciad.model.ILcdModel;
import com.luciad.model.ILcdModelDecoder;
import com.luciad.model.TLcdCompositeModelDecoder;
import com.luciad.util.service.TLcdServiceLoader;
import com.luciad.view.gxy.ILcdGXYLayer;
import com.luciad.view.gxy.ILcdGXYLayerFactory;
import com.luciad.view.gxy.ILcdGXYView;
import com.luciad.view.gxy.TLcdCompositeGXYLayerFactory;
import com.luciad.view.map.TLcdMapJPanel;
import com.luciad.view.swing.TLcdLayerTree;

public class FirstGXYApplicationTutorial {

  public JFrame createUI() {
    JFrame frame = new JFrame("First GXY application");

    TLcdMapJPanel view = createView();
    frame.add(view, BorderLayout.CENTER);

    try {
      ILcdGXYLayer backgroundLayer = createLayer(createRasterModel());
      view.addGXYLayer(backgroundLayer);

      ILcdGXYLayer citiesLayer = createLayer(createSHPModel());
      view.addGXYLayer(citiesLayer);
    } catch (IOException e) {
      throw new RuntimeException("Problem during data decoding", e);
    }

    //Move the grid layer to the top of the view,
    //so that it is rendered on top of the other layers
    view.moveLayerAt(view.layerCount() - 1, view.getGridLayer());

    JComponent layerControl = createLayerControl(view);
    frame.add(layerControl, BorderLayout.EAST);

    frame.setSize(800, 600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    return frame;
  }

  private TLcdMapJPanel createView() {
    TLcdMapJPanel view = new TLcdMapJPanel();

    TLcdCompositeGXYLayerFactory layerFactory =
        new TLcdCompositeGXYLayerFactory(TLcdServiceLoader.getInstance(ILcdGXYLayerFactory.class));
    //Install the layer factory on the view
    //When adding models to the view, this factory is used to create layers for those models
    view.setGXYLayerFactory(layerFactory);

    return view;
  }

  private ILcdModel createSHPModel() throws IOException {
    // This composite decoder can decode all supported formats
    ILcdModelDecoder decoder =
        new TLcdCompositeModelDecoder(TLcdServiceLoader.getInstance(ILcdModelDecoder.class));

    // Decode city_125.shp to create an ILcdModel
    ILcdModel shpModel = decoder.decode("Data/Shp/Usa/city_125.shp");

    return shpModel;
  }

  private ILcdModel createRasterModel() throws IOException {
    // This composite decoder can decode all supported formats
    ILcdModelDecoder decoder =
        new TLcdCompositeModelDecoder(TLcdServiceLoader.getInstance(ILcdModelDecoder.class));

    // Decode a sample data set (imagery data)
    ILcdModel geopackageModel = decoder.decode("Data/GeoPackage/bluemarble.gpkg");

    return geopackageModel;
  }

  private ILcdGXYLayer createLayer(ILcdModel aModel) {
    TLcdCompositeGXYLayerFactory layerFactory =
        new TLcdCompositeGXYLayerFactory(TLcdServiceLoader.getInstance(ILcdGXYLayerFactory.class));

    ILcdGXYLayer layer = layerFactory.createGXYLayer(aModel);
    if (layer != null) {
      return layer;
    }
    throw new RuntimeException("Could not create a layer for " + aModel.getModelDescriptor().getDisplayName());
  }

  private JComponent createLayerControl(ILcdGXYView aView) {
    return new TLcdLayerTree(aView);
  }

  public static void main(String[] args) {
    //Swing components must be created on the Event Dispatch Thread
    EventQueue.invokeLater(() -> {
      JFrame frame = new FirstGXYApplicationTutorial().createUI();
      frame.setVisible(true);
    });
  }
}