compile_utils.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*******************************************************************************
  2. AToMPM - A Tool for Multi-Paradigm Modelling
  3. Copyright (c) 2011 Raphael Mannadiar (raphael.mannadiar@mail.mcgill.ca)
  4. Modified by Conner Hansen (chansen@crimson.ua.edu)
  5. This file is part of AToMPM.
  6. AToMPM is free software: you can redistribute it and/or modify it under the
  7. terms of the GNU Lesser General Public License as published by the Free Software
  8. Foundation, either version 3 of the License, or (at your option) any later
  9. version.
  10. AToMPM is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  12. PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License along
  14. with AToMPM. If not, see <http://www.gnu.org/licenses/>.
  15. *******************************************************************************/
  16. ///////////////////////////////////////////////////////////////////////////////
  17. // DEPRECATED FUNCTIONS
  18. ///////////////////////////////////////////////////////////////////////////////
  19. function _compileToASMM(fname){
  20. AtomPMClient.alertDeprecatedFunctionCall("_compileToASMM");
  21. CompileUtils.compileToASMM(fname);
  22. }
  23. function _compileToCSMM(fname){
  24. AtomPMClient.alertDeprecatedFunctionCall("_compileToCSMM");
  25. CompileUtils.compileToCSMM(fname);
  26. }
  27. function _compileToPatternMM(fname){
  28. AtomPMClient.alertDeprecatedFunctionCall("_compileToPatternMM");
  29. CompileUtils.compileToPatternMM(fname);
  30. }
  31. ///////////////////////////////////////////////////////////////////////////////
  32. // DEPRECATED FUNCTIONS
  33. ///////////////////////////////////////////////////////////////////////////////
  34. /* draw the icon specified by the given icon model on the provided canvas
  35. 0. initialize a group that will contain the 'compiled' icon
  36. 1. for every ConcreteSyntax primitive (e.g., Rectangle, Text) contained
  37. within the given icon model
  38. a. create appropriate Raphael-SVG objects
  39. b. style these and position/orient/scale them
  40. c. initialize various attributes and remember various values that are used
  41. to manipulate and reason about vobjects later on
  42. d. apply layout constraint solver and/or link decorator positioner
  43. transformations... to remain backward-compatible with existing
  44. pre-lcs/ldp models, default values are provided if needed
  45. e. add objects to group from step 0.
  46. 2. apply options:
  47. size: force the icon to have a certain size
  48. wrap: wrap the canvas around the icon
  49. id: this icon is a canvas icon (not a toolbar icon)... save it
  50. and its vobjects in __icons and give it some default
  51. parameters
  52. attrs: add extra attributes to icon
  53. behaviour: indicates that the icon should have event listeners
  54. NOTE:: because text's origin defaults to (0,height/2), we move text elements
  55. by (0,height/2) (via __valignText()) to ensure their top-left corner
  56. lands on the specified (x,y)... for the computed height to be correct,
  57. we must style the text element beforehand
  58. NOTE:: because (x,y) are not parameters of Raphael.Paths, we translate paths
  59. according to vobj['position'] manually
  60. NOTE:: when im.nodes is empty (this may occur when a Link defines no
  61. decorators), a Raphael.point is used to ensure the icon is not empty
  62. NOTE:: text is made non-selectable to avoid default browser mouse dragging
  63. behaviour (select any encoutered text) and subsequent default browser
  64. selected text dragging behaviour
  65. NOTE:: throught this function, we use *.initial and *.latest values... the
  66. former are attributes of vobjects as they were drawn by the user...
  67. the latter are added translations/rotations/scalings applied by the
  68. layout constraint solver and link decorator positioner */
  69. CompileUtils = function(){
  70. /* draw the edge specified by the given segments
  71. 1. draw the edge given the specified segments and style
  72. 2. encapsulate it within a group, remember the uris of the start and end
  73. icons, and save the edge and segments in __edges
  74. 3. give the edge appropriate event handlers and return it
  75. NOTE:: we use insertBefore() to ensure that edges get drawn beneath link
  76. decorators, if any */
  77. /**
  78. * Draws the edge specified by the given segments
  79. *
  80. * 1. Draws the edge given the specified segments and style
  81. * 2. Encapsulates it within a group and remembers the URIs of the start and
  82. * end icons, then saves the edge and segments in the AtomPMClient edges
  83. * variable
  84. * 3. Assigns the appropriate event handles to the edge and then returns
  85. */
  86. this.compileAndDrawEdge = function(segments, linkStyle, start, end, linkuri){
  87. var path = __canvas.path(segments),
  88. edge = __canvas.group(),
  89. edgeId = start+'--'+end;
  90. edge.insertBefore(__getIcon(linkuri).node);
  91. path.attr(linkStyle);
  92. edge.push(path);
  93. edge.setAttr('__edgeId',edgeId);
  94. edge.setAttr('__linkuri',linkuri);
  95. edge.setAttr('class','clickable');
  96. __edges[edgeId] =
  97. {'icon':edge,
  98. 'start':start,
  99. 'end':end,
  100. 'segments':segments};
  101. edge.node.onmousedown =
  102. function(event)
  103. {
  104. if( event.button == 0 )
  105. BehaviorManager.handleUserEvent(__EVENT_LEFT_PRESS_ICON,event);
  106. else if( event.button == 2 )
  107. BehaviorManager.handleUserEvent(__EVENT_RIGHT_PRESS_ICON,event);
  108. };
  109. edge.node.onmouseup =
  110. function(event)
  111. {
  112. if( event.button == 0 )
  113. BehaviorManager.handleUserEvent(__EVENT_LEFT_RELEASE_ICON,event);
  114. else if( event.button == 2 )
  115. BehaviorManager.handleUserEvent(__EVENT_RIGHT_RELEASE_ICON,event);
  116. else if( event.button == 1 )
  117. BehaviorManager.handleUserEvent(__EVENT_MIDDLE_RELEASE_ICON,event);
  118. };
  119. /*edge.node.onmouseover =
  120. function(event)
  121. {
  122. BehaviorManager.handleUserEvent(__EVENT_MOUSE_OVER_ICON,event);
  123. }*/
  124. return edge;
  125. };
  126. // TODO: split this function up
  127. /**
  128. * Compiles the icon and returns it in order to be placed
  129. * on the canvas
  130. */
  131. this.compileAndDrawIconModel = function( im, canvas, options ){
  132. var icon = canvas.group(),
  133. vobjects = {};
  134. for( var vid in im.nodes )
  135. {
  136. var vobj = im.nodes[vid],
  137. x = __getVobjGeomAttrVal(vobj['position']['value'][0]),
  138. y = __getVobjGeomAttrVal(vobj['position']['value'][1]);
  139. if( vobj['$type'].match(/\/Text$/) )
  140. {
  141. var tc = vobj['textContent']['value'];
  142. vobjects[vid] = canvas.text(x.initial,y.initial,tc);
  143. vobjects[vid].attr('text-anchor','start');
  144. vobjects[vid].attr(vobj['style']['value']);
  145. __valignText(vobjects[vid]);
  146. vobjects[vid].node.setAttribute('class','unselectable');
  147. }
  148. else if( vobj['$type'].match(/\/Rectangle$/) )
  149. {
  150. var w = parseFloat(vobj['width']['value']),
  151. h = parseFloat(vobj['height']['value']),
  152. cr = parseFloat(vobj['cornerRadius']['value']);
  153. vobjects[vid] = canvas.rect(x.initial,y.initial,w,h,cr);
  154. }
  155. else if( vobj['$type'].match(/\/Circle$/) )
  156. {
  157. var r = parseFloat(vobj['r']['value']);
  158. vobjects[vid] = canvas.circle(x.initial+r,y.initial+r,r);
  159. }
  160. else if( vobj['$type'].match(/\/Ellipse$/) )
  161. {
  162. var rx = parseFloat(vobj['rx']['value']),
  163. ry = parseFloat(vobj['ry']['value']);
  164. vobjects[vid] = canvas.ellipse(x.initial+rx,y.initial+ry,rx,ry);
  165. }
  166. else if( vobj['$type'].match(/\/Polygon$/) )
  167. {
  168. var r = parseFloat(vobj['r']['value']),
  169. sides = parseInt(vobj['sides']['value']);
  170. vobjects[vid] = __drawPolygon(canvas,x.initial,y.initial,r,sides);
  171. }
  172. else if( vobj['$type'].match(/\/Star$/) )
  173. {
  174. var r = parseFloat(vobj['r']['value']),
  175. rays = parseInt(vobj['rays']['value']);
  176. vobjects[vid] = __drawStar(canvas,x.initial,y.initial,r,rays);
  177. }
  178. else if( vobj['$type'].match(/\/Path$/) )
  179. {
  180. var segments = vobj['segments']['value'];
  181. vobjects[vid] = canvas.path(segments);
  182. __translatePath(vobjects[vid],x.initial,y.initial);
  183. }
  184. else if( vobj['$type'].match(/\/Image$/) )
  185. {
  186. var w = parseFloat(vobj['width']['value']),
  187. h = parseFloat(vobj['height']['value']),
  188. src = __relativizeURL(vobj['src']['value']);
  189. vobjects[vid] = canvas.image(src,x.initial,y.initial,w,h);
  190. }
  191. else
  192. /* ignore layout constraints */
  193. continue;
  194. //set default values if not present
  195. if (!('orientation' in vobj)){
  196. vobj['orientation'] = {'type': 'double', 'value': 0};
  197. }
  198. if (!('scale' in vobj)){
  199. vobj['scale'] = {'type': 'list<double>', 'value': [1,1]};
  200. }
  201. var r = __getVobjGeomAttrVal(vobj['orientation']['value']),
  202. sx = __getVobjGeomAttrVal(vobj['scale']['value'][0])
  203. sy = __getVobjGeomAttrVal(vobj['scale']['value'][1]);
  204. vobjects[vid].attr(vobj['style']['value']);
  205. vobjects[vid].transform(
  206. 'r'+r.initial+','+x.initial+','+y.initial+
  207. 's'+sx.initial+','+sy.initial+','+x.initial+','+y.initial);
  208. vobjects[vid].node.setAttribute('__vobjuri',vid);
  209. vobjects[vid].node.setAttribute('__x',
  210. utils.buildVobjGeomAttrVal(x.initial, x.latest || 0));
  211. vobjects[vid].node.setAttribute('__y',
  212. utils.buildVobjGeomAttrVal(y.initial, y.latest || 0));
  213. vobjects[vid].node.setAttribute('__r',
  214. utils.buildVobjGeomAttrVal(r.initial, r.latest || 0));
  215. vobjects[vid].node.setAttribute('__sx',
  216. utils.buildVobjGeomAttrVal(sx.initial, sx.latest || 1));
  217. vobjects[vid].node.setAttribute('__sy',
  218. utils.buildVobjGeomAttrVal(sy.initial, sy.latest || 1));
  219. __setVobjectTransform(vobjects[vid]);
  220. icon.push(vobjects[vid]);
  221. }
  222. if( utils.keys(im.nodes).length == 0 )
  223. {
  224. icon.push( __canvas.point(0,0) );
  225. icon.setAttr('class','empty_icon');
  226. }
  227. if( options != undefined )
  228. {
  229. if( 'size' in options )
  230. {
  231. var size = options['size'],
  232. bbox = icon.getBBox();
  233. scaleBy = Math.min(size/bbox.width,size/bbox.height);
  234. icon.scale( scaleBy, scaleBy );
  235. }
  236. if( 'wrap' in options )
  237. {
  238. var bbox = icon.getBBox(),
  239. size = Math.max(bbox.width, bbox.height);
  240. icon.translate( -bbox.x, -bbox.y );
  241. bbox = icon.getBBox();
  242. if( bbox.width > bbox.height )
  243. icon.translate( 0, (size/2 - bbox.height/2) );
  244. else
  245. icon.translate( (size/2 - bbox.width/2), 0 );
  246. icon.translate( 1, 1 );
  247. canvas.setSize( size+2, size+2);
  248. }
  249. if( 'id' in options )
  250. {
  251. var id = options['id'];
  252. __icons[id] =
  253. {'icon':icon,
  254. 'vobjects':vobjects,
  255. 'edgesIn':[],
  256. 'edgesOut':[]};
  257. icon.setAttr('__csuri',id);
  258. icon.setAttr('__x',0);
  259. icon.setAttr('__y',0);
  260. icon.setAttr('__r',0);
  261. icon.setAttr('__sx',1);
  262. icon.setAttr('__sy',1);
  263. }
  264. if( 'attrs' in options )
  265. {
  266. var attrs = options['attrs'];
  267. for( var attr in attrs )
  268. icon.setAttr(attr,attrs[attr]);
  269. }
  270. if( 'behaviours' in options )
  271. {
  272. icon.setAttr('class','clickable');
  273. icon.node.onmousedown =
  274. function(event)
  275. {
  276. if( event.button == 0 )
  277. BehaviorManager.handleUserEvent(__EVENT_LEFT_PRESS_ICON,event);
  278. else if( event.button == 2 )
  279. BehaviorManager.handleUserEvent(__EVENT_RIGHT_PRESS_ICON,event);
  280. };
  281. icon.node.onmouseup =
  282. function(event)
  283. {
  284. if( event.button == 0 )
  285. {
  286. if( event.shiftKey )
  287. BehaviorManager.handleUserEvent(__EVENT_SHIFT_LEFT_RELEASE_ICON,event);
  288. else
  289. BehaviorManager.handleUserEvent(__EVENT_LEFT_RELEASE_ICON,event);
  290. }
  291. else if( event.button == 2 )
  292. BehaviorManager.handleUserEvent(__EVENT_RIGHT_RELEASE_ICON,event);
  293. else if( event.button == 1 )
  294. {
  295. if( event.shiftKey )
  296. BehaviorManager.handleUserEvent(__EVENT_SHIFT_MIDDLE_RELEASE_ICON,event);
  297. else
  298. BehaviorManager.handleUserEvent(__EVENT_MIDDLE_RELEASE_ICON,event);
  299. }
  300. };
  301. icon.node.onmousewheel =
  302. function(event)
  303. {
  304. if( event.shiftKey )
  305. {
  306. BehaviorManager.handleUserEvent(__EVENT_SHIFT_WHEEL_ICON,event);
  307. return false;
  308. }
  309. };
  310. /*icon.node.onmouseover =
  311. function(event)
  312. {
  313. BehaviorManager.handleUserEvent(__EVENT_MOUSE_OVER_ICON,event);
  314. };
  315. icon.node.onmouseout =
  316. function(event)
  317. {
  318. BehaviorManager.handleUserEvent(__EVENT_MOUSE_OUT_ICON,event);
  319. };*/
  320. }
  321. }
  322. return icon;
  323. };
  324. /**
  325. * Compile the current model to an Abstract Syntax Metamodel
  326. */
  327. this.compileToASMM = function(fname){
  328. if( ! __isAbstractSyntaxMetamodel(fname) )
  329. WindowManagement.openDialog(
  330. _ERROR,
  331. 'invalid extension... abstract syntax metamodels are "*.metamodel" files');
  332. else
  333. HttpUtils.httpReq('PUT', HttpUtils.url(fname,__FORCE_GET));
  334. };
  335. /**
  336. * Compile the current model to a Concrete Syntax Metamodel
  337. */
  338. this.compileToCSMM = function(fname){
  339. if( ! __isIconMetamodel(fname) )
  340. WindowManagement.openDialog(
  341. _ERROR,
  342. 'invalid extension... icon definition metamodels are "*Icons.metamodel" files');
  343. else
  344. HttpUtils.httpReq('PUT', HttpUtils.url(fname,__FORCE_GET));
  345. };
  346. /**
  347. * Compiles the current model to an Icon Pattern Metamodel
  348. */
  349. this.compileToPatternMM = function(fname){
  350. if( ! __isAbstractSyntaxMetamodel(fname) )
  351. WindowManagement.openDialog(
  352. _ERROR,
  353. 'invalid extension... abstract syntax metamodels are "*.metamodel" files');
  354. else
  355. {
  356. var patternmm = fname.substring(0,fname.length-'metamodel'.length)+'pattern.metamodel';
  357. HttpUtils.httpReq('PUT', HttpUtils.url(patternmm,__FORCE_GET));
  358. }
  359. };
  360. return this;
  361. }();