forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxHtmlCanvas.java
More file actions
348 lines (296 loc) · 8.51 KB
/
mxHtmlCanvas.java
File metadata and controls
348 lines (296 loc) · 8.51 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
/**
* Copyright (c) 2007, Gaudenz Alder
*/
package com.mxgraph.canvas;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import com.mxgraph.util.mxConstants;
import com.mxgraph.util.mxPoint;
import com.mxgraph.util.mxRectangle;
import com.mxgraph.util.mxUtils;
import com.mxgraph.view.mxCellState;
/**
* An implementation of a canvas that uses HTML for painting.
*/
public class mxHtmlCanvas extends mxBasicCanvas
{
/**
* Holds the HTML document that represents the canvas.
*/
protected Document document;
/**
* Constructs a new HTML canvas for the specified dimension and scale.
*/
public mxHtmlCanvas()
{
this(null);
}
/**
* Constructs a new HTML canvas for the specified bounds, scale and
* background color.
*/
public mxHtmlCanvas(Document document)
{
setDocument(document);
}
/**
*
*/
public void appendHtmlElement(Element node)
{
if (document != null)
{
Node body = document.getDocumentElement().getFirstChild()
.getNextSibling();
if (body != null)
{
body.appendChild(node);
}
}
}
/**
*
*/
public void setDocument(Document document)
{
this.document = document;
}
/**
* Returns a reference to the document that represents the canvas.
*
* @return Returns the document.
*/
public Document getDocument()
{
return document;
}
/*
* (non-Javadoc)
* @see com.mxgraph.canvas.mxICanvas#drawCell()
*/
public Object drawCell(mxCellState state)
{
Map<String, Object> style = state.getStyle();
if (state.getAbsolutePointCount() > 1)
{
List<mxPoint> pts = state.getAbsolutePoints();
// Transpose all points by cloning into a new array
pts = mxUtils.translatePoints(pts, translate.getX(), translate.getY());
drawLine(pts, style);
}
else
{
int x = (int) (state.getX() + translate.getX());
int y = (int) (state.getY() + translate.getY());
int w = (int) state.getWidth();
int h = (int) state.getHeight();
if (!mxUtils.getString(style, mxConstants.STYLE_SHAPE, "").equals(
mxConstants.SHAPE_SWIMLANE))
{
drawShape(x, y, w, h, style);
}
else
{
int start = (int) Math.round(mxUtils.getInt(style,
mxConstants.STYLE_STARTSIZE,
mxConstants.DEFAULT_STARTSIZE)
* scale);
// Removes some styles to draw the content area
Map<String, Object> cloned = new Hashtable<String, Object>(
style);
cloned.remove(mxConstants.STYLE_FILLCOLOR);
cloned.remove(mxConstants.STYLE_ROUNDED);
if (mxUtils.isTrue(style, mxConstants.STYLE_HORIZONTAL, true))
{
drawShape(x, y, w, start, style);
drawShape(x, y + start, w, h - start, cloned);
}
else
{
drawShape(x, y, start, h, style);
drawShape(x + start, y, w - start, h, cloned);
}
}
}
return null;
}
/*
* (non-Javadoc)
* @see com.mxgraph.canvas.mxICanvas#drawLabel()
*/
public Object drawLabel(String label, mxCellState state, boolean html)
{
mxRectangle bounds = state.getLabelBounds();
if (drawLabels && bounds != null)
{
int x = (int) (bounds.getX() + translate.getY());
int y = (int) (bounds.getY() + translate.getY());
int w = (int) bounds.getWidth();
int h = (int) bounds.getHeight();
Map<String, Object> style = state.getStyle();
return drawText(label, x, y, w, h, style);
}
return null;
}
/**
* Draws the shape specified with the STYLE_SHAPE key in the given style.
*
* @param x X-coordinate of the shape.
* @param y Y-coordinate of the shape.
* @param w Width of the shape.
* @param h Height of the shape.
* @param style Style of the the shape.
*/
public Element drawShape(int x, int y, int w, int h,
Map<String, Object> style)
{
String fillColor = mxUtils
.getString(style, mxConstants.STYLE_FILLCOLOR);
String strokeColor = mxUtils.getString(style,
mxConstants.STYLE_STROKECOLOR);
float strokeWidth = (float) (mxUtils.getFloat(style,
mxConstants.STYLE_STROKEWIDTH, 1) * scale);
// Draws the shape
String shape = mxUtils.getString(style, mxConstants.STYLE_SHAPE);
Element elem = document.createElement("div");
if (shape.equals(mxConstants.SHAPE_LINE))
{
String direction = mxUtils.getString(style,
mxConstants.STYLE_DIRECTION, mxConstants.DIRECTION_EAST);
if (direction.equals(mxConstants.DIRECTION_EAST)
|| direction.equals(mxConstants.DIRECTION_WEST))
{
y = Math.round(y + h / 2);
h = 1;
}
else
{
x = Math.round(y + w / 2);
w = 1;
}
}
if (mxUtils.isTrue(style, mxConstants.STYLE_SHADOW, false)
&& fillColor != null)
{
Element shadow = (Element) elem.cloneNode(true);
String s = "overflow:hidden;position:absolute;" + "left:"
+ String.valueOf(x + mxConstants.SHADOW_OFFSETX) + "px;"
+ "top:" + String.valueOf(y + mxConstants.SHADOW_OFFSETY)
+ "px;" + "width:" + String.valueOf(w) + "px;" + "height:"
+ String.valueOf(h) + "px;background:"
+ mxConstants.W3C_SHADOWCOLOR
+ ";border-style:solid;border-color:"
+ mxConstants.W3C_SHADOWCOLOR + ";border-width:"
+ String.valueOf(Math.round(strokeWidth)) + ";";
shadow.setAttribute("style", s);
appendHtmlElement(shadow);
}
if (shape.equals(mxConstants.SHAPE_IMAGE))
{
String img = getImageForStyle(style);
if (img != null)
{
elem = document.createElement("img");
elem.setAttribute("border", "0");
elem.setAttribute("src", img);
}
}
// TODO: Draw other shapes. eg. SHAPE_LINE here
String s = "overflow:hidden;position:absolute;" + "left:"
+ String.valueOf(x) + "px;" + "top:" + String.valueOf(y)
+ "px;" + "width:" + String.valueOf(w) + "px;" + "height:"
+ String.valueOf(h) + "px;background:" + fillColor + ";"
+ ";border-style:solid;border-color:" + strokeColor
+ ";border-width:" + String.valueOf(Math.round(strokeWidth))
+ ";";
elem.setAttribute("style", s);
appendHtmlElement(elem);
return elem;
}
/**
* Draws the given lines as segments between all points of the given list
* of mxPoints.
*
* @param pts List of points that define the line.
* @param style Style to be used for painting the line.
*/
public void drawLine(List<mxPoint> pts, Map<String, Object> style)
{
String strokeColor = mxUtils.getString(style,
mxConstants.STYLE_STROKECOLOR);
int strokeWidth = (int) (mxUtils.getInt(style,
mxConstants.STYLE_STROKEWIDTH, 1) * scale);
if (strokeColor != null && strokeWidth > 0)
{
mxPoint last = pts.get(0);
for (int i = 1; i < pts.size(); i++)
{
mxPoint pt = pts.get(i);
drawSegment((int) last.getX(), (int) last.getY(), (int) pt
.getX(), (int) pt.getY(), strokeColor, strokeWidth);
last = pt;
}
}
}
/**
* Draws the specified segment of a line.
*
* @param x0 X-coordinate of the start point.
* @param y0 Y-coordinate of the start point.
* @param x1 X-coordinate of the end point.
* @param y1 Y-coordinate of the end point.
* @param strokeColor Color of the stroke to be painted.
* @param strokeWidth Width of the stroke to be painted.
*/
protected void drawSegment(int x0, int y0, int x1, int y1,
String strokeColor, int strokeWidth)
{
int tmpX = Math.min(x0, x1);
int tmpY = Math.min(y0, y1);
int width = Math.max(x0, x1) - tmpX;
int height = Math.max(y0, y1) - tmpY;
x0 = tmpX;
y0 = tmpY;
if (width == 0 || height == 0)
{
String s = "overflow:hidden;position:absolute;" + "left:"
+ String.valueOf(x0) + "px;" + "top:" + String.valueOf(y0)
+ "px;" + "width:" + String.valueOf(width) + "px;"
+ "height:" + String.valueOf(height) + "px;"
+ "border-color:" + strokeColor + ";"
+ "border-style:solid;" + "border-width:1 1 0 0px;";
Element elem = document.createElement("div");
elem.setAttribute("style", s);
appendHtmlElement(elem);
}
else
{
int x = x0 + (x1 - x0) / 2;
drawSegment(x0, y0, x, y0, strokeColor, strokeWidth);
drawSegment(x, y0, x, y1, strokeColor, strokeWidth);
drawSegment(x, y1, x1, y1, strokeColor, strokeWidth);
}
}
/**
* Draws the specified text either using drawHtmlString or using drawString.
*
* @param text Text to be painted.
* @param x X-coordinate of the text.
* @param y Y-coordinate of the text.
* @param w Width of the text.
* @param h Height of the text.
* @param style Style to be used for painting the text.
*/
public Element drawText(String text, int x, int y, int w, int h,
Map<String, Object> style)
{
Element table = mxUtils.createTable(document, text, x, y, w, h, scale,
style);
appendHtmlElement(table);
return table;
}
}