Package ptolemy.vergil.kernel

Examples of ptolemy.vergil.kernel.Link


         *  Ptolemy model.
         *  @param edge The edge.
         *  @return A valid MoML string.
         */
        public String getDeleteEdgeMoML(Object edge) {
            final Link link = (Link) edge;
            NamedObj linkHead = (NamedObj) link.getHead();
            NamedObj linkTail = (NamedObj) link.getTail();
            Relation linkRelation = link.getRelation();

            // This moml is parsed to execute the change
            StringBuffer moml = new StringBuffer();

            // Make the request in the context of the container.
View Full Code Here


         @param newLinkHead The new head for the edge, which is assumed to
         *  be a location representing a port, a port or a vertex.
         *  @see #getHead(Object)
         */
        public void setHead(final Object edge, final Object newLinkHead) {
            final Link link = (Link) edge;
            final NamedObj linkHead = (NamedObj) link.getHead();
            final NamedObj linkTail = (NamedObj) link.getTail();
            Relation linkRelation = link.getRelation();

            // This moml is parsed to execute the change
            final StringBuffer moml = new StringBuffer();

            // This moml is parsed in case the change fails.
            final StringBuffer failmoml = new StringBuffer();
            moml.append("<group>\n");
            failmoml.append("<group>\n");

            // Make the request in the context of the container.
            final CompositeEntity container = (CompositeEntity) getPtolemyModel();

            String relationName = "";

            // Flag specifying whether we have actually created any MoML.
            boolean appendedMoML = false;

            try {
                // create moml to unlink any existing.
                appendedMoML = _unlinkMoML(container, moml, linkHead, linkTail,
                        linkRelation);

                // create moml to make the new links.
                relationName = _linkMoML(container, moml, failmoml,
                        (NamedObj) newLinkHead, linkTail);

                // FIXME: Above can return an empty name, so the following
                // test is not quite right.
                appendedMoML = appendedMoML || (relationName != null);
            } catch (Exception ex) {
                // The link is bad... remove it.
                _linkSet.remove(link);
                link.setHead(null);
                link.setTail(null);
                dispatchGraphEvent(new GraphEvent(ActorGraphModel.this,
                        GraphEvent.STRUCTURE_CHANGED, getRoot()));
            }

            moml.append("</group>\n");
            failmoml.append("</group>\n");

            final String relationNameToAdd = relationName;
            final boolean nonEmptyMoML = appendedMoML;

            // Here the source IS the graph model, because we need to
            // handle the event dispatch specially:  An event is only
            // dispatched if both the head and the tail are attached.
            // This rather obnoxious hack is here because edge creation
            // is tricky and we can't rerender the edge while we are dragging
            // it.
            MoMLChangeRequest request = new MoMLChangeRequest(
                    ActorGraphModel.this, container, moml.toString()) {
                protected void _execute() throws Exception {
                    // If nonEmptyMoML is false, then the MoML code is empty.
                    // Do not execute it, as this will put spurious empty
                    // junk on the undo stack.
                    if (nonEmptyMoML) {
                        super._execute();
                    }

                    link.setHead(newLinkHead);

                    if (relationNameToAdd != null) {
                        ComponentRelation relation = container
                                .getRelation(relationNameToAdd);

                        if (relation == null) {
                            throw new InternalErrorException(
                                    "Tried to find relation with name "
                                            + relationNameToAdd
                                            + " in context " + container);
                        }

                        link.setRelation(relation);
                    } else {
                        link.setRelation(null);
                    }
                }
            };

            // Handle what happens if the mutation fails.
View Full Code Here

         *  assumed to be a location representing a port, a port or a
         *  vertex.
         *  @see #getTail(Object)
         */
        public void setTail(final Object edge, final Object newLinkTail) {
            final Link link = (Link) edge;
            final NamedObj linkHead = (NamedObj) link.getHead();
            final NamedObj linkTail = (NamedObj) link.getTail();
            Relation linkRelation = link.getRelation();

            // This moml is parsed to execute the change
            final StringBuffer moml = new StringBuffer();

            // This moml is parsed in case the change fails.
            final StringBuffer failmoml = new StringBuffer();
            moml.append("<group>\n");
            failmoml.append("<group>\n");

            // Make the request in the context of the container.
            // JDK1.2.2 fails to compile the next line.
            final CompositeEntity container = (CompositeEntity) getPtolemyModel();

            String relationName = "";

            // Flag specifying whether we have actually created any MoML.
            boolean appendedMoML = false;

            try {
                // create moml to unlink any existing.
                appendedMoML = _unlinkMoML(container, moml, linkHead, linkTail,
                        linkRelation);

                // create moml to make the new links.
                relationName = _linkMoML(container, moml, failmoml, linkHead,
                        (NamedObj) newLinkTail);

                appendedMoML = appendedMoML || (relationName != null);
            } catch (Exception ex) {
                // The link is bad... remove it.
                _linkSet.remove(link);
                link.setHead(null);
                link.setTail(null);
                dispatchGraphEvent(new GraphEvent(ActorGraphModel.this,
                        GraphEvent.STRUCTURE_CHANGED, getRoot()));
            }

            moml.append("</group>\n");
            failmoml.append("</group>\n");

            final String relationNameToAdd = relationName;
            final boolean nonEmptyMoML = appendedMoML;

            // Here the source IS the graph model, because we need to
            // handle the event dispatch specially:  An event is only
            // dispatched if both the head and the tail are attached.
            // This rather obnoxious hack is here because edge creation
            // is tricky and we can't rerender the edge while we are dragging
            // it.
            MoMLChangeRequest request = new MoMLChangeRequest(
                    ActorGraphModel.this, container, moml.toString()) {
                protected void _execute() throws Exception {
                    // If nonEmptyMoML is false, then the MoML code is empty.
                    // Do not execute it, as this will put spurious empty
                    // junk on the undo stack.
                    if (nonEmptyMoML) {
                        super._execute();
                    }

                    link.setTail(newLinkTail);

                    if (relationNameToAdd != null) {
                        ComponentRelation relation = container
                                .getRelation(relationNameToAdd);

                        if (relation == null) {
                            throw new InternalErrorException(
                                    "Tried to find relation with name "
                                            + relationNameToAdd
                                            + " in context " + container);
                        }

                        link.setRelation(relation);
                    } else {
                        link.setRelation(null);
                    }
                }
            };

            // Handle what happens if the mutation fails.
View Full Code Here

        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
            // to the list of links.
            ActorGraphModel model = (ActorGraphModel) getGraphModel();
            model.getLinkModel().setTail(link, sourceObject);
View Full Code Here

            // those we are connected to.
            List portLinkList = new LinkedList();
            Iterator links = _linkSet.iterator();

            while (links.hasNext()) {
                Link link = (Link) links.next();
                Object head = link.getHead();

                if ((head != null) && head.equals(port)) {
                    portLinkList.add(link);
                }
            }
View Full Code Here

            // those we are connected to.
            List portLinkList = new LinkedList();
            Iterator links = _linkSet.iterator();

            while (links.hasNext()) {
                Link link = (Link) links.next();
                Object tail = link.getTail();

                if ((tail != null) && tail.equals(port)) {
                    portLinkList.add(link);
                }
            }
View Full Code Here

            // those we are connected to.
            List vertexLinkList = new LinkedList();
            Iterator links = _linkSet.iterator();

            while (links.hasNext()) {
                Link link = (Link) links.next();
                Object head = link.getHead();

                if ((head != null) && head.equals(vertex)) {
                    vertexLinkList.add(link);
                }
            }
View Full Code Here

            // those we are connected to.
            List vertexLinkList = new LinkedList();
            Iterator links = _linkSet.iterator();

            while (links.hasNext()) {
                Link link = (Link) links.next();
                Object tail = link.getTail();

                if ((tail != null) && tail.equals(vertex)) {
                    vertexLinkList.add(link);
                }
            }
View Full Code Here

        // Go through all the links that currently exist, and remove
        // any that don't have both ends in the model.
        Iterator links = _linkSet.iterator();

        while (links.hasNext()) {
            Link link = (Link) links.next();
            Relation relation = link.getRelation();

            // Undo needs this: Check that the relation hasn't been removed
            if ((relation == null) || (relation.getContainer() == null)
                    || _isHidden(relation)) {
                // NOTE: We used to not do the next three lines when
                // relation == null, but this seems better.
                // EAL 6/26/05.
                link.setHead(null);
                link.setTail(null);
                links.remove();
                continue;
            }

            boolean headOK = GraphUtilities.isContainedNode(link.getHead(),
                    getRoot(), this);
            boolean tailOK = GraphUtilities.isContainedNode(link.getTail(),
                    getRoot(), this);

            // If the head or tail has been removed, then remove this link.
            if (!(headOK && tailOK)) {
                Object headObj = getSemanticObject(link.getHead());
                Object tailObj = getSemanticObject(link.getTail());
                link.setHead(null);
                link.setTail(null);
                links.remove();

                if (headObj instanceof Port && tailObj instanceof Port
                        && (relation.getContainer() != null)) {
                    NamedObj container = getPtolemyModel();
View Full Code Here

        int linkedObjectsCount = linkedObjects.size();

        Iterator links = new LinkedList(_linkSet).iterator();

        while (links.hasNext()) {
            Link link = (Link) links.next();

            // If this link matches a link in the linkedObjects list,
            // then we remove that link from that list, since we don't
            // have to manufacture that link.
            Object tail = link.getTail();
            Object tailObj = getSemanticObject(tail);
            Object head = link.getHead();
            Object headObj = getSemanticObject(head);

            if ((tailObj != relation) && (headObj != relation)
                    && (link.getRelation() != relation)) {
                // The link does not involve this relation. Skip it.
                // NOTE: Used to skip it if the relation field of the link
                // didn't match this relation. But we need to ignore
                // that field for links between relations, since that
                // field will be arbitrarily one of the relations,
                // and we'll end up creating two links where there
                // should be one.
                // EAL 6/26/05
                continue;
            }

            if ((tailObj != null) && linkedObjects.contains(tailObj)) {
                // The tail is an object in the list.
                linkedObjects.remove(tailObj);
            } else if (tailObj != relation) {
                // Unless the tail object is this relation, the link
                // must be spurious. Remove the link.
                link.setHead(null);
                link.setTail(null);
                _linkSet.remove(link);
            }

            if ((headObj != null) && linkedObjects.contains(headObj)) {
                // The head is an object in the list.
                linkedObjects.remove(headObj);
            } else if (headObj != relation) {
                // Unless the head object is this relation, the link
                // must be spurious. Remove the link.
                link.setHead(null);
                link.setTail(null);
                _linkSet.remove(link);
            }
        }

        // Count the remaining linked objects, which are those
        // for which there is no Link object.
        int unlinkedPortCount = linkedObjects.size();

        // If there are no links left to create, then just return.
        if (unlinkedPortCount == 0) {
            return;
        }

        // Get the Root vertex.  This is where we will manufacture links.
        // The root vertex is the one with no linked vertices.
        Vertex rootVertex = null;
        Iterator vertexes = relation.attributeList(Vertex.class).iterator();

        while (vertexes.hasNext()) {
            Vertex v = (Vertex) vertexes.next();

            if (v.getLinkedVertex() == null) {
                rootVertex = v;
            }
        }

        // If there are no verticies, and the relation has exactly
        // two connections, neither of which has been made yet, then
        // create a link without a vertex for the relation.
        if ((rootVertex == null) && (linkedObjectsCount == 2)
                && (unlinkedPortCount == 2)
                && linkedObjects.get(0) instanceof Port
                && linkedObjects.get(1) instanceof Port) {
            Port port1 = (Port) linkedObjects.get(0);
            Port port2 = (Port) linkedObjects.get(1);
            Object head = null;
            Object tail = null;

            if (port1.getContainer().equals(getRoot())) {
                head = _getLocation(port1);
            } else {
                head = port1;
            }

            if (port2.getContainer().equals(getRoot())) {
                tail = _getLocation(port2);
            } else {
                tail = port2;
            }

            Link link;

            try {
                link = new Link();
                _linkSet.add(link);
            } catch (Exception e) {
                throw new InternalErrorException("Failed to create "
                        + "new link, even though one does not "
                        + "already exist:" + e.getMessage());
            }

            link.setRelation(relation);
            link.setHead(head);
            link.setTail(tail);
        } else {
            // A regular relation with a diamond.
            // Create a vertex if one is not found.
            if (rootVertex == null) {
                try {
                    String name = relation.uniqueName("vertex");
                    rootVertex = new Vertex(relation, name);

                    // Have to manually handle propagation, since
                    // the MoML parser is not involved.
                    // FIXME: This could cause a name collision!
                    // (Unlikely though since auto naming will take
                    // into account subclasses).
                    rootVertex.propagateExistence();
                } catch (Throwable throwable) {
                    throw new InternalErrorException(null, throwable,
                            "Failed to create "
                                    + "new vertex, even though one does not "
                                    + "already exist:" + throwable.getMessage());
                }
            }

            // Create any required links for this relation.
            Iterator linkedObjectsIterator = linkedObjects.iterator();

            while (linkedObjectsIterator.hasNext()) {
                Object portOrRelation = linkedObjectsIterator.next();

                // Set the head to the port or relation. More precisely:
                //   If it is a port belonging to the composite, then
                //   set the head to a Location contained by the port.
                //   If is a port belonging to an actor, then set
                //   the head to the port.
                //   If it is a relation, then set the head to the
                //   root vertex of the relation.
                Object head = null;

                if (portOrRelation instanceof Port) {
                    Port port = (Port) portOrRelation;

                    if (port.getContainer().equals(getRoot())) {
                        head = _getLocation(port);
                    } else {
                        head = port;
                    }
                } else {
                    // Get the Root vertex of the other relation.
                    // The root vertex is the one with no linked vertices.
                    vertexes = ((Relation) portOrRelation).attributeList(
                            Vertex.class).iterator();

                    while (vertexes.hasNext()) {
                        Vertex v = (Vertex) vertexes.next();

                        if (v.getLinkedVertex() == null) {
                            head = v;
                        }
                    }
                }

                Link link;

                try {
                    link = new Link();
                    _linkSet.add(link);
                } catch (Exception e) {
                    throw new InternalErrorException("Failed to create "
                            + "new link, even though one does not "
                            + "already exist:" + e.getMessage());
                }

                link.setRelation(relation);
                link.setHead(head);
                link.setTail(rootVertex);
            }
        }
    }
View Full Code Here

TOP

Related Classes of ptolemy.vergil.kernel.Link

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.