2 /*******************************************************************************
3 * Copyright (c) 2012-2014 University of Stuttgart.
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * and the Apache License 2.0 which both accompany this distribution,
7 * and are available at http://www.eclipse.org/legal/epl-v10.html
8 * and http://www.apache.org/licenses/LICENSE-2.0
11 * Oliver Kopp - initial API and implementation and/or initial documentation
12 *******************************************************************************/
14 <%@tag description="Registers the connection types to jsPlumb" pageEncoding="UTF-8"%>
16 <%@attribute name="relationshipTypes" description="the known relationship types" required="true" type="java.util.Collection"%>
17 <%@attribute name="relationshipTemplates" description="the relationship templates to render" required="true" type="java.util.Collection"%>
18 <%@attribute name="ondone" description="JavaScript code executed when everything has been done" required="false"%>
19 <%@attribute name="repositoryURL" required="true" %>
20 <%@attribute name="readOnly" required="false" type="java.lang.Boolean"%>
22 <%@tag import="java.util.Collection"%>
23 <%@tag import="org.eclipse.winery.model.tosca.TRequirement"%>
24 <%@tag import="org.eclipse.winery.model.tosca.TCapability"%>
25 <%@tag import="org.eclipse.winery.model.tosca.TNodeTemplate"%>
26 <%@tag import="org.eclipse.winery.model.tosca.TRelationshipType"%>
27 <%@tag import="org.eclipse.winery.model.tosca.TRelationshipTemplate"%>
28 <%@tag import="org.eclipse.winery.model.tosca.TRelationshipTemplate.SourceElement"%>
29 <%@tag import="org.eclipse.winery.model.tosca.TRelationshipTemplate.TargetElement"%>
30 <%@tag import="org.eclipse.winery.common.Util"%>
32 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
35 Collection<TRelationshipType> relationshipTypesCasted = (Collection<TRelationshipType>) relationshipTypes;
36 Collection<TRelationshipTemplate> relationshipTemplatesCasted = (Collection<TRelationshipTemplate>) relationshipTemplates;
40 // we should load jquery and jquery.ui to have it available at jsPlumb
41 // however, jquery.ui cannot be loaded as it conflicts with bootstrap (when using toogle buttons)
42 require(["jquery", "jsplumb", "winery-common-topologyrendering"], function(globallyavailablea, jsPlumb, wct) {
43 jsPlumb.bind("ready", function() {
45 * Shows error if req or cap does not exist
47 function getNodeTemplateIdForReqOrCapId(id) {
48 var reqOrCap = $("#" + id);
49 if (reqOrCap.length == 0) {
50 vShowError("Requirement/Capability with id " + id + " not found");
52 var res = reqOrCap.parent().parent().parent().attr("id");
56 // register the "selected" type to enable selection of arrows
57 jsPlumb.registerConnectionTypes({
59 paintStyle:{ strokeStyle:"red", lineWidth:5 },
60 hoverPaintStyle:{ lineWidth: 7 }
68 if (relationshipTypesCasted.isEmpty()) {
70 vShowError("No relationship types exist. Please add relationship types to the repository");
73 for (TRelationshipType relationshipType: relationshipTypesCasted) {
74 String fnName = "ajaxRTdata" + i;
75 when = when + fnName + "(), ";
76 whenRes = whenRes + fnName + ", ";
78 function <%=fnName%>() { return $.ajax({
79 url: "<%=repositoryURL%>/relationshiptypes/<%=Util.DoubleURLencode(relationshipType.getTargetNamespace())%>/<%=Util.DoubleURLencode(relationshipType.getName())%>/visualappearance/",
81 success: function(data, textStatus, jqXHR) {
82 jsPlumb.registerConnectionType(
83 "{<%=relationshipType.getTargetNamespace()%>}<%=relationshipType.getName()%>",
86 error: function(jqXHR, textStatus, errorThrown) {
87 vShowAJAXError("Could not load relationship type {<%=relationshipType.getTargetNamespace()%>}<%=relationshipType.getName()%>", jqXHR, errorThrown);
94 if (!relationshipTypesCasted.isEmpty()) {
96 when = when.substring(0, when.length()-2);
97 whenRes = whenRes.substring(0, whenRes.length()-2);
100 // as soon as all relationship types are registered as jsPlumb object,
101 // create connection end points and connect the nodes
102 $.when(<%=when%>).done(function(<%=whenRes%>){
103 require(["winery-common-topologyrendering"], function(wct) {
104 // A NodeTemplateShape also appears in the palette. There, it is hidden.
105 // These should not be initialized as the template will be initialized later on
107 <c:if test="${readOnly}">wct.setReadOnly();</c:if>
109 // Quick hack: All node templates are draggable, even in the readonly view
110 wct.initNodeTemplate(jsPlumb.getSelector(".NodeTemplateShape:not('.hidden')"), true);
115 for (TRelationshipTemplate relationshipTemplate : relationshipTemplatesCasted) {
120 // Source: Either NodeTemplate or Requirement
121 SourceElement sourceElement = relationshipTemplate.getSourceElement();
122 if (sourceElement == null) {
123 %>vShowError("sourceElement is null for <%=relationshipTemplate.getId()%>");<%
126 Object source = sourceElement.getRef();
127 if (source instanceof TRequirement) {
129 req = "<%=((TRequirement)source).getId()%>";
130 sourceId = getNodeTemplateIdForReqOrCapId(req);
133 TNodeTemplate sourceT = (TNodeTemplate) source;
134 if (sourceT == null) {
135 %>vShowError("sourceElement.getRef() is null for <%=relationshipTemplate.getId()%>");<%
139 sourceId = "<%=sourceT.getId()%>";
143 // Target: Either NodeTemplate or Requirement
144 TargetElement targetElement = relationshipTemplate.getTargetElement();
145 if (targetElement == null) {
146 %>vShowError("targetElement is null for <%=relationshipTemplate.getId()%>");<%
149 Object target = targetElement.getRef();
150 if (target instanceof TCapability) {
152 cap = "<%=((TCapability)target).getId()%>";
153 targetId = getNodeTemplateIdForReqOrCapId(cap);
156 TNodeTemplate targetT = (TNodeTemplate) target;
157 if (targetT == null) {
158 %>vShowError("targetElement.getRef() is null for <%=relationshipTemplate.getId()%>");<%
162 targetId = "<%=targetT.getId()%>";
166 var c = jsPlumb.connect({
169 type:"<%=relationshipTemplate.getType()%>"
171 wct.handleConnectionCreated(c);
172 // we have to store the TOSCA id as jsPlumb does not allow to pass ids from user's side
173 // we could overwrite c.id, but we are not aware the side effects...
174 winery.connections[c.id].id = "<%=relationshipTemplate.getId()%>";
176 winery.connections[c.id].req = req;
179 winery.connections[c.id].cap = cap;
182 if (relationshipTemplate.getName() != null) {
184 winery.connections[c.id].name = "<%=relationshipTemplate.getName()%>";
190 // all connections are there
191 // we can register the events now
193 jsPlumb.bind("connection", wct.handleConnectionCreated);
197 // end of the when waiting for all relationship types
199 // end of require binding