Package diva.canvas

Examples of diva.canvas.Figure


        int width = sceneGraphIn.getWidth();

        for (int i = 0; i < width; i++) {
            if (sceneGraphIn.hasToken(i)) {
                Scene2DToken figureToken = (Scene2DToken) sceneGraphIn.get(i);
                Figure figure = figureToken.getFigure();
                _figure.add(figure);
            }
        }

        sceneGraphOut.send(0, new Scene2DToken(_figure));
View Full Code Here


                }

                Object location = objToHighlight.getAttribute("_location");

                if (location != null) {
                    Figure figure = getFigure(location);

                    if (figure != null) {
                        if (_animationRenderer == null) {
                            _animationRenderer = new AnimationRenderer();
                        }
View Full Code Here

         @return The figure that is rendered.
         */
        public Figure render(Object n) {
            Polygon2D.Double polygon = new Polygon2D.Double();

            Figure figure;

            // Wrap the figure in a TerminalFigure to set the direction that
            // connectors exit the port.  Note that this direction is the
            // OPPOSITE direction that is used to layout the port in the
            // Entity Controller.
            int direction;
            Locatable location = (Location) n;

            if (location != null) {
                final Port port = (Port) location.getContainer();

                Color fill;

                if (!(port instanceof IOPort)) {
                    polygon.moveTo(-6, 6);
                    polygon.lineTo(0, 6);
                    polygon.lineTo(8, 0);
                    polygon.lineTo(0, -6);
                    polygon.lineTo(-6, -6);
                    fill = Color.black;
                } else {
                    IOPort ioport = (IOPort) port;
                    polygon.moveTo(-8, 4);
                    if (ioport.isMultiport()) {
                        if (ioport instanceof ParameterPort) {
                            // It would be better to couple these to the
                            // parameters by position, but this is impossible
                            // in diva, so we assign a special color.
                            // FIXME: are Multiport ParameterPorts possible?
                            // FIXME: Should this be lightGrey?
                            fill = Color.darkGray;
                        } else {
                            fill = Color.white;
                        }
                        if (ioport.isOutput() && ioport.isInput()) {
                            polygon.lineTo(-4, 4);
                            polygon.lineTo(-4, 9);
                            polygon.lineTo(2, 4);
                            polygon.lineTo(2, 9);
                            polygon.lineTo(8, 4);
                            polygon.lineTo(12, 4);
                            polygon.lineTo(12, -4);
                            polygon.lineTo(8, -4);
                            polygon.lineTo(2, -9);
                            polygon.lineTo(2, -4);
                            polygon.lineTo(-4, -9);
                            polygon.lineTo(-4, -4);
                            polygon.lineTo(-8, -4);
                        } else if (ioport.isOutput()) {
                            polygon.lineTo(-8, 4);
                            polygon.lineTo(-8, 9);
                            polygon.lineTo(-2, 4);
                            polygon.lineTo(-2, 9);
                            polygon.lineTo(4, 4);
                            polygon.lineTo(12, 4);
                            polygon.lineTo(12, -4);
                            polygon.lineTo(4, -4);
                            polygon.lineTo(-2, -9);
                            polygon.lineTo(-2, -4);
                            polygon.lineTo(-8, -9);
                        } else if (ioport.isInput()) {
                            polygon.lineTo(-5, 4);
                            polygon.lineTo(-5, 9);
                            polygon.lineTo(1, 4);
                            polygon.lineTo(1, 9);
                            polygon.lineTo(7, 4);
                            polygon.lineTo(12, 0);
                            polygon.lineTo(7, -4);
                            polygon.lineTo(1, -9);
                            polygon.lineTo(1, -4);
                            polygon.lineTo(-5, -9);
                            polygon.lineTo(-5, -4);
                            polygon.lineTo(-8, -4);
                        } else {
                            throw new InternalErrorException(port, null,
                                    "Port is neither input nor output");
                        }
                    } else {
                        if (ioport instanceof ParameterPort) {
                            // It would be better to couple these to the
                            // parameters by position, but this is impossible
                            // in diva, so we assign a special color.
                            // FIXME: what about multiports para
                            fill = Color.lightGray;
                        } else {
                            fill = Color.black;
                        }
                        if (ioport.isOutput() && ioport.isInput()) {
                            polygon.lineTo(0, 4);
                            polygon.lineTo(0, 9);
                            polygon.lineTo(6, 4);
                            polygon.lineTo(12, 4);
                            polygon.lineTo(12, -4);
                            polygon.lineTo(6, -4);
                            polygon.lineTo(0, -9);
                            polygon.lineTo(0, -4);
                            polygon.lineTo(-8, -4);
                        } else if (ioport.isOutput()) {
                            polygon.lineTo(-8, 9);
                            polygon.lineTo(-2, 4);
                            polygon.lineTo(12, 4);
                            polygon.lineTo(12, -4);
                            polygon.lineTo(-2, -4);
                            polygon.lineTo(-8, -9);
                        } else if (ioport.isInput()) {
                            polygon.lineTo(0, 4);
                            polygon.lineTo(0, 9);
                            //polygon.lineTo(6, 4);
                            polygon.lineTo(12, 0);
                            //polygon.lineTo(6, -4);
                            polygon.lineTo(0, -9);
                            polygon.lineTo(0, -4);
                            polygon.lineTo(-8, -4);
                        } else {
                            throw new InternalErrorException(port, null,
                                    "Port is neither input nor output");
                        }
                    }
                }
                polygon.closePath();

                if (port instanceof ParameterPort) {
                    // Create a PaintedList that has two PaintedPaths,
                    // the usual icon and the > shape.
                    //                     PaintedList paintedList = new PaintedList();
                    //                     paintedList.add(new PaintedPath(polygon, (float) 1.5, fill));
                    //                     Polygon2D.Double polygon2 = new Polygon2D.Double();
                    //                     //polygon2.moveTo(-15,-15);
                    //                     //polygon2.lineTo(-3,-5);
                    //                     //polygon2.lineTo(-16,-5);

                    //                     polygon2.moveTo(5, 9);

                    //                     polygon2.lineTo(17, 0);
                    //                     polygon2.lineTo(5, -9);

                    //                     polygon2.lineTo(5, -9);
                    //                     polygon2.lineTo(17, 0);
                    //                     polygon2.lineTo(5, 9);

                    //                     //polygon2.lineTo(5, -9);
                    //                     //polygon2.lineTo(17, 0);
                    //                     //polygon2.lineTo(5, 9);

                    //                     polygon2.closePath();
                    //                     paintedList.add(new PaintedPath(polygon2, (float) 1.0, Color.black));
                    //                     figure = new PaintedFigure(paintedList);
                    figure = new BasicFigure(polygon, new Color(0, 0, 0, 0),
                            (float) 0.0);
                } else {
                    figure = new BasicFigure(polygon, fill, (float) 1.5);
                }

                if (!(port instanceof IOPort)) {
                    direction = SwingConstants.NORTH;
                } else {
                    IOPort ioport = (IOPort) port;

                    if (ioport.isInput() && ioport.isOutput()) {
                        direction = SwingConstants.NORTH;
                    } else if (ioport.isInput()) {
                        direction = SwingConstants.EAST;
                    } else if (ioport.isOutput()) {
                        direction = SwingConstants.WEST;
                    } else {
                        // should never happen
                        direction = SwingConstants.NORTH;
                    }
                }

                double normal = CanvasUtilities.getNormal(direction);

                String name = port.getName();
                Rectangle2D backBounds = figure.getBounds();
                figure = new CompositeFigure(figure) {
                    // Override this because we want to show the type.
                    // It doesn't work to set it once because the type
                    // has not been resolved, and anyway, it may
                    // change. NOTE: This is copied from above.
                    public String getToolTipText() {
                        return _portTooltip(port);
                    }
                };

                if ((name != null) && !name.equals("")
                        && !(port instanceof ParameterPort)) {
                    LabelFigure label = new LabelFigure(name, _labelFont, 1.0,
                            SwingConstants.SOUTH_WEST);

                    // Shift the label slightly right so it doesn't
                    // collide with ports.
                    label.translateTo(backBounds.getX(), backBounds.getY());
                    ((CompositeFigure) figure).add(label);
                }

                if (port instanceof IOPort) {
                    // Create a diagonal connector for multiports, if necessary.
                    IOPort ioPort = (IOPort) port;

                    if (ioPort.isMultiport()) {
                        int numberOfLinks = ioPort.insideRelationList().size();

                        if (numberOfLinks > 1) {
                            // The diagonal is necessary.
                            // Line depends on the orientation.
                            double startX;

                            // The diagonal is necessary.
                            // Line depends on the orientation.
                            double startY;

                            // The diagonal is necessary.
                            // Line depends on the orientation.
                            double endX;

                            // The diagonal is necessary.
                            // Line depends on the orientation.
                            double endY;
                            Rectangle2D bounds = figure.getShape()
                                    .getBounds2D();
                            double x = bounds.getX();
                            double y = bounds.getY();
                            double width = bounds.getWidth();
                            double height = bounds.getHeight();
                            int extent = numberOfLinks - 1;

                            if (direction == SwingConstants.EAST) {
                                startX = x + width;
                                startY = y + (height / 2);
                                endX = startX
                                        + (extent * IOPortController.MULTIPORT_CONNECTION_SPACING);
                                endY = startY
                                        + (extent * IOPortController.MULTIPORT_CONNECTION_SPACING);
                            } else if (direction == SwingConstants.WEST) {
                                startX = x;
                                startY = y + (height / 2);
                                endX = startX
                                        - (extent * IOPortController.MULTIPORT_CONNECTION_SPACING);
                                endY = startY
                                        - (extent * IOPortController.MULTIPORT_CONNECTION_SPACING);
                            } else if (direction == SwingConstants.NORTH) {
                                startX = x + (width / 2);
                                startY = y;
                                endX = startX
                                        - (extent * IOPortController.MULTIPORT_CONNECTION_SPACING);
                                endY = startY
                                        - (extent * IOPortController.MULTIPORT_CONNECTION_SPACING);
                            } else {
                                startX = x + (width / 2);
                                startY = y + height;
                                endX = startX
                                        + (extent * IOPortController.MULTIPORT_CONNECTION_SPACING);
                                endY = startY
                                        + (extent * IOPortController.MULTIPORT_CONNECTION_SPACING);
                            }

                            Line2D line = new Line2D.Double(startX, startY,
                                    endX, endY);
                            Figure lineFigure = new BasicFigure(line, fill,
                                    (float) 2.0);
                            ((CompositeFigure) figure).add(lineFigure);
                        }
                    }

View Full Code Here

     *  the icon of the last such entity.  If there are none, then it
     *  behaves like the base class.
     *  @return A new figure.
     */
    public Figure createBackgroundFigure() {
        Figure result = null;
        Nameable container = getContainer();

        if (container instanceof CompositeEntity) {
            CompositeEntity myContainer = ((CompositeEntity) container);
            ComponentEntity entity = null;
            Iterator entities = myContainer.entityList().iterator();

            while (entities.hasNext()) {
                entity = (ComponentEntity) entities.next();
            }

            try {
                if (entity != null) {
                    // Look for an icon within the entity.
                    EditorIcon icon = null;
                    Iterator icons = entity.attributeList(EditorIcon.class)
                            .iterator();

                    while (icons.hasNext()) {
                        icon = (EditorIcon) icons.next();
                    }

                    if (icon != null) {
                        result = icon.createBackgroundFigure();
                    } else {
                        // If there is no icon, then maybe there is an
                        // _iconDescription attribute.
                        SingletonConfigurableAttribute description = (SingletonConfigurableAttribute) entity
                                .getAttribute("_iconDescription",
                                        SingletonConfigurableAttribute.class);

                        if (description != null) {
                            // Look for an icon description in my container.
                            SingletonConfigurableAttribute myDescription = (SingletonConfigurableAttribute) myContainer
                                    .getAttribute(
                                            "_iconDescription",
                                            SingletonConfigurableAttribute.class);

                            if (myDescription != null) {
                                // Save my original description, in case I go
                                // back to having nothing inside.
                                if (_originalDescription == null) {
                                    _originalDescription = myDescription
                                            .getConfigureText();
                                }

                                myDescription.configure(null, null, description
                                        .getConfigureText());
                            }
                        }
                    }
                } else {
                    // Restore the original description if we don't have
                    // one now.
                    if ((result == null) && (_originalDescription != null)) {
                        // Restore the original icon description.
                        // Look for an icon description in my container.
                        SingletonConfigurableAttribute myDescription = (SingletonConfigurableAttribute) myContainer
                                .getAttribute("_iconDescription",
                                        SingletonConfigurableAttribute.class);

                        if (myDescription != null) {
                            myDescription.configure(null, null,
                                    _originalDescription);
                        }
                    }
                }
            } catch (Throwable throwable) {
                // Ignore and use default icon.
            }
        }

        // If all else fails, behave like the superclass.
        if (result == null) {
            result = super.createBackgroundFigure();
        }

        // Wrap in a CompositeFigure with echos of the bounding box.
        // Note that the bounds here are actually bigger than the
        // bounding box, which may be OK in this case.
        Rectangle2D bounds = result.getBounds();
        CompositeFigure composite = new CompositeFigure();
        BasicRectangle rectangle = new BasicRectangle(bounds.getX() + 10.0,
                bounds.getY() + 10.0, bounds.getWidth(), bounds.getHeight(),
                Color.white);
        composite.add(rectangle);
View Full Code Here

        // queued mutation.  So the translation we get isn't right.
        Iterator targets = targets();
        double[] originalUpperLeft = null;

        while (targets.hasNext()) {
            Figure figure = (Figure) targets.next();
            originalUpperLeft = new double[2];
            originalUpperLeft[0] = figure.getOrigin().getX();
            originalUpperLeft[1] = figure.getOrigin().getY();

            // Only snap the first figure in the set.
            break;
        }

        double[] snapTranslation;

        if (originalUpperLeft == null) {
            // No location found in the selection, so we just quantize
            // the translation.
            double[] oldTranslation = new double[2];
            oldTranslation[0] = x;
            oldTranslation[1] = y;
            snapTranslation = _snapConstraint.constrain(oldTranslation);
        } else {
            double[] newUpperLeft = new double[2];
            newUpperLeft[0] = originalUpperLeft[0] + x;
            newUpperLeft[1] = originalUpperLeft[1] + y;

            double[] snapLocation = _snapConstraint.constrain(newUpperLeft);
            snapTranslation = new double[2];
            snapTranslation[0] = snapLocation[0] - originalUpperLeft[0];
            snapTranslation[1] = snapLocation[1] - originalUpperLeft[1];
        }

        // NOTE: The following seems no longer necessary, since the
        // translation occurs as a consequence of setting the location
        // attribute. However, for reasons that I don't understand,
        // without this, drag doesn't work.  The new location ends
        // up identical to the old because of the snap, so no translation
        // occurs.  Oddly, the superclass call performs a translation
        // even if the snapTranslation is zero.  Beats me.  EAL 7/31/02.
        super.translate(e, snapTranslation[0], snapTranslation[1]);

        // Set the location attribute of each item that is translated.
        // NOTE: this works only because all the nodes that allow
        // dragging are location nodes.  If nodes can be dragged that
        // aren't locatable nodes, then they shouldn't be able to be
        // selected at the same time as a locatable node.
        try {
            targets = targets();

            while (targets.hasNext()) {
                Figure figure = (Figure) targets.next();
                Object node = figure.getUserObject();

                if (_controller.getController().getGraphModel().isNode(node)) {
                    // NOTE: This used to get the location and then set it,
                    // but since the returned value is the internal array,
                    // then setLocation() believed there was no change,
                    // so the change would not be persistent.
                    double[] location = new double[2];
                    location[0] = figure.getOrigin().getX();
                    location[1] = figure.getOrigin().getY();
                    _controller.setLocation(node, location);
                }
            }
        } catch (IllegalActionException ex) {
            MessageHandler.error("could not set location", ex);
View Full Code Here

     *  one terminal to another, creating a link to connect them.
     */
    protected class LinkCreator extends AbstractInteractor {
        /** Create a new edge when the mouse is pressed. */
        public void mousePressed(LayerEvent event) {
            Figure source = event.getFigureSource();
            NamedObj sourceObject = (NamedObj) source.getUserObject();

            // Create the new edge.
            Link link = new Link();

            // Set the tail, going through the model so the link is added
View Full Code Here

            if (iconRoles != null) {
                GUIUtilities.addIcons(this, iconRoles);
            } else {
                // Standard toolbar icons are 25x25 pixels.
                NodeRenderer renderer = _relationController.getNodeRenderer();
                Figure figure = renderer.render(null);

                FigureIcon icon = new FigureIcon(figure, 25, 25, 1, true);
                putValue(diva.gui.GUIUtilities.LARGE_ICON, icon);
            }
            putValue("tooltip", "Control-click to create a new relation");
View Full Code Here

    /** Draw the node at its location. This overrides the base class
     *  to highlight the actor to indicate that it is a class definition.
     */
    protected Figure _renderNode(Object node) {
        Figure nf = super._renderNode(node);

        if (nf instanceof CompositeFigure) {
            // This cast should be safe...
            CompositeFigure cf = (CompositeFigure) nf;
            Figure backgroundFigure = cf.getBackgroundFigure();

            // This might be null because the node is hidden.
            if (backgroundFigure != null) {
                BasicFigure bf = new BasicFigure(backgroundFigure.getBounds(),
                        4.0f);
                bf.setStrokePaint(_HIGHLIGHT_COLOR);
                // Put the highlighting in the background,
                // behind the actor label.
                int index = cf.getFigureCount();
View Full Code Here

        public Figure render(Object n) {
            Locatable location = (Locatable) n;
            final NamedObj object = location.getContainer();

            // NOTE: this code is similar to that in PtolemyTreeCellRenderer
            Figure result = null;

            try {
                List iconList = object.attributeList(EditorIcon.class);

                // Check to see whether there is an icon that has been created,
                // but not inserted.
                if (iconList.size() == 0) {
                    XMLIcon alreadyCreated = (XMLIcon) _iconsPendingContainer
                            .get(object);

                    if (alreadyCreated != null) {
                        iconList.add(alreadyCreated);
                    }
                }

                // If there are still no icons, then we need to create one.
                if (iconList.size() == 0) {
                    // NOTE: This used to directly create an XMLIcon within
                    // the container "object". However, this is not cosher,
                    // since we may not be able to get write access on the
                    // workspace. We instead use a hack supported by XMLIcon
                    // to create an XMLIcon with no container (this does not
                    // require write access to the workspace), and specify
                    // to it what the container will eventually be. Then
                    // we queue a change request to make that the container.
                    // Further, we have to make a record of the figure, indexed
                    // by the object, in case some other change request is
                    // executed before this gets around to setting the
                    // container.  Otherwise, that second change request
                    // will result in the creation of a second figure.
                    final EditorIcon icon = new XMLIcon(object.workspace(),
                            "_icon");
                    icon.setContainerToBe(object);
                    icon.setPersistent(false);
                    result = icon.createFigure();

                    // NOTE: Make sure this is done before the change request
                    // below is executed, which may be as early as when it is
                    // requested.
                    _iconsPendingContainer.put(object, icon);

                    // NOTE: Make sure the source of this change request is
                    // the graph model. Otherwise, this change request will
                    // trigger a redraw of the entire graph, which will result
                    // in another call to this very same method, which will
                    // result in creation of yet another figure before this
                    // method even returns!
                    GraphController controller = IconController.this
                            .getController();
                    GraphModel graphModel = controller.getGraphModel();
                    ChangeRequest request = new ChangeRequest(graphModel,
                            "Set the container of a new XMLIcon.") {
                        // NOTE: The KernelException should not be thrown,
                        // but if it is, it will be handled properly.
                        protected void _execute() throws KernelException {
                            _iconsPendingContainer.remove(object);

                            // If the icon already has a container, do nothing.
                            if (icon.getContainer() != null) {
                                return;
                            }

                            // If the container already has an icon, do nothing.
                            if (object.getAttribute("_icon") != null) {
                                return;
                            }

                            icon.setContainer(object);
                        }
                    };

                    request.setPersistent(false);
                    object.requestChange(request);
                } else if (iconList.size() >= 1) {
                    // Use only the last icon in the list.
                    EditorIcon icon = (EditorIcon) iconList
                            .get(iconList.size() - 1);
                    result = icon.createFigure();
                }
            } catch (KernelException ex) {
                throw new InternalErrorException(null, ex,
                        "Could not create icon " + "in " + object + " even "
                                + "though one did not previously exist.");
            }

            result.setToolTipText(object.getClassName());

            // Check to see if it has
            // attributes that specify its color or an explanation.
            // Old way to specify a color.
            try {
                StringAttribute colorAttr = (StringAttribute) (object
                        .getAttribute("_color", StringAttribute.class));
                if (colorAttr != null) {
                    String color = colorAttr.getExpression();
                    AnimationRenderer animationRenderer = new AnimationRenderer(
                            SVGUtilities.getColor(color));
                    animationRenderer.renderSelected(result);
                }
            } catch (IllegalActionException e) {
                // Ignore
            }
            // New way to specify a highlight color.
            try {
                ColorAttribute highlightAttribute = (ColorAttribute) (object
                        .getAttribute("_highlightColor", ColorAttribute.class));
                if (highlightAttribute != null
                        && !highlightAttribute.getExpression().trim()
                                .equals("")) {
                    Color color = highlightAttribute.asColor();
                    AnimationRenderer animationRenderer = new AnimationRenderer(
                            color);
                    animationRenderer.renderSelected(result);
                }
            } catch (IllegalActionException e) {
                // Ignore.
            }
            try {
                StringAttribute explanationAttribute = (StringAttribute) (object
                        .getAttribute("_explanation", StringAttribute.class));
                if (explanationAttribute != null) {
                    result.setToolTipText(explanationAttribute.getExpression());
                }
            } catch (IllegalActionException e) {
                // Ignore.
            }
View Full Code Here

            // Have to defer this to the event thread, or repaint
            // doesn't work properly.
            Runnable action = new Runnable() {
                public void run() {
                    Locatable location = (Locatable) settable;
                    Figure figure = getFigure(location);
                    if (figure != null) {
                        Point2D origin = figure.getOrigin();

                        double originalUpperLeftX = origin.getX();
                        double originalUpperLeftY = origin.getY();

                        // NOTE: the following call may trigger an evaluation,
                        // which results in another recursive call to this method.
                        // Thus, we ignore the inside call and detect it with a
                        // private variable.
                        double[] newLocation;

                        try {
                            _inValueChanged = true;
                            newLocation = location.getLocation();
                        } finally {
                            _inValueChanged = false;
                        }

                        double translationX = newLocation[0]
                                - originalUpperLeftX;
                        double translationY = newLocation[1]
                                - originalUpperLeftY;

                        if ((translationX != 0.0) || (translationY != 0.0)) {
                            // The translate method supposedly handles the required
                            // repaint.
                            figure.translate(translationX, translationY);

                            // Reroute edges linked to this figure.
                            GraphModel model = getGraphModel();
                            Object userObject = figure.getUserObject();

                            if (userObject != null) {
                                Iterator inEdges = model.inEdges(userObject);

                                while (inEdges.hasNext()) {
                                    Figure connector = getFigure(inEdges.next());

                                    if (connector instanceof Connector) {
                                        ((Connector) connector).reroute();
                                    }
                                }

                                Iterator outEdges = model.outEdges(userObject);

                                while (outEdges.hasNext()) {
                                    Figure connector = getFigure(outEdges
                                            .next());

                                    if (connector instanceof Connector) {
                                        ((Connector) connector).reroute();
                                    }
                                }

                                if (model.isComposite(userObject)) {
                                    Iterator edges = GraphUtilities
                                            .partiallyContainedEdges(
                                                    userObject, model);

                                    while (edges.hasNext()) {
                                        Figure connector = getFigure(edges
                                                .next());

                                        if (connector instanceof Connector) {
                                            ((Connector) connector).reroute();
                                        }
View Full Code Here

TOP

Related Classes of diva.canvas.Figure

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.