Class BaseContainer

All Implemented Interfaces:
IContainer, HoverListener, ItemChangeListener, ImageObserver, MenuContainer, Serializable, EventListener
Direct Known Subclasses:
MapContainer

public class BaseContainer extends JComponent implements IContainer, ItemChangeListener, HoverListener
Primary 2D drawing container for MDI.

A container holds a world-coordinate viewport and a z-stack of Layers. Each layer holds a collection of AItems.

Layer architecture

This container defines three standard layers:
  • Connections (protected; drawn first / bottom)
  • Content (default user layer; typical items go here)
  • Annotations (protected; drawn last / top)

Important implementation note

Layer's constructor auto-registers the new layer with the container via IContainer.addLayer(Layer). During this container's constructor, the protected layer fields are not yet assigned when the first addLayer calls occur.

Therefore, after constructing the layers we explicitly remove the protected layers from _layers and re-add the default user layer if needed. This guarantees that:

  • _layers contains only user layers
  • protected layers are still drawn and hit-tested in their fixed positions

Visibility and locking

Container-wide operations that involve user interaction (hit testing, selection, deletion, enclosed selection) respect:

Tool handler extensibility

Subclasses that need a specialised BaseToolHandler should override createToolHandler() rather than overriding setToolBar(edu.cnu.mdi.graphics.toolbar.AToolBar). This keeps the base class free of instanceof checks against specific subclasses.

File drag-and-drop

Drop support is no longer installed here. It is managed centrally by BaseView.enableFileDrop(), which attaches a single FileDropHandler to the canvas component (this object), the scroll-pane viewport (when present), and the view frame itself. Subclasses of BaseView activate drop support with one call:

     enableFileDrop(myFileFilter);   // in the BaseView subclass constructor
 
See Also:
  • Field Details

    • _layers

      protected List<Layer> _layers
      The user-managed z-layers (does NOT include protected layers).

      Ordering convention: index 0 is back (drawn earlier), last index is front.

    • _toolBar

      protected AToolBar _toolBar
      Optional toolbar for this container.
    • _feedbackPane

      protected FeedbackPane _feedbackPane
      Optional feedback (mouse-over) pane.
    • _afterDraw

      protected IDrawable _afterDraw
      Optional drawable invoked after all z layers are drawn.
    • _beforeDraw

      protected IDrawable _beforeDraw
      Optional drawable invoked before user layers are drawn.
    • _view

      protected BaseView _view
      The view that holds this container (may be null for viewless container).
    • _worldSystem

      protected Rectangle2D.Double _worldSystem
      World coordinate viewport.
    • _defaultWorldSystem

      protected Rectangle2D.Double _defaultWorldSystem
      Default/original world system.
    • _previousWorldSystem

      protected Rectangle2D.Double _previousWorldSystem
      Previous world system (used to undo last zoom).
    • hoverWindow

      protected HoverInfoWindow hoverWindow
      Lazily-created popup window used for hover messages.
    • _defaultLayer

      protected Layer _defaultLayer
      Default user content layer (drawn between connection and annotation layers). Most developer-created items go here by default.
    • _annotationLayer

      protected Layer _annotationLayer
      Annotation layer (protected; always drawn last).
    • _connectionLayer

      protected Layer _connectionLayer
      Connection layer (protected; always drawn first).
    • _feedbackControl

      protected final FeedbackControl _feedbackControl
      Feedback control for mouse-over feedback providers.
    • localToWorld

      protected AffineTransform localToWorld
      Transform local(screen) → world.
    • worldToLocal

      protected AffineTransform worldToLocal
      Transform world → local(screen).
    • toolHandler

      protected BaseToolHandler toolHandler
      Tool handler for this container.
  • Constructor Details

    • BaseContainer

      public BaseContainer(Rectangle2D.Double worldSystem)
      Construct a container with the given initial world system.
      Parameters:
      worldSystem - the default world coordinate viewport; must not be null
  • Method Details

    • setView

      public void setView(BaseView view)
      Associate this container with its hosting view.

      File drag-and-drop wiring is not done here; it is the responsibility of the view itself via BaseView.enableFileDrop(java.util.function.Predicate). This keeps all drop-surface management in one place and avoids the container needing to know about the transfer layer.

      Set the container's view.
      Specified by:
      setView in interface IContainer
      Parameters:
      view - the view to set.
    • shareModel

      public void shareModel(BaseContainer sContainer)
      Share the model with another container (used for magnification windows).

      This is not a deep copy: layers and items are shared by reference.

      Parameters:
      sContainer - the source container
    • getDefaultLayer

      public Layer getDefaultLayer()
      Get the default user content layer.
      Specified by:
      getDefaultLayer in interface IContainer
      Returns:
      the default user content layer (never null after construction)
    • isProtectedLayer

      public boolean isProtectedLayer(Layer layer)
      Determine whether a layer is one of the protected layers.
      Parameters:
      layer - layer to test
      Returns:
      true if layer is the connection or annotation layer
    • getAllLayers

      public List<Layer> getAllLayers()
      Get all layers in draw order (bottom -> top), including protected layers.

      Order:

      1. Connections
      2. User layers
      3. Annotations
      Returns:
      immutable snapshot list in draw order (never null)
    • getAllLayersForHitTesting

      public List<Layer> getAllLayersForHitTesting()
      Get all layers in hit-test order (top -> bottom), including protected layers.

      Order:

      1. Annotations
      2. User layers (top -> bottom)
      3. Connections
      Returns:
      immutable snapshot list in hit-test order (never null)
    • clipBounds

      public void clipBounds(Graphics g)
      Clip graphics context to container bounds.
      Parameters:
      g - graphics context
    • approximateZoomFactor

      public double approximateZoomFactor()
      Get the approximate zoom factor based on the current and default world systems.
      Specified by:
      approximateZoomFactor in interface IContainer
      Returns:
      the approximate zoom factor. Numbers greater than 1 mean zoomed in; less than 1 mean zoomed out.
    • paintComponent

      public void paintComponent(Graphics g)
      Override paint command. Draw all layers.

      Rendering order:

      1. background
      2. connection layer
      3. before-draw
      4. user layers
      5. after-draw
      6. annotation layer
      Overrides:
      paintComponent in class JComponent
      Parameters:
      g - graphics context
    • addLayer

      public void addLayer(Layer layer)
      Add a z layer for containing items rendered on this container..
      Specified by:
      addLayer in interface IContainer
      Parameters:
      layer - the layer to add.
    • getAnnotationLayer

      public Layer getAnnotationLayer()
      Get the annotation layer for this container.
      Specified by:
      getAnnotationLayer in interface IContainer
      Returns:
      the annotation layer for this container. All drawing tools draw on the annotation layer, which is kept on top.
    • getConnectionLayer

      public Layer getConnectionLayer()
      Get the connection layer for this container.
      Specified by:
      getConnectionLayer in interface IContainer
      Returns:
      the connection layer for this container. Connection items are drawn on first, i.e. connections are under other items.
    • getLayerByName

      public Layer getLayerByName(String name)
      Gets an z layer by name. Do not use for the annotation layer or connection layer-- for that use getAnnotationLayer() or getConnectionLayer().
      Specified by:
      getLayerByName in interface IContainer
      Parameters:
      name - the name of the layer.
      Returns:
      the z layer, or null.
    • localToWorld

      public void localToWorld(Point pp, Point2D.Double wp)
      This converts a screen or pixel point to a world point.
      Specified by:
      localToWorld in interface IContainer
      Parameters:
      pp - contains the local (screen-pixel) point.
      wp - will hold the resultant world point.
    • worldToLocal

      public void worldToLocal(Point pp, Point2D.Double wp)
      This converts a world point to a screen or pixel point.
      Specified by:
      worldToLocal in interface IContainer
      Parameters:
      pp - will hold the resultant local (screen-pixel) point.
      wp - contains world point.
    • worldToLocal

      public void worldToLocal(Rectangle r, Rectangle2D.Double wr)
      This converts a world rectangle to a screen or pixel rectangle.
      Specified by:
      worldToLocal in interface IContainer
      Parameters:
      r - will hold the resultant local (screen-pixel) rectangle.
      wr - contains the world rectangle.
    • localToWorld

      public void localToWorld(Rectangle r, Rectangle2D.Double wr)
      This converts a screen or local rectangle to a world rectangle.
      Specified by:
      localToWorld in interface IContainer
      Parameters:
      r - contains the local (screen-pixel) rectangle.
      wr - will hold the resultant world rectangle.
    • worldToLocal

      public void worldToLocal(Point pp, double wx, double wy)
      This converts a world point to a screen or pixel point.
      Specified by:
      worldToLocal in interface IContainer
      Parameters:
      pp - will hold the resultant local (screen-pixel) point.
      wx - the world x coordinate.
      wy - the world y coordinate.
    • pan

      public void pan(int dh, int dv)
      Pan the container.
      Specified by:
      pan in interface IContainer
      Parameters:
      dh - the horizontal step in pixels.
      dv - the vertical step in pixels.
    • recenter

      public void recenter(Point pp)
      Recenter the container at the point of a click.
      Specified by:
      recenter in interface IContainer
      Parameters:
      pp - the point in question. It will be the new center.
    • prepareToZoom

      public void prepareToZoom()
      Begin preparations for a zoom.
      Specified by:
      prepareToZoom in interface IContainer
    • restoreDefaultWorld

      public void restoreDefaultWorld()
      Restore the default world. This gets us back to the original zoom level.
      Specified by:
      restoreDefaultWorld in interface IContainer
    • resetWorldSystem

      public void resetWorldSystem(Rectangle2D.Double worldSystem)
      Reset the world system to a new value. Resets the default and previous world systems as well.
      Specified by:
      resetWorldSystem in interface IContainer
      Parameters:
      worldSystem - the new world system.
    • refresh

      public void refresh()
      Refresh the container.
      Specified by:
      refresh in interface IContainer
    • scale

      public void scale(double scaleFactor)
      Convenience routine to scale the container.
      Specified by:
      scale in interface IContainer
      Parameters:
      scaleFactor - the scale factor.
    • undoLastZoom

      public void undoLastZoom()
      Undo that last zoom.
      Specified by:
      undoLastZoom in interface IContainer
    • rubberBanded

      public void rubberBanded(Rectangle b)
      This is called when we have completed a rubber banding. pane.
      Specified by:
      rubberBanded in interface IContainer
      Parameters:
      b - The rubber band bounds.
    • setDirty

      public void setDirty(boolean dirty)
      Convenience method for setting the dirty flag for all items on all item lists.
      Specified by:
      setDirty in interface IContainer
      Parameters:
      dirty - the new value of the dirty flag.
    • getItemAtPoint

      public AItem getItemAtPoint(Point pp)
      Find an item, if any, at the point.

      Includes protected layers and respects visibility/locking.

      Specified by:
      getItemAtPoint in interface IContainer
      Parameters:
      pp - The pixel point in question.
      Returns:
      the topmost satisfying item, or null.
    • getEnclosedItems

      public ArrayList<AItem> getEnclosedItems(Rectangle rect)
      Obtain a collection of all enclosed items across all lists.

      Includes protected layers and respects visibility/locking.

      Specified by:
      getEnclosedItems in interface IContainer
      Parameters:
      rect - the rectangle in question.
      Returns:
      all items on all item lists enclosed by the rectangle.
    • getItemsAtPoint

      public ArrayList<AItem> getItemsAtPoint(Point lp)
      Find all items, if any, at the point.

      Includes protected layers and returns items in top -> bottom order.

      Specified by:
      getItemsAtPoint in interface IContainer
      Parameters:
      lp - the pixel point in question.
      Returns:
      all items across all item lists that contain the given point. It may be an empty vector, but it won't be null.
    • anySelectedItems

      public boolean anySelectedItems()
      Check whether at least one item on any item list is selected.

      Includes protected layers. Does not require visibility, so selections on hidden layers still count (if your tools allow that state).

      Specified by:
      anySelectedItems in interface IContainer
      Returns:
      true if at least one item on any item list is selected.
    • deleteSelectedItems

      public void deleteSelectedItems()
      Delete all selected items, across all item lists.

      Includes protected layers and respects visibility/locking.

      Specified by:
      deleteSelectedItems in interface IContainer
    • getSelectedItems

      public List<AItem> getSelectedItems()
      Get all selected items, across all item lists.

      Includes protected layers, and does not consider layer locking: even a locked layer's currently-selected, visible items are returned (locking only prevents new selection/hit-testing, not reporting of items that are already selected). Delegates to Layer.getSelectedItems().

      Specified by:
      getSelectedItems in interface IContainer
      Returns:
      a list of all currently selected items; never null
    • selectAllItems

      public void selectAllItems(boolean select)
      Select or deselect all items, across all item lists.

      Includes protected layers and respects locking.

      Specified by:
      selectAllItems in interface IContainer
      Parameters:
      select - the selection flag.
    • zoom

      public void zoom(double xmin, double xmax, double ymin, double ymax)
      Zooms to the specified area.
      Specified by:
      zoom in interface IContainer
      Parameters:
      xmin - minimum x coordinate.
      xmax - maximum x coordinate.
      ymin - minimum y coordinate.
      ymax - maximum y coordinate.
    • getToolBar

      public AToolBar getToolBar()
      Get this container's tool bar interface.
      Specified by:
      getToolBar in interface IContainer
      Returns:
      this container's tool bar, or null.
    • setToolBar

      public void setToolBar(AToolBar toolBar)
      Set this container's tool bar.

      Installs the toolbar and creates the tool handler via createToolHandler(), which subclasses may override to supply a different handler without requiring an instanceof branch here.

      Specified by:
      setToolBar in interface IContainer
      Parameters:
      toolBar - the new toolbar interface.
    • createToolHandler

      protected BaseToolHandler createToolHandler()
      Factory method that creates the BaseToolHandler appropriate for this container.

      The default implementation returns a plain BaseToolHandler. Subclasses that require specialised tool behaviour (e.g. MapContainer) should override this method to return their own handler rather than relying on an instanceof check in setToolBar(edu.cnu.mdi.graphics.toolbar.AToolBar).

      Returns:
      a new tool handler bound to this container; never null
    • setToolBarState

      public void setToolBarState()
      Check and set the toolbar state (e.g., button enable/disable)
    • getLocation

      protected Point2D.Double getLocation(MouseEvent me)
      Convert the mouse event location to a world point.
      Parameters:
      me - mouse event
      Returns:
      world location, or null
    • updateStatusText

      public void updateStatusText(Point pp, Point2D.Double wp)
      Update the toolbar status text with the current mouse location.
      Specified by:
      updateStatusText in interface IContainer
      Parameters:
      pp - the current mouse location in local (screen) coordinates
      wp - the current mouse location in world coordinates
    • feedbackTrigger

      public void feedbackTrigger(MouseEvent mouseEvent, boolean dragging)
      Convenience method to update the location string in the toolbar.

      Updates the toolbar status text (if present) and feedback control (if present) with the current mouse location in both local and world coordinates.

      Specified by:
      feedbackTrigger in interface IContainer
      Parameters:
      mouseEvent - the causal event.
      dragging - true if we are dragging
    • getView

      public BaseView getView()
      Get the view (internal frame) that holds this container.
      Specified by:
      getView in interface IContainer
      Returns:
      the view (internal frame) that holds this container.
    • onFilesDropped

      protected void onFilesDropped(List<File> files)
      Called when files are dropped onto this container's canvas.

      The default implementation logs each dropped filename and then delegates to BaseView.filesDropped(List) if a view is attached. Subclasses may override this to handle files differently before or instead of passing them to the view.

      Parameters:
      files - the validated files that were dropped; never null
    • itemChanged

      public void itemChanged(Layer layer, AItem item, ItemChangeType type)
      Responds to item change events fired by layers owned by this container.

      The base implementation:

      • Updates the toolbar state (e.g. enables/disables the delete button) when an item is selected or deselected.
      • Marks the affected item dirty on any change, triggering a repaint on the next refresh() call.
      All other change types are no-ops in the base class. Subclasses may override to add behaviour for specific types such as MOVED or DELETED.
      Specified by:
      itemChanged in interface ItemChangeListener
      Parameters:
      layer - the layer containing the changed item
      item - the affected item; may be null for layer-level events
      type - the type of change
    • setFeedbackPane

      public void setFeedbackPane(FeedbackPane feedbackPane)
      Sets the feedback pane. This is an optional alternative to a HUD.
      Specified by:
      setFeedbackPane in interface IContainer
      Parameters:
      feedbackPane - the feedback pane.
    • getFeedbackPane

      public FeedbackPane getFeedbackPane()
      Get the optional feedback pane.
      Specified by:
      getFeedbackPane in interface IContainer
      Returns:
      the feedbackPane
    • getFeedbackControl

      public FeedbackControl getFeedbackControl()
      Return the object that controls the container's feedback. You can and and remove feedback providers using this object.
      Specified by:
      getFeedbackControl in interface IContainer
      Returns:
      the object that controls the container's feedback.
    • getComponent

      public Component getComponent()
      Get the underlying JComponent
      Specified by:
      getComponent in interface IContainer
      Returns:
      the underlying component
    • setAfterDraw

      public void setAfterDraw(IDrawable afterDraw)
      Set the after-draw drawable for this container.
      Specified by:
      setAfterDraw in interface IContainer
      Parameters:
      afterDraw - the new after-draw drawable.
    • getAfterDraw

      public IDrawable getAfterDraw()
      Returns:
      after-draw drawable (may be null)
    • setBeforeDraw

      public void setBeforeDraw(IDrawable beforeDraw)
      Set the before-draw drawable.
      Specified by:
      setBeforeDraw in interface IContainer
      Parameters:
      beforeDraw - the new before-draw drawable.
    • getBeforeDraw

      public IDrawable getBeforeDraw()
      Returns:
      before-draw drawable (may be null)
    • getWorldSystem

      public Rectangle2D.Double getWorldSystem()
      Get the current world system.

      Note: implementations may return the live, mutable rectangle backing the container's viewport, not a defensive copy (unlike IContainer.setWorldSystem(java.awt.geom.Rectangle2D.Double) and resetWorldSystem, which both copy). Mutating the returned rectangle directly can silently change the container's viewport without going through setDirty/ refresh. Callers that need to keep a stable snapshot should copy it themselves.

      Specified by:
      getWorldSystem in interface IContainer
      Returns:
      the world system; may be the container's live instance
    • setWorldSystem

      public void setWorldSystem(Rectangle2D.Double wr)
      Set the world system (does not cause redraw)
      Specified by:
      setWorldSystem in interface IContainer
      Parameters:
      wr - the new world system
    • setAffineTransforms

      protected void setAffineTransforms()
      Compute transforms for world to local and local to world conversion.
    • localToWorld

      public void localToWorld(Polygon polygon, WorldPolygon worldPolygon)
      This converts a screen or local polygon to a world polygon.
      Specified by:
      localToWorld in interface IContainer
      Parameters:
      polygon - contains the local (screen-pixel) polygon.
      worldPolygon - will hold the resultant world polygon.
    • worldToLocal

      public void worldToLocal(Polygon polygon, WorldPolygon worldPolygon)
      This converts a world polygon to a screen or pixel polygon.
      Specified by:
      worldToLocal in interface IContainer
      Parameters:
      polygon - will hold the resultant local (screen-pixel) polygon.
      worldPolygon - contains the world polygon.
    • getImage

      public BufferedImage getImage()
      Get the background image.
      Specified by:
      getImage in interface IContainer
      Returns:
      the fully painted background image.
    • prepareForExit

      public void prepareForExit()
      Releases hover resources held by this container.
      Specified by:
      prepareForExit in interface IContainer
    • getHoverWindow

      public HoverInfoWindow getHoverWindow()
      Lazily creates the hover popup window.
      Specified by:
      getHoverWindow in interface IContainer
      Returns:
      the hover popup, or null if the component is not yet realized
    • hoverUp

      public void hoverUp(HoverEvent he)
      Description copied from interface: HoverListener
      Called when a hover event starts. Implementations can use the provided HoverEvent to access the source component and hover location.
      Specified by:
      hoverUp in interface HoverListener
      Parameters:
      he - the HoverEvent containing details about the hover action
    • hoverDown

      public void hoverDown(HoverEvent he)
      Description copied from interface: HoverListener
      Called when a hover event ends. Implementations can perform cleanup or state updates as needed when the hover is no longer active.
      Specified by:
      hoverDown in interface HoverListener
      Parameters:
      he - the HoverEvent containing details about the hover action that is ending