Class BaseToolBar

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

public class BaseToolBar extends AToolBar
Default MDI toolbar implementation for a canvas.

This class supports a mix of:

  • Mutually exclusive toggle tools (pointer, pan, magnify, box zoom, drawing tools, connector, etc.)
  • One-shot actions (zoom in/out, undo/reset zoom, delete, style, camera, printer)
  • Status field (optional)

GestureContext-first

All interactions are dispatched to IToolHandler using GestureContext. Tools that originate from mouse gestures (press/drag/release, hover move, rubberband) create/update a GestureContext. One-shot actions synthesize a context at the canvas center with a null MouseEvent.

Listener swapping

Only the active toggle tool receives mouse events from the canvas. This toolbar swaps canvas listeners when the active toggle changes, tracking the currently installed listeners directly for O(1) removal.

Extensibility

Applications can add their own buttons after construction using the id-aware methods from AToolBar:


 toolbar.addToggle("myTool", new JToggleButton("..."));
 toolbar.addButton("myAction", new JButton("..."));
 toolbar.setButtonEnabled("myAction", false);
 
Author:
heddle
See Also:
  • Field Details

    • DEFAULT_MIN_SIZE_PX

      public static final int DEFAULT_MIN_SIZE_PX
      Default minimum size in pixels for rubberbanded shapes.
      See Also:
    • DEFAULT_BUTTON_SIZE

      public static final Dimension DEFAULT_BUTTON_SIZE
      Default button size.
    • DEFAULT_ICON_SIZE

      public static final int DEFAULT_ICON_SIZE
      Default icon size (w and h).
      See Also:
    • DEFAULT_MAGNIFY_SIZE

      public static final Dimension DEFAULT_MAGNIFY_SIZE
      Default magnify viewport size.
    • canvas

      protected final Component canvas
      The canvas this toolbar is associated with.
    • bits

      protected final long bits
      Bits controlling which predefined buttons are added.
    • handler

      protected IToolHandler handler
      Handles the gestures and actions of the tools.
    • boxZoomPolicy

      protected final ARubberband.Policy boxZoomPolicy
      Policy to use for box zoom.
    • pointerPolicy

      protected final ARubberband.Policy pointerPolicy
      Policy to use for pointer tool.
  • Constructor Details

    • BaseToolBar

      public BaseToolBar(Component canvas, IToolHandler handler, long bits)
      Creates a new horizontal toolbar associated with a canvas using common defaults: pointer uses ARubberband.Policy.RECTANGLE and box zoom uses ARubberband.Policy.RECTANGLE_PRESERVE_ASPECT.
      Parameters:
      canvas - the canvas component this toolbar is associated with
      handler - the tool handler to notify of tool gestures
      bits - controls which predefined buttons are added to the toolbar
    • BaseToolBar

      public BaseToolBar(Component canvas, IToolHandler handler, long bits, ARubberband.Policy pointerPolicy, ARubberband.Policy boxZoomPolicy)
      Creates a new horizontal toolbar associated with a canvas, specifying pointer and box-zoom rubberband policies.
      Parameters:
      canvas - the canvas component this toolbar is associated with
      handler - the tool handler to notify of tool gestures
      bits - controls which predefined buttons are added to the toolbar
      pointerPolicy - the rubberband policy to use for the pointer tool
      boxZoomPolicy - the rubberband policy to use for the box zoom tool
    • BaseToolBar

      public BaseToolBar(Component canvas, IToolHandler handler, long bits, int orientation, ARubberband.Policy pointerPolicy, ARubberband.Policy boxZoomPolicy)
      Creates a new toolbar associated with a canvas.
      Parameters:
      canvas - the canvas component this toolbar is associated with
      handler - the tool handler to notify of tool gestures
      bits - controls which predefined buttons are added to the toolbar
      orientation - toolbar orientation (SwingConstants.HORIZONTAL or SwingConstants.VERTICAL)
      pointerPolicy - the rubberband policy to use for the pointer tool
      boxZoomPolicy - the rubberband policy to use for the box zoom tool
  • Method Details

    • setHandler

      public void setHandler(IToolHandler handler)
      Replace the current tool handler.
      Parameters:
      handler - the new handler (non-null)
    • actionContext

      protected GestureContext actionContext()
      Create a synthetic context for one-shot actions.

      Uses the canvas center as a representative point and provides null for the MouseEvent.

      Returns:
      a synthetic context suitable for one-shot actions
    • addInfoButton

      public void addInfoButton()
      Add the info button if it is not already present. Intended for use by applications that want to add the info button after construction.
    • configureButton

      public void configureButton(AbstractButton button, String iconPath, String tip)
      Configure a toolbar button based on a raw icon path and tooltip.

      Intended for application-added buttons.

      Parameters:
      button - button to configure
      iconPath - path to icon resource
      tip - tooltip text
    • activeToggleButtonChanged

      protected void activeToggleButtonChanged(JToggleButton newlyActive)
      Called whenever the active toggle tool changes.

      This swaps the canvas listeners so only the active tool receives events.

      Specified by:
      activeToggleButtonChanged in class AToolBar
      Parameters:
      newlyActive - the button that just became selected (never null).
    • isToolActive

      public boolean isToolActive(String toolId)
      Check if the tool with the given id is currently active.
      Parameters:
      toolId - the id of the tool to check (should correspond to a toggle)
      Returns:
      true if the tool is active, false otherwise
    • isPointerActive

      public boolean isPointerActive()
      Check if the pointer tool is currently active.
      Returns:
      true if the pointer tool is active, false otherwise
    • setToolsEnabled

      public void setToolsEnabled(boolean enabled, String... ids)
      Enable or disable tools/buttons by id.

      Works for both predefined tools (via ToolBits.getId(long)) and application-added tools (via AToolBar.addToggle(String, JToggleButton) and AToolBar.addButton(String, JButton)).

      Parameters:
      enabled - new enabled state
      ids - tool ids to update (null/blank entries are ignored)
    • hasTool

      public boolean hasTool(String toolId)
      Check if a tool/button with the given id exists in the toolbar registry.
      Parameters:
      toolId - the id of the tool to check
      Returns:
      true if the tool exists, false otherwise
    • hasDeleteTool

      public boolean hasDeleteTool()
      Check if the delete tool/button exists in the toolbar.
      Returns:
      true if the delete tool exists, false otherwise
    • setDeleteEnabled

      public void setDeleteEnabled(boolean enabled)
      Enable or disable the delete tool/button if it exists.
      Parameters:
      enabled - new enabled state for the delete tool
    • invokeDelete

      public void invokeDelete()
      Invoke the delete action as if the delete button had been pressed.

      If the delete button is not present or not enabled, this method does nothing.

    • createStatusTextField

      protected JTextField createStatusTextField()
      Create the status text field shown on the toolbar.

      When enabled via ToolBits.STATUS, it will claim empty space at the end of the toolbar via glue.

      Returns:
      the status text field