Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLucas Bullen2018-04-18 14:15:48 +0000
committerLeo Ufimtsev2018-04-26 19:50:15 +0000
commit9f15aa7692178890d7c74e1e0ef79a329c36f9fa (patch)
treea005ed026efac910ba8fd263a841da273aaf70b8 /examples/org.eclipse.swt.snippets/src
parentcdc67cbff3fb701108f19e6dbec01306872e31dc (diff)
downloadeclipse.platform.swt-9f15aa7692178890d7c74e1e0ef79a329c36f9fa.tar.gz
eclipse.platform.swt-9f15aa7692178890d7c74e1e0ef79a329c36f9fa.tar.xz
eclipse.platform.swt-9f15aa7692178890d7c74e1e0ef79a329c36f9fa.zip
Bug 232501 - Snippet to compute browser size
Change-Id: Ib12e18cbdb4a0b1773a74c8818d4f0ca3469266a Signed-off-by: Lucas Bullen <lbullen@redhat.com>
Diffstat (limited to 'examples/org.eclipse.swt.snippets/src')
-rw-r--r--examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet372.java70
1 files changed, 70 insertions, 0 deletions
diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet372.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet372.java
new file mode 100644
index 0000000000..289780cfca
--- /dev/null
+++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet372.java
@@ -0,0 +1,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