]> git.argeo.org Git - lgpl/argeo-commons.git/blob - rcp/org.argeo.swt.minidesktop/src/org/argeo/minidesktop/MiniExplorer.java
Work on typing
[lgpl/argeo-commons.git] / rcp / org.argeo.swt.minidesktop / src / org / argeo / minidesktop / MiniExplorer.java
1 package org.argeo.minidesktop;
2
3 import java.io.IOException;
4 import java.nio.file.DirectoryStream;
5 import java.nio.file.Files;
6 import java.nio.file.Path;
7 import java.nio.file.Paths;
8
9 import org.eclipse.swt.SWT;
10 import org.eclipse.swt.events.MouseAdapter;
11 import org.eclipse.swt.events.MouseEvent;
12 import org.eclipse.swt.events.SelectionAdapter;
13 import org.eclipse.swt.events.SelectionEvent;
14 import org.eclipse.swt.graphics.Point;
15 import org.eclipse.swt.layout.FillLayout;
16 import org.eclipse.swt.layout.GridData;
17 import org.eclipse.swt.layout.GridLayout;
18 import org.eclipse.swt.program.Program;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Display;
21 import org.eclipse.swt.widgets.Shell;
22 import org.eclipse.swt.widgets.Table;
23 import org.eclipse.swt.widgets.TableItem;
24 import org.eclipse.swt.widgets.Text;
25
26 public class MiniExplorer {
27 private Path path;
28 private Text addressT;
29 private Table browser;
30
31 private boolean showHidden = false;
32
33 public MiniExplorer(Composite parent, String url) {
34 this(parent);
35 setUrl(url);
36 }
37
38 public MiniExplorer(Composite parent) {
39 parent.setLayout(noSpaceGridLayout(new GridLayout()));
40
41 Composite toolBar = new Composite(parent, SWT.NONE);
42 toolBar.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
43 toolBar.setLayout(new FillLayout());
44 addressT = new Text(toolBar, SWT.SINGLE);
45 addressT.addSelectionListener(new SelectionAdapter() {
46
47 @Override
48 public void widgetDefaultSelected(SelectionEvent e) {
49 setUrl(addressT.getText().trim());
50 }
51 });
52 browser = createTable(parent, this.path);
53
54 }
55
56 public void setPath(Path url) {
57 this.path = url;
58 if (addressT != null)
59 addressT.setText(url.toString());
60 if (browser != null) {
61 Composite parent = browser.getParent();
62 browser.dispose();
63 browser = createTable(parent, this.path);
64 parent.layout(true, true);
65 }
66 pathChanged(url);
67 }
68
69 protected void pathChanged(Path path) {
70
71 }
72
73 protected Table createTable(Composite parent, Path path) {
74 Table table = new Table(parent, SWT.BORDER);
75 table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
76 table.addMouseListener(new MouseAdapter() {
77
78 @Override
79 public void mouseDoubleClick(MouseEvent e) {
80 Point pt = new Point(e.x, e.y);
81 TableItem item = table.getItem(pt);
82 Path path = (Path) item.getData();
83 if (Files.isDirectory(path)) {
84 setPath(path);
85 } else {
86 Program.launch(path.toString());
87 }
88 }
89 });
90
91 if (path != null) {
92 if (path.getParent() != null) {
93 TableItem parentTI = new TableItem(table, SWT.NONE);
94 parentTI.setText("..");
95 parentTI.setData(path.getParent());
96 }
97
98 try {
99 // directories
100 DirectoryStream<Path> ds = Files.newDirectoryStream(path, p -> Files.isDirectory(p) && isShown(p));
101 ds.forEach(p -> {
102 TableItem ti = new TableItem(table, SWT.NONE);
103 ti.setText(p.getFileName().toString() + "/");
104 ti.setData(p);
105 });
106 // files
107 ds = Files.newDirectoryStream(path, p -> !Files.isDirectory(p) && isShown(p));
108 ds.forEach(p -> {
109 TableItem ti = new TableItem(table, SWT.NONE);
110 ti.setText(p.getFileName().toString());
111 ti.setData(p);
112 });
113 } catch (IOException e1) {
114 // TODO Auto-generated catch block
115 e1.printStackTrace();
116 }
117 }
118 return table;
119 }
120
121 protected boolean isShown(Path path) {
122 if (showHidden)
123 return true;
124 try {
125 return !Files.isHidden(path);
126 } catch (IOException e) {
127 throw new IllegalArgumentException("Cannot check " + path, e);
128 }
129 }
130
131 public void setUrl(String url) {
132 setPath(Paths.get(url));
133 }
134
135 private static GridLayout noSpaceGridLayout(GridLayout layout) {
136 layout.horizontalSpacing = 0;
137 layout.verticalSpacing = 0;
138 layout.marginWidth = 0;
139 layout.marginHeight = 0;
140 return layout;
141 }
142
143 public static void main(String[] args) {
144 Display display = Display.getCurrent() == null ? new Display() : Display.getCurrent();
145 Shell shell = new Shell(display, SWT.SHELL_TRIM);
146
147 String url = args.length > 0 ? args[0] : System.getProperty("user.home");
148 new MiniExplorer(shell, url) {
149
150 @Override
151 protected void pathChanged(Path path) {
152 shell.setText(path.toString());
153 }
154
155 };
156
157 shell.open();
158 shell.setSize(new Point(800, 480));
159 while (!shell.isDisposed()) {
160 if (!display.readAndDispatch())
161 display.sleep();
162 }
163 }
164
165 }