]> git.argeo.org Git - lgpl/argeo-commons.git/blob - swt/org.argeo.swt.minidesktop/src/org/argeo/minidesktop/MiniTextEditor.java
Move Equinox specific code to the appropriate variant
[lgpl/argeo-commons.git] / swt / org.argeo.swt.minidesktop / src / org / argeo / minidesktop / MiniTextEditor.java
1 package org.argeo.minidesktop;
2
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;
11 import java.net.URL;
12 import java.nio.charset.StandardCharsets;
13 import java.nio.file.Files;
14 import java.nio.file.Path;
15 import java.nio.file.Paths;
16
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;
30
31 public class MiniTextEditor {
32 private URL url;
33 private Text text;
34
35 public MiniTextEditor(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(text.getShell());
48 String path = fileDialog.open();
49 if (path != null) {
50 setUrl(path);
51 }
52 }
53
54 });
55
56 Button save = new Button(toolBar, SWT.FLAT);
57 save.setText("\u2193");// down arrow
58 // save.setText("\u1F609");// emoji
59 save.addSelectionListener(new SelectionAdapter() {
60
61 @Override
62 public void widgetSelected(SelectionEvent e) {
63 save(url);
64 }
65
66 });
67
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));
70 }
71
72 protected void load(URL url) {
73 text.setText("");
74 // TODO deal with encoding and binary data
75 try (BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8))) {
76 String line = null;
77 while ((line = in.readLine()) != null) {
78 text.append(line + "\n");
79 }
80 text.setEditable(true);
81 } catch (IOException e) {
82 if (e instanceof FileNotFoundException) {
83 Path path = url2path(url);
84 try {
85 Files.createFile(path);
86 load(url);
87 return;
88 } catch (IOException e1) {
89 e = e1;
90 }
91 }
92 text.setText(e.getMessage());
93 text.setEditable(false);
94 e.printStackTrace();
95 // throw new IllegalStateException("Cannot load " + url, e);
96 }
97 }
98
99 protected Path url2path(URL url) {
100 try {
101 Path path = Paths.get(url.toURI());
102 return path;
103 } catch (URISyntaxException e) {
104 throw new IllegalStateException("Cannot convert " + url + " to uri", e);
105 }
106 }
107
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);
116 }
117 }
118
119 public void setUrl(URL url) {
120 this.url = url;
121 if (text != null)
122 load(url);
123 }
124
125 public void setUrl(String url) {
126 try {
127 setUrl(new URL(url));
128 } catch (MalformedURLException e) {
129 // try with http
130 try {
131 setUrl(new URL("file://" + url));
132 return;
133 } catch (MalformedURLException e1) {
134 // nevermind...
135 }
136 throw new IllegalArgumentException("Cannot interpret URL " + url, e);
137 }
138 }
139
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);
143
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);
148 shell.setText(url);
149 } else {
150 shell.setText("*");
151 }
152
153 shell.open();
154 shell.setSize(new Point(800, 480));
155 while (!shell.isDisposed()) {
156 if (!display.readAndDispatch())
157 display.sleep();
158 }
159 }
160
161 }