Merge sources from S-Core's RSA git (release)
[sdk/ide/common-eplugin.git] / org.tizen.common.externals / src / org / mihalis / opal / panels / DarkPanel.java
1 /*******************************************************************************\r
2  * Copyright (c) 2011 Laurent CARON\r
3  * All rights reserved. This program and the accompanying materials\r
4  * are made available under the terms of the Eclipse Public License v1.0\r
5  * which accompanies this distribution, and is available at\r
6  * http://www.eclipse.org/legal/epl-v10.html\r
7  *\r
8  * Contributors:\r
9  *     Laurent CARON (laurent.caron at gmail dot com) - initial API and implementation \r
10  *******************************************************************************/\r
11 package org.mihalis.opal.panels;\r
12 \r
13 import org.eclipse.swt.SWT;\r
14 import org.eclipse.swt.SWTException;\r
15 import org.eclipse.swt.events.PaintEvent;\r
16 import org.eclipse.swt.events.PaintListener;\r
17 import org.eclipse.swt.graphics.GC;\r
18 import org.eclipse.swt.graphics.Rectangle;\r
19 import org.eclipse.swt.layout.FillLayout;\r
20 import org.eclipse.swt.widgets.Canvas;\r
21 import org.eclipse.swt.widgets.Event;\r
22 import org.eclipse.swt.widgets.Listener;\r
23 import org.eclipse.swt.widgets.Shell;\r
24 \r
25 /**\r
26  * Instances of this class are controls located on the top of a shell. They\r
27  * display a dark panel on this shell\r
28  */\r
29 public class DarkPanel {\r
30         private final Shell parent;\r
31         private static final String DARK_PANEL_KEY = "org.mihalis.opal.BluredPanel.DarkPanel";\r
32         private int alpha;\r
33         private Shell panel;\r
34         private Canvas canvas;\r
35 \r
36         /**\r
37          * Constructs a new instance of this class given its parent.\r
38          * \r
39          * @param shell a shell that will be the parent of the new instance (cannot\r
40          *            be null)\r
41          * @exception IllegalArgumentException <ul>\r
42          *                <li>ERROR_NULL_ARGUMENT - if the parent is null</li>\r
43          *                </ul>\r
44          * @exception SWTException <ul>\r
45          *                <li>ERROR_WIDGET_DISPOSED - if the parent has been\r
46          *                disposed</li>\r
47          *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the\r
48          *                thread that created the parent</li>\r
49          *                </ul>\r
50          */\r
51         public DarkPanel(final Shell shell) {\r
52                 if (shell == null) {\r
53                         SWT.error(SWT.ERROR_NULL_ARGUMENT);\r
54                 }\r
55 \r
56                 if (shell.isDisposed()) {\r
57                         SWT.error(SWT.ERROR_INVALID_ARGUMENT);\r
58                 }\r
59 \r
60                 this.parent = shell;\r
61                 if (shell.getData(DARK_PANEL_KEY) != null) {\r
62                         throw new IllegalArgumentException("This shell has already an infinite panel attached on it !");\r
63                 }\r
64                 shell.setData(DARK_PANEL_KEY, this);\r
65                 this.alpha = 100;\r
66         }\r
67 \r
68         /**\r
69          * Show the dark panel\r
70          */\r
71         public void show() {\r
72                 if (this.parent.isDisposed()) {\r
73                         SWT.error(SWT.ERROR_WIDGET_DISPOSED);\r
74                 }\r
75 \r
76                 this.panel = new Shell(this.parent, SWT.APPLICATION_MODAL | SWT.NO_TRIM);\r
77                 this.panel.setLayout(new FillLayout());\r
78                 this.panel.setAlpha(this.alpha);\r
79 \r
80                 this.panel.addListener(SWT.KeyUp, new Listener() {\r
81 \r
82                         @Override\r
83                         public void handleEvent(final Event event) {\r
84                                 event.doit = false;\r
85                         }\r
86                 });\r
87 \r
88                 this.canvas = new Canvas(this.panel, SWT.NO_BACKGROUND | SWT.DOUBLE_BUFFERED);\r
89                 this.canvas.addPaintListener(new PaintListener() {\r
90 \r
91                         @Override\r
92                         public void paintControl(final PaintEvent e) {\r
93                                 paintCanvas(e);\r
94                         }\r
95                 });\r
96 \r
97                 this.panel.setBounds(this.panel.getDisplay().map(this.parent, null, this.parent.getClientArea()));\r
98                 this.panel.open();\r
99 \r
100         }\r
101 \r
102         /**\r
103          * Paint the canvas that holds the panel\r
104          * \r
105          * @param e {@link PaintEvent}\r
106          */\r
107         private void paintCanvas(final PaintEvent e) {\r
108                 // Paint the panel\r
109                 final Rectangle clientArea = ((Canvas) e.widget).getClientArea();\r
110                 final GC gc = e.gc;\r
111                 gc.setBackground(this.panel.getDisplay().getSystemColor(SWT.COLOR_BLACK));\r
112                 gc.fillRectangle(clientArea);\r
113         }\r
114 \r
115         /**\r
116          * Hide the dark panel\r
117          */\r
118         public void hide() {\r
119                 if (this.parent.isDisposed()) {\r
120                         SWT.error(SWT.ERROR_WIDGET_DISPOSED);\r
121                 }\r
122 \r
123                 if (this.panel == null || this.panel.isDisposed()) {\r
124                         return;\r
125                 }\r
126 \r
127                 this.panel.dispose();\r
128         }\r
129 \r
130         /**\r
131          * @return the alpha value\r
132          */\r
133         public int getAlpha() {\r
134                 return this.alpha;\r
135         }\r
136 \r
137         /**\r
138          * @param alpha the alpha to set\r
139          */\r
140         public void setAlpha(final int alpha) {\r
141                 this.alpha = alpha;\r
142         }\r
143 \r
144 }\r