~cdparra/gelee/trunk

« back to all changes in this revision

Viewing changes to webui/editor/draw2d/Figure.js

  • Committer: parra
  • Date: 2010-03-15 15:56:56 UTC
  • Revision ID: svn-v4:ac5bba68-f036-4e09-846e-8f32731cc928:trunk/gelee:1448
merged gelee at svn

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This notice must be untouched at all times.
 
2
 
 
3
Open-jACOB Draw2D
 
4
The latest version is available at
 
5
http://www.openjacob.org
 
6
 
 
7
Copyright (c) 2006 Andreas Herz. All rights reserved.
 
8
Created 5. 11. 2006 by Andreas Herz (Web: http://www.freegroup.de )
 
9
 
 
10
LICENSE: LGPL
 
11
 
 
12
This library is free software; you can redistribute it and/or
 
13
modify it under the terms of the GNU Lesser General Public
 
14
License (LGPL) as published by the Free Software Foundation; either
 
15
version 2.1 of the License, or (at your option) any later version.
 
16
 
 
17
This library is distributed in the hope that it will be useful,
 
18
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
20
Lesser General Public License for more details.
 
21
 
 
22
You should have received a copy of the GNU Lesser General Public
 
23
License along with this library; if not, write to the Free Software
 
24
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA,
 
25
or see http://www.gnu.org/copyleft/lesser.html
 
26
*/
 
27
 
 
28
/**
 
29
 * 
 
30
 * @version 0.8.7
 
31
 * @author Andreas Herz
 
32
 * @constructor
 
33
 */
 
34
draw2d.Figure=function()
 
35
{
 
36
  this.construct();
 
37
}
 
38
/** @private **/
 
39
draw2d.Figure.prototype.type="Figure";
 
40
 
 
41
/** @private **/
 
42
draw2d.Figure.ZOrderBaseIndex = 100;
 
43
 
 
44
/**
 
45
 * Set the common z-index of the window element. This method exists for
 
46
 * compatibility reason to dojo or another UI javascript library. 
 
47
 * It is now possible to arange the draw2d elements behind/before other UI elements-
 
48
 *
 
49
 * @see #setZOrder
 
50
 * @static
 
51
 * @param {int} index The z-order for all new figure objects.
 
52
 **/
 
53
draw2d.Figure.setZOrderBaseIndex=function(/*:int*/ index)
 
54
{
 
55
  draw2d.Figure.ZOrderBaseIndex = index;
 
56
}
 
57
 
 
58
/**
 
59
 * @private
 
60
 **/
 
61
draw2d.Figure.prototype.construct=function()
 
62
{
 
63
  this.lastDragStartTime =0;
 
64
  /** @private **/
 
65
  this.x   = 0; /*int*/
 
66
  /** @private **/
 
67
  this.y   = 0; /*:int*/
 
68
  /** @private **/
 
69
  this.border=null;  /*:draw2d.Border*/
 
70
  this.setDimension(10,10);
 
71
  /** @private **/
 
72
  this.id   = this.generateUId();  /*:String*/
 
73
  /** @private **/
 
74
  this.html = this.createHTMLElement(); /*:HTMLElement*/
 
75
  /** @private **/
 
76
  this.canvas = null;    /*:draw2d.Canvas*/ 
 
77
  /** @private **/
 
78
  this.workflow = null;  /*:draw2d.Workflow*/
 
79
  /** @private **/
 
80
  this.draggable = null; /*:HTMLElement*/
 
81
  /** @private **/
 
82
  this.parent    = null; /*:draw2d.CompartmentFigure*/
 
83
  /** @private **/
 
84
  this.isMoving  = false; /*:boolean*/
 
85
  /** @private **/
 
86
  this.canSnapToHelper = true; /*:boolean*/
 
87
  /** @private **/
 
88
  this.snapToGridAnchor = new draw2d.Point(0,0);
 
89
  /** @private **/
 
90
  this.timer = -1; // Fadein/Fadeout timer id.
 
91
 
 
92
  // It is important to set the flags below. Otherwise the flags will be <null>
 
93
  //
 
94
  this.setDeleteable(true);
 
95
  this.setCanDrag(true);
 
96
  this.setResizeable(true);
 
97
  this.setSelectable(true);
 
98
 
 
99
  // a figure can store additional, user defined properties
 
100
  //
 
101
  this.properties = new Object(); /*:Map<name,value>*/
 
102
 
 
103
  // Hier werden Object registriert welche informiert werden wollen wenn sich dieses
 
104
  // Object bewegt hat.
 
105
  //
 
106
  this.moveListener = new draw2d.ArrayList();
 
107
}
 
108
 
 
109
/**
 
110
 * Override this method to free your resource too.
 
111
 *
 
112
 * @private
 
113
 **/
 
114
draw2d.Figure.prototype.dispose=function()
 
115
{
 
116
  //this.id   = null; don't dispose the id! This is important for deregistration
 
117
  //this.html = null; don't dispose the html! This is important for deregistration
 
118
  this.canvas = null;
 
119
  this.workflow = null;
 
120
  this.moveListener = null;
 
121
  if(this.draggable!=null)
 
122
  {
 
123
    this.draggable.removeEventListener("mouseenter", this.tmpMouseEnter);
 
124
    this.draggable.removeEventListener("mouseleave", this.tmpMouseLeave);
 
125
    this.draggable.removeEventListener("dragend", this.tmpDragend);
 
126
    this.draggable.removeEventListener("dragstart",this.tmpDragstart );
 
127
    this.draggable.removeEventListener("drag",this.tmpDrag);
 
128
    this.draggable.removeEventListener("dblclick",this.tmpDoubleClick );
 
129
    this.draggable.node = null;
 
130
  }
 
131
  this.draggable = null;
 
132
  if(this.border!=null)
 
133
    this.border.dispose();
 
134
  this.border = null;
 
135
 
 
136
  // remove this figure from the parent CompartmentFigure
 
137
  //
 
138
  if(this.parent!=null)
 
139
    this.parent.removeChild(this);
 
140
}
 
141
 
 
142
 
 
143
 
 
144
 
 
145
/**
 
146
 * A figure can store user defined attributes. This method returns all properties stored in this figure.<br>
 
147
 *
 
148
 * @see #setProperty
 
149
 * @returns All user defined properties of the figure
 
150
 * @type Map
 
151
 **/
 
152
draw2d.Figure.prototype.getProperties=function()
 
153
{
 
154
  return this.properties;
 
155
}
 
156
 
 
157
/**
 
158
 * A figure can store user defined attributes. This method returns the requested property.<br>
 
159
 *
 
160
 * @see #setProperty
 
161
 * @returns The user defined property of this figure.
 
162
 * @type String
 
163
 **/
 
164
draw2d.Figure.prototype.getProperty=function(/*:String*/ key)
 
165
{
 
166
  return this.properties[key];
 
167
}
 
168
 
 
169
 
 
170
/**
 
171
 * A figure can store any type of information. You can use this to attach any String or Object to this
 
172
 * figure.
 
173
 *
 
174
 * @see #getProperty
 
175
 * @param {String} key The key of the property.
 
176
 * @param {String} value The value of the property.
 
177
 **/
 
178
draw2d.Figure.prototype.setProperty=function(/*:String*/ key,/*:String*/ value)
 
179
{
 
180
  this.properties[key]=value;
 
181
  this.setDocumentDirty();
 
182
}
 
183
 
 
184
/**
 
185
 * Return the document unique id of this element. It is not an uuid or guid
 
186
 * @type String
 
187
 **/
 
188
draw2d.Figure.prototype.getId=function()
 
189
{
 
190
  return this.id;
 
191
}
 
192
 
 
193
 
 
194
/**
 
195
 * @private
 
196
 * @param {draw2d.Canvas} canvas
 
197
 **/
 
198
draw2d.Figure.prototype.setCanvas= function(/*:draw2d.Canvas*/ canvas)
 
199
{
 
200
  this.canvas = canvas;
 
201
}
 
202
 
 
203
/**
 
204
 * @type Workflow
 
205
 **/
 
206
draw2d.Figure.prototype.getWorkflow=function()
 
207
{
 
208
   return this.workflow;
 
209
}
 
210
 
 
211
/**
 
212
 * @private
 
213
 * @param {draw2d.Workflow} workflow
 
214
 **/
 
215
draw2d.Figure.prototype.setWorkflow= function(/*:draw2d.Workflow*/ workflow)
 
216
{
 
217
  // The parent is a Workflow class - now we create the Drag-Objekt
 
218
  //
 
219
  if(this.draggable==null)
 
220
  {
 
221
    // Firefox seems to need to have the tabindex="0" property set to some value 
 
222
    // so it knows this Div or Span is keyboard selectable. That allows the keyboard 
 
223
    // event to be triggered. It is not so dumb - you might want to trap Delete or 
 
224
    // Insert keys on a figure etc. 
 
225
    this.html.tabIndex="0";
 
226
 
 
227
    var oThis = this;
 
228
 
 
229
    this.keyDown=function(event)
 
230
    {
 
231
      event.cancelBubble = true; // Stop event propagation
 
232
      event.returnValue = true;  // Execute the standard event for this event. Important for Input Fields/Dialogs
 
233
      oThis.onKeyDown(event.keyCode, event.ctrlKey);
 
234
    }
 
235
    if (this.html.addEventListener) 
 
236
      this.html.addEventListener("keydown", this.keyDown, false);
 
237
    else if (this.html.attachEvent) 
 
238
      this.html.attachEvent("onkeydown", this.keyDown);
 
239
 
 
240
    this.draggable = new draw2d.Draggable(this.html, draw2d.Draggable.DRAG_X | draw2d.Draggable.DRAG_Y);
 
241
    this.draggable.node = this;
 
242
    this.tmpContextMenu = function (oEvent){oThis.onContextMenu(oThis.x+oEvent.x, oEvent.y+oThis.y);};
 
243
    this.tmpMouseEnter  = function (oEvent){oThis.onMouseEnter();};
 
244
    this.tmpMouseLeave  = function (oEvent){oThis.onMouseLeave();};
 
245
    this.tmpDragend     = function (oEvent){oThis.onDragend();};
 
246
    this.tmpDragstart   = function (oEvent){
 
247
       var w = oThis.workflow;
 
248
       w.showMenu(null);
 
249
       if(oThis.workflow.toolPalette && oThis.workflow.toolPalette.activeTool)
 
250
       {
 
251
          oEvent.returnValue = false;
 
252
          oThis.workflow.onMouseDown(oThis.x+oEvent.x, oEvent.y+oThis.y);
 
253
          oThis.workflow.onMouseUp(oThis.x+oEvent.x, oEvent.y+oThis.y);
 
254
          return;
 
255
       }
 
256
       oEvent.returnValue = oThis.onDragstart(oEvent.x,oEvent.y);
 
257
    };
 
258
    this.tmpDrag        = function (oEvent){oThis.onDrag();};
 
259
    this.tmpDoubleClick = function (oEvent){oThis.onDoubleClick();};
 
260
 
 
261
    this.draggable.addEventListener("contextmenu", this.tmpContextMenu);
 
262
    this.draggable.addEventListener("mouseenter", this.tmpMouseEnter);
 
263
    this.draggable.addEventListener("mouseleave", this.tmpMouseLeave);
 
264
    this.draggable.addEventListener("dragend", this.tmpDragend);
 
265
    this.draggable.addEventListener("dragstart",this.tmpDragstart );
 
266
    this.draggable.addEventListener("drag",this.tmpDrag);
 
267
    this.draggable.addEventListener("dblclick",this.tmpDoubleClick );
 
268
  }
 
269
  this.workflow = workflow;
 
270
}
 
271
 
 
272
/**
 
273
 * @private
 
274
 **/
 
275
draw2d.Figure.prototype.createHTMLElement=function()
 
276
{
 
277
    var item = document.createElement('div');
 
278
    item.id        = this.id;
 
279
    item.style.position="absolute";
 
280
    item.style.left   = this.x+"px";
 
281
    item.style.top    = this.y+"px";
 
282
    item.style.height = this.width+"px";
 
283
    item.style.width  = this.height+"px";
 
284
    item.style.margin = "0px";
 
285
    item.style.padding= "0px";
 
286
    item.style.outline= "none";
 
287
    item.style.zIndex = ""+draw2d.Figure.ZOrderBaseIndex;
 
288
 
 
289
    return item;
 
290
}
 
291
 
 
292
 
 
293
/**
 
294
 * Set the parent of this figure.
 
295
 * Don't call them manually. Is CompartmentFigre.appendChild() instead.
 
296
 
 
297
 * @param {draw2d.CompartmentFigure} parent The new parent of this figure
 
298
 * @private
 
299
 **/
 
300
draw2d.Figure.prototype.setParent=function(/*:draw2d.CompartmentFigure*/ parent)
 
301
{
 
302
  this.parent = parent;
 
303
}
 
304
 
 
305
/**
 
306
 * Get the parent of this figure.
 
307
 *
 
308
 * @type draw2d.CompartmentFigure
 
309
 **/
 
310
draw2d.Figure.prototype.getParent=function()
 
311
{
 
312
  return this.parent;
 
313
}
 
314
 
 
315
 
 
316
/**
 
317
 * @return Returns the z-index of the element.
 
318
 * @type int
 
319
 **/
 
320
draw2d.Figure.prototype.getZOrder=function()
 
321
{
 
322
    return this.html.style.zIndex;
 
323
}
 
324
 
 
325
/**
 
326
 * @param {int} index Set the new z-index of the element
 
327
 **/
 
328
draw2d.Figure.prototype.setZOrder=function(/*:int*/ index)
 
329
{
 
330
    this.html.style.zIndex=index;
 
331
}
 
332
 
 
333
 
 
334
/**
 
335
 * Return true if the origin of the Object is the window and not
 
336
 * the document. This is usefull if you want implement a window or a
 
337
 * dialog element. The element doesn't move if the user scroll the document.
 
338
 *
 
339
 * @returns Returns [true] if the origin of the object the window.
 
340
 * @type boolean
 
341
 **/
 
342
draw2d.Figure.prototype.hasFixedPosition=function()
 
343
{
 
344
  return false;
 
345
}
 
346
 
 
347
/**
 
348
 * This value is relevant for the interactive resize of the figure.
 
349
 *
 
350
 * @returns Returns the min width of this object.
 
351
 * @type int
 
352
 **/
 
353
draw2d.Figure.prototype.getMinWidth=function()
 
354
{
 
355
  return 5;
 
356
}
 
357
 
 
358
/**
 
359
 * This value is relevant for the interactive resize of the figure.
 
360
 *
 
361
 * @returns Returns the min height of this object.
 
362
 * @type int
 
363
 **/
 
364
draw2d.Figure.prototype.getMinHeight=function()
 
365
{
 
366
  return 5;
 
367
}
 
368
 
 
369
/**
 
370
 * @private
 
371
 **/
 
372
draw2d.Figure.prototype.getHTMLElement=function()
 
373
{
 
374
  if(this.html==null)
 
375
    this.html = this.createHTMLElement();
 
376
  return this.html;
 
377
}
 
378
 
 
379
/**
 
380
 * @see draw2d.Circle for an implementation.
 
381
 * @private
 
382
 **/
 
383
draw2d.Figure.prototype.paint=function()
 
384
{
 
385
  // called after the element has been added to the document
 
386
}
 
387
 
 
388
/**
 
389
 * @param {draw2d.Border} border Set the border for this figure
 
390
 **/
 
391
draw2d.Figure.prototype.setBorder=function(/*:draw2d.Border*/ border)
 
392
{
 
393
  if(this.border!=null)
 
394
    this.border.figure=null;
 
395
 
 
396
  this.border=border;
 
397
  this.border.figure=this;
 
398
  this.border.refresh();
 
399
  this.setDocumentDirty();
 
400
}
 
401
 
 
402
/**
 
403
 * Callback method for the context menu interaction.
 
404
 * Don't override this method! Implement getContextMenu instead.
 
405
 *
 
406
 * @see #getContextMenu
 
407
 * @private
 
408
 * @final
 
409
 * @param {int} x The absolute x coordinate of the right mouse button click
 
410
 * @param {int} y The absolute y coordinate of the right mouse button click
 
411
 **/
 
412
draw2d.Figure.prototype.onContextMenu=function(/*:int*/ x, /*:int*/y)
 
413
{
 
414
    var menu = this.getContextMenu();
 
415
    if(menu!=null)
 
416
      this.workflow.showMenu(menu,x,y);
 
417
}
 
418
 
 
419
/**
 
420
 * @returns null or the Menu object for this figure.
 
421
 * @type draw2d.Menu
 
422
 **/
 
423
draw2d.Figure.prototype.getContextMenu=function()
 
424
{
 
425
   return null;
 
426
}
 
427
 
 
428
/**
 
429
 * Callback method for the double click event of user interaction.
 
430
 * Sub classes can override this method to implement their own behaviour.
 
431
 **/
 
432
draw2d.Figure.prototype.onDoubleClick=function()
 
433
{
 
434
}
 
435
 
 
436
/**
 
437
 * Callback method for the mouse enter event. Usefull for mouse hover-effects.
 
438
 * Sub classes can override this method to implement their own behaviour.
 
439
 **/
 
440
draw2d.Figure.prototype.onMouseEnter=function()
 
441
{
 
442
}
 
443
 
 
444
 
 
445
/**
 
446
 * Callback method for the mouse leave event. Usefull for mouse hover-effects.
 
447
 * 
 
448
 **/
 
449
draw2d.Figure.prototype.onMouseLeave=function()
 
450
{
 
451
}
 
452
 
 
453
/**
 
454
 * Don't call them manually. This will be done by the framework.<br>
 
455
 * Will be called if the object are moved via drag and drop.
 
456
 * Sub classes can override this method to implement additional stuff. Don't forget to call
 
457
 * the super implementation via <code>Figure.prototype.onDrag.call(this);</code>
 
458
 * @private
 
459
 **/
 
460
draw2d.Figure.prototype.onDrag = function()
 
461
{
 
462
  this.x = this.draggable.getLeft();
 
463
  this.y = this.draggable.getTop();
 
464
 
 
465
  // enable the alpha blending o the first real move of the object
 
466
  //
 
467
  if(this.isMoving==false)
 
468
  {
 
469
   this.isMoving = true;
 
470
   this.setAlpha(0.5);
 
471
  }
 
472
  this.fireMoveEvent();
 
473
}
 
474
 
 
475
/**
 
476
 * Will be called after a drag and drop action.<br>
 
477
 * Sub classes can override this method to implement additional stuff. Don't forget to call
 
478
 * the super implementation via <code>Figure.prototype.onDragend.call(this);</code>
 
479
 * @private
 
480
 **/
 
481
draw2d.Figure.prototype.onDragend = function()
 
482
{
 
483
   if(this.getWorkflow().getEnableSmoothFigureHandling()==true)
 
484
   {
 
485
      var oFigure = this;
 
486
      var slowShow = function()
 
487
      {
 
488
         if(oFigure.alpha<1.0)
 
489
            oFigure.setAlpha(Math.min(1.0,oFigure.alpha+0.05));
 
490
         else
 
491
         {
 
492
            window.clearInterval(oFigure.timer);
 
493
            oFigure.timer = -1;
 
494
         }
 
495
      };
 
496
      if(oFigure.timer>0)
 
497
         window.clearInterval(oFigure.timer);
 
498
      oFigure.timer = window.setInterval(slowShow,20);
 
499
  }
 
500
  else
 
501
  {
 
502
      this.setAlpha(1.0);
 
503
  }
 
504
  // Element ist zwar schon an seine Position, das Command muss aber trotzdem
 
505
  // in dem CommandStack gelegt werden damit das Undo funktioniert.
 
506
  //
 
507
  this.command.setPosition(this.x, this.y);
 
508
  this.workflow.commandStack.execute(this.command);
 
509
  this.command = null;
 
510
  this.isMoving = false;
 
511
  this.workflow.hideSnapToHelperLines();
 
512
  this.fireMoveEvent();
 
513
}
 
514
 
 
515
/**
 
516
 * Will be called if the drag and drop action beginns. You can return [false] if you
 
517
 * want avoid the that the figure can be move.
 
518
 * 
 
519
 * @type boolean
 
520
 **/
 
521
draw2d.Figure.prototype.onDragstart = function(/*:int*/ x, /*:int*/ y)
 
522
{
 
523
  if(!this.canDrag)
 
524
    return false;
 
525
 
 
526
  this.command = new draw2d.CommandMove(this, this.x,this.y);
 
527
  return true;
 
528
}
 
529
 
 
530
/**
 
531
 * Switch on/off the drag drop behaviour of this object
 
532
 *
 
533
 * @param {boolean} flag The new drag drop indicator
 
534
 **/
 
535
draw2d.Figure.prototype.setCanDrag=function(/*:boolean*/flag)
 
536
{
 
537
  this.canDrag= flag;
 
538
  if(flag)
 
539
    this.html.style.cursor="move";
 
540
  else
 
541
    this.html.style.cursor=null;
 
542
}
 
543
 
 
544
/**
 
545
 * Set the alpha blending of this figure. 
 
546
 *
 
547
 * @param {float} percent Value between 0-1.
 
548
 **/
 
549
draw2d.Figure.prototype.setAlpha=function(/*:float 0-1*/ percent)
 
550
{
 
551
  if(this.alpha==percent)
 
552
     return;
 
553
  try
 
554
  {
 
555
   // FireFox
 
556
   this.html.style.MozOpacity=percent ;
 
557
  } 
 
558
  catch(exc){}
 
559
  try
 
560
  {
 
561
   // standard. Like Apple Safari Browser
 
562
   this.html.style.opacity=percent ;
 
563
  } 
 
564
  catch(exc){}
 
565
  try
 
566
  {
 
567
   // InternetExplorer
 
568
   var opacityValue = Math.round(percent * 100);
 
569
   // remove the alpha filter complete if we don't want any.
 
570
   if(opacityValue>=99)
 
571
      this.html.style.filter = "";
 
572
   else
 
573
      this.html.style.filter = "alpha(opacity=" + opacityValue + ")"; 
 
574
 
 
575
  } catch(exc){}
 
576
  this.alpha = percent;
 
577
}
 
578
 
 
579
/**
 
580
 * Set the new width and height of the figure. 
 
581
 *
 
582
 * @see #getMinWidth
 
583
 * @see #getMinHeight
 
584
 * @param {int} w The new width of the figure
 
585
 * @param {int} h The new height of the figure
 
586
 **/
 
587
draw2d.Figure.prototype.setDimension=function(/*:int*/ w, /*:int*/ h)
 
588
{
 
589
  this.width = Math.max(this.getMinWidth(),w);
 
590
  this.height= Math.max(this.getMinHeight(),h);
 
591
 
 
592
  // Falls das Element noch nie gezeichnet wurde, dann braucht aus das HTML nicht 
 
593
  // aktualisiert werden
 
594
  //
 
595
  if(this.html==null)
 
596
    return;
 
597
 
 
598
  this.html.style.width  = this.width+"px";
 
599
  this.html.style.height = this.height+"px";
 
600
 
 
601
  this.fireMoveEvent();
 
602
 
 
603
  // Update the resize handles if the user change the dimension via an API call
 
604
  //
 
605
  if(this.workflow!=null && this.workflow.getCurrentSelection()==this)
 
606
     this.workflow.showResizeHandles(this);
 
607
}
 
608
 
 
609
/**
 
610
 * Set the position of the object.
 
611
 *
 
612
 * @param {int} xPos The new x coordinate of the figure
 
613
 * @param {int} yPos The new y coordinate of the figure 
 
614
 **/
 
615
draw2d.Figure.prototype.setPosition=function(/*:int*/ xPos , /*:int*/ yPos )
 
616
{
 
617
//  this.x = Math.max(0,xPos);
 
618
//  this.y = Math.max(0,yPos);
 
619
  this.x= xPos;
 
620
  this.y= yPos;
 
621
  // Falls das Element noch nie gezeichnet wurde, dann braucht aus das HTML nicht 
 
622
  // aktualisiert werden
 
623
  //
 
624
  if(this.html==null)
 
625
    return;
 
626
 
 
627
  this.html.style.left = this.x+"px";
 
628
  this.html.style.top  = this.y+"px";
 
629
 
 
630
  this.fireMoveEvent();
 
631
 
 
632
  // Update the resize handles if the user change the position of the element via an API call.
 
633
  //
 
634
  if(this.workflow!=null && this.workflow.getCurrentSelection()==this)
 
635
     this.workflow.showResizeHandles(this);
 
636
}
 
637
 
 
638
/**
 
639
 * Returns the true if the figure can be resized.
 
640
 *
 
641
 * @see #setResizeable
 
642
 * @type boolean
 
643
 **/
 
644
draw2d.Figure.prototype.isResizeable=function()
 
645
{
 
646
  return this.resizeable;
 
647
}
 
648
 
 
649
/**
 
650
 * You can change the resizeable behaviour of this object. Hands over [false] and
 
651
 * the figure has no resizehandles if you select them with the mouse.<br>
 
652
 *
 
653
 * @see #getResizeable
 
654
 * @param {boolean} flag The resizeable flag.
 
655
 **/
 
656
draw2d.Figure.prototype.setResizeable=function(/*:boolean*/ flag)
 
657
{
 
658
  this.resizeable=flag;
 
659
}
 
660
 
 
661
/**
 
662
 * 
 
663
 * @type boolean
 
664
 **/
 
665
draw2d.Figure.prototype.isSelectable=function()
 
666
{
 
667
  return this.selectable;
 
668
}
 
669
 
 
670
 
 
671
/**
 
672
 * You can change the selectable behaviour of this object. Hands over [false] and
 
673
 * the figure has no selection handles if you try to select them with the mouse.<br>
 
674
 *
 
675
 * @param {boolean} flag The selectable flag.
 
676
 **/
 
677
draw2d.Figure.prototype.setSelectable=function(/*:boolean*/ flag)
 
678
{
 
679
  this.selectable=flag;
 
680
}
 
681
 
 
682
/**
 
683
 * Return true if the object doesn't care about the aspect ratio.
 
684
 * You can change the hight and width indipendent.
 
685
 * @type boolean
 
686
 */
 
687
draw2d.Figure.prototype.isStrechable=function()
 
688
{
 
689
  return true;
 
690
}
 
691
 
 
692
/**
 
693
 * Return false if you avoid that the user can delete your figure.
 
694
 * Sub class can override this method.
 
695
 * @type boolean
 
696
 **/
 
697
draw2d.Figure.prototype.isDeleteable=function()
 
698
{
 
699
  return this.deleteable;
 
700
}
 
701
 
 
702
/**
 
703
 * Return false if you avoid that the user can delete your figure.
 
704
 * 
 
705
 * @param {boolean} flag Enable or disable flag for the delete operation
 
706
 **/
 
707
draw2d.Figure.prototype.setDeleteable=function(/*:boolean */flag)
 
708
{
 
709
  this.deleteable = flag;
 
710
}
 
711
 
 
712
 
 
713
/**
 
714
 * Set the flag if this object can snap to grid or geometry.
 
715
 * A window of dialog should set this flag to false.
 
716
 * @param {boolean} flag The snap to grid/geometry enable flag.
 
717
 *
 
718
 **/
 
719
draw2d.Figure.prototype.setCanSnapToHelper=function(/*:boolean */flag)
 
720
{
 
721
  this.canSnapToHelper = flag;
 
722
}
 
723
 
 
724
/**
 
725
 * Returns true if the figure cna snap to any helper like a grid, guide, geometrie
 
726
 * or something else.
 
727
 *
 
728
 * @type boolean
 
729
 **/
 
730
draw2d.Figure.prototype.getCanSnapToHelper=function()
 
731
{
 
732
  return this.canSnapToHelper;
 
733
}
 
734
 
 
735
/**
 
736
 *
 
737
 * @type draw2d.Point
 
738
 **/
 
739
draw2d.Figure.prototype.getSnapToGridAnchor=function()
 
740
{
 
741
  return this.snapToGridAnchor;
 
742
}
 
743
 
 
744
/**
 
745
 *
 
746
 * @type draw2d.Point
 
747
 **/
 
748
draw2d.Figure.prototype.setSnapToGridAnchor=function(/*:draw2d.Point*/ point)
 
749
{
 
750
  this.snapToGridAnchor = point;
 
751
}
 
752
 
 
753
/**
 
754
 * @type draw2d.Dimension
 
755
 **/
 
756
draw2d.Figure.prototype.getBounds=function()
 
757
{
 
758
  return new draw2d.Dimension(this.getX(),this.getY(),this.getWidth(),this.getHeight());
 
759
}
 
760
 
 
761
 
 
762
/**
 
763
 * @type int
 
764
 **/
 
765
draw2d.Figure.prototype.getWidth=function()
 
766
{
 
767
  return this.width;
 
768
}
 
769
 
 
770
/**
 
771
 * @type int
 
772
 **/
 
773
draw2d.Figure.prototype.getHeight=function()
 
774
{
 
775
  return this.height;
 
776
}
 
777
 
 
778
/**
 
779
 * @returns The y-offset to the parent figure.
 
780
 * @type int
 
781
 **/
 
782
draw2d.Figure.prototype.getY=function()
 
783
{
 
784
    return this.y;
 
785
}
 
786
 
 
787
/**
 
788
 * @returns the x-offset to the parent figure
 
789
 * @type int
 
790
 **/
 
791
draw2d.Figure.prototype.getX=function()
 
792
{
 
793
    return this.x;
 
794
}
 
795
 
 
796
/**
 
797
 * @returns The Y coordinate in relation the Canvas.
 
798
 * @type int
 
799
 **/
 
800
draw2d.Figure.prototype.getAbsoluteY=function()
 
801
{
 
802
  return this.y;
 
803
}
 
804
 
 
805
/**
 
806
 * @returns The X coordinate in relation to the canvas
 
807
 * @type int
 
808
 **/
 
809
draw2d.Figure.prototype.getAbsoluteX=function()
 
810
{
 
811
  return this.x;
 
812
}
 
813
 
 
814
/**
 
815
 * This method will be called from the framework if the objects is selected and the user press any key.
 
816
 * Sub class can override this method to implement their own stuff.
 
817
 * 
 
818
 * @param {int} keyCode The code of the pressed key
 
819
 **/
 
820
draw2d.Figure.prototype.onKeyDown=function(/*:int*/ keyCode, /*:boolean*/ ctrl)
 
821
{
 
822
  if(keyCode==46 && this.isDeleteable()==true)
 
823
  {
 
824
    this.workflow.commandStack.execute(new draw2d.CommandDelete(this));
 
825
  }
 
826
 
 
827
  // redirect any CTRL key strokes to the parent workflow/canvas
 
828
  //
 
829
  if(ctrl)
 
830
  {
 
831
     this.workflow.onKeyDown(keyCode,ctrl);
 
832
  }
 
833
}
 
834
 
 
835
/**
 
836
 * Returns the position of the figure.
 
837
 *
 
838
 * @type draw2d.Point
 
839
 * @deprecated
 
840
 **/
 
841
draw2d.Figure.prototype.getPosition=function()
 
842
{
 
843
  return new draw2d.Point(this.x, this.y);
 
844
}
 
845
 
 
846
 
 
847
draw2d.Figure.prototype.isOver = function (/*:int*/ iX ,/*:int*/ iY)
 
848
{
 
849
    var x = this.getAbsoluteX();
 
850
    var y = this.getAbsoluteY();
 
851
    var iX2 = x + this.width;
 
852
    var iY2 = y + this.height;
 
853
    return (iX >= x && iX <= iX2 && iY >= y && iY <= iY2);
 
854
}
 
855
 
 
856
/**
 
857
 * @param {draw2d.Figure} figure The figure to monitor
 
858
 *
 
859
 **/
 
860
draw2d.Figure.prototype.attachMoveListener = function(/*:draw2d.Figure*/ figure)
 
861
{
 
862
  if(figure==null || this.moveListener==null)
 
863
    return;
 
864
 
 
865
  this.moveListener.add(figure);
 
866
}
 
867
 
 
868
 
 
869
/**
 
870
 * @param {draw2d.Figure} figure The figure to remove the monitor
 
871
 *
 
872
 **/
 
873
draw2d.Figure.prototype.detachMoveListener = function(/*:draw2d.Figure*/ figure) 
 
874
{
 
875
  if(figure==null || this.moveListener==null)
 
876
    return;
 
877
 
 
878
  this.moveListener.remove(figure);
 
879
}
 
880
 
 
881
/**
 
882
 * @private
 
883
 **/
 
884
draw2d.Figure.prototype.fireMoveEvent=function()
 
885
{
 
886
  this.setDocumentDirty();
 
887
  var size= this.moveListener.getSize();
 
888
  for(var i=0;i<size;i++)
 
889
  {
 
890
    this.moveListener.get(i).onOtherFigureMoved(this);
 
891
  }
 
892
}
 
893
 
 
894
 
 
895
/**
 
896
 * Falls man sich zuvor an einem Object mit attacheMoveListener(..) registriert hat,
 
897
 * wird man hierüber dann informiert wenn sich das Objekt bewegt hat.
 
898
 *
 
899
 * @param {draw2d.Figure} figure The figure which has changed its position
 
900
 * @private
 
901
 */
 
902
draw2d.Figure.prototype.onOtherFigureMoved=function(/*:draw2d.Figure*/ figure)
 
903
{
 
904
}
 
905
 
 
906
/**
 
907
 * This method will be called if the figure has changed any postion, color, dimension or something else.
 
908
 *
 
909
 * @private
 
910
 **/
 
911
draw2d.Figure.prototype.setDocumentDirty=function()
 
912
{
 
913
  if(this.workflow!=null)
 
914
    this.workflow.setDocumentDirty();
 
915
}
 
916
 
 
917
 
 
918
/**
 
919
 * @private
 
920
 * @returns String
 
921
 **/
 
922
draw2d.Figure.prototype.generateUId=function() 
 
923
{
 
924
  var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
 
925
  var string_length = 10;
 
926
  var maxTry = 10;
 
927
  nbTry = 0
 
928
  while (nbTry < 1000) 
 
929
  {
 
930
      var id = '';
 
931
      // generate string
 
932
      for (var i=0; i<string_length; i++) 
 
933
      {
 
934
          var rnum = Math.floor(Math.random() * chars.length);
 
935
          id += chars.substring(rnum,rnum+1);
 
936
      }
 
937
      // check if there
 
938
      elem = document.getElementById(id);
 
939
      if (!elem)
 
940
          return id
 
941
      nbTry += 1
 
942
  }
 
943
  return null
 
944
}
 
945
 
 
946
 
 
947
/**
 
948
 * Utility function to disable text selection on the handsover element
 
949
 *
 
950
 * @private
 
951
 **/
 
952
draw2d.Figure.prototype.disableTextSelection=function(/*:HTMLElement*/ e)
 
953
{
 
954
   // disable text selection
 
955
   //
 
956
   if (typeof e.onselectstart!="undefined") //IE route
 
957
      e.onselectstart=function(){return false}
 
958
   else if (typeof e.style.MozUserSelect!="undefined") //Firefox route
 
959
      e.style.MozUserSelect="none"
 
960
}
 
961