Java Reference
In-Depth Information
public void mouseReleased(MouseEvent e) {
if(e.isPopupTrigger()) {
start = e.getPoint();
if(highlightElement == null)
theApp.getWindow().getPopup().show((Component)e.getSource(),
start.x, start.y);
else
elementPopup.show((Component)e.getSource(), start.x, start.y);
start = null;
}
// Plus the rest of the code as before...
}
This just adds an if-else to display the element dialog when there is an element highlighted. If you
recompile Sketcher you should get a different context menu depending on whether an element is under
the cursor or not.
How It Works
The mouseReleased() method in the MouseHandler inner class now pops one or other of the two
pop-ups we have, depending on whether the reference in highlightElement is null or not. You
can select items from the general pop-up to set the color or the element type, but the element pop-up
menu does nothing at present. It just needs a few lines of code somewhere to do moves and rotations
and stuff. Don't worry - it'll be like falling off a log - but not so painful.
Deleting Elements
Let's take the easiest one first - deleting an element. All that's involved here is calling remove() for the
model object from the actionPerformed() method in SketchView . Let's give it a try.
Try It Out - Deleting Elements
The code we need to add to actionPerformed() in the SketchView class looks like this:
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source == moveItem) {
// Process a move...
} else if(source == deleteItem) {
if(highlightElement != null) { // If there's an element
theApp.getModel().remove(highlightElement); // then remove it
highlightElement = null; // Remove the reference
}
} else if(source == rotateItem) {
// Process a rotate
} else if(source == sendToBackItem) {
// Process a send-to-back...
}
}
Search WWH ::




Custom Search