Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 289780cfca6e0edd7b1962f88b3fa2ef0a70f8ea (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
/*******************************************************************************
 * Copyright (c) 2018 Red Hat Inc. 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:
 *     Lucas Bullen (Red Hat Inc.) - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.snippets;

/*
 * Browser: Resize the shell to the fit the html content after it's loaded.
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 *
 * @since 4.8
 */
import org.eclipse.swt.*;
import org.eclipse.swt.browser.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Snippet372 {
	static Double width = null;
	public static void main(String [] args) {
		int maximumWidth = 200;
		int maximumHeight= 2000;

		String html = "<HTML><HEAD><TITLE>HTML Test</TITLE></HEAD><BODY>";
		for (int i = 0; i < 15; i++) html += "<P>This is line "+i+"</P>";
		html += "</BODY></HTML>";

		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
		Browser browser;
		try {
			browser = new Browser(shell, SWT.NONE);
		} catch (SWTError e) {
			System.out.println("Could not instantiate Browser: " + e.getMessage());
			display.dispose();
			return;
		}
		browser.setText(html);
		browser.addProgressListener(ProgressListener.completedAdapter(event -> {
			// Set the display to something known to be smaller than the content
			shell.setSize(50, 50);
			browser.execute("document.getElementsByTagName(\"html\")[0].style.whiteSpace = \"nowrap\""); //$NON-NLS-1$
			// Save the width to either be a decided maximum or the browser's content width plus the margin
			Double width = Math.min(maximumWidth, 10 + (Double) browser.evaluate("return document.body.scrollWidth;")); //$NON-NLS-1$
			shell.setSize(width.intValue(), 0);
			browser.execute("document.getElementsByTagName(\"html\")[0].style.whiteSpace = \"normal\""); //$NON-NLS-1$
			shell.layout();
			// Set the height to either be a decided maximum or the browser's content height plus the margin
			Double height = Math.min(maximumHeight, 5 + (Double) browser.evaluate("return document.body.scrollHeight;")); //$NON-NLS-1$
			shell.setSize(width.intValue(), height.intValue());
		}));

		shell.open();
		while (!shell.isDisposed()) {
			shell.layout();
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
}

Back to the top