View Javadoc
1   package org.argeo.swt.desktop;
2   
3   import java.io.IOException;
4   import java.net.MalformedURLException;
5   import java.net.URISyntaxException;
6   import java.net.URL;
7   import java.nio.file.Path;
8   import java.nio.file.Paths;
9   
10  import org.eclipse.swt.SWT;
11  import org.eclipse.swt.events.PaintEvent;
12  import org.eclipse.swt.events.PaintListener;
13  import org.eclipse.swt.events.SelectionAdapter;
14  import org.eclipse.swt.events.SelectionEvent;
15  import org.eclipse.swt.graphics.Image;
16  import org.eclipse.swt.graphics.ImageData;
17  import org.eclipse.swt.graphics.ImageLoader;
18  import org.eclipse.swt.graphics.Point;
19  import org.eclipse.swt.layout.GridData;
20  import org.eclipse.swt.layout.GridLayout;
21  import org.eclipse.swt.layout.RowLayout;
22  import org.eclipse.swt.widgets.Button;
23  import org.eclipse.swt.widgets.Canvas;
24  import org.eclipse.swt.widgets.Composite;
25  import org.eclipse.swt.widgets.Display;
26  import org.eclipse.swt.widgets.FileDialog;
27  import org.eclipse.swt.widgets.Shell;
28  
29  public class MiniImageViewer implements PaintListener {
30  	private URL url;
31  	private Canvas area;
32  
33  	private Image image;
34  
35  	public MiniImageViewer(Composite parent, int style) {
36  		parent.setLayout(new GridLayout());
37  
38  		Composite toolBar = new Composite(parent, SWT.NONE);
39  		toolBar.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
40  		toolBar.setLayout(new RowLayout());
41  		Button load = new Button(toolBar, SWT.FLAT);
42  		load.setText("\u2191");// up arrow
43  		load.addSelectionListener(new SelectionAdapter() {
44  
45  			@Override
46  			public void widgetSelected(SelectionEvent e) {
47  				FileDialog fileDialog = new FileDialog(area.getShell());
48  				String path = fileDialog.open();
49  				if (path != null) {
50  					setUrl(path);
51  				}
52  			}
53  
54  		});
55  
56  		area = new Canvas(parent, SWT.NONE);
57  		area.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
58  		area.addPaintListener(this);
59  	}
60  
61  	protected void load(URL url) {
62  		try {
63  			ImageLoader imageLoader = new ImageLoader();
64  			ImageData[] data = imageLoader.load(url.openStream());
65  			image = new Image(area.getDisplay(), data[0]);
66  		} catch (IOException e) {
67  			// TODO Auto-generated catch block
68  			e.printStackTrace();
69  		}
70  	}
71  
72  	@Override
73  	public void paintControl(PaintEvent e) {
74  		e.gc.drawImage(image, 0, 0);
75  
76  	}
77  
78  	protected Path url2path(URL url) {
79  		try {
80  			Path path = Paths.get(url.toURI());
81  			return path;
82  		} catch (URISyntaxException e) {
83  			throw new IllegalStateException("Cannot convert " + url + " to uri", e);
84  		}
85  	}
86  
87  	public void setUrl(URL url) {
88  		this.url = url;
89  		if (area != null)
90  			load(this.url);
91  	}
92  
93  	public void setUrl(String url) {
94  		try {
95  			setUrl(new URL(url));
96  		} catch (MalformedURLException e) {
97  			// try with http
98  			try {
99  				setUrl(new URL("file://" + url));
100 				return;
101 			} catch (MalformedURLException e1) {
102 				// nevermind...
103 			}
104 			throw new IllegalArgumentException("Cannot interpret URL " + url, e);
105 		}
106 	}
107 
108 	public static void main(String[] args) {
109 		Display display = Display.getCurrent() == null ? new Display() : Display.getCurrent();
110 		Shell shell = new Shell(display, SWT.SHELL_TRIM);
111 
112 		MiniImageViewer miniBrowser = new MiniImageViewer(shell, SWT.NONE);
113 		String url = args.length > 0 ? args[0] : "";
114 		if (!url.trim().equals("")) {
115 			miniBrowser.setUrl(url);
116 			shell.setText(url);
117 		} else {
118 			shell.setText("*");
119 		}
120 
121 		shell.open();
122 		shell.setSize(new Point(800, 480));
123 		while (!shell.isDisposed()) {
124 			if (!display.readAndDispatch())
125 				display.sleep();
126 		}
127 	}
128 
129 }