summaryrefslogtreecommitdiffstats
path: root/examples/org.eclipse.swt.examples.browser.demos/src/org/eclipse/swt/examples/browser/demos/views/EditorTab.java
blob: e17d1c37aee673b9048168eade54b791dc427a81 (plain)
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
/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.examples.browser.demos.views;

import org.eclipse.swt.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.browser.*;
import org.eclipse.swt.graphics.*;

public class EditorTab {
	Browser browser;
	Text htmlText, scriptText;
	Button htmlButton, scriptButton;
	static String html = 
		"<html>\r\n"+
		"	<body>\r\n"+
		"		<h1 id='myid'>HTML Document</h1>\r\n"+
		"		<h2>Set HTML content</h2>\r\n"+
		"		<ol>\r\n"+
		"			<li>Enter html data into the 'setText' pane</li>\r\n"+
		"			<li>Click on 'setText' to set the new content</li>\r\n"+
		"		</ol>\r\n"+
		"		<h2>Query or modify HTML document</h2>\r\n"+
		"		<ol>\r\n"+
		"		<li>Enter javascript commands into the 'execute' pane</li>\r\n"+
		"		<li>Click on 'execute' to run the javascript in the current document</li>\r\n"+
		"		</ol>\r\n"+
		"	</body>\r\n"+
		"</html>";
	
	static String script = 
		"var node = document.createElement('P');\r\n"+
		"var text = document.createTextNode('Content inserted!');\r\n"+
		"node.appendChild(text);\r\n"+
		"document.getElementById('myid').appendChild(node);\r\n\r\n"+
		"document.bgColor = 'yellow';";
	
	public EditorTab(TabItem item) {
		final Composite parent = new Composite(item.getParent(), SWT.NONE);
		item.setText("Editor");
		item.setControl(parent);
		
		try {
			browser = new Browser(parent, SWT.NONE);
		} catch (SWTError e) {
			e.printStackTrace();
			return;
		}
		final Sash sash = new Sash(parent, SWT.VERTICAL);
		Composite panel = new Composite(parent, SWT.NONE);
		final FormLayout form = new FormLayout();
		parent.setLayout(form);
		
		FormData data = new FormData();
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(sash, 0);
		data.top = new FormAttachment(0, 0);
		data.bottom = new FormAttachment(100, 0);
		browser.setLayoutData(data);
		
		final FormData sashData = new FormData();
		sashData.left = new FormAttachment(50, 0);
		sashData.top = new FormAttachment(0, 0);
		sashData.bottom = new FormAttachment(100, 0);
		sash.setLayoutData(sashData);
		sash.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				Rectangle rect = sash.getBounds();
				Rectangle parentRect = sash.getParent().getClientArea();
				int right = parentRect.width - rect.width - 20;
				e.x = Math.max(Math.min(e.x, right), 20);
				if (e.x != rect.x) {
					sashData.left = new FormAttachment(0, e.x);
					parent.layout();
				}
			}			
		});
		data = new FormData();
		data.left = new FormAttachment(sash, 0);
		data.right = new FormAttachment(100, 0);
		data.top = new FormAttachment(0, 0);
		data.bottom = new FormAttachment(100, 0);
		panel.setLayoutData(data);
		
		/* Initialize Panel */
		panel.setLayout(new FillLayout(SWT.VERTICAL));
		Group htmlGroup = new Group(panel, SWT.NONE);
		htmlGroup.setText("setText");
		htmlText = new Text(htmlGroup, SWT.MULTI);
		htmlButton = new Button(htmlGroup, SWT.PUSH);
		htmlButton.setText("setText");
		GridLayout gridLayout = new GridLayout();
		htmlGroup.setLayout(gridLayout);
		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		htmlText.setLayoutData(gridData);
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.END;
		htmlButton.setLayoutData(gridData);
		htmlGroup.layout();
		
		Group scriptGroup = new Group(panel, SWT.NONE);
		scriptGroup.setText("execute");
		scriptText = new Text(scriptGroup, SWT.MULTI);
		scriptButton = new Button(scriptGroup, SWT.PUSH);
		scriptButton.setText("execute");
		gridLayout = new GridLayout();
		scriptGroup.setLayout(gridLayout);
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		scriptText.setLayoutData(gridData);
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.END;
		scriptButton.setLayoutData(gridData);
		scriptGroup.layout();
		
		browser.setText(html);
		htmlText.setText(html);
		scriptText.setText(script);
		parent.layout();
		
		Listener listener = new Listener() {
			public void handleEvent(Event e) {
				Widget w = e.widget;
				if (w == htmlButton) browser.setText(htmlText.getText());
				if (w == scriptButton) browser.execute(scriptText.getText());
			}
		};
		
		htmlButton.addListener(SWT.Selection, listener);
		scriptButton.addListener(SWT.Selection, listener);
	}
}