public void mousePressed(Canvas canvas, MouseEvent e) {
if (curText != null) {
commitText(canvas);
}
Text clicked = null;
boolean found = false;
int mx = e.getX();
int my = e.getY();
Location mloc = Location.create(mx, my);
for (CanvasObject o : canvas.getModel().getObjectsFromTop()) {
if (o instanceof Text && o.contains(mloc, true)) {
clicked = (Text) o;
found = true;
break;
}
}
if (!found) {
clicked = attrs.applyTo(new Text(mx, my, ""));
}
curText = clicked;
curCanvas = canvas;
isTextNew = !found;
clicked.getLabel().configureTextField(field, canvas.getZoomFactor());
field.setText(clicked.getText());
canvas.add(field);
Point fieldLoc = field.getLocation();
double zoom = canvas.getZoomFactor();
fieldLoc.x = (int) Math.round(mx * zoom - fieldLoc.x);
fieldLoc.y = (int) Math.round(my * zoom - fieldLoc.y);
int caret = field.viewToModel(fieldLoc);
if (caret >= 0) {
field.setCaretPosition(caret);
}
field.requestFocus();
canvas.getSelection().setSelected(clicked, true);
canvas.getSelection().setHidden(Collections.singleton(clicked), true);
clicked.addAttributeListener(fieldListener);
canvas.repaint();
}