Java Reference
In-Depth Information
If the element found is not the same as last time, you check whether the
highlightElement
variable
is
null
— it is
null
if the cursor newly entered the rectangle for an element and previously none were
highlighted. If
highlightElement
is not
null
, you must restore the normal color to the old element be-
fore you highlight the new one. To do this you call the
setHighlighted()
method for the old element
with the argument
false
, and then call the
repaint()
method for the view with the bounding rectangle
for the element as the argument. This causes the area occupied by the old element to be repainted.
To highlight the new element, you call its
setHighlighted()
method with the argument
true
, and then
store a reference to the element in
highlightElement
. You then call
repaint()
for the view to get the
element drawn in the highlight color.
The next block of code in the method executes if you exit the
for
loop because no element is under the
cursor. In this case you must check if there was an element highlighted last time around. If there was,
you unhighlight it, repaint the element bounding rectangle in the view to redraw the element in its normal
color, and reset
highlightElement
to
null
.
Defining the Other Context Menu
You already have the menu defined in
SketcherFrame
for when the cursor is not over an element. It's sens-
ible to keep it there, because the menu items are a subset of those from the application windows menus. The
context menu when the cursor is over an element has a new set of menu items, specific to operating on in-
dividual elements, so it can be defined in the view. All you need is the code to define the new context menu
— plus the code to decide which menu to display when
isPopupTrigger()
returns
true
for a mouse event.
You already know from
Figure 20-13
that you have four menu items in the element context menu:
•
Move
: This moves the element under the cursor to a new position. This operation works by drag-
ging it with the left mouse button down (button 1).
•
Delete
This operation deletes the element under the cursor.
•
Rotate
: This operation enables you to rotate the element under the cursor about the top-left corner
of its bounding rectangle by dragging it while holding button 1 (normally the left mouse button)
down.
•
Send-to-back
: This operation overcomes the problem of an element being impossible to highlight
because it is masked by the bounding rectangle of another element.
Because you highlight an element by searching the list from the beginning, an element is never high-
lighted if the rectangle for an earlier element in the list completely encloses it. Moving the earlier element
in the list that is hogging the highlighting to the end of the list allows the formerly masked element to be
highlighted. This is what the Send-to-back operation does.
TRY IT OUT: Creating Context Menus
The element operations can be handled entirely within the view, so add a data member to the
Sketcher-
View
class that stores the element pop-up menu reference:
private JPopupMenu elementPopup = new JPopupMenu("Element
Operations");