]> git.argeo.org Git - gpl/argeo-slc.git/blob - MiniDesktopLayer.java
f53e6a3c7e61a88f3a7e10f067a3ea6a296a8dbd
[gpl/argeo-slc.git] / MiniDesktopLayer.java
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
8 import org.eclipse.swt.SWT;
9 import org.eclipse.swt.events.SelectionAdapter;
10 import org.eclipse.swt.events.SelectionEvent;
11 import org.eclipse.swt.graphics.Image;
12 import org.eclipse.swt.graphics.ImageData;
13 import org.eclipse.swt.layout.GridData;
14 import org.eclipse.swt.layout.GridLayout;
15 import org.eclipse.swt.layout.RowData;
16 import org.eclipse.swt.layout.RowLayout;
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.Display;
21 import org.eclipse.swt.widgets.Label;
22
23 /** Implementation of an active desktop with icons. */
24 public class MiniDesktopLayer {
25 public void init(Composite parent, Path context) {
26 parent.setLayout(new RowLayout());
27 try {
28 DirectoryStream<Path> ds = Files.newDirectoryStream(context);
29 ds.forEach((path) -> createIcon(parent, path));
30 } catch (IOException e) {
31 e.printStackTrace();
32 }
33 }
34
35 protected void createIcon(Composite parent, Path path) {
36 String fileName = path.getFileName().toString();
37 String ext = fileName.substring(fileName.lastIndexOf('.') + 1);
38 Program program = Program.findProgram(ext);
39 if (program == null) {
40 createDefaultIcon(parent, path);
41 return;
42 }
43
44 Display display = parent.getDisplay();
45 ImageData iconData = program.getImageData();
46
47 Image iconImage;
48 if (iconData == null) {
49 iconImage = null;
50 } else {
51 iconImage = new Image(display, iconData);
52 }
53
54 Composite icon = new Composite(parent, SWT.BORDER);
55 icon.setLayoutData(new RowData(48, 72));
56 icon.setLayout(new GridLayout());
57 // Button
58 Button iconB = new Button(icon, SWT.FLAT);
59 iconB.setImage(iconImage);
60 // iconB.setLayoutData(new GridData(iconData.width, iconData.height));
61 iconB.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
62 iconB.addSelectionListener(new SelectionAdapter() {
63
64 @Override
65 public void widgetSelected(SelectionEvent e) {
66 program.execute(path.toString());
67 }
68
69 });
70 // Label
71 Label iconL = new Label(icon, SWT.WRAP);
72 iconL.setText(path.getFileName().toString());
73 iconL.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
74 }
75
76 protected void createDefaultIcon(Composite parent, Path path) {
77 Composite icon = new Composite(parent, SWT.NONE);
78 icon.setLayout(new GridLayout());
79 Label iconL = new Label(icon, SWT.NONE);
80 iconL.setText(path.getFileName().toString());
81 iconL.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
82 }
83 }