My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
* Copyright 2010-2011 Hydro4GE, Incorporated. http://www.hydro4ge.com/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hydro4ge.raphaelgwt.client;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Widget;
import java.util.ArrayList;

public class Raphael extends Widget {

private RaphaelJS overlay;
private final ArrayList<Shape> shapes = new ArrayList<Shape>();
private boolean attached = true;

public Raphael(int width, int height) {
super();
Element raphaelDiv = DOM.createDiv();
setElement(raphaelDiv);
overlay = RaphaelJS.create(raphaelDiv, width, height);
}

public static boolean isSupported() {
return RaphaelJS.isDefined();
}

public void clear() {
overlay.clear();
shapes.clear();
}

public void setSize(int width, int height) {
overlay.setSize(width, height);
}

/**
* attach/detach our children explicitly here since they were
* initially attached via JavaScript outside of GWT framework
*/
@Override
protected void doAttachChildren() {
super.doAttachChildren();
if (!this.attached) {
for (Shape s : shapes) {
s.doAttach();
}
this.attached = true;
}
}
@Override
protected void doDetachChildren() {
super.doDetachChildren();
if (this.attached) {
for (Shape s : shapes) {
s.doDetach();
}
this.attached = false;
}
}

public class Set {
protected RaphaelJS.Set set;
private final ArrayList<Shape> sh = new ArrayList<Shape>();
public Set() {
this.set = overlay.set();
}
public Set push(Shape s) {
set.push(s.el);
sh.add(s);
return this;
}
public Set animate(JSONObject newAttrs, int duration) {
set.animate(newAttrs.getJavaScriptObject(), duration);
return this;
}
public Set animate(JSONObject newAttrs, int duration, AnimationCallback callback) {
set.animate(newAttrs.getJavaScriptObject(), duration, callback);
return this;
}
public Set animate(JSONObject newAttrs, int duration, String easing) {
set.animate(newAttrs.getJavaScriptObject(), duration, easing);
return this;
}
public Set animate(JSONObject newAttrs, int duration, String easing, AnimationCallback callback) {
set.animate(newAttrs.getJavaScriptObject(), duration, easing, callback);
return this;
}
public Set attr(String attributeName, String value) {
set.attr(attributeName, value);
return this;
}
public Set attr(String attributeName, double value) {
set.attr(attributeName, value);
return this;
}
public Set attr(JSONObject params) {
set.attr(params.getJavaScriptObject());
return this;
}
public double attrAsDouble(String name) {
return set.attrAsDouble(name);
}
public String attrAsString(String name) {
return set.attrAsString(name);
}
public JSONObject attr(JSONArray attributeNames) {
return new JSONObject(set.attr(attributeNames.getJavaScriptObject()));
}
public BBox getBBox() {
return set.getBBox();
}
public void remove() {
set.remove();
}
public Set rotate(double degree) {
set.rotate(degree);
return this;
}
public Set rotate(double degree, boolean isAbsolute) {
set.rotate(degree, isAbsolute);
return this;
}
public Set rotate(double degree, double cx, double cy) {
set.rotate(degree, cx, cy);
return this;
}
public Set rotate(double degree, double cx, double cy, boolean isAbsolute) {
for (Shape s : sh)
s.rotate(degree, cx, cy, isAbsolute);
return this;
}
public Set scale(double sx, double sy) {
set.scale(sx, sy);
return this;
}
public Set scale(double sx, double sy, double cx, double cy) {
set.scale(sx, sy, cx, cy);
return this;
}
public Set toFront() {
set.toFront();
return this;
}
public Set toBack() {
set.toBack();
return this;
}
public Set translate(double dx, double dy) {
set.translate(dx, dy);
return this;
}
}

public class Shape extends Widget {
protected RaphaelJS.Element el;
protected double rot = 0;
protected Shape(RaphaelJS.Element obj) {
super();
setElement(obj.node());
shapes.add(this);
el = obj;
onAttach(); // signal that the widget has been attached
}

/**
* this is ugly, but necessary for the parent Raphael widget
* to attach/detach this widget from the DOM, because the
* onAttach()/onDetach() methods are protected.
*/
public void doAttach() {
onAttach();
}
public void doDetach() {
onDetach();
}

public Shape animate(JSONObject newAttrs, int duration) {
el.animate(newAttrs.getJavaScriptObject(), duration);
return this;
}
public Shape animate(JSONObject newAttrs, int duration, AnimationCallback callback) {
el.animate(newAttrs.getJavaScriptObject(), duration, callback);
return this;
}
public Shape animate(JSONObject newAttrs, int duration, String easing) {
el.animate(newAttrs.getJavaScriptObject(), duration, easing);
return this;
}
public Shape animate(JSONObject newAttrs, int duration, String easing, AnimationCallback callback) {
el.animate(newAttrs.getJavaScriptObject(), duration, easing, callback);
return this;
}

public Shape animateWith(Shape shape, JSONObject newAttrs, int duration) {
el.animateWith(shape.el, newAttrs.getJavaScriptObject(), duration);
return this;
}
public Shape animateWith(Shape shape, JSONObject newAttrs, int duration, AnimationCallback callback) {
el.animateWith(shape.el, newAttrs.getJavaScriptObject(), duration, callback);
return this;
}
public Shape animateWith(Shape shape, JSONObject newAttrs, int duration, String easing) {
el.animateWith(shape.el, newAttrs.getJavaScriptObject(), duration, easing);
return this;
}
public Shape animateWith(Shape shape, JSONObject newAttrs, int duration, String easing, AnimationCallback callback) {
el.animateWith(shape.el, newAttrs.getJavaScriptObject(), duration, easing, callback);
return this;
}

public Shape animateAlong(Path path, int duration) {
el.animateAlong(path.el, duration);
return this;
}
public Shape animateAlong(Path path, int duration, boolean rotate) {
el.animateAlong(path.el, duration, rotate);
return this;
}
public Shape animateAlong(Path path, int duration, boolean rotate, AnimationCallback callback) {
el.animateAlong(path.el, duration, rotate, callback);
return this;
}

public Shape animateAlongBack(Path path, int duration) {
el.animateAlongBack(path.el, duration);
return this;
}
public Shape animateAlongBack(Path path, int duration, boolean rotate) {
el.animateAlongBack(path.el, duration, rotate);
return this;
}
public Shape animateAlongBack(Path path, int duration, boolean rotate, AnimationCallback callback) {
el.animateAlongBack(path.el, duration, rotate, callback);
return this;
}

public Shape attr(String attributeName, String value) {
el.attr(attributeName, value);
return this;
}
public Shape attr(String attributeName, double value) {
el.attr(attributeName, value);
return this;
}
public Shape attr(JSONObject params) {
el.attr(params.getJavaScriptObject());
return this;
}
public double attrAsDouble(String name) {
return el.attrAsDouble(name);
}
public String attrAsString(String name) {
return el.attrAsString(name);
}
public JSONObject attr(JSONArray attributeNames) {
return new JSONObject(el.attr(attributeNames.getJavaScriptObject()));
}

public void drag(DragCallback callback) {
el.drag(callback);
}

public BBox getBBox() {
return el.getBBox();
}

public void hide() {
el.hide();
}

public void remove() {
el.remove();
}

public Shape rotate(double degree) {
rot = degree;
el.rotate(degree);
return this;
}
public Shape rotate(double degree, boolean isAbsolute) {
if (isAbsolute)
rot = degree;
else
rot += degree;
el.rotate(degree, isAbsolute);
return this;
}
public Shape rotate(double degree, double cx, double cy) {
rot = degree;
el.rotate(degree, cx, cy);
return this;
}
public Shape rotate(double degree, double cx, double cy, boolean isAbsolute) {
if (isAbsolute)
rot = degree;
else
rot += degree;
el.rotate(rot, cx, cy);
return this;
}

public Shape scale(double sx, double sy) {
el.scale(sx, sy);
return this;
}
public Shape scale(double sx, double sy, double cx, double cy) {
el.scale(sx, sy, cx, cy);
return this;
}

public void show() {
el.show();
}

public Shape toFront() {
el.toFront();
return this;
}

public Shape toBack() {
el.toBack();
return this;
}

public Shape translate(double dx, double dy) {
el.translate(dx, dy);
return this;
}

}

public class Circle extends Shape {
public Circle(double x, double y, double r) {
super(overlay.circle(x, y, r));
}
}

public class Text extends Shape {
public Text(double x, double y, String text) {
super(overlay.text(x, y, text));
}
}

public class Rect extends Shape {
public Rect(double x, double y, double w, double h) {
super(overlay.rect(x, y, w, h));
}
public Rect(double x, double y, double w, double h, double r) {
super(overlay.rect(x, y, w, h, r));
}
}

public class Ellipse extends Shape {
public Ellipse(double x, double y, double rx, double ry) {
super(overlay.ellipse(x, y, rx, ry));
}
}

public class Image extends Shape {
public Image(String src, double x, double y, double width, double height) {
super(overlay.image(src, x, y, width, height));
}
}

public class Path extends Shape {
public Path() {
super(overlay.path());
}

public Path(String pathString) {
super(overlay.path(pathString));
}

public Path(PathBuilder builder) {
this(builder.toString());
}

public double getTotalLength() {
return ((RaphaelJS.Path)el).getTotalLength();
}

public Point getPointAtLength(double length) {
return ((RaphaelJS.Path)el).getPointAtLength(length);
}

public String getSubpath(double from, double to) {
return ((RaphaelJS.Path)el).getSubpath(from, to);
}
}

}

Change log

r42 by geoffspeicher on Nov 14, 2011   Diff
clear shapes ArrayList when the widget is
cleared

this fixes the memory leak reported in
 Issue 19 

Thanks-to: armin.kanitsar
Go to: 
Project members, sign in to write a code review

Older revisions

r40 by geoffspeicher on Oct 29, 2011   Diff
Explicity mark Java sources with
appropriate copyright information.
This should have been done long ago
and it recently came to my attention
that it is only made more necessary
...
r38 by geoffspeicher on Oct 29, 2011   Diff
support for Raphael's builtin
drag-n-drop ( issue #18 )
- new DragCallback class
- new drag() method on
Raphael+RaphaelJS
...
r35 by geoffspeicher on Oct 13, 2011   Diff
uncomment Raphael.getSubpath() and
change its parameter types
from int to double since testing shows
that it seems to work
All revisions of this file

File info

Size: 11486 bytes, 408 lines
Powered by Google Project Hosting