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