Java Reference
In-Depth Information
// Check for TEXT being the element type
if(theApp.getWindow().getElementType() == TEXT) {
if(last != null) {
start = last = null;
}
return;
}
// Rest of the method code as before...
}
Directory "Sketcher 7 with element context menus"
If you recompile Sketcher you should get a different context menu in response to a button 2 click, de-
pending on whether or not an element is under the cursor. If you draw a few concentric circles, you can
try out the Send To Back operation, too.
How It Works
Using an anonymous class for each menu item means that the implementation of the actionPer-
formed() method for a listener only has to deal with the event for a specific menu item. As you see from
the implementation of the Send To Back operation, it becomes very easy. The sendToBack() method
that is called just calls remove() for the sketch object to remove the highlighted element and then calls
add() for the sketch to add it back at the end of the list. The effect is that the element is only found by
the highlighting mechanism if no other element in the list is found to qualify for highlighting. The checks
in the method are belt and suspenders, really. You don't expect not to find the highlighted element in the
sketch object.
Where the implementation of an anonymous class method needs access to fields in the containing class,
it is often easiest to put the implementation in an outer class method that the anonymous class method
can call, as you have done here. This is because there are restrictions on accessing outer class fields from
an anonymous class. In general, outer class fields must be declared as final if you want to access them
from an anonymous inner class.
The showContextMenu() method in the MouseHandler inner class now pops one or another of the two
pop-ups you have, depending on whether or not the reference in highlightElement is null . It returns
true if the pop-up trigger was recognized and false otherwise, so the caller method can respond appro-
priately. The method is called by the mousePressed() method and the mouseReleased() method, and
which method receives the pop-up trigger event depends on how the pop-up trigger is defined in your
environment.
You can select items from the general pop-up to set the color or the element type, but in the element pop-
up menu only Send To Back works at present. It just needs a few lines of code somewhere to do element
moves, rotations and deletions. Don't worry — it'll be like falling off a log, but not so painful.
NOTE Anonymous classes can be very helpful in simplifying code, but they don't
comeentirelyfree.Themoreclassesyouhaveinyourapplication,themorememory
Search WWH ::




Custom Search