1 /*******************************************************************************
2  * Copyright (c) 2018 Red Hat and others. All rights reserved.
3  * The contents of this file are made available under the terms
4  * of the GNU Lesser General Public License (LGPL) Version 2.1 that
5  * accompanies this distribution (lgpl-v21.txt).  The LGPL is also
6  * available at http://www.gnu.org/licenses/lgpl.html.  If the version
7  * of the LGPL at http://www.gnu.org is different to the version of
8  * the LGPL accompanying this distribution and there is any conflict
9  * between the two license versions, the terms of the LGPL accompanying
10  * this distribution shall govern.
11  *
12  * Contributors:
13  *     Red Hat - initial API and implementation
14  *******************************************************************************/
15 package org.eclipse.swt.tests.gtk.snippets;
16 
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.browser.Browser;
19 import org.eclipse.swt.browser.BrowserFunction;
20 import org.eclipse.swt.events.KeyAdapter;
21 import org.eclipse.swt.events.KeyEvent;
22 import org.eclipse.swt.events.SelectionAdapter;
23 import org.eclipse.swt.events.SelectionEvent;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.layout.RowLayout;
27 import org.eclipse.swt.widgets.Button;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Display;
30 import org.eclipse.swt.widgets.Shell;
31 import org.eclipse.swt.widgets.Text;
32 
33 /*
34  * Title:  [GTK3][Webkit2] Implement webkit2 support for browser function (Part 2: Java return a value from callback.)
35  * How to run: Snippet to execute javascript via input prompt, to test Browser Func return value ability.
36  * Bug description:
37  * Expected results:
38  * GTK Version(s):
39  */
40 public class Bug510905_Browser_TwoJsConsoles {
41 
42 	static int count = 0;
43 
main(String[] args)44 	public static void main(String[] args) {
45 		Display display = new Display();
46 		Shell shell = new Shell(display);
47 		shell.setSize(500, 600);
48 
49 		shell.setLayout(new RowLayout());
50 
51 		Composite leftBrowser = new Composite(shell, SWT.NONE);
52 		Composite rightBrowser = new Composite(shell, SWT.None);
53 		Button button = new Button (rightBrowser, SWT.PUSH);
54 		button.setText("my button");
55 
56 
57 		final Browser browser = makeBrowserWithConsole(leftBrowser, "theJavaFunction");
58 		new CustomFunction (browser, "theJavaFunction");
59 
60 		final Browser browser2 = makeBrowserWithConsole(rightBrowser, "theJavaFunction");
61 		new CustomFunction (browser2, "theJavaFunction");
62 
63 		shell.open();
64 		while (!shell.isDisposed()) {
65 			if (!display.readAndDispatch())
66 				display.sleep();
67 		}
68 		display.dispose();
69 	}
70 
71 	/**
72 	 * @param leftBrowser
73 	 * @return
74 	 */
makeBrowserWithConsole(Composite leftBrowser, String funcName)75 	private static Browser makeBrowserWithConsole(Composite leftBrowser, String funcName) {
76 		GridLayout gridLayout = new GridLayout();
77 		leftBrowser.setLayout(gridLayout);
78 
79 		final Text jsConsole = new Text(leftBrowser, SWT.BORDER);
80 		jsConsole.setText("document.body.innerHTML = " + funcName + "(123)"); // Case where there are no paramaters.
81 		jsConsole.setSelection(jsConsole.getText().length());
82 		GridData data = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
83 		jsConsole.setLayoutData(data);
84 
85 		final Browser browser = new Browser(leftBrowser, SWT.NONE);
86 		browser.setText("hello <b>world!</b>");
87 		data = new GridData(SWT.FILL, SWT.FILL, true, true);
88 		browser.setLayoutData(data);
89 
90 		jsConsole.addKeyListener(new KeyAdapter() {
91 			@Override
92 			public void keyPressed(KeyEvent e) {
93 				if (e.keyCode == 13) { // 13 = Enter
94 					browser.execute(jsConsole.getText());
95 				}
96 			}
97 		});
98 
99 		Button loadNewPage = new Button(leftBrowser, SWT.PUSH);
100 		loadNewPage.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
101 		loadNewPage.setText("Load new Page");
102 		loadNewPage.addSelectionListener(new SelectionAdapter() {
103 			@Override
104 			public void widgetSelected(SelectionEvent e) {
105 				browser.setText("New page!" + count++);
106 			}
107 		});
108 		return browser;
109 	}
110 
111 	static class CustomFunction extends BrowserFunction { // copied from snippet 307
CustomFunction(Browser browser, String name)112 		CustomFunction (Browser browser, String name) {
113 			super (browser, name);
114 		}
115 		@Override
function(Object[] arguments)116 		public Object function (Object[] arguments) {
117 			System.out.println ("theJavaFunction() called from javascript with args:");
118 			for (int i = 0; i < arguments.length; i++) {
119 				Object arg = arguments[i];
120 				if (arg == null) {
121 					System.out.println ("\t-->null");
122 				} else {
123 					System.out.println ("\t-->" + arg.getClass ().getName () + ": " + arg.toString ());
124 				}
125 			}
126 			return arguments;
127 		}
128 	}
129 }
130