forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxCellRenderer.java
More file actions
215 lines (182 loc) · 5 KB
/
mxCellRenderer.java
File metadata and controls
215 lines (182 loc) · 5 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
/**
* Copyright (c) 2007-2012, JGraph Ltd
*/
package com.mxgraph.util;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import org.w3c.dom.Document;
import com.mxgraph.canvas.mxGraphics2DCanvas;
import com.mxgraph.canvas.mxHtmlCanvas;
import com.mxgraph.canvas.mxICanvas;
import com.mxgraph.canvas.mxImageCanvas;
import com.mxgraph.canvas.mxSvgCanvas;
import com.mxgraph.canvas.mxVmlCanvas;
import com.mxgraph.view.mxGraph;
import com.mxgraph.view.mxGraphView;
import com.mxgraph.view.mxTemporaryCellStates;
public class mxCellRenderer
{
/**
*
*/
private mxCellRenderer()
{
// static class
}
/**
* Draws the given cells using a Graphics2D canvas and returns the buffered image
* that represents the cells.
*
* @param graph Graph to be painted onto the canvas.
* @return Returns the image that represents the canvas.
*/
public static mxICanvas drawCells(mxGraph graph, Object[] cells,
double scale, mxRectangle clip, CanvasFactory factory)
{
mxICanvas canvas = null;
if (cells == null)
{
cells = new Object[] { graph.getModel().getRoot() };
}
// Gets the current state of the view
mxGraphView view = graph.getView();
// Keeps the existing translation as the cells might
// be aligned to the grid in a different way in a graph
// that has a translation other than zero
boolean eventsEnabled = view.isEventsEnabled();
// Disables firing of scale events so that there is no
// repaint or update of the original graph
view.setEventsEnabled(false);
// Uses the view to create temporary cell states for each cell
mxTemporaryCellStates temp = new mxTemporaryCellStates(view, scale,
cells);
try
{
if (clip == null)
{
clip = graph.getPaintBounds(cells);
}
if (clip != null && clip.getWidth() > 0 && clip.getHeight() > 0)
{
Rectangle rect = clip.getRectangle();
canvas = factory.createCanvas(rect.width + 1, rect.height + 1);
if (canvas != null)
{
double previousScale = canvas.getScale();
mxPoint previousTranslate = canvas.getTranslate();
try
{
canvas.setTranslate(-rect.x, -rect.y);
canvas.setScale(view.getScale());
for (int i = 0; i < cells.length; i++)
{
graph.drawCell(canvas, cells[i]);
}
}
finally
{
canvas.setScale(previousScale);
canvas.setTranslate(previousTranslate.getX(),
previousTranslate.getY());
}
}
}
}
finally
{
temp.destroy();
view.setEventsEnabled(eventsEnabled);
}
return canvas;
}
/**
*
*/
public static BufferedImage createBufferedImage(mxGraph graph,
Object[] cells, double scale, Color background, boolean antiAlias,
mxRectangle clip)
{
return createBufferedImage(graph, cells, scale, background, antiAlias,
clip, new mxGraphics2DCanvas());
}
/**
*
*/
public static BufferedImage createBufferedImage(mxGraph graph,
Object[] cells, double scale, final Color background,
final boolean antiAlias, mxRectangle clip,
final mxGraphics2DCanvas graphicsCanvas)
{
mxImageCanvas canvas = (mxImageCanvas) drawCells(graph, cells, scale,
clip, new CanvasFactory()
{
public mxICanvas createCanvas(int width, int height)
{
return new mxImageCanvas(graphicsCanvas, width, height,
background, antiAlias);
}
});
return (canvas != null) ? canvas.destroy() : null;
}
/**
*
*/
public static Document createHtmlDocument(mxGraph graph, Object[] cells,
double scale, Color background, mxRectangle clip)
{
mxHtmlCanvas canvas = (mxHtmlCanvas) drawCells(graph, cells, scale,
clip, new CanvasFactory()
{
public mxICanvas createCanvas(int width, int height)
{
return new mxHtmlCanvas(mxDomUtils.createHtmlDocument());
}
});
return (canvas != null) ? canvas.getDocument() : null;
}
/**
*
*/
public static Document createSvgDocument(mxGraph graph, Object[] cells,
double scale, Color background, mxRectangle clip)
{
mxSvgCanvas canvas = (mxSvgCanvas) drawCells(graph, cells, scale, clip,
new CanvasFactory()
{
public mxICanvas createCanvas(int width, int height)
{
return new mxSvgCanvas(mxDomUtils.createSvgDocument(width,
height));
}
});
return (canvas != null) ? canvas.getDocument() : null;
}
/**
*
*/
public static Document createVmlDocument(mxGraph graph, Object[] cells,
double scale, Color background, mxRectangle clip)
{
mxVmlCanvas canvas = (mxVmlCanvas) drawCells(graph, cells, scale, clip,
new CanvasFactory()
{
public mxICanvas createCanvas(int width, int height)
{
return new mxVmlCanvas(mxDomUtils.createVmlDocument());
}
});
return (canvas != null) ? canvas.getDocument() : null;
}
/**
*
*/
public static abstract class CanvasFactory
{
/**
* Separates the creation of the canvas from its initialization, when the
* size of the required graphics buffer / document / container is known.
*/
public abstract mxICanvas createCanvas(int width, int height);
}
}