1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpeclipse.views.browser;
12 import java.text.MessageFormat;
13 import java.util.MissingResourceException;
14 import java.util.ResourceBundle;
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.SWTError;
17 import org.eclipse.swt.browser.Browser;
18 import org.eclipse.swt.browser.CloseWindowListener;
19 import org.eclipse.swt.browser.LocationEvent;
20 import org.eclipse.swt.browser.LocationListener;
21 import org.eclipse.swt.browser.OpenWindowListener;
22 import org.eclipse.swt.browser.ProgressEvent;
23 import org.eclipse.swt.browser.ProgressListener;
24 import org.eclipse.swt.browser.StatusTextEvent;
25 import org.eclipse.swt.browser.StatusTextListener;
26 import org.eclipse.swt.browser.TitleEvent;
27 import org.eclipse.swt.browser.TitleListener;
28 import org.eclipse.swt.browser.VisibilityWindowListener;
29 import org.eclipse.swt.browser.WindowEvent;
30 import org.eclipse.swt.graphics.Image;
31 import org.eclipse.swt.graphics.ImageData;
32 import org.eclipse.swt.graphics.Point;
33 import org.eclipse.swt.graphics.Rectangle;
34 import org.eclipse.swt.layout.FillLayout;
35 import org.eclipse.swt.layout.FormAttachment;
36 import org.eclipse.swt.layout.FormData;
37 import org.eclipse.swt.layout.FormLayout;
38 import org.eclipse.swt.widgets.Canvas;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.swt.widgets.Display;
41 import org.eclipse.swt.widgets.Event;
42 import org.eclipse.swt.widgets.Label;
43 import org.eclipse.swt.widgets.Listener;
44 import org.eclipse.swt.widgets.ProgressBar;
45 import org.eclipse.swt.widgets.Shell;
46 import org.eclipse.swt.widgets.Text;
47 import org.eclipse.swt.widgets.ToolBar;
48 import org.eclipse.swt.widgets.ToolItem;
50 public class PHPBrowser {
51 static ResourceBundle resourceBundle = ResourceBundle
52 .getBundle("net.sourceforge.phpeclipse.views.browser.browser");
58 static final String[] imageLocations = {"eclipse01.bmp", "eclipse02.bmp",
59 "eclipse03.bmp", "eclipse04.bmp", "eclipse05.bmp", "eclipse06.bmp",
60 "eclipse07.bmp", "eclipse08.bmp", "eclipse09.bmp", "eclipse10.bmp",
61 "eclipse11.bmp", "eclipse12.bmp",};
62 static final String iconLocation = "document.gif";
64 * Creates an instance of a ControlExample embedded inside the supplied
68 * the container of the example
70 public PHPBrowser(Composite parent) {
72 final Display display = parent.getDisplay();
73 FormLayout layout = new FormLayout();
74 parent.setLayout(layout);
75 ToolBar toolbar = new ToolBar(parent, SWT.NONE);
76 final ToolItem itemBack = new ToolItem(toolbar, SWT.PUSH);
77 itemBack.setText(getResourceString("Back"));
78 final ToolItem itemForward = new ToolItem(toolbar, SWT.PUSH);
79 itemForward.setText(getResourceString("Forward"));
80 final ToolItem itemStop = new ToolItem(toolbar, SWT.PUSH);
81 itemStop.setText(getResourceString("Stop"));
82 final ToolItem itemRefresh = new ToolItem(toolbar, SWT.PUSH);
83 itemRefresh.setText(getResourceString("Refresh"));
84 final ToolItem itemGo = new ToolItem(toolbar, SWT.PUSH);
85 itemGo.setText(getResourceString("Go"));
86 location = new Text(parent, SWT.BORDER);
87 final Canvas canvas = new Canvas(parent, SWT.NO_BACKGROUND);
88 final Rectangle rect = images[0].getBounds();
89 canvas.addListener(SWT.Paint, new Listener() {
90 public void handleEvent(Event e) {
91 Point pt = canvas.getSize();
92 e.gc.drawImage(images[index], 0, 0, rect.width, rect.height, 0, 0,
96 canvas.addListener(SWT.MouseDown, new Listener() {
97 public void handleEvent(Event e) {
98 browser.setUrl(getResourceString("Startup"));
101 display.asyncExec(new Runnable() {
103 if (canvas.isDisposed())
107 if (index == images.length)
111 display.timerExec(150, this);
114 final Label status = new Label(parent, SWT.NONE);
115 final ProgressBar progressBar = new ProgressBar(parent, SWT.NONE);
116 FormData data = new FormData();
117 data.top = new FormAttachment(0, 5);
118 toolbar.setLayoutData(data);
119 data = new FormData();
120 data.left = new FormAttachment(0, 0);
121 data.right = new FormAttachment(100, 0);
122 data.top = new FormAttachment(canvas, 5, SWT.DEFAULT);
123 data.bottom = new FormAttachment(status, -5, SWT.DEFAULT);
125 browser = new Browser(parent, SWT.NONE);
126 browser.setLayoutData(data);
127 } catch (SWTError e) {
128 /* Browser widget could not be instantiated */
129 Label label = new Label(parent, SWT.CENTER | SWT.WRAP);
130 label.setText(getResourceString("BrowserNotCreated"));
131 label.setLayoutData(data);
133 data = new FormData();
136 data.top = new FormAttachment(0, 5);
137 data.right = new FormAttachment(100, -5);
138 canvas.setLayoutData(data);
139 data = new FormData();
140 data.top = new FormAttachment(toolbar, 0, SWT.TOP);
141 data.left = new FormAttachment(toolbar, 5, SWT.RIGHT);
142 data.right = new FormAttachment(canvas, -5, SWT.DEFAULT);
143 location.setLayoutData(data);
144 data = new FormData();
145 data.left = new FormAttachment(0, 5);
146 data.right = new FormAttachment(progressBar, 0, SWT.DEFAULT);
147 data.bottom = new FormAttachment(100, -5);
148 status.setLayoutData(data);
149 data = new FormData();
150 data.right = new FormAttachment(100, -5);
151 data.bottom = new FormAttachment(100, -5);
152 progressBar.setLayoutData(data);
153 if (browser != null) {
154 itemBack.setEnabled(browser.isBackEnabled());
155 itemForward.setEnabled(browser.isForwardEnabled());
156 Listener listener = new Listener() {
157 public void handleEvent(Event event) {
158 ToolItem item = (ToolItem) event.widget;
159 if (item == itemBack)
161 else if (item == itemForward)
163 else if (item == itemStop)
165 else if (item == itemRefresh)
167 else if (item == itemGo)
168 browser.setUrl(location.getText());
171 browser.addLocationListener(new LocationListener() {
172 public void changed(LocationEvent event) {
175 location.setText(event.location);
177 public void changing(LocationEvent event) {
180 browser.addProgressListener(new ProgressListener() {
181 public void changed(ProgressEvent event) {
182 if (event.total == 0)
184 int ratio = event.current * 100 / event.total;
185 progressBar.setSelection(ratio);
186 busy = event.current != event.total;
192 public void completed(ProgressEvent event) {
193 itemBack.setEnabled(browser.isBackEnabled());
194 itemForward.setEnabled(browser.isForwardEnabled());
195 progressBar.setSelection(0);
201 browser.addStatusTextListener(new StatusTextListener() {
202 public void changed(StatusTextEvent event) {
203 status.setText(event.text);
206 if (parent instanceof Shell) {
207 final Shell shell = (Shell) parent;
208 browser.addTitleListener(new TitleListener() {
209 public void changed(TitleEvent event) {
210 shell.setText(event.title + " - "
211 + getResourceString("window.title"));
215 itemBack.addListener(SWT.Selection, listener);
216 itemForward.addListener(SWT.Selection, listener);
217 itemStop.addListener(SWT.Selection, listener);
218 itemRefresh.addListener(SWT.Selection, listener);
219 itemGo.addListener(SWT.Selection, listener);
220 location.addListener(SWT.DefaultSelection, new Listener() {
221 public void handleEvent(Event e) {
222 browser.setUrl(location.getText());
225 initialize(display, browser);
226 browser.setUrl(getResourceString("Startup"));
230 * Gets a string from the resource bundle. We don't want to crash because of
231 * a missing String. Returns the key if not found.
233 static String getResourceString(String key) {
235 return resourceBundle.getString(key);
236 } catch (MissingResourceException e) {
238 } catch (NullPointerException e) {
239 return "!" + key + "!";
243 * Gets a string from the resource bundle and binds it with the given
244 * arguments. If the key is not found, return the key.
246 static String getResourceString(String key, Object[] args) {
248 return MessageFormat.format(getResourceString(key), args);
249 } catch (MissingResourceException e) {
251 } catch (NullPointerException e) {
252 return "!" + key + "!";
255 static void initialize(final Display display, Browser browser) {
256 browser.addOpenWindowListener(new OpenWindowListener() {
257 public void open(WindowEvent event) {
258 Shell shell = new Shell(display);
259 shell.setLayout(new FillLayout());
260 Browser browser = new Browser(shell, SWT.NONE);
261 initialize(display, browser);
262 event.browser = browser;
265 browser.addVisibilityWindowListener(new VisibilityWindowListener() {
266 public void hide(WindowEvent event) {
268 public void show(WindowEvent event) {
269 Browser browser = (Browser) event.widget;
270 Shell shell = browser.getShell();
271 if (event.location != null)
272 shell.setLocation(event.location);
273 if (event.size != null) {
274 Point size = event.size;
275 shell.setSize(shell.computeSize(size.x, size.y));
280 browser.addCloseWindowListener(new CloseWindowListener() {
281 public void close(WindowEvent event) {
282 Browser browser = (Browser) event.widget;
283 Shell shell = browser.getShell();
289 * Disposes of all resources associated with a particular instance of the
292 public void dispose() {
296 * Frees the resources
298 void freeResources() {
299 if (images != null) {
300 for (int i = 0; i < images.length; ++i) {
301 final Image image = images[i];
311 public void setFocus() {
315 * Loads the resources
317 void initResources() {
318 final Class clazz = this.getClass();
319 if (resourceBundle != null) {
321 if (images == null) {
322 images = new Image[imageLocations.length];
323 for (int i = 0; i < imageLocations.length; ++i) {
324 ImageData source = new ImageData(clazz
325 .getResourceAsStream(imageLocations[i]));
326 ImageData mask = source.getTransparencyMask();
327 images[i] = new Image(null, source, mask);
331 } catch (Throwable t) {
334 String error = (resourceBundle != null)
335 ? getResourceString("error.CouldNotLoadResources")
336 : "Unable to load resources";
338 throw new RuntimeException(error);
340 public static void main(String[] args) {
341 Display display = new Display();
342 Shell shell = new Shell(display);
343 shell.setLayout(new FillLayout());
344 shell.setText(getResourceString("window.title"));
345 PHPBrowser instance = new PHPBrowser(shell);
346 Image icon = new Image(display, PHPBrowser.class
347 .getResourceAsStream(iconLocation));
348 shell.setImage(icon);
350 while (!shell.isDisposed()) {
351 if (!display.readAndDispatch())