1 package org
.argeo
.minidesktop
;
3 import java
.io
.BufferedReader
;
4 import java
.io
.BufferedWriter
;
5 import java
.io
.FileNotFoundException
;
6 import java
.io
.IOException
;
7 import java
.io
.InputStreamReader
;
8 import java
.io
.OutputStreamWriter
;
9 import java
.net
.MalformedURLException
;
10 import java
.net
.URISyntaxException
;
12 import java
.nio
.charset
.StandardCharsets
;
13 import java
.nio
.file
.Files
;
14 import java
.nio
.file
.Path
;
15 import java
.nio
.file
.Paths
;
17 import org
.eclipse
.swt
.SWT
;
18 import org
.eclipse
.swt
.events
.SelectionAdapter
;
19 import org
.eclipse
.swt
.events
.SelectionEvent
;
20 import org
.eclipse
.swt
.graphics
.Point
;
21 import org
.eclipse
.swt
.layout
.GridData
;
22 import org
.eclipse
.swt
.layout
.GridLayout
;
23 import org
.eclipse
.swt
.layout
.RowLayout
;
24 import org
.eclipse
.swt
.widgets
.Button
;
25 import org
.eclipse
.swt
.widgets
.Composite
;
26 import org
.eclipse
.swt
.widgets
.Display
;
27 import org
.eclipse
.swt
.widgets
.FileDialog
;
28 import org
.eclipse
.swt
.widgets
.Shell
;
29 import org
.eclipse
.swt
.widgets
.Text
;
31 public class MiniTextEditor
{
35 public MiniTextEditor(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(text
.getShell());
48 String path
= fileDialog
.open();
56 Button save
= new Button(toolBar
, SWT
.FLAT
);
57 save
.setText("\u2193");// down arrow
58 // save.setText("\u1F609");// emoji
59 save
.addSelectionListener(new SelectionAdapter() {
62 public void widgetSelected(SelectionEvent e
) {
68 text
= new Text(parent
, SWT
.WRAP
| SWT
.MULTI
| SWT
.BORDER
| SWT
.V_SCROLL
);
69 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
72 protected void load(URL url
) {
74 // TODO deal with encoding and binary data
75 try (BufferedReader in
= new BufferedReader(new InputStreamReader(url
.openStream(), StandardCharsets
.UTF_8
))) {
77 while ((line
= in
.readLine()) != null) {
78 text
.append(line
+ "\n");
80 text
.setEditable(true);
81 } catch (IOException e
) {
82 if (e
instanceof FileNotFoundException
) {
83 Path path
= url2path(url
);
85 Files
.createFile(path
);
88 } catch (IOException e1
) {
92 text
.setText(e
.getMessage());
93 text
.setEditable(false);
95 // throw new IllegalStateException("Cannot load " + url, e);
99 protected Path
url2path(URL url
) {
101 Path path
= Paths
.get(url
.toURI());
103 } catch (URISyntaxException e
) {
104 throw new IllegalStateException("Cannot convert " + url
+ " to uri", e
);
108 protected void save(URL url
) {
109 if (!url
.getProtocol().equals("file"))
110 throw new IllegalArgumentException(url
.getProtocol() + " protocol is not supported for write");
111 Path path
= url2path(url
);
112 try (BufferedWriter out
= new BufferedWriter(new OutputStreamWriter(Files
.newOutputStream(path
)))) {
113 out
.write(text
.getText());
114 } catch (IOException e
) {
115 throw new IllegalStateException("Cannot save " + url
+ " to " + path
, e
);
119 public void setUrl(URL url
) {
125 public void setUrl(String url
) {
127 setUrl(new URL(url
));
128 } catch (MalformedURLException e
) {
131 setUrl(new URL("file://" + url
));
133 } catch (MalformedURLException e1
) {
136 throw new IllegalArgumentException("Cannot interpret URL " + url
, e
);
140 public static void main(String
[] args
) {
141 Display display
= Display
.getCurrent() == null ?
new Display() : Display
.getCurrent();
142 Shell shell
= new Shell(display
, SWT
.SHELL_TRIM
);
144 MiniTextEditor miniBrowser
= new MiniTextEditor(shell
, SWT
.NONE
);
145 String url
= args
.length
> 0 ? args
[0] : "";
146 if (!url
.trim().equals("")) {
147 miniBrowser
.setUrl(url
);
154 shell
.setSize(new Point(800, 480));
155 while (!shell
.isDisposed()) {
156 if (!display
.readAndDispatch())