Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 53f55e4a4e9d4282813366bdf95d92a15b1502c5 (plain) (blame)
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
/*****************************************************************************
 * Copyright (c) 2017 CEA LIST.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Benoit Maggi (CEA LIST) - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.uml.export.svgextension;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.batik.anim.dom.SVGDOMImplementation;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gmf.runtime.common.core.util.Log;
import org.eclipse.gmf.runtime.common.core.util.Trace;
import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
import org.eclipse.gmf.runtime.diagram.ui.image.PartPositionInfo;
import org.eclipse.gmf.runtime.diagram.ui.l10n.SharedImages;
import org.eclipse.gmf.runtime.diagram.ui.render.internal.DiagramUIRenderDebugOptions;
import org.eclipse.gmf.runtime.diagram.ui.render.internal.DiagramUIRenderPlugin;
import org.eclipse.gmf.runtime.draw2d.ui.render.RenderedImage;
import org.eclipse.gmf.runtime.draw2d.ui.render.awt.internal.image.ImageConverter;
import org.eclipse.gmf.runtime.draw2d.ui.render.awt.internal.svg.export.GraphicsSVG;
import org.eclipse.gmf.runtime.draw2d.ui.render.factory.RenderedImageFactory;
import org.eclipse.gmf.runtime.draw2d.ui.render.internal.RenderedImageDescriptor;
import org.eclipse.gmf.runtime.notation.Shape;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.papyrus.uml.export.Activator;
import org.eclipse.papyrus.uml.export.extension.AnnotateSVG;
import org.eclipse.papyrus.uml.export.svgextension.internal.OpenAPIDiagramSVGGenerator;
import org.eclipse.uml2.uml.NamedElement;
import org.w3c.dom.Element;

/**
 * The Class PapyrusDiagramSVGGenerator.
 */
public class PapyrusDiagramSVGGenerator extends OpenAPIDiagramSVGGenerator {

	/** The annotations. */
	private List<AnnotateSVG> annotations = new ArrayList<>();

	/**
	 * Instantiates a new papyrus diagram SVG generator.
	 *
	 * @param diagramEditPart
	 *            the diagram edit part
	 * @param annotations
	 *            the annotations
	 */
	public PapyrusDiagramSVGGenerator(DiagramEditPart diagramEditPart, List<AnnotateSVG> annotations) {
		super(diagramEditPart);
		this.annotations = annotations;
	}

	/** The rendered image. */
	private RenderedImage renderedImage = null;

	/** The svg root. */
	private Element svgRoot = null;

	/** The view box. */
	private Rectangle viewBox = null;

	/**
	 * @see org.eclipse.gmf.runtime.diagram.ui.render.clipboard.DiagramSVGGenerator#setUpGraphics(int, int)
	 *
	 * @param width
	 * @param height
	 * @return
	 */
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gmf.runtime.diagram.ui.render.clipboard.DiagramGenerator#
	 * setUpGraphics(int, int)
	 */
	@Override
	protected Graphics setUpGraphics(int width, int height) {
		viewBox = new Rectangle(0, 0, width, height);
		return GraphicsSVG.getInstance(viewBox);
	}

	/**
	 * @see org.eclipse.gmf.runtime.diagram.ui.render.clipboard.DiagramSVGGenerator#getImageDescriptor(org.eclipse.draw2d.Graphics)
	 *
	 * @param g
	 * @return
	 */
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gmf.runtime.diagram.ui.render.clipboard.DiagramGenerator#
	 * getImageDescriptor(org.eclipse.draw2d.Graphics)
	 */
	@Override
	protected ImageDescriptor getImageDescriptor(Graphics g) {
		try {
			Activator.log(IStatus.INFO, "Start transformation from Graphics to image descriptor");

			GraphicsSVG svgG = (GraphicsSVG) g;
			svgRoot = svgG.getRoot();

			//////// Papyrus Specific code //////////
			List<PartPositionInfo> allPartPositionInfo = this.getDiagramPartInfo();

			Collections.reverse(allPartPositionInfo);// Required to have property after class
			for (PartPositionInfo partPositionInfo : allPartPositionInfo) {
				Element rectangle = svgG.getDocument().createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, "rect");
				if (partPositionInfo.getSemanticElement() instanceof NamedElement) {
					NamedElement nameElement = (NamedElement) partPositionInfo.getSemanticElement();
					rectangle.setAttributeNS(null, "id", nameElement.getName());// useful for svg debug
				}

				rectangle.setAttributeNS(null, "x", String.valueOf(partPositionInfo.getPartX()));
				rectangle.setAttributeNS(null, "y", String.valueOf(partPositionInfo.getPartY()));
				rectangle.setAttributeNS(null, "width", String.valueOf(partPositionInfo.getPartWidth()));
				rectangle.setAttributeNS(null, "height", String.valueOf(partPositionInfo.getPartHeight()));
				rectangle.setAttributeNS(null, "fill-opacity", "0"); // transparent
				rectangle.setAttributeNS(null, "stroke-opacity", "0"); // no border
				View view = partPositionInfo.getView();
				if (view instanceof Shape) { // filter on shape only to avoid duplication

					boolean hasAnnotation = applyAll(view, svgG, rectangle);
					if (hasAnnotation) {
						svgRoot.appendChild(rectangle);
					}
				}

			}

			/////////////////////////


			ByteArrayOutputStream os = new ByteArrayOutputStream(5000); // 5K
																		// buffer
			stream(os);
			os.close();

			setRenderedImage(RenderedImageFactory.getInstance(os.toByteArray()));

			return RenderedImageDescriptor
					.createFromRenderedImage(getRenderedImage());
		} catch (IOException ex) {
			Log.error(DiagramUIRenderPlugin.getInstance(), IStatus.ERROR, ex
					.getMessage(), ex);
		}

		return null;
	}


	/**
	 * Apply all.
	 *
	 * @param view
	 *            the view
	 * @param svgG
	 *            the svg G
	 * @param rectangle
	 *            the rectangle
	 * @return true, if successful
	 */
	public boolean applyAll(View view, GraphicsSVG svgG, Element rectangle) {
		boolean res = false;
		for (AnnotateSVG annotateSVG : annotations) {
			res = res || annotateSVG.addAnnotation(view, svgG, rectangle);
		}
		return true;
	}

	/**
	 * Writes the SVG Model out to a file.
	 * 
	 * @param outputStream
	 *            output stream to store the SVG Model
	 */
	@Override
	public void stream(OutputStream outputStream) {
		try {
			// Define the view box
			svgRoot.setAttributeNS(null, "viewBox", String.valueOf(viewBox.x) + " " + //$NON-NLS-1$ //$NON-NLS-2$
					String.valueOf(viewBox.y) + " " + //$NON-NLS-1$
					String.valueOf(viewBox.width) + " " + //$NON-NLS-1$
					String.valueOf(viewBox.height));

			// Write the document to the stream
			Transformer transformer = TransformerFactory.newInstance().newTransformer();
			transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
			transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //$NON-NLS-1$
			transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$

			DOMSource source = new DOMSource(svgRoot);
			StreamResult result = new StreamResult(outputStream);
			transformer.transform(source, result);
		} catch (Exception ex) {
			Log.error(DiagramUIRenderPlugin.getInstance(), IStatus.ERROR, ex.getMessage(), ex);
		}
	}

	/**
	 * @see org.eclipse.gmf.runtime.diagram.ui.render.clipboard.DiagramSVGGenerator#createAWTImageForParts(java.util.List, org.eclipse.swt.graphics.Rectangle)
	 *
	 * @param editparts
	 * @param sourceRect
	 * @return
	 */
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gmf.runtime.diagram.ui.internal.clipboard.DiagramGenerator#
	 * createAWTImageForParts(java.util.List)
	 */
	@Override
	public Image createAWTImageForParts(List editparts, org.eclipse.swt.graphics.Rectangle sourceRect) {
		createSWTImageDescriptorForParts(editparts, sourceRect);
		if (getRenderedImage() != null) {
			try {
				BufferedImage bufImg = getRenderedImage().getAdapter(BufferedImage.class);
				if (bufImg == null)
					bufImg = ImageConverter.convert(getRenderedImage().getSWTImage());
				return bufImg;
			} catch (Error e) {
				// log the Error but allow execution to continue
				Trace.catching(DiagramUIRenderPlugin.getInstance(), DiagramUIRenderDebugOptions.EXCEPTIONS_THROWING,
						getClass(), "createAWTImageForParts() failed to generate image", //$NON-NLS-1$
						e);
				return ImageConverter.convert(SharedImages.get(SharedImages.IMG_ERROR));

			} catch (Exception ex) {
				// log the Exception but allow execution to continue
				Trace.catching(DiagramUIRenderPlugin.getInstance(), DiagramUIRenderDebugOptions.EXCEPTIONS_THROWING,
						getClass(), "createAWTImageForParts() failed to generate image", //$NON-NLS-1$
						ex);
				return ImageConverter.convert(SharedImages.get(SharedImages.IMG_ERROR));
			}
		}

		return ImageConverter.convert(SharedImages.get(SharedImages.IMG_ERROR));
	}

	/**
	 * Gets the rendered image.
	 *
	 * @return Returns the rendered image created by previous call to
	 *         createSWTImageDescriptorForParts
	 */
	@Override
	public RenderedImage getRenderedImage() {
		return renderedImage;
	}

	/**
	 * Sets the rendered image.
	 *
	 * @param renderedImage
	 *            the new rendered image
	 */
	private void setRenderedImage(RenderedImage renderedImage) {
		this.renderedImage = renderedImage;
	}

}

Back to the top