Class ShapefileMenu

All Implemented Interfaces:
ImageObserver, ItemSelectable, MenuContainer, Serializable, Accessible, MenuElement, SwingConstants

public class ShapefileMenu extends JMenu
A JMenu that provides interactive shapefile loading and per-layer visibility control for a MapView2D.
 Shapefiles
   Open Shapefile...
   Recent Shapefiles ▸
     1  ne_10m_lakes
     2  ne_10m_rivers_lake_centerlines
     ─────────────────
     Clear Recent
   ─────────────────
   ☑ Rivers          ← added when first layer is loaded
   ☑ Lakes
   ☐ Urban Areas     ← unchecked = hidden
 

Opening shapefiles

"Open Shapefile..." presents a JFileChooser filtered to .shp files. The companion .dbf is located automatically from the same directory. After loading, a default style is chosen based on the geometry type of the first feature in the file:

  • Polygon — semi-transparent blue fill with a slightly darker border, suitable for lakes, urban areas, or any filled area.
  • Polyline — blue stroke, suitable for rivers, roads, or coastlines.
  • Point — red dot markers.
The style can be refined programmatically via ShapeFeatureRenderer.setStyle(edu.cnu.mdi.mapping.shapefile.ShapeFeatureStyle) after the layer is added.

Recent shapefiles

Every successfully loaded .shp file — whether opened via this menu or dropped directly onto the map (see MapView2D.filesDropped(java.util.List<java.io.File>)) — is recorded in a persistent, most-recently-used list (backed by RecentFiles/Preferences, capped at 10 entries) and offered again under "Recent Shapefiles". A file that fails to load, or is later found missing, is dropped from the list rather than left as a dead entry.

Programmatic layers

Layers added at startup via MapView2D.addShapefile(ShapeFeatureRenderer, String) are automatically reflected in the menu with a checkbox. The menu does not need to know whether a layer came from user interaction or from code.

Visibility

Toggling a checkbox changes the corresponding layer's visibility and triggers a repaint. The layer remains in the layer list; it simply produces no output when hidden, so re-showing it is instantaneous.

Usage in MapView2D


 ShapefileMenu shpMenu = new ShapefileMenu(this);
 applyFocusFix(shpMenu, this);
 getJMenuBar().add(shpMenu, 1);  // insert after File menu
 
See Also:
  • Constructor Details

    • ShapefileMenu

      public ShapefileMenu(MapView2D mapView)
      Creates a Shapefiles menu bound to the given map view.
      Parameters:
      mapView - the map view to control; must not be null
  • Method Details

    • recordRecentlyOpened

      public void recordRecentlyOpened(File file)
      Records a file as recently opened and refreshes the "Recent Shapefiles" submenu, without (re)loading it.

      Called by MapView2D.filesDropped(java.util.List<java.io.File>) after a dropped shapefile has already been loaded successfully, so drag-and-drop and the "Open Shapefile..." action populate the same recent list.

      Parameters:
      file - the shapefile that was just loaded; ignored if null
    • defaultStyle

      public static ShapeFeatureStyle defaultStyle(int shapeType)
      Returns a sensible default ShapeFeatureStyle based on the given shape type. Polygon layers get a semi-transparent fill; polyline layers get a stroked line; point layers get dot markers.
      Parameters:
      shapeType - one of the TYPE_* constants in ShapefileGeometryReader
      Returns:
      a pre-configured style
    • baseName

      public static String baseName(Path path)
      Returns the filename without its extension, used as the default layer display name.
      Parameters:
      path - path to the .shp file
      Returns:
      base filename without extension