Search in sources :

Example 1 with ArrowAnnotationImpl

use of org.cytoscape.ding.impl.cyannotator.annotations.ArrowAnnotationImpl 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)

Example 2 with ArrowAnnotationImpl

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

the class AddArrowTaskFactory method isReady.

@Override
public boolean isReady(CyNetworkView networkView, Point2D javaPt, Point2D xformPt) {
    // We need to be over an annotation
    CyAnnotator cyAnnotator = ((DGraphView) networkView).getCyAnnotator();
    DingAnnotation annotation = cyAnnotator.getAnnotationAt(javaPt);
    if (annotation == null || annotation instanceof ArrowAnnotationImpl)
        return false;
    return true;
}
Also used : ArrowAnnotationImpl(org.cytoscape.ding.impl.cyannotator.annotations.ArrowAnnotationImpl) CyAnnotator(org.cytoscape.ding.impl.cyannotator.CyAnnotator) DingAnnotation(org.cytoscape.ding.impl.cyannotator.annotations.DingAnnotation) DGraphView(org.cytoscape.ding.impl.DGraphView)

Example 3 with ArrowAnnotationImpl

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

the class ArrowAnnotationDialog method initComponents.

private void initComponents() {
    setTitle(create ? "Create Arrow Annotation" : "Modify Arrow Annotation");
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setModalityType(DEFAULT_MODALITY_TYPE);
    setResizable(false);
    // Create the preview panel
    preview = new ArrowAnnotationImpl(view, getOwner());
    preview.setUsedForPreviews(true);
    ((ArrowAnnotationImpl) preview).setSize(400.0, 100.0);
    final PreviewPanel previewPanel = new PreviewPanel(preview);
    arrowAnnotationPanel = new ArrowAnnotationPanel(annotation, previewPanel);
    applyButton = new JButton(new AbstractAction("OK") {

        @Override
        public void actionPerformed(ActionEvent e) {
            applyButtonActionPerformed(e);
        }
    });
    cancelButton = new JButton(new AbstractAction("Cancel") {

        @Override
        public void actionPerformed(ActionEvent e) {
            dispose();
        }
    });
    final JPanel buttonPanel = LookAndFeelUtil.createOkCancelPanel(applyButton, cancelButton);
    final JPanel contents = new JPanel();
    final GroupLayout layout = new GroupLayout(contents);
    contents.setLayout(layout);
    layout.setAutoCreateContainerGaps(true);
    layout.setAutoCreateGaps(true);
    layout.setHorizontalGroup(layout.createParallelGroup(LEADING, true).addComponent(arrowAnnotationPanel).addComponent(previewPanel, DEFAULT_SIZE, PREVIEW_WIDTH, Short.MAX_VALUE).addComponent(buttonPanel));
    layout.setVerticalGroup(layout.createSequentialGroup().addComponent(arrowAnnotationPanel, PREFERRED_SIZE, DEFAULT_SIZE, PREFERRED_SIZE).addComponent(previewPanel, DEFAULT_SIZE, PREVIEW_HEIGHT, Short.MAX_VALUE).addComponent(buttonPanel, PREFERRED_SIZE, DEFAULT_SIZE, PREFERRED_SIZE));
    LookAndFeelUtil.setDefaultOkCancelKeyStrokes(getRootPane(), applyButton.getAction(), cancelButton.getAction());
    getRootPane().setDefaultButton(applyButton);
    getContentPane().add(contents);
    pack();
}
Also used : JPanel(javax.swing.JPanel) ArrowAnnotationImpl(org.cytoscape.ding.impl.cyannotator.annotations.ArrowAnnotationImpl) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) GroupLayout(javax.swing.GroupLayout) AbstractAction(javax.swing.AbstractAction)

Example 4 with ArrowAnnotationImpl

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

the class CyAnnotator method loadAnnotations.

public void loadAnnotations() {
    // Make sure we're on the EDT since we directly add annotations to the canvas
    if (!SwingUtilities.isEventDispatchThread()) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                loadAnnotations();
            }
        });
        return;
    }
    // System.out.println("Loading annotations");
    CyNetwork network = view.getModel();
    // Now, see if this network has any existing annotations
    final CyTable networkAttributes = network.getTable(CyNetwork.class, CyNetwork.LOCAL_ATTRS);
    if (networkAttributes.getColumn(ANNOTATION_ATTRIBUTE) == null) {
        networkAttributes.createListColumn(ANNOTATION_ATTRIBUTE, String.class, false, Collections.EMPTY_LIST);
    }
    List<String> annotations = network.getRow(network, CyNetwork.LOCAL_ATTRS).getList(ANNOTATION_ATTRIBUTE, String.class);
    List<Map<String, String>> arrowList = // Keep a list of arrows
    new ArrayList<Map<String, String>>();
    Map<GroupAnnotation, String> groupMap = // Keep a map of groups and uuids
    new HashMap<GroupAnnotation, String>();
    Map<String, Annotation> uuidMap = new HashMap<String, Annotation>();
    Map<Object, Map<Integer, DingAnnotation>> zOrderMap = new HashMap<>();
    if (annotations != null) {
        for (String s : annotations) {
            Map<String, String> argMap = createArgMap(s);
            DingAnnotation annotation = null;
            String type = argMap.get("type");
            if (type == null)
                continue;
            if (type.equals("ARROW") || type.equals("org.cytoscape.view.presentation.annotations.ArrowAnnotation")) {
                arrowList.add(argMap);
                continue;
            }
            Annotation a = annotationFactoryManager.createAnnotation(type, view, argMap);
            if (a == null || !(a instanceof DingAnnotation))
                continue;
            annotation = (DingAnnotation) a;
            uuidMap.put(annotation.getUUID().toString(), annotation);
            Object canvas;
            if (annotation.getCanvas() != null) {
                annotation.getCanvas().add(annotation.getComponent());
                canvas = annotation.getCanvas();
            } else {
                canvas = foreGroundCanvas;
                foreGroundCanvas.add(annotation.getComponent());
            }
            if (argMap.containsKey(Annotation.Z)) {
                int zOrder = Integer.parseInt(argMap.get(Annotation.Z));
                if (zOrder >= 0) {
                    if (!zOrderMap.containsKey(canvas))
                        zOrderMap.put(canvas, new TreeMap<>());
                    zOrderMap.get(canvas).put(zOrder, annotation);
                }
            }
            addAnnotation(annotation);
            // If this is a group, save the annotation and the memberUIDs list
            if (type.equals("GROUP") || type.equals("org.cytoscape.view.presentation.annotations.GroupAnnotation")) {
                // Don't bother adding the group if it doesn't have any children
                if (argMap.containsKey("memberUUIDs"))
                    groupMap.put((GroupAnnotation) a, argMap.get("memberUUIDs"));
            }
        }
        // Now, handle all of our groups
        for (GroupAnnotation group : groupMap.keySet()) {
            String uuids = groupMap.get(group);
            String[] uuidArray = uuids.split(",");
            for (String uuid : uuidArray) {
                if (uuidMap.containsKey(uuid)) {
                    Annotation child = uuidMap.get(uuid);
                    group.addMember(child);
                }
            }
        }
        // Now, handle all of our arrows
        for (Map<String, String> argMap : arrowList) {
            String type = argMap.get("type");
            Annotation annotation = annotationFactoryManager.createAnnotation(type, view, argMap);
            if (annotation instanceof ArrowAnnotationImpl) {
                ArrowAnnotationImpl arrow = (ArrowAnnotationImpl) annotation;
                arrow.getSource().addArrow(arrow);
                Object canvas;
                if (arrow.getCanvas() != null) {
                    arrow.getCanvas().add(arrow.getComponent());
                    canvas = arrow.getCanvas();
                } else {
                    foreGroundCanvas.add(arrow.getComponent());
                    canvas = foreGroundCanvas;
                }
                if (argMap.containsKey(Annotation.Z)) {
                    int zOrder = Integer.parseInt(argMap.get(Annotation.Z));
                    if (zOrder >= 0) {
                        if (!zOrderMap.containsKey(canvas))
                            zOrderMap.put(canvas, new TreeMap<>());
                        zOrderMap.get(canvas).put(zOrder, arrow);
                    }
                }
                addAnnotation(arrow);
            }
        }
        // We use a TreeMap so that the keys (the zOrder are ordered)
        for (Map<Integer, DingAnnotation> map : zOrderMap.values()) {
            for (Integer zOrder : map.keySet()) {
                // System.out.println("zOrder = "+zOrder);
                DingAnnotation a = map.get(zOrder);
                if (a.getCanvas() != null)
                    a.getCanvas().setComponentZOrder(a.getComponent(), zOrder);
                else
                    foreGroundCanvas.setComponentZOrder(a.getComponent(), zOrder);
            }
        }
    }
}
Also used : GroupAnnotation(org.cytoscape.view.presentation.annotations.GroupAnnotation) HashMap(java.util.HashMap) ArrowAnnotationImpl(org.cytoscape.ding.impl.cyannotator.annotations.ArrowAnnotationImpl) ArrayList(java.util.ArrayList) CyNetwork(org.cytoscape.model.CyNetwork) DingAnnotation(org.cytoscape.ding.impl.cyannotator.annotations.DingAnnotation) TreeMap(java.util.TreeMap) AbstractAnnotation(org.cytoscape.ding.impl.cyannotator.annotations.AbstractAnnotation) GroupAnnotation(org.cytoscape.view.presentation.annotations.GroupAnnotation) Annotation(org.cytoscape.view.presentation.annotations.Annotation) DingAnnotation(org.cytoscape.ding.impl.cyannotator.annotations.DingAnnotation) CyTable(org.cytoscape.model.CyTable) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Aggregations

ArrowAnnotationImpl (org.cytoscape.ding.impl.cyannotator.annotations.ArrowAnnotationImpl)4 DingAnnotation (org.cytoscape.ding.impl.cyannotator.annotations.DingAnnotation)3 AbstractAnnotation (org.cytoscape.ding.impl.cyannotator.annotations.AbstractAnnotation)2 Component (java.awt.Component)1 Dimension (java.awt.Dimension)1 ActionEvent (java.awt.event.ActionEvent)1 Point2D (java.awt.geom.Point2D)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 AbstractAction (javax.swing.AbstractAction)1 GroupLayout (javax.swing.GroupLayout)1 JButton (javax.swing.JButton)1 JPanel (javax.swing.JPanel)1 DGraphView (org.cytoscape.ding.impl.DGraphView)1 CyAnnotator (org.cytoscape.ding.impl.cyannotator.CyAnnotator)1 ShapeAnnotationImpl (org.cytoscape.ding.impl.cyannotator.annotations.ShapeAnnotationImpl)1 CyNetwork (org.cytoscape.model.CyNetwork)1 CyNode (org.cytoscape.model.CyNode)1