forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxStencilShape.java
More file actions
832 lines (728 loc) · 17.8 KB
/
mxStencilShape.java
File metadata and controls
832 lines (728 loc) · 17.8 KB
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
/**
* Copyright (c) 2010-2012, JGraph Ltd
*/
package com.mxgraph.shape;
import org.w3c.dom.Node;
import com.mxgraph.canvas.mxGraphics2DCanvas;
import com.mxgraph.util.mxUtils;
import com.mxgraph.util.mxXmlUtils;
import com.mxgraph.util.svg.AWTPathProducer;
import com.mxgraph.util.svg.AWTPolygonProducer;
import com.mxgraph.util.svg.AWTPolylineProducer;
import com.mxgraph.util.svg.CSSConstants;
import com.mxgraph.util.svg.ExtendedGeneralPath;
import com.mxgraph.view.mxCellState;
import java.awt.Color;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
* Stencil shape drawing that takes an XML definition of the shape and renders
* it.
*
* See http://projects.gnome.org/dia/custom-shapes for specs. See
* http://dia-installer.de/shapes_de.html for shapes.
*/
public class mxStencilShape extends mxBasicShape
{
private static final Logger log = Logger.getLogger(mxStencilShape.class.getName());
public mxStencilShape()
{
super();
}
protected GeneralPath shapePath;
/**
* Reference to the root node of the Dia shape description.
*/
protected Node root;
protected svgShape rootShape;
protected Rectangle2D boundingBox;
protected String name;
protected String iconPath;
/**
* Transform cached to save instance created. Used to scale the internal
* path of shapes where possible
*/
protected AffineTransform cachedTransform = new AffineTransform();
/**
* Constructs a new stencil for the given Dia shape description.
*/
public mxStencilShape(String shapeXml)
{
this(mxXmlUtils.parseXml(shapeXml));
}
public mxStencilShape(Document document)
{
if (document != null)
{
NodeList nameList = document.getElementsByTagName("name");
if (nameList != null && nameList.getLength() > 0)
{
this.name = nameList.item(0).getTextContent();
}
NodeList iconList = document.getElementsByTagName("icon");
if (iconList != null && iconList.getLength() > 0)
{
this.iconPath = iconList.item(0).getTextContent();
}
NodeList svgList = document.getElementsByTagName("svg:svg");
if (svgList != null && svgList.getLength() > 0)
{
this.root = svgList.item(0);
}
else
{
svgList = document.getElementsByTagName("svg");
if (svgList != null && svgList.getLength() > 0)
{
this.root = svgList.item(0);
}
}
if (this.root != null)
{
rootShape = new svgShape(null, null);
createShape(this.root, rootShape);
}
}
}
/**
*
*/
@Override
public void paintShape(mxGraphics2DCanvas canvas, mxCellState state)
{
double x = state.getX();
double y = state.getY();
double w = state.getWidth();
double h = state.getHeight();
canvas.getGraphics().translate(x, y);
double widthRatio = 1;
double heightRatio = 1;
if (boundingBox != null)
{
widthRatio = w / boundingBox.getWidth();
heightRatio = h / boundingBox.getHeight();
}
this.paintNode(canvas, state, rootShape, widthRatio, heightRatio);
canvas.getGraphics().translate(-x, -y);
}
/**
*
*/
public void paintNode(mxGraphics2DCanvas canvas, mxCellState state,
svgShape shape, double widthRatio, double heightRatio)
{
Shape associatedShape = shape.shape;
boolean fill = false;
boolean stroke = true;
Color fillColor = null;
Color strokeColor = null;
Map<String, Object> style = shape.style;
if (style != null)
{
String fillStyle = mxUtils.getString(style,
CSSConstants.CSS_FILL_PROPERTY);
String strokeStyle = mxUtils.getString(style,
CSSConstants.CSS_STROKE_PROPERTY);
if (strokeStyle != null
&& strokeStyle.equals(CSSConstants.CSS_NONE_VALUE))
{
if (strokeStyle.equals(CSSConstants.CSS_NONE_VALUE))
{
stroke = false;
}
else if (strokeStyle.trim().startsWith("#"))
{
int hashIndex = strokeStyle.indexOf("#");
strokeColor = mxUtils.parseColor(strokeStyle
.substring(hashIndex + 1));
}
}
if (fillStyle != null)
{
if (fillStyle.equals(CSSConstants.CSS_NONE_VALUE))
{
fill = false;
}
else if (fillStyle.trim().startsWith("#"))
{
int hashIndex = fillStyle.indexOf("#");
fillColor = mxUtils.parseColor(fillStyle
.substring(hashIndex + 1));
fill = true;
}
else
{
fill = true;
}
}
}
if (associatedShape != null)
{
boolean wasScaled = false;
if (widthRatio != 1 || heightRatio != 1)
{
transformShape(associatedShape, 0.0, 0.0, widthRatio,
heightRatio);
wasScaled = true;
}
// Paints the background
if (fill && configureGraphics(canvas, state, true))
{
if (fillColor != null)
{
canvas.getGraphics().setColor(fillColor);
}
canvas.getGraphics().fill(associatedShape);
}
// Paints the foreground
if (stroke && configureGraphics(canvas, state, false))
{
if (strokeColor != null)
{
canvas.getGraphics().setColor(strokeColor);
}
canvas.getGraphics().draw(associatedShape);
}
if (wasScaled)
{
transformShape(associatedShape, 0.0, 0.0, 1.0 / widthRatio,
1.0 / heightRatio);
}
}
/*
* If root is a group element, then we should add it's styles to the
* children.
*/
for (svgShape subShape : shape.subShapes)
{
paintNode(canvas, state, subShape, widthRatio, heightRatio);
}
}
/**
* Scales the points composing this shape by the x and y ratios specified
*
* @param shape
* the shape to scale
* @param transX
* the x translation
* @param transY
* the y translation
* @param widthRatio
* the x co-ordinate scale
* @param heightRatio
* the y co-ordinate scale
*/
protected void transformShape(Shape shape, double transX, double transY,
double widthRatio, double heightRatio)
{
if (shape instanceof Rectangle2D)
{
Rectangle2D rect = (Rectangle2D) shape;
if (transX != 0 || transY != 0)
{
rect.setFrame(rect.getX() + transX, rect.getY() + transY,
rect.getWidth(), rect.getHeight());
}
if (widthRatio != 1 || heightRatio != 1)
{
rect.setFrame(rect.getX() * widthRatio, rect.getY()
* heightRatio, rect.getWidth() * widthRatio,
rect.getHeight() * heightRatio);
}
}
else if (shape instanceof Line2D)
{
Line2D line = (Line2D) shape;
if (transX != 0 || transY != 0)
{
line.setLine(line.getX1() + transX, line.getY1() + transY,
line.getX2() + transX, line.getY2() + transY);
}
if (widthRatio != 1 || heightRatio != 1)
{
line.setLine(line.getX1() * widthRatio, line.getY1()
* heightRatio, line.getX2() * widthRatio, line.getY2()
* heightRatio);
}
}
else if (shape instanceof GeneralPath)
{
GeneralPath path = (GeneralPath) shape;
cachedTransform.setToScale(widthRatio, heightRatio);
cachedTransform.translate(transX, transY);
path.transform(cachedTransform);
}
else if (shape instanceof ExtendedGeneralPath)
{
ExtendedGeneralPath path = (ExtendedGeneralPath) shape;
cachedTransform.setToScale(widthRatio, heightRatio);
cachedTransform.translate(transX, transY);
path.transform(cachedTransform);
}
else if (shape instanceof Ellipse2D)
{
Ellipse2D ellipse = (Ellipse2D) shape;
if (transX != 0 || transY != 0)
{
ellipse.setFrame(ellipse.getX() + transX, ellipse.getY()
+ transY, ellipse.getWidth(), ellipse.getHeight());
}
if (widthRatio != 1 || heightRatio != 1)
{
ellipse.setFrame(ellipse.getX() * widthRatio, ellipse.getY()
* heightRatio, ellipse.getWidth() * widthRatio,
ellipse.getHeight() * heightRatio);
}
}
}
/**
*
*/
public void createShape(Node root, svgShape shape)
{
Node child = root.getFirstChild();
/*
* If root is a group element, then we should add it's styles to the
* childrens...
*/
while (child != null)
{
if (isGroup(child.getNodeName()))
{
String style = ((Element) root).getAttribute("style");
Map<String, Object> styleMap = mxStencilShape
.getStylenames(style);
svgShape subShape = new svgShape(null, styleMap);
createShape(child, subShape);
}
svgShape subShape = createElement(child);
if (subShape != null)
{
shape.subShapes.add(subShape);
}
child = child.getNextSibling();
}
for (svgShape subShape : shape.subShapes)
{
if (subShape != null && subShape.shape != null)
{
if (boundingBox == null)
{
boundingBox = subShape.shape.getBounds2D();
}
else
{
boundingBox.add(subShape.shape.getBounds2D());
}
}
}
// If the shape does not butt up against either or both axis,
// ensure it is flush against both
if (boundingBox != null
&& (boundingBox.getX() != 0 || boundingBox.getY() != 0))
{
for (svgShape subShape : shape.subShapes)
{
if (subShape != null && subShape.shape != null)
{
transformShape(subShape.shape, -boundingBox.getX(),
-boundingBox.getY(), 1.0, 1.0);
}
}
}
}
/**
* Forms an internal representation of the specified SVG element and returns
* that representation
*
* @param root
* the SVG element to represent
* @return the internal representation of the element, or null if an error
* occurs
*/
public svgShape createElement(Node root)
{
Element element = null;
if (root instanceof Element)
{
element = (Element) root;
String style = element.getAttribute("style");
Map<String, Object> styleMap = mxStencilShape.getStylenames(style);
if (isRectangle(root.getNodeName()))
{
svgShape rectShape = null;
try
{
String xString = element.getAttribute("x");
String yString = element.getAttribute("y");
String widthString = element.getAttribute("width");
String heightString = element.getAttribute("height");
// Values default to zero if not specified
double x = 0;
double y = 0;
double width = 0;
double height = 0;
if (xString.length() > 0)
{
x = Double.valueOf(xString);
}
if (yString.length() > 0)
{
y = Double.valueOf(yString);
}
if (widthString.length() > 0)
{
width = Double.valueOf(widthString);
if (width < 0)
{
return null; // error in SVG spec
}
}
if (heightString.length() > 0)
{
height = Double.valueOf(heightString);
if (height < 0)
{
return null; // error in SVG spec
}
}
String rxString = element.getAttribute("rx");
String ryString = element.getAttribute("ry");
double rx = 0;
double ry = 0;
if (rxString.length() > 0)
{
rx = Double.valueOf(rxString);
if (rx < 0)
{
return null; // error in SVG spec
}
}
if (ryString.length() > 0)
{
ry = Double.valueOf(ryString);
if (ry < 0)
{
return null; // error in SVG spec
}
}
if (rx > 0 || ry > 0)
{
// Specification rules on rx and ry
if (rx > 0 && ryString.length() == 0)
{
ry = rx;
}
else if (ry > 0 && rxString.length() == 0)
{
rx = ry;
}
if (rx > width / 2.0)
{
rx = width / 2.0;
}
if (ry > height / 2.0)
{
ry = height / 2.0;
}
rectShape = new svgShape(new RoundRectangle2D.Double(x,
y, width, height, rx, ry), styleMap);
}
else
{
rectShape = new svgShape(new Rectangle2D.Double(x, y,
width, height), styleMap);
}
}
catch (Exception e)
{
log.log(Level.SEVERE, "Failed to create SVG element", e);
}
return rectShape;
}
else if (isLine(root.getNodeName()))
{
String x1String = element.getAttribute("x1");
String x2String = element.getAttribute("x2");
String y1String = element.getAttribute("y1");
String y2String = element.getAttribute("y2");
double x1 = 0;
double x2 = 0;
double y1 = 0;
double y2 = 0;
if (x1String.length() > 0)
{
x1 = Double.valueOf(x1String);
}
if (x2String.length() > 0)
{
x2 = Double.valueOf(x2String);
}
if (y1String.length() > 0)
{
y1 = Double.valueOf(y1String);
}
if (y2String.length() > 0)
{
y2 = Double.valueOf(y2String);
}
svgShape lineShape = new svgShape(new Line2D.Double(x1, y1, x2,
y2), styleMap);
return lineShape;
}
else if (isPolyline(root.getNodeName())
|| isPolygon(root.getNodeName()))
{
String pointsString = element.getAttribute("points");
Shape shape;
if (isPolygon(root.getNodeName()))
{
shape = AWTPolygonProducer.createShape(pointsString,
GeneralPath.WIND_NON_ZERO);
}
else
{
shape = AWTPolylineProducer.createShape(pointsString,
GeneralPath.WIND_NON_ZERO);
}
if (shape != null)
{
return new svgShape(shape, styleMap);
}
return null;
}
else if (isCircle(root.getNodeName()))
{
double cx = 0;
double cy = 0;
double r = 0;
String cxString = element.getAttribute("cx");
String cyString = element.getAttribute("cy");
String rString = element.getAttribute("r");
if (cxString.length() > 0)
{
cx = Double.valueOf(cxString);
}
if (cyString.length() > 0)
{
cy = Double.valueOf(cyString);
}
if (rString.length() > 0)
{
r = Double.valueOf(rString);
if (r < 0)
{
return null; // error in SVG spec
}
}
return new svgShape(new Ellipse2D.Double(cx - r, cy - r, r * 2,
r * 2), styleMap);
}
else if (isEllipse(root.getNodeName()))
{
double cx = 0;
double cy = 0;
double rx = 0;
double ry = 0;
String cxString = element.getAttribute("cx");
String cyString = element.getAttribute("cy");
String rxString = element.getAttribute("rx");
String ryString = element.getAttribute("ry");
if (cxString.length() > 0)
{
cx = Double.valueOf(cxString);
}
if (cyString.length() > 0)
{
cy = Double.valueOf(cyString);
}
if (rxString.length() > 0)
{
rx = Double.valueOf(rxString);
if (rx < 0)
{
return null; // error in SVG spec
}
}
if (ryString.length() > 0)
{
ry = Double.valueOf(ryString);
if (ry < 0)
{
return null; // error in SVG spec
}
}
return new svgShape(new Ellipse2D.Double(cx - rx, cy - ry,
rx * 2, ry * 2), styleMap);
}
else if (isPath(root.getNodeName()))
{
String d = element.getAttribute("d");
Shape pathShape = AWTPathProducer.createShape(d,
GeneralPath.WIND_NON_ZERO);
return new svgShape(pathShape, styleMap);
}
}
return null;
}
/*
*
*/
private boolean isRectangle(String tag)
{
return tag.equals("svg:rect") || tag.equals("rect");
}
/*
*
*/
private boolean isPath(String tag)
{
return tag.equals("svg:path") || tag.equals("path");
}
/*
*
*/
private boolean isEllipse(String tag)
{
return tag.equals("svg:ellipse") || tag.equals("ellipse");
}
/*
*
*/
private boolean isLine(String tag)
{
return tag.equals("svg:line") || tag.equals("line");
}
/*
*
*/
private boolean isPolyline(String tag)
{
return tag.equals("svg:polyline") || tag.equals("polyline");
}
/*
*
*/
private boolean isCircle(String tag)
{
return tag.equals("svg:circle") || tag.equals("circle");
}
/*
*
*/
private boolean isPolygon(String tag)
{
return tag.equals("svg:polygon") || tag.equals("polygon");
}
private boolean isGroup(String tag)
{
return tag.equals("svg:g") || tag.equals("g");
}
protected class svgShape
{
public Shape shape;
/**
* Contains an array of key, value pairs that represent the style of the
* cell.
*/
protected Map<String, Object> style;
public List<svgShape> subShapes;
/**
* Holds the current value to which the shape is scaled in X
*/
protected double currentXScale;
/**
* Holds the current value to which the shape is scaled in Y
*/
protected double currentYScale;
public svgShape(Shape shape, Map<String, Object> style)
{
this.shape = shape;
this.style = style;
subShapes = new ArrayList<svgShape>();
}
public double getCurrentXScale()
{
return currentXScale;
}
public void setCurrentXScale(double currentXScale)
{
this.currentXScale = currentXScale;
}
public double getCurrentYScale()
{
return currentYScale;
}
public void setCurrentYScale(double currentYScale)
{
this.currentYScale = currentYScale;
}
}
/**
* Returns the stylenames in a style of the form stylename[;key=value] or an
* empty array if the given style does not contain any stylenames.
*
* @param style
* String of the form stylename[;stylename][;key=value].
* @return Returns the stylename from the given formatted string.
*/
protected static Map<String, Object> getStylenames(String style)
{
if (style != null && style.length() > 0)
{
Map<String, Object> result = new Hashtable<String, Object>();
if (style != null)
{
String[] pairs = style.split(";");
for (int i = 0; i < pairs.length; i++)
{
String[] keyValue = pairs[i].split(":");
if (keyValue.length == 2)
{
result.put(keyValue[0].trim(), keyValue[1].trim());
}
}
}
return result;
}
return null;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getIconPath()
{
return iconPath;
}
public void setIconPath(String iconPath)
{
this.iconPath = iconPath;
}
public Rectangle2D getBoundingBox()
{
return boundingBox;
}
public void setBoundingBox(Rectangle2D boundingBox)
{
this.boundingBox = boundingBox;
}
}