1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.editors;
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.jface.resource.ImageDescriptor;
16 import org.eclipse.jface.resource.JFaceResources;
17 import org.eclipse.jface.resource.LocalResourceManager;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.browser.LocationEvent;
20 import org.eclipse.swt.browser.LocationListener;
21 import org.eclipse.swt.browser.ProgressEvent;
22 import org.eclipse.swt.browser.ProgressListener;
23 import org.eclipse.swt.browser.StatusTextEvent;
24 import org.eclipse.swt.browser.StatusTextListener;
25 import org.eclipse.swt.graphics.Image;
26 import org.eclipse.swt.layout.FillLayout;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Event;
31 import org.eclipse.swt.widgets.Label;
32 import org.eclipse.swt.widgets.Listener;
33 import org.eclipse.swt.widgets.ProgressBar;
34 import org.eclipse.swt.widgets.Shell;
35 import org.eclipse.swt.widgets.Text;
36 import org.eclipse.swt.widgets.ToolBar;
37 import org.eclipse.swt.widgets.ToolItem;
38 import org.eclipse.ui.IEditorInput;
39 import org.eclipse.ui.IEditorSite;
40 import org.eclipse.ui.PartInitException;
41 import org.eclipse.ui.part.EditorPart;
45 * Visiblity of controls (back, forward, url, ...) can be configured with the BrowserInput
47 * @author Marko Luukkainen <Marko.Luukkainen@vtt.fi>
50 public class Browser extends EditorPart {
52 public static final String ID = "org.simantics.editors.browser";
54 org.eclipse.swt.browser.Browser browser;
56 LocalResourceManager resourceManager;
58 private final ImageDescriptor BACK_IMAGE = Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/browser/arrow_left.png");
59 private final ImageDescriptor FORWARD_IMAGE = Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/browser/arrow_right.png");
60 private final ImageDescriptor STOP_IMAGE = Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/browser/stop.png");
61 private final ImageDescriptor REFRESH_IMAGE = Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/browser/arrow_rotate_clockwise.png");
63 private Shell fsShell;
70 public void doSave(IProgressMonitor monitor) {
75 public void doSaveAs() {
80 public void init(IEditorSite site, IEditorInput input)
81 throws PartInitException {
82 if (!(input instanceof BrowserInput))
83 throw new PartInitException("Input must be instance of BrowserInput");
87 // Set initial part name according to the name given by IEditorInput
88 setPartName(getEditorInput().getName());
92 public boolean isDirty() {
97 public boolean isSaveAsAllowed() {
102 public void createPartControl(Composite parent) {
103 resourceManager = new LocalResourceManager(JFaceResources.getResources(), parent);
104 Image backImage = resourceManager.createImage(BACK_IMAGE);
105 Image forwardImage = resourceManager.createImage(FORWARD_IMAGE);
106 Image stopImage = resourceManager.createImage(STOP_IMAGE);
107 Image refreshImage = resourceManager.createImage(REFRESH_IMAGE);
109 // http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet128.java
110 BrowserInput input = (BrowserInput)getEditorInput();
111 boolean fullscreen = input.isFullscreen();
113 fsShell = new Shell(parent.getDisplay());
115 fsShell.setLayout(new GridLayout(1,false));
116 fsShell.setLayout(new FillLayout());
117 parent = new Composite(fsShell,SWT.NONE);
119 fsShell.setFullScreen(true);
120 parent.setLayout(new FillLayout());
123 String url = input.getUrl().toString();
124 boolean showControls = input.isShowControls();
126 Composite composite = new Composite(parent, SWT.NONE);
127 GridLayout gridLayout = new GridLayout();
128 gridLayout.numColumns = 3;
129 composite.setLayout(gridLayout);
130 toolbar = new ToolBar(composite, SWT.NONE);
131 ToolItem itemBack = new ToolItem(toolbar, SWT.PUSH);
132 itemBack.setText("Back");
133 itemBack.setImage(backImage);
134 ToolItem itemForward = new ToolItem(toolbar, SWT.PUSH);
135 itemForward.setText("Forward");
136 itemForward.setImage(forwardImage);
137 ToolItem itemStop = new ToolItem(toolbar, SWT.PUSH);
138 itemStop.setText("Stop");
139 itemStop.setImage(stopImage);
140 ToolItem itemRefresh = new ToolItem(toolbar, SWT.PUSH);
141 itemRefresh.setText("Refresh");
142 itemRefresh.setImage(refreshImage);
143 // ToolItem itemGo = new ToolItem(toolbar, SWT.PUSH);
144 // itemGo.setText("Go");
146 GridData data = new GridData();
147 data.grabExcessHorizontalSpace = true;
148 data.grabExcessVerticalSpace = false;
149 data.horizontalAlignment = SWT.FILL;
150 data.verticalAlignment = SWT.CENTER;
151 data.horizontalSpan = 3;
152 toolbar.setLayoutData(data);
154 Label labelAddress = new Label(composite, SWT.NONE);
155 labelAddress.setText("Address");
157 final Text location = new Text(composite, SWT.BORDER);
158 data = new GridData();
159 data.horizontalAlignment = GridData.FILL;
160 data.horizontalSpan = 2;
161 data.grabExcessHorizontalSpace = true;
162 location.setLayoutData(data);
164 browser = new org.eclipse.swt.browser.Browser(composite, input.getBrowserStyle());
166 data = new GridData();
167 data.grabExcessHorizontalSpace = true;
168 data.grabExcessVerticalSpace = true;
169 data.horizontalAlignment = SWT.FILL;
170 data.verticalAlignment = SWT.FILL;
171 data.horizontalSpan = 3;
172 browser.setLayoutData(data);
174 final Label statusl = new Label(composite, SWT.NONE);
175 data = new GridData(GridData.FILL_HORIZONTAL);
176 data.horizontalSpan = 2;
177 statusl.setLayoutData(data);
179 final ProgressBar progressBar = new ProgressBar(composite, SWT.NONE);
180 data = new GridData();
181 data.horizontalAlignment = GridData.END;
182 progressBar.setLayoutData(data);
184 browser.addProgressListener(new ProgressListener() {
186 public void changed(ProgressEvent event) {
187 if (event.total == 0)
189 int ratio = event.current * 100 / event.total;
190 progressBar.setSelection(ratio);
194 public void completed(ProgressEvent event) {
195 progressBar.setSelection(0);
198 browser.addStatusTextListener(new StatusTextListener() {
200 public void changed(StatusTextEvent event) {
201 statusl.setText(event.text);
204 browser.addLocationListener(new LocationListener() {
206 public void changed(LocationEvent event) {
208 location.setText(event.location);
209 scheduleSetPartName();
213 public void changing(LocationEvent event) {
217 Listener listener = new Listener() {
219 public void handleEvent(Event event) {
220 ToolItem item = (ToolItem) event.widget;
221 String string = item.getText();
222 if (string.equals("Back"))
224 else if (string.equals("Forward"))
226 else if (string.equals("Stop"))
228 else if (string.equals("Refresh"))
230 // else if (string.equals("Go"))
231 // browser.setUrl(location.getText());
235 itemBack.addListener(SWT.Selection, listener);
236 itemForward.addListener(SWT.Selection, listener);
237 itemStop.addListener(SWT.Selection, listener);
238 itemRefresh.addListener(SWT.Selection, listener);
239 } else { // !showControls
240 browser = new org.eclipse.swt.browser.Browser(parent, input.getBrowserStyle());
241 browser.addLocationListener(new LocationListener() {
243 public void changed(LocationEvent event) {
244 scheduleSetPartName();
247 public void changing(LocationEvent event) {
255 fsShell.layout(true, true);
259 protected void scheduleSetPartName() {
260 browser.getDisplay().asyncExec(new Runnable() {
263 if (browser.isDisposed())
265 String name = getEditorInput().getName();
272 public void setFocus() {
277 public void dispose() {
283 public org.eclipse.swt.browser.Browser getBrowser() {