Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ef6bddbd539b4da361c2937e10fe2e4cbea61c55 (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
/*****************************************************************************
 * Copyright (c) 2016 CEA LIST and others.
 * 
 * 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:
 *   Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr
 *****************************************************************************/

package org.eclipse.papyrus.uml.diagram.common.figure.node;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.ImageFigure;
import org.eclipse.draw2d.ScrollPane;
import org.eclipse.gmf.runtime.draw2d.ui.internal.figures.AnimatableScrollPane;
import org.eclipse.nebula.widgets.richtext.RichTextPainter;
import org.eclipse.papyrus.uml.diagram.common.Activator;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;

/**
 * @since 2.0
 */
public class RichTextRenderer implements HTMLRenderer {

	/** HTML painter */
	protected RichTextPainter painter;

	/** Figure to paint on */
	protected ImageFigure renderFigure;

	/** Image representing HTML content */
	protected Image htmlImage;
	
	private boolean validated;
	
	/**
	 * Scroll pane to wrap the renderFigure
	 */
	protected AnimatableScrollPane contentPane;
	//protected ScrollPane contentPane;

	@Override
	public IFigure getFigure() {
		renderFigure = new ImageFigure();
		renderFigure.setOpaque(false);
		
		contentPane = new AnimatableScrollPane();
		//contentPane = new ScrollPane();
		contentPane.setOpaque(false);
		contentPane.setScrollBarVisibility(ScrollPane.AUTOMATIC);
		contentPane.setContents(renderFigure);

		painter = new RichTextPainter(true);

		validated = false;
		
		return contentPane;
	}

	@Override
	public void paintHTML(String text, int width, int height, int x, int y) {
		if (renderFigure == null || painter == null || contentPane == null) {
			return;
		}
		
		// Validate first time, otherwise infinite layout loop
		if (!validated) {
			contentPane.setPreferredSize(width, height);
			contentPane.validate();
		}
		
		if (htmlImage != null) {
			htmlImage.dispose();
		}

		// Create a transparent background
		htmlImage = getTransparentBackground(Display.getDefault(), width, height);
		if (htmlImage == null) {
			htmlImage = new Image(Display.getDefault(), width, height);
		}
		
		if (htmlImage != null) {
			// Compute the preferred size of the image to display all of the HTML content
			GC gc = new GC(htmlImage);
			Rectangle bounds = new Rectangle(x, y, width, height);
			painter.preCalculate(text != null ? text : "", gc, bounds, true);
			boolean changeBounds = false;
			if (width < getPreferredSize().x) {
				width = getPreferredSize().x;
				changeBounds = true;
			}
			if (height < getPreferredSize().y) {
				height = getPreferredSize().y;
				changeBounds = true;
			}

			// Changing bounds means creating a new image with new width and height, its gc, and the new bounds for the painter
			if (changeBounds) {
				gc.dispose();
				htmlImage.dispose();

				htmlImage = getTransparentBackground(Display.getDefault(), width, height);
				if (htmlImage == null) {
					htmlImage = new Image(Display.getDefault(), width, height);
				}
				gc = new GC(htmlImage);

				bounds = new Rectangle(x, y, width, height);
			}
			
			// Paint and clean up
			painter.paintHTML(text != null ? text : "", gc, bounds); //$NON-NLS-1$
			if (gc != null) {
				gc.dispose();
			}
			
			renderFigure.setImage(htmlImage);
		}
	}

	@Override
	public Point getPreferredSize() {
		return painter.getPreferredSize();
	}

	private Image getTransparentBackground(Display device, int width, int height) {
		try {
			Image src = new Image(device, width, height);
			ImageData imageData = src.getImageData();
			imageData.alpha = 0;
			src.dispose();
			return new Image(device, imageData); 
		} catch (Exception e) {
			Activator.log.error(e);
		}

		return null;
	}
}

Back to the top