View Javadoc
1   package org.argeo.cms.desktop;
2   
3   import org.eclipse.swt.SWT;
4   import org.eclipse.swt.events.SelectionAdapter;
5   import org.eclipse.swt.events.SelectionEvent;
6   import org.eclipse.swt.graphics.Point;
7   import org.eclipse.swt.graphics.Rectangle;
8   import org.eclipse.swt.layout.GridData;
9   import org.eclipse.swt.layout.GridLayout;
10  import org.eclipse.swt.widgets.Composite;
11  import org.eclipse.swt.widgets.Display;
12  import org.eclipse.swt.widgets.Shell;
13  import org.eclipse.swt.widgets.ToolBar;
14  import org.eclipse.swt.widgets.ToolItem;
15  
16  public class CmsDesktopManager {
17  	private Display display;
18  
19  	private Shell rootShell;
20  	private Shell toolBarShell;
21  
22  	public void init() {
23  		display = Display.getCurrent();
24  		if (display != null)
25  			throw new IllegalStateException("Already a display " + display);
26  		display = new Display();
27  
28  		int toolBarSize = 48;
29  
30  		if (isFullScreen()) {
31  			rootShell = new Shell(display, SWT.NO_TRIM);
32  			// rootShell.setMaximized(true);
33  			rootShell.setFullScreen(true);
34  			Rectangle bounds = display.getBounds();
35  
36  			rootShell.setSize(bounds.width, bounds.height);
37  //			Point realSize = rootShell.getSize();
38  //			rootShell.setBounds(bounds);
39  //			Rectangle realBounds = rootShell.getBounds();
40  		} else {
41  			rootShell = new Shell(display, SWT.SHELL_TRIM);
42  			Rectangle shellArea = rootShell.computeTrim(200, 200, 800, 480);
43  			rootShell.setSize(shellArea.width, shellArea.height);
44  		}
45  
46  		rootShell.setLayout(new GridLayout(2, false));
47  		Composite toolBarArea = new Composite(rootShell, SWT.NONE);
48  		toolBarArea.setLayoutData(new GridData(toolBarSize, rootShell.getSize().y));
49  
50  		ToolBar toolBar;
51  		if (isFullScreen()) {
52  			toolBarShell = new Shell(rootShell, SWT.NO_TRIM | SWT.ON_TOP);
53  			toolBar = new ToolBar(toolBarShell, SWT.VERTICAL | SWT.FLAT | SWT.BORDER);
54  			createDock(toolBar);
55  			toolBarShell.pack();
56  			toolBarArea.setLayoutData(new GridData(toolBar.getSize().x, toolBar.getSize().y));
57  		} else {
58  			toolBar = new ToolBar(toolBarArea, SWT.VERTICAL | SWT.FLAT | SWT.BORDER);
59  			createDock(toolBar);
60  			toolBarArea.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false));
61  		}
62  
63  		Composite backgroundArea = new Composite(rootShell, SWT.NONE);
64  		backgroundArea.setLayout(new GridLayout(6, true));
65  		DesktopLayer desktopLayer = new DesktopLayer();
66  		desktopLayer.init(backgroundArea);
67  		rootShell.open();
68  		// rootShell.layout(true, true);
69  
70  		if (toolBarShell != null) {
71  			toolBarShell.setLocation(new Point(0, 0));
72  			toolBarShell.open();
73  		}
74  	}
75  
76  	protected void createDock(ToolBar toolBar) {
77  
78  		// toolBar.setLocation(clientArea.x, clientArea.y);
79  
80  		ToolItem closeI = new ToolItem(toolBar, SWT.PUSH);
81  		closeI.setImage(display.getSystemImage(SWT.ICON_ERROR));
82  		closeI.setToolTipText("Close");
83  		closeI.addSelectionListener(new SelectionAdapter() {
84  
85  			@Override
86  			public void widgetSelected(SelectionEvent e) {
87  				rootShell.dispose();
88  			}
89  
90  		});
91  
92  		ToolItem searchI = new ToolItem(toolBar, SWT.PUSH);
93  		searchI.setImage(display.getSystemImage(SWT.ICON_QUESTION));
94  		searchI.setToolTipText("Search");
95  		searchI.addSelectionListener(new SelectionAdapter() {
96  
97  			@Override
98  			public void widgetSelected(SelectionEvent e) {
99  				// rootShell.dispose();
100 			}
101 
102 		});
103 		// toolBar.setSize(48, toolBar.getSize().y);
104 		toolBar.pack();
105 	}
106 
107 	public void run() {
108 		while (!rootShell.isDisposed()) {
109 			if (!display.readAndDispatch())
110 				display.sleep();
111 		}
112 	}
113 
114 	public void dispose() {
115 		if (!rootShell.isDisposed())
116 			rootShell.dispose();
117 	}
118 
119 	protected boolean isFullScreen() {
120 		return true;
121 	}
122 
123 	public static void main(String[] args) {
124 		CmsDesktopManager desktopManager = new CmsDesktopManager();
125 		desktopManager.init();
126 		// Runtime.getRuntime().addShutdownHook(new Thread(() ->
127 		// desktopManager.dispose(), "Dispose desktop manager"));
128 		desktopManager.run();
129 		desktopManager.dispose();
130 	}
131 
132 }