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