1 package org
.argeo
.swt
.desktop
;
3 import java
.io
.IOException
;
4 import java
.net
.MalformedURLException
;
5 import java
.net
.URISyntaxException
;
7 import java
.nio
.file
.Path
;
8 import java
.nio
.file
.Paths
;
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
;
29 public class MiniImageViewer
implements PaintListener
{
35 public MiniImageViewer(Composite parent
, int style
) {
36 parent
.setLayout(new GridLayout());
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() {
46 public void widgetSelected(SelectionEvent e
) {
47 FileDialog fileDialog
= new FileDialog(area
.getShell());
48 String path
= fileDialog
.open();
56 area
= new Canvas(parent
, SWT
.NONE
);
57 area
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
58 area
.addPaintListener(this);
61 protected void load(URL url
) {
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
73 public void paintControl(PaintEvent e
) {
74 e
.gc
.drawImage(image
, 0, 0);
78 protected Path
url2path(URL url
) {
80 Path path
= Paths
.get(url
.toURI());
82 } catch (URISyntaxException e
) {
83 throw new IllegalStateException("Cannot convert " + url
+ " to uri", e
);
87 public void setUrl(URL url
) {
93 public void setUrl(String url
) {
96 } catch (MalformedURLException e
) {
99 setUrl(new URL("file://" + url
));
101 } catch (MalformedURLException e1
) {
104 throw new IllegalArgumentException("Cannot interpret URL " + url
, e
);
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
);
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
);
122 shell
.setSize(new Point(800, 480));
123 while (!shell
.isDisposed()) {
124 if (!display
.readAndDispatch())