Class BaseToolBar
- All Implemented Interfaces:
ImageObserver,MenuContainer,Serializable,Accessible,SwingConstants
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:
-
Nested Class Summary
Nested classes/interfaces inherited from class javax.swing.JToolBar
JToolBar.AccessibleJToolBar, JToolBar.SeparatorNested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponentNested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainerNested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final longBits controlling which predefined buttons are added.protected final ARubberband.PolicyPolicy to use for box zoom.protected final ComponentThe canvas this toolbar is associated with.static final DimensionDefault button size.static final intDefault icon size (w and h).static final DimensionDefault magnify viewport size.static final intDefault minimum size in pixels for rubberbanded shapes.protected IToolHandlerHandles the gestures and actions of the tools.protected final ARubberband.PolicyPolicy to use for pointer tool.Fields inherited from class edu.cnu.mdi.graphics.toolbar.AToolBar
CLIENTPROP_TOOL_ID, statusField, toggleGroupFields inherited from class javax.swing.JComponent
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOWFields inherited from class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENTFields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTHFields inherited from interface javax.swing.SwingConstants
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST -
Constructor Summary
ConstructorsConstructorDescriptionBaseToolBar(Component canvas, IToolHandler handler, long bits) Creates a new horizontal toolbar associated with a canvas using common defaults: pointer usesARubberband.Policy.RECTANGLEand box zoom usesARubberband.Policy.RECTANGLE_PRESERVE_ASPECT.BaseToolBar(Component canvas, IToolHandler handler, long bits, int orientation, ARubberband.Policy pointerPolicy, ARubberband.Policy boxZoomPolicy) Creates a new toolbar associated with a canvas.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. -
Method Summary
Modifier and TypeMethodDescriptionprotected GestureContextCreate a synthetic context for one-shot actions.protected voidactiveToggleButtonChanged(JToggleButton newlyActive) Called whenever the active toggle tool changes.voidAdd the info button if it is not already present.voidconfigureButton(AbstractButton button, String iconPath, String tip) Configure a toolbar button based on a raw icon path and tooltip.protected JTextFieldCreate the status text field shown on the toolbar.booleanCheck if the delete tool/button exists in the toolbar.booleanCheck if a tool/button with the given id exists in the toolbar registry.voidInvoke the delete action as if the delete button had been pressed.booleanCheck if the pointer tool is currently active.booleanisToolActive(String toolId) Check if the tool with the given id is currently active.voidsetDeleteEnabled(boolean enabled) Enable or disable the delete tool/button if it exists.voidsetHandler(IToolHandler handler) Replace the current tool handler.voidsetToolsEnabled(boolean enabled, String... ids) Enable or disable tools/buttons by id.Methods inherited from class edu.cnu.mdi.graphics.toolbar.AToolBar
addButton, addToggle, addToggle, addToggle, getActiveButton, getButton, getButton, getButtonRegistryView, getDefaultToggleButton, getToolId, hasStatusField, registerButton, registerExternalToggle, removeButton, removeToggle, requireButton, resetDefaultToggleButton, setButtonEnabled, setDefaultToggleButton, spacer, updateStatusTextMethods inherited from class javax.swing.JToolBar
add, addImpl, addSeparator, addSeparator, createActionChangeListener, createActionComponent, getAccessibleContext, getComponentAtIndex, getComponentIndex, getMargin, getOrientation, getUI, getUIClassID, isBorderPainted, isFloatable, isRollover, paintBorder, paramString, setBorderPainted, setFloatable, setLayout, setMargin, setOrientation, setRollover, setUI, updateUIMethods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, updateMethods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, transferFocusDownCycle, validate, validateTreeMethods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
-
Field Details
-
DEFAULT_MIN_SIZE_PX
public static final int DEFAULT_MIN_SIZE_PXDefault minimum size in pixels for rubberbanded shapes.- See Also:
-
DEFAULT_BUTTON_SIZE
Default button size. -
DEFAULT_ICON_SIZE
public static final int DEFAULT_ICON_SIZEDefault icon size (w and h).- See Also:
-
DEFAULT_MAGNIFY_SIZE
Default magnify viewport size. -
canvas
The canvas this toolbar is associated with. -
bits
protected final long bitsBits controlling which predefined buttons are added. -
handler
Handles the gestures and actions of the tools. -
boxZoomPolicy
Policy to use for box zoom. -
pointerPolicy
Policy to use for pointer tool.
-
-
Constructor Details
-
BaseToolBar
Creates a new horizontal toolbar associated with a canvas using common defaults: pointer usesARubberband.Policy.RECTANGLEand box zoom usesARubberband.Policy.RECTANGLE_PRESERVE_ASPECT.- Parameters:
canvas- the canvas component this toolbar is associated withhandler- the tool handler to notify of tool gesturesbits- 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 withhandler- the tool handler to notify of tool gesturesbits- controls which predefined buttons are added to the toolbarpointerPolicy- the rubberband policy to use for the pointer toolboxZoomPolicy- 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 withhandler- the tool handler to notify of tool gesturesbits- controls which predefined buttons are added to the toolbarorientation- toolbar orientation (SwingConstants.HORIZONTALorSwingConstants.VERTICAL)pointerPolicy- the rubberband policy to use for the pointer toolboxZoomPolicy- the rubberband policy to use for the box zoom tool
-
-
Method Details
-
setHandler
Replace the current tool handler.- Parameters:
handler- the new handler (non-null)
-
actionContext
Create a synthetic context for one-shot actions.Uses the canvas center as a representative point and provides
nullfor theMouseEvent.- 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
Configure a toolbar button based on a raw icon path and tooltip.Intended for application-added buttons.
- Parameters:
button- button to configureiconPath- path to icon resourcetip- tooltip text
-
activeToggleButtonChanged
Called whenever the active toggle tool changes.This swaps the canvas listeners so only the active tool receives events.
- Specified by:
activeToggleButtonChangedin classAToolBar- Parameters:
newlyActive- the button that just became selected (never null).
-
isToolActive
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
Enable or disable tools/buttons by id.Works for both predefined tools (via
ToolBits.getId(long)) and application-added tools (viaAToolBar.addToggle(String, JToggleButton)andAToolBar.addButton(String, JButton)).- Parameters:
enabled- new enabled stateids- tool ids to update (null/blank entries are ignored)
-
hasTool
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
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
-