]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.editors/src/org/simantics/editors/Browser.java
Make transformChanged and dropQuality abstract in NavigationNode
[simantics/platform.git] / bundles / org.simantics.editors / src / org / simantics / editors / Browser.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.editors;
13
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;
42
43 /**
44  * Web Browser
45  * Visiblity of controls (back, forward, url, ...) can be configured with the BrowserInput
46  * 
47  * @author Marko Luukkainen <Marko.Luukkainen@vtt.fi>
48  *
49  */
50 public class Browser extends EditorPart {
51
52     public static final String ID = "org.simantics.editors.browser";
53
54     org.eclipse.swt.browser.Browser browser;
55     ToolBar toolbar;
56     LocalResourceManager resourceManager;
57
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");
62
63     private Shell fsShell;
64
65     public Browser() {
66
67     }
68
69     @Override
70     public void doSave(IProgressMonitor monitor) {
71
72     }
73
74     @Override
75     public void doSaveAs() {
76
77     }
78
79     @Override
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");
84         setSite(site);
85         setInput(input);
86
87         // Set initial part name according to the name given by IEditorInput
88         setPartName(getEditorInput().getName());
89     }
90
91     @Override
92     public boolean isDirty() {
93         return false;
94     }
95
96     @Override
97     public boolean isSaveAsAllowed() {
98         return false;
99     }
100
101     @Override
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);
108
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();
112         if (fullscreen) {
113             fsShell = new Shell(parent.getDisplay());
114
115             fsShell.setLayout(new GridLayout(1,false));
116             fsShell.setLayout(new FillLayout());
117             parent = new Composite(fsShell,SWT.NONE);
118             fsShell.open();
119             fsShell.setFullScreen(true);
120             parent.setLayout(new FillLayout());
121         }
122
123         String url = input.getUrl().toString();
124         boolean showControls = input.isShowControls();
125         if (showControls) {
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");
145
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);
153
154             Label labelAddress = new Label(composite, SWT.NONE);
155             labelAddress.setText("Address");
156
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);
163
164             browser = new org.eclipse.swt.browser.Browser(composite, input.getBrowserStyle());
165
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);
173
174             final Label statusl = new Label(composite, SWT.NONE);
175             data = new GridData(GridData.FILL_HORIZONTAL);
176             data.horizontalSpan = 2;
177             statusl.setLayoutData(data);
178
179             final ProgressBar progressBar = new ProgressBar(composite, SWT.NONE);
180             data = new GridData();
181             data.horizontalAlignment = GridData.END;
182             progressBar.setLayoutData(data);
183
184             browser.addProgressListener(new ProgressListener() {
185                 @Override
186                 public void changed(ProgressEvent event) {
187                     if (event.total == 0)
188                         return;
189                     int ratio = event.current * 100 / event.total;
190                     progressBar.setSelection(ratio);
191                 }
192
193                 @Override
194                 public void completed(ProgressEvent event) {
195                     progressBar.setSelection(0);
196                 }
197             });
198             browser.addStatusTextListener(new StatusTextListener() {
199                 @Override
200                 public void changed(StatusTextEvent event) {
201                     statusl.setText(event.text);
202                 }
203             });
204             browser.addLocationListener(new LocationListener() {
205                 @Override
206                 public void changed(LocationEvent event) {
207                     if (event.top)
208                         location.setText(event.location);
209                     scheduleSetPartName();
210                 }
211
212                 @Override
213                 public void changing(LocationEvent event) {
214                 }
215             });
216
217             Listener listener = new Listener() {
218                 @Override
219                 public void handleEvent(Event event) {
220                     ToolItem item = (ToolItem) event.widget;
221                     String string = item.getText();
222                     if (string.equals("Back"))
223                         browser.back();
224                     else if (string.equals("Forward"))
225                         browser.forward();
226                     else if (string.equals("Stop"))
227                         browser.stop();
228                     else if (string.equals("Refresh"))
229                         browser.refresh();
230                     // else if (string.equals("Go"))
231                     // browser.setUrl(location.getText());
232                 }
233             };
234
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() {
242                 @Override
243                 public void changed(LocationEvent event) {
244                     scheduleSetPartName();
245                 }
246                 @Override
247                 public void changing(LocationEvent event) {
248                 }
249             });
250         }
251
252         browser.setUrl(url);
253
254         if (fullscreen) {
255             fsShell.layout(true, true);
256         }
257     }
258
259     protected void scheduleSetPartName() {
260         browser.getDisplay().asyncExec(new Runnable() {
261             @Override
262             public void run() {
263                 if (browser.isDisposed())
264                     return;
265                 String name = getEditorInput().getName();
266                 setPartName(name);
267             }
268         });
269     }
270
271     @Override
272     public void setFocus() {
273         browser.setFocus();
274     }
275
276     @Override
277     public void dispose() {
278         if (fsShell != null)
279             fsShell.dispose();
280         super.dispose();
281     }
282
283     public org.eclipse.swt.browser.Browser getBrowser() {
284         return browser;
285     }
286
287 }