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
9 * Laurent CARON (laurent.caron at gmail dot com) - initial API and implementation
\r
10 *******************************************************************************/
\r
11 package org.mihalis.opal.panels;
\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
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
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
33 private Shell panel;
\r
34 private Canvas canvas;
\r
37 * Constructs a new instance of this class given its parent.
\r
39 * @param shell a shell that will be the parent of the new instance (cannot
\r
41 * @exception IllegalArgumentException <ul>
\r
42 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
\r
44 * @exception SWTException <ul>
\r
45 * <li>ERROR_WIDGET_DISPOSED - if the parent has been
\r
47 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
\r
48 * thread that created the parent</li>
\r
51 public DarkPanel(final Shell shell) {
\r
52 if (shell == null) {
\r
53 SWT.error(SWT.ERROR_NULL_ARGUMENT);
\r
56 if (shell.isDisposed()) {
\r
57 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
\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
64 shell.setData(DARK_PANEL_KEY, this);
\r
69 * Show the dark panel
\r
71 public void show() {
\r
72 if (this.parent.isDisposed()) {
\r
73 SWT.error(SWT.ERROR_WIDGET_DISPOSED);
\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
80 this.panel.addListener(SWT.KeyUp, new Listener() {
\r
83 public void handleEvent(final Event event) {
\r
88 this.canvas = new Canvas(this.panel, SWT.NO_BACKGROUND | SWT.DOUBLE_BUFFERED);
\r
89 this.canvas.addPaintListener(new PaintListener() {
\r
92 public void paintControl(final PaintEvent e) {
\r
97 this.panel.setBounds(this.panel.getDisplay().map(this.parent, null, this.parent.getClientArea()));
\r
103 * Paint the canvas that holds the panel
\r
105 * @param e {@link PaintEvent}
\r
107 private void paintCanvas(final PaintEvent e) {
\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
116 * Hide the dark panel
\r
118 public void hide() {
\r
119 if (this.parent.isDisposed()) {
\r
120 SWT.error(SWT.ERROR_WIDGET_DISPOSED);
\r
123 if (this.panel == null || this.panel.isDisposed()) {
\r
127 this.panel.dispose();
\r
131 * @return the alpha value
\r
133 public int getAlpha() {
\r
138 * @param alpha the alpha to set
\r
140 public void setAlpha(final int alpha) {
\r
141 this.alpha = alpha;
\r