Class Layer
- Direct Known Subclasses:
CityLayer,CountryBoundaryLayer,CountryFillLayer,Etopo5Layer,GraticuleLayer,ShapefileLayer
AItem objects.
Role in the MDI rendering model
View → Container → Layers → Items
Layers are rendered bottom-to-top by the owning container. The layer manages
item ordering, selection state, hit-testing, and notifies registered
ItemChangeListeners when items are added, removed, or selected.
Visibility vs. locking
- Visible layers are drawn and participate in hit-testing.
- Locked layers are still drawn (if visible) but their items are
treated as non-interactive: hit-testing returns
null, selection helpers are no-ops, and deletion helpers skip locked layers.
Why delegation instead of inheritance
This class delegates to an internal ArrayList rather than extending
one. Extending ArrayList would expose the full List mutation
API (add(int, E), set(), addAll(), etc.) to callers,
making it trivial to bypass the change-notification logic in add(edu.cnu.mdi.item.AItem) and
remove(edu.cnu.mdi.item.AItem). The delegation approach exposes only the operations this
layer actually supports, each of which fires the correct event.
Thread safety
Mutating operations synchronize on this to provide basic safety when
background threads modify items while the Swing EDT is painting.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionLayer(IContainer container, String name) Create a z-layer and register it with the owning container. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdd an item to the top of this layer and notify listeners.voidaddEnclosedItems(IContainer container, ArrayList<AItem> dest, Rectangle rect) Append all visible items contained within the given rectangle todest.voidaddItemChangeListener(ItemChangeListener listener) Register anItemChangeListener.voidaddItemsAtPoint(ArrayList<AItem> dest, IContainer container, Point screenPoint) Append all visible items at the given screen point todest.voidaddSelectedItems(ArrayList<AItem> dest) Append all selected, visible items todest.voidafterDraw(Graphics2D g2, IContainer container) Hook for subclasses to perform custom drawing after the items are drawn.booleanReturntrueif at least one item on this layer is selected.voidbeforeDraw(Graphics2D g2, IContainer container) Hook for subclasses to perform custom drawing before the items are drawn.voidclearAllItems(IContainer container) Delete all deletable items from this layer, properly detaching each from container-managed services (feedback control, etc.).booleanReturntrueif this layer contains the given item.voiddeleteItem(AItem item) Delete a single item from this layer.voiddeleteSelectedItems(IContainer container) Delete all currently selected, deletable items.voiddraw(Graphics2D g, IContainer container) Draw all items on this layer in bottom-to-top order.voidEdit the layer-level parameters of this layer.booleanIdentity-based equality: two layer references are equal only when they are the same object.Return a snapshot of all items on this layer, in draw order (bottom-to-top).Return the owning container.getItemAtPoint(IContainer container, Point screenPoint) Return the topmost visible item that contains the given screen point.getItemsAtPoint(IContainer container, Point screenPoint) Return all visible items that contain the given screen point, topmost first.getName()Return the human-readable name of this layer.intReturn the number of selected items on this layer.Return a snapshot of all selected, visible items on this layer.inthashCode()booleanReturntrueif this layer is editable.booleanisEmpty()Returntrueif this layer contains no items.booleanisLocked()Returntrueif this layer is locked (edit-locked).booleanReturntrueif this layer is visible.voidnotifyItemChangeListeners(AItem item, ItemChangeType type) Notify all registered listeners that an item changed.booleanRemove an item from this layer.voidremoveItemChangeListener(ItemChangeListener listener) Remove a previously registeredItemChangeListener.voidselectAllItems(boolean select) Select or deselect all items on this layer.voidselectAllItems(boolean select, AItem excludedItem) Select or deselect all items on this layer, optionally excluding one.voidselectItem(AItem item, boolean select) Select or deselect a single item and notify listeners.voidsendBackward(AItem item) Move an item one step toward the back (lower draw order).voidsendForward(AItem item) Move an item one step toward the front (higher draw order).voidsendToBack(AItem item) Send an item to the very back (bottom draw order) of this layer.voidsendToFront(AItem item) Bring an item to the very front (top draw order) of this layer.voidsetDirty(boolean dirty) Mark all items on this layer dirty or clean.voidsetEditable(boolean editable) Set the editable state of this layer.voidsetLocked(boolean locked) Set the locked state of this layer.voidSet the human-readable name of this layer.voidsetVisible(boolean visible) Set the visibility of this layer.intsize()Return the number of items on this layer.toString()Return a human-readable description of this layer.
-
Constructor Details
-
Layer
Create a z-layer and register it with the owning container.The layer auto-registers itself via
IContainer.addLayer(Layer).- Parameters:
container- the owning container; must not benullname- a human-readable layer name; must not benull- Throws:
IllegalArgumentException- if either argument isnull
-
-
Method Details
-
add
Add an item to the top of this layer and notify listeners.Items normally add themselves to their layer inside
AItem(Layer, Object...). Re-adding an item already present on this layer is a no-op.- Parameters:
item- the item to add; must belong to this layer- Throws:
IllegalArgumentException- ifitemisnullor belongs to a different layer
-
remove
Remove an item from this layer.If the item is present, it is removed first, then
AItem.prepareForRemoval()is called and aItemChangeType.DELETEDevent is fired. An item not owned by this layer is left untouched.- Parameters:
item- the item to remove; ifnullor not on this layer the call is a no-op- Returns:
trueif the item was found and removed
-
clearAllItems
Delete all deletable items from this layer, properly detaching each from container-managed services (feedback control, etc.).If this layer is locked the call is a no-op.
- Parameters:
container- retained for source compatibility; item cleanup uses this layer's owning container
-
deleteSelectedItems
Delete all currently selected, deletable items.If this layer is locked or not visible the call is a no-op.
- Parameters:
container- retained for source compatibility; item cleanup uses this layer's owning container
-
deleteItem
Delete a single item from this layer.If this layer is locked the call is a no-op. The item is detached from the container's feedback control and
AItem.prepareForRemoval()is called before removal.- Parameters:
item- the item to delete; ifnullthe call is a no-op
-
size
public int size()Return the number of items on this layer.- Returns:
- item count (≥ 0)
-
isEmpty
public boolean isEmpty()Returntrueif this layer contains no items.- Returns:
trueif empty
-
contains
Returntrueif this layer contains the given item.- Parameters:
item- the item to test; ifnullreturnsfalse- Returns:
trueifitemis on this layer
-
getAllItems
Return a snapshot of all items on this layer, in draw order (bottom-to-top).The returned list is a defensive copy — modifications to it do not affect this layer.
- Returns:
- a new, mutable
ArrayListof all items; nevernull
-
draw
Draw all items on this layer in bottom-to-top order.If this layer is not visible the method returns immediately.
- Parameters:
g- the graphics contextcontainer- the container being rendered
-
beforeDraw
Hook for subclasses to perform custom drawing before the items are drawn.The default implementation is a no-op.
- Parameters:
g2- the graphics contextcontainer- the container being rendered
-
afterDraw
Hook for subclasses to perform custom drawing after the items are drawn.The default implementation is a no-op.
- Parameters:
g2- the graphics contextcontainer- the container being rendered
-
isEditable
public boolean isEditable()Returntrueif this layer is editable.- Returns:
- the editable flag
-
setEditable
public void setEditable(boolean editable) Set the editable state of this layer.- Parameters:
editable-trueto make the layer editable
-
edit
Edit the layer-level parameters of this layer.The default implementation is a no-op. Subclasses may override to provide a custom editing dialog or panel.
- Parameters:
parent- the parent component for any dialogs; may benull
-
getItemAtPoint
Return the topmost visible item that contains the given screen point.Returns
nullif this layer is not visible or is locked.- Parameters:
container- the container performing the hit testscreenPoint- the pixel location to test- Returns:
- the topmost matching item, or
null
-
getItemsAtPoint
Return all visible items that contain the given screen point, topmost first.Returns
nullif this layer is not visible or is locked, or if no items match.- Parameters:
container- the container performing the hit testscreenPoint- the pixel location to test- Returns:
- matching items (topmost first), or
null
-
addEnclosedItems
Append all visible items contained within the given rectangle todest.Does nothing if this layer is locked or not visible.
- Parameters:
container- the container performing the testdest- destination list; items are appended to itrect- the enclosing rectangle in screen coordinates
-
addItemsAtPoint
Append all visible items at the given screen point todest.- Parameters:
dest- destination list; items are appended to itcontainer- the container performing the hit testscreenPoint- the pixel location to test
-
anySelected
public boolean anySelected()Returntrueif at least one item on this layer is selected.- Returns:
trueif any item is selected
-
getSelectedCount
public int getSelectedCount()Return the number of selected items on this layer.- Returns:
- selected item count (≥ 0)
-
getSelectedItems
Return a snapshot of all selected, visible items on this layer.- Returns:
- selected items; never
nullbut may be empty
-
addSelectedItems
Append all selected, visible items todest.- Parameters:
dest- destination list; items are appended to it
-
selectAllItems
public void selectAllItems(boolean select) Select or deselect all items on this layer.If this layer is locked the call is a no-op.
- Parameters:
select-trueto select,falseto deselect
-
selectAllItems
Select or deselect all items on this layer, optionally excluding one.Both the layer lock and each item's own lock are respected. If this layer is locked the call is a no-op.
- Parameters:
select- new selection stateexcludedItem- item to skip; may benull
-
selectItem
Select or deselect a single item and notify listeners.If this layer is locked, or the item itself is locked, the call is a no-op.
- Parameters:
item- the target item; ifnullthe call is a no-opselect- new selection state
-
sendToFront
Bring an item to the very front (top draw order) of this layer.- Parameters:
item- the item to move; no-op if not on this layer
-
sendToBack
Send an item to the very back (bottom draw order) of this layer.- Parameters:
item- the item to move; no-op if not on this layer
-
sendForward
Move an item one step toward the front (higher draw order).- Parameters:
item- the item to move; no-op if not on this layer or already at front
-
sendBackward
Move an item one step toward the back (lower draw order).- Parameters:
item- the item to move; no-op if not on this layer or already at back
-
setDirty
public void setDirty(boolean dirty) Mark all items on this layer dirty or clean.- Parameters:
dirty- new dirty state for each item
-
addItemChangeListener
Register anItemChangeListener.Duplicate registrations are silently ignored (the listener is removed then re-added to avoid duplicates).
- Parameters:
listener- the listener to add; ifnullthe call is a no-op
-
removeItemChangeListener
Remove a previously registeredItemChangeListener.- Parameters:
listener- the listener to remove; ifnullthe call is a no-op
-
notifyItemChangeListeners
Notify all registered listeners that an item changed.- Parameters:
item- the affected item; may benullfor layer-level eventstype- the type of change
-
getContainer
Return the owning container.- Returns:
- the container that owns this layer; never
null
-
getName
Return the human-readable name of this layer.- Returns:
- the layer name; never
null
-
setName
Set the human-readable name of this layer.- Parameters:
name- the new name; must not benull- Throws:
IllegalArgumentException- ifnameisnull
-
isVisible
public boolean isVisible()Returntrueif this layer is visible.- Returns:
- the visibility flag
-
setVisible
public void setVisible(boolean visible) Set the visibility of this layer.- Parameters:
visible-trueto make the layer visible
-
isLocked
public boolean isLocked()Returntrueif this layer is locked (edit-locked).A locked layer is still drawn if
isVisible()returnstrue, but its items are treated as non-interactive.- Returns:
- the locked flag
-
setLocked
public void setLocked(boolean locked) Set the locked state of this layer.- Parameters:
locked-trueto lock the layer
-
equals
Identity-based equality: two layer references are equal only when they are the same object. -
hashCode
public int hashCode() -
toString
Return a human-readable description of this layer.
-