-
Notifications
You must be signed in to change notification settings - Fork 98
/
BlurredPanel.java
145 lines (124 loc) · 3.81 KB
/
BlurredPanel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*******************************************************************************
* Copyright (c) 2011 Laurent CARON
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Laurent CARON (laurent.caron at gmail dot com) - initial API and implementation
*******************************************************************************/
package org.eclipse.nebula.widgets.opal.panels;
import org.eclipse.nebula.widgets.opal.commons.SWTGraphicUtil;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Shell;
/**
* Instances of this class are controls located on the top of a shell. They
* display a blurred version of the content of the shell
*/
public class BlurredPanel {
private final Shell parent;
private static final String BLURED_PANEL_KEY = "org.eclipse.nebula.widgets.opal.panels.BlurredPanel";
private int radius;
private Shell panel;
private Canvas canvas;
/**
* Constructs a new instance of this class given its parent.
*
* @param shell a shell that will be the parent of the new instance (cannot
* be null)
* @exception IllegalArgumentException
* <ul>
* <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
* </ul>
* @exception SWTException
* <ul>
* <li>ERROR_WIDGET_DISPOSED - if the parent has been
* disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
* thread that created the parent</li>
* </ul>
*/
public BlurredPanel(final Shell shell) {
if (shell == null) {
SWT.error(SWT.ERROR_NULL_ARGUMENT);
}
if (shell.isDisposed()) {
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
}
parent = shell;
if (shell.getData(BLURED_PANEL_KEY) != null) {
throw new IllegalArgumentException("This shell has already an infinite panel attached on it !");
}
shell.setData(BLURED_PANEL_KEY, this);
radius = 2;
}
/**
* Show the blurred panel
*/
public void show() {
if (parent.isDisposed()) {
SWT.error(SWT.ERROR_WIDGET_DISPOSED);
}
panel = new Shell(parent, SWT.APPLICATION_MODAL | SWT.NO_TRIM);
panel.setLayout(new FillLayout());
panel.addListener(SWT.KeyUp, event -> {
event.doit = false;
});
canvas = new Canvas(panel, SWT.NO_BACKGROUND | SWT.DOUBLE_BUFFERED);
canvas.addPaintListener(event -> {
paintCanvas(event);
});
panel.setBounds(panel.getDisplay().map(parent, null, parent.getClientArea()));
panel.open();
}
/**
* Paint the canvas that holds the panel
*
* @param e {@link PaintEvent}
*/
private void paintCanvas(final PaintEvent e) {
// Paint the panel
e.gc.drawImage(createBlurredImage(), 0, 0);
}
private Image createBlurredImage() {
final GC gc = new GC(parent);
final Image image = new Image(parent.getDisplay(), parent.getSize().x, parent.getSize().y);
gc.copyArea(image, 0, 0);
gc.dispose();
return new Image(parent.getDisplay(), SWTGraphicUtil.blur(image.getImageData(), radius));
}
/**
* Hide the panel
*/
public void hide() {
if (parent.isDisposed()) {
SWT.error(SWT.ERROR_WIDGET_DISPOSED);
}
if (panel == null || panel.isDisposed()) {
return;
}
panel.dispose();
}
/**
* @return the radius of the blur effect
*/
public int getRadius() {
return radius;
}
/**
* @param radius the radius to set
*/
public void setRadius(final int radius) {
this.radius = radius;
}
}