/**
  * Binds this manipulator to the specified SVG rect.
  *
  * @param element The SVG rect this manipulator is applied to.
  * @return The root element of the manipulator
  */
 @Override
 public OMSVGElement bind(Record record) {
   this.record = record;
   SVGViewBoxElementModel model = (SVGViewBoxElementModel) record.getModel();
   mode = Mode.PASSIVE;
   // Create the graphical representations for the manipulator
   // The manipulator has the following SVG structure
   // <g>
   //  <rect/>    position
   //  <g>
   //   <rect/>   top-left corner
   //   <rect/>   bottom-right corner
   //  </g>
   // </g>
   OMSVGRectElement rect = (OMSVGRectElement) model.getElementWrapper();
   svg = rect.getOwnerSVGElement();
   OMSVGDocument document = (OMSVGDocument) svg.getOwnerDocument();
   g = document.createSVGGElement();
   g.setClassNameBaseVal(AppBundle.INSTANCE.css().rectGeometryManipulator());
   posHandle = document.createSVGRectElement();
   OMSVGGElement handleGroup = document.createSVGGElement();
   topLeftHandle = document.createSVGRectElement();
   bottomRightHandle = document.createSVGRectElement();
   g.appendChild(posHandle);
   g.appendChild(handleGroup);
   handleGroup.appendChild(topLeftHandle);
   handleGroup.appendChild(bottomRightHandle);
   monitorModel = true;
   model.addChangeListener(this);
   scheduleInit();
   return g;
 }
 @Override
 public boolean processMouseUp(MouseUpEvent event) {
   if (mode != Mode.PASSIVE) {
     mode = Mode.PASSIVE;
     monitorModel = false;
     record.beginEdit();
     record.set(SVGConstants.SVG_X_ATTRIBUTE, posHandle.getX().getBaseVal().getValue());
     record.set(SVGConstants.SVG_Y_ATTRIBUTE, posHandle.getY().getBaseVal().getValue());
     record.set(SVGConstants.SVG_WIDTH_ATTRIBUTE, posHandle.getWidth().getBaseVal().getValue());
     record.set(SVGConstants.SVG_HEIGHT_ATTRIBUTE, posHandle.getHeight().getBaseVal().getValue());
     record.endEdit();
     record.commit(false);
     monitorModel = true;
   }
   return true;
 }
 @Override
 public boolean processMouseDown(MouseDownEvent event) {
   JavaScriptObject target = event.getNativeEvent().getEventTarget();
   m = g.getScreenCTM().inverse();
   delta = getCoordinates(event, m);
   float x = posHandle.getX().getBaseVal().getValue();
   float y = posHandle.getY().getBaseVal().getValue();
   float width = posHandle.getWidth().getBaseVal().getValue();
   float height = posHandle.getHeight().getBaseVal().getValue();
   OMSVGPoint p = svg.createSVGPoint();
   if (target == posHandle.getElement()) {
     mode = Mode.POS;
     p.setX(x);
     p.setY(y);
   } else if (target == topLeftHandle.getElement()) {
     p.setX(x);
     p.setY(y);
     mode = Mode.TOP_LEFT;
   } else if (target == bottomRightHandle.getElement()) {
     p.setX(x + width);
     p.setY(y + height);
     mode = Mode.BOTTOM_RIGHT;
   }
   if (mode.consumeEvent()) {
     delta.substract(p);
     event.preventDefault();
     event.stopPropagation();
   }
   return true;
 }
 @Override
 public void modelChanged(ChangeEvent event) {
   if (monitorModel) {
     SVGViewBoxElementModel model = (SVGViewBoxElementModel) record.getModel();
     if (model.getElement().hasAttribute(SVGConstants.SVG_TRANSFORM_ATTRIBUTE)) {
       g.setAttribute(
           SVGConstants.SVG_TRANSFORM_ATTRIBUTE,
           model.getElement().getAttribute(SVGConstants.SVG_TRANSFORM_ATTRIBUTE));
     }
     float x = model.get(SVGConstants.SVG_X_ATTRIBUTE);
     float y = model.get(SVGConstants.SVG_Y_ATTRIBUTE);
     float width = model.get(SVGConstants.SVG_WIDTH_ATTRIBUTE);
     float height = model.get(SVGConstants.SVG_HEIGHT_ATTRIBUTE);
     posHandle.getX().getBaseVal().newValueSpecifiedUnits(Unit.PX, x);
     posHandle.getY().getBaseVal().newValueSpecifiedUnits(Unit.PX, y);
     posHandle.getWidth().getBaseVal().newValueSpecifiedUnits(Unit.PX, width);
     posHandle.getHeight().getBaseVal().newValueSpecifiedUnits(Unit.PX, height);
     update();
   }
 }
 @Override
 public boolean processMouseMove(MouseMoveEvent event) {
   if (mode.consumeEvent()) {
     float x = posHandle.getX().getBaseVal().getValue();
     float y = posHandle.getY().getBaseVal().getValue();
     float width = posHandle.getWidth().getBaseVal().getValue();
     float height = posHandle.getHeight().getBaseVal().getValue();
     OMSVGPoint p = getCoordinates(event, m).substract(delta);
     switch (mode) {
       case POS:
         {
           posHandle.getX().getBaseVal().setValue(p.getX());
           posHandle.getY().getBaseVal().setValue(p.getY());
         }
         break;
       case TOP_LEFT:
         {
           float xmax = Math.min(p.getX(), x + width);
           float ymax = Math.min(p.getY(), y + height);
           posHandle.getX().getBaseVal().setValue(xmax);
           posHandle.getY().getBaseVal().setValue(ymax);
           posHandle.getWidth().getBaseVal().setValue(width + x - xmax);
           posHandle.getHeight().getBaseVal().setValue(height + y - ymax);
         }
         break;
       case BOTTOM_RIGHT:
         {
           float xmin = Math.max(p.getX(), x);
           float ymin = Math.max(p.getY(), y);
           posHandle.getWidth().getBaseVal().setValue(xmin - x);
           posHandle.getHeight().getBaseVal().setValue(ymin - y);
         }
         break;
     }
     update();
     event.preventDefault();
     event.stopPropagation();
   }
   return true;
 }
 private void update() {
   float x = posHandle.getX().getBaseVal().getValue();
   float y = posHandle.getY().getBaseVal().getValue();
   float width = posHandle.getWidth().getBaseVal().getValue();
   float height = posHandle.getHeight().getBaseVal().getValue();
   float hs = Math.max(5, Math.min(width, height) * 0.2f);
   topLeftHandle.getX().getBaseVal().setValue(x);
   topLeftHandle.getY().getBaseVal().setValue(y);
   topLeftHandle.getWidth().getBaseVal().setValue(hs);
   topLeftHandle.getHeight().getBaseVal().setValue(hs);
   bottomRightHandle.getX().getBaseVal().setValue(x + width - hs);
   bottomRightHandle.getY().getBaseVal().setValue(y + height - hs);
   bottomRightHandle.getWidth().getBaseVal().setValue(hs);
   bottomRightHandle.getHeight().getBaseVal().setValue(hs);
 }