Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3250c011374d640f86250d86546a8cc012d8f9ca (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
/**
 * Copyright (c) 2014 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:
 *  CEA LIST - Initial API and implementation
 */
package org.eclipse.papyrus.dd.dg.editor;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Frame;

import org.apache.batik.swing.JSVGCanvas;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.papyrus.dd.editor.DDEditorPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * This is a page that renders on canvas the corresponding SVG for a DG model
 */
public class DGSVGCanvasPage extends DDEditorPage {

	/**
	 * The page id
	 */
	public static final String ID = DGSVGCanvasPage.class.getName();

	/**
	 * The canvas viewer of the page
	 */
	protected SVGCanvasViewer viewer;

	/**
	 * Constructs a new SVG canvas page for a given DG editor
	 * 
	 * @param editor
	 *            The DG editor
	 */
	public DGSVGCanvasPage(DGEditor editor) {
		super(editor, ID, getString("DGSVGCanvasPage.title"));
	}

	@Override
	protected Viewer createViewer(Composite formBody) {
		viewer = new SVGCanvasViewer(formBody);
		return viewer;
	}

	@Override
	public void refresh() {
		EditingDomain editingDomain = getDDEditor().getEditingDomain();
		Resource resource = editingDomain.getResourceSet().getResources()
				.get(0);

		// (re)generate the SVG document
		Document svgDocument = getConverter().convert(resource);

		// update the input of the viewer
		viewer.setInput(svgDocument);
	}

	/**
	 * Gets an instance of <code>DGToSVGConverter</code>
	 * 
	 * @return DGToSVGConverter
	 */
	protected DGToSVGConverter getConverter() {
		return new DGToSVGConverter();
	}

	/**
	 * This is a viewer that embeds a SVG canvas in a SWT composite
	 */
	public static class SVGCanvasViewer extends Viewer {

		/**
		 * This is main widget of the viewer
		 */
		private Composite composite;

		/**
		 * This is the embedded SVG canvas
		 */
		private JSVGCanvas svgCanvas;

		/**
		 * Constructs a new Canvas viewer
		 * 
		 * @param parent
		 *            the SWT composite parent control
		 */
		public SVGCanvasViewer(Composite parent) {
			composite = new Composite(parent, SWT.EMBEDDED);

			Frame frame = SWT_AWT.new_Frame(composite);
			frame.setVisible(true);

			svgCanvas = new JSVGCanvas();
			svgCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
			frame.add(svgCanvas);
		}

		@Override
		public Control getControl() {
			return composite;
		}

		@Override
		public Object getInput() {
			return svgCanvas.getSVGDocument();
		}

		@Override
		public void setInput(final Object input) {
			EventQueue.invokeLater(new Runnable() {

				@Override
				public void run() {
					Document doc = (Document) input;
					svgCanvas.setDocument(doc);

					// sets the background color of the canvas explicitly since
					// the CSS background-color
					// property is not supported by Batik yet
					Element svg = doc.getDocumentElement();
					String value = svg
							.getAttribute(DGToSVGConverter.SVG_BACKGROUND_COLOR_ATTRIBUTE);

					if (value != null && value.length() > 0)
						svgCanvas.setBackground(Color.decode(value));
					else
						svgCanvas.setBackground(Color.white);
				}
			});
		}

		@Override
		public ISelection getSelection() {
			return StructuredSelection.EMPTY;
		}

		@Override
		public void refresh() {
			// do nothing
		}

		@Override
		public void setSelection(ISelection selection, boolean reveal) {
			// do nothing
		}
	};
}

Back to the top