]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.swt.minidesktop/src/org/argeo/minidesktop/MiniExplorer.java
1 package org
.argeo
.minidesktop
;
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
;
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
;
26 public class MiniExplorer
{
28 private Text addressT
;
29 private Table browser
;
31 private boolean showHidden
= false;
33 public MiniExplorer(Composite parent
, String url
) {
38 public MiniExplorer(Composite parent
) {
39 parent
.setLayout(noSpaceGridLayout(new GridLayout()));
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() {
48 public void widgetDefaultSelected(SelectionEvent e
) {
49 setUrl(addressT
.getText().trim());
52 browser
= createTable(parent
, this.path
);
56 public void setPath(Path url
) {
59 addressT
.setText(url
.toString());
60 if (browser
!= null) {
61 Composite parent
= browser
.getParent();
63 browser
= createTable(parent
, this.path
);
64 parent
.layout(true, true);
69 protected void pathChanged(Path path
) {
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() {
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
)) {
86 Program
.launch(path
.toString());
92 if (path
.getParent() != null) {
93 TableItem parentTI
= new TableItem(table
, SWT
.NONE
);
94 parentTI
.setText("..");
95 parentTI
.setData(path
.getParent());
100 DirectoryStream
<Path
> ds
= Files
.newDirectoryStream(path
, p
-> Files
.isDirectory(p
) && isShown(p
));
102 TableItem ti
= new TableItem(table
, SWT
.NONE
);
103 ti
.setText(p
.getFileName().toString() + "/");
107 ds
= Files
.newDirectoryStream(path
, p
-> !Files
.isDirectory(p
) && isShown(p
));
109 TableItem ti
= new TableItem(table
, SWT
.NONE
);
110 ti
.setText(p
.getFileName().toString());
113 } catch (IOException e1
) {
114 // TODO Auto-generated catch block
115 e1
.printStackTrace();
121 protected boolean isShown(Path path
) {
125 return !Files
.isHidden(path
);
126 } catch (IOException e
) {
127 throw new IllegalArgumentException("Cannot check " + path
, e
);
131 public void setUrl(String url
) {
132 setPath(Paths
.get(url
));
135 private static GridLayout
noSpaceGridLayout(GridLayout layout
) {
136 layout
.horizontalSpacing
= 0;
137 layout
.verticalSpacing
= 0;
138 layout
.marginWidth
= 0;
139 layout
.marginHeight
= 0;
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
);
147 String url
= args
.length
> 0 ? args
[0] : System
.getProperty("user.home");
148 new MiniExplorer(shell
, url
) {
151 protected void pathChanged(Path path
) {
152 shell
.setText(path
.toString());
158 shell
.setSize(new Point(800, 480));
159 while (!shell
.isDisposed()) {
160 if (!display
.readAndDispatch())