View Javadoc
1   package org.argeo.cms.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.apache.commons.io.FilenameUtils;
10  import org.eclipse.swt.SWT;
11  import org.eclipse.swt.events.SelectionAdapter;
12  import org.eclipse.swt.events.SelectionEvent;
13  import org.eclipse.swt.graphics.Image;
14  import org.eclipse.swt.graphics.ImageData;
15  import org.eclipse.swt.layout.GridData;
16  import org.eclipse.swt.layout.GridLayout;
17  import org.eclipse.swt.program.Program;
18  import org.eclipse.swt.widgets.Button;
19  import org.eclipse.swt.widgets.Composite;
20  import org.eclipse.swt.widgets.Control;
21  import org.eclipse.swt.widgets.Display;
22  import org.eclipse.swt.widgets.Label;
23  
24  public class DesktopLayer {
25  	// TODO make it configurable
26  	private Path desktopDir = Paths.get(System.getProperty("user.home"), "tmp");
27  
28  	public void init(Composite parentShell) {
29  //		Decorations shell = new Decorations(parentShell, SWT.CLOSE);
30  //		shell.setLayoutData(new GridData(GridData.FILL_BOTH));
31  		createUi(parentShell, desktopDir);
32  		// shell.open();
33  	}
34  
35  	public Control createUi(Composite parent, Path context) {
36  		// parent.setLayout(new FillLayout());
37  		try {
38  			DirectoryStream<Path> ds = Files.newDirectoryStream(context);
39  			ds.forEach((path) -> createIcon(parent, path));
40  		} catch (IOException e) {
41  			// TODO Auto-generated catch block
42  			e.printStackTrace();
43  		}
44  		return parent;
45  
46  	}
47  
48  	protected void createIcon(Composite parent, Path path) {
49  		String ext = FilenameUtils.getExtension(path.getFileName().toString());
50  		Program program = Program.findProgram(ext);
51  		if (program == null) {
52  			createDefaultIcon(parent, path);
53  			return;
54  		}
55  
56  		Display display = parent.getDisplay();
57  		ImageData iconData = program.getImageData();
58  
59  		Image iconImage;
60  		if (iconData == null) {
61  			iconImage = display.getSystemImage(SWT.ICON_INFORMATION);
62  			iconData = iconImage.getImageData();
63  		} else {
64  			iconImage = new Image(display, iconData);
65  		}
66  
67  		Composite icon = new Composite(parent, SWT.NONE);
68  		icon.setLayoutData(new GridData(48, 72));
69  		icon.setLayout(new GridLayout());
70  		// Button
71  		Button iconB = new Button(icon, SWT.FLAT);
72  		iconB.setImage(iconImage);
73  		// iconB.setLayoutData(new GridData(iconData.width, iconData.height));
74  		iconB.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
75  		iconB.addSelectionListener(new SelectionAdapter() {
76  
77  			@Override
78  			public void widgetSelected(SelectionEvent e) {
79  				program.execute(path.toString());
80  			}
81  
82  		});
83  		// Label
84  		Label iconL = new Label(icon, SWT.WRAP);
85  		iconL.setText(path.getFileName().toString());
86  		iconL.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
87  	}
88  
89  	protected void createDefaultIcon(Composite parent, Path path) {
90  		Composite icon = new Composite(parent, SWT.NONE);
91  		icon.setLayout(new GridLayout());
92  		Label iconL = new Label(icon, SWT.NONE);
93  		iconL.setText(path.getFileName().toString());
94  		iconL.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
95  	}
96  }