Search in sources :

Example 1 with AbstractAnnotation

use of org.cytoscape.ding.impl.cyannotator.annotations.AbstractAnnotation in project cytoscape-impl by cytoscape.

the class CanvasMouseMotionListener method mouseMoved.

public void mouseMoved(MouseEvent e) {
    AbstractAnnotation resizeAnnotation = cyAnnotator.getResizeShape();
    DingAnnotation moveAnnotation = cyAnnotator.getMovingAnnotation();
    ArrowAnnotationImpl repositionAnnotation = cyAnnotator.getRepositioningArrow();
    if (resizeAnnotation == null && moveAnnotation == null && repositionAnnotation == null) {
        networkCanvas.mouseMoved(e);
        return;
    }
    int mouseX = e.getX();
    int mouseY = e.getY();
    if (moveAnnotation != null) {
        // Get our current transform
        double[] nextLocn = new double[2];
        nextLocn[0] = (double) mouseX;
        nextLocn[1] = (double) mouseY;
        view.xformComponentToNodeCoords(nextLocn);
        // OK, now update
        moveAnnotation.moveAnnotation(new Point2D.Double(nextLocn[0], nextLocn[1]));
        moveAnnotation.update();
        moveAnnotation.getCanvas().repaint();
    } else if (resizeAnnotation != null) {
        Component resizeComponent = resizeAnnotation.getComponent();
        int cornerX1 = resizeComponent.getX();
        int cornerY1 = resizeComponent.getY();
        // int cornerX2 = cornerX1 + resizeComponent.getWidth();
        // int cornerY2 = cornerY1 + resizeComponent.getHeight();
        int cornerX2 = mouseX;
        int cornerY2 = mouseY;
        double borderWidth = 0;
        if (resizeAnnotation instanceof ShapeAnnotationImpl)
            borderWidth = ((ShapeAnnotationImpl) resizeAnnotation).getBorderWidth();
        /*
			 * TODO: change over to use anchors at some point
			 */
        /*
			if (Math.abs(mouseX-cornerX1) < Math.abs(mouseX-cornerX2)) {
				// Left
				cornerX1 = mouseX;
			} else {
				// Right
				cornerX2 = mouseX;
			}
			*/
        // System.out.println("X1 = "+cornerX1+", X2 = "+cornerX2+" width = "+resizeComponent.getWidth());
        double width = (double) cornerX2 - (double) cornerX1 - (borderWidth * 2 * resizeAnnotation.getZoom());
        // System.out.println("width = "+width);
        /*
			if (mouseY <= cornerY1) {
				// Upper
				cornerY1 = mouseY;
			} else if (mouseY >= cornerY2-resizeComponent.getHeight()/2) {
				// Lower
				cornerY2 = mouseY;
			}
			*/
        double height = (double) cornerY2 - (double) cornerY1 - (borderWidth * 2 * resizeAnnotation.getZoom());
        if (width == 0.0)
            width = 2;
        if (height == 0.0)
            height = 2;
        if ((Math.abs(width - resizeComponent.getWidth()) < 5) && (Math.abs(height - resizeComponent.getHeight()) < 5))
            return;
        Dimension d = new Dimension();
        d.setSize(width, height);
        // If shift is down, adjust to preserve the aspect ratio
        if (e.isShiftDown()) {
            d = resizeAnnotation.adjustAspectRatio(d);
        }
        // resizeComponent.setLocation(cornerX1, cornerY1);
        resizeAnnotation.setSize(d);
        resizeAnnotation.update();
        resizeAnnotation.getCanvas().repaint();
    } else if (repositionAnnotation != null) {
        Point2D mousePoint = new Point2D.Double(mouseX, mouseY);
        // See what's under our mouse
        // Annotation?
        List<DingAnnotation> annotations = cyAnnotator.getAnnotationsAt(mousePoint);
        if (annotations.contains(repositionAnnotation))
            annotations.remove(repositionAnnotation);
        if (annotations.size() > 0) {
            repositionAnnotation.setTarget(annotations.get(0));
        // Node?
        } else if (overNode(mousePoint)) {
            CyNode overNode = getNodeAtLocation(mousePoint);
            repositionAnnotation.setTarget(overNode);
        // Nope, just set the point
        } else {
            repositionAnnotation.setTarget(mousePoint);
        }
        repositionAnnotation.update();
        repositionAnnotation.getCanvas().repaint();
    }
}
Also used : ArrowAnnotationImpl(org.cytoscape.ding.impl.cyannotator.annotations.ArrowAnnotationImpl) Dimension(java.awt.Dimension) DingAnnotation(org.cytoscape.ding.impl.cyannotator.annotations.DingAnnotation) AbstractAnnotation(org.cytoscape.ding.impl.cyannotator.annotations.AbstractAnnotation) ShapeAnnotationImpl(org.cytoscape.ding.impl.cyannotator.annotations.ShapeAnnotationImpl) Point2D(java.awt.geom.Point2D) CyNode(org.cytoscape.model.CyNode) Component(java.awt.Component)

Aggregations

Component (java.awt.Component)1 Dimension (java.awt.Dimension)1 Point2D (java.awt.geom.Point2D)1 AbstractAnnotation (org.cytoscape.ding.impl.cyannotator.annotations.AbstractAnnotation)1 ArrowAnnotationImpl (org.cytoscape.ding.impl.cyannotator.annotations.ArrowAnnotationImpl)1 DingAnnotation (org.cytoscape.ding.impl.cyannotator.annotations.DingAnnotation)1 ShapeAnnotationImpl (org.cytoscape.ding.impl.cyannotator.annotations.ShapeAnnotationImpl)1 CyNode (org.cytoscape.model.CyNode)1