public code v1
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
|
||||
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>flintstones.group</groupId>
|
||||
<artifactId>flintstones.bundles</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>org.eclipse.nebula.widgets.opal.notifier</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<packaging>eclipse-plugin</packaging>
|
||||
<name>[bundle] Opal Notifier Widget plug-in</name>
|
||||
</project>
|
||||
@@ -0,0 +1,45 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>org.eclipse.nebula.widgets.opal.notifier</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.pde.ManifestBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.pde.SchemaBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||
<nature>org.eclipse.pde.PluginNature</nature>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
<filteredResources>
|
||||
<filter>
|
||||
<id>1779484362759</id>
|
||||
<name></name>
|
||||
<type>30</type>
|
||||
<matcher>
|
||||
<id>org.eclipse.core.resources.regexFilterMatcher</id>
|
||||
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
|
||||
</matcher>
|
||||
</filter>
|
||||
</filteredResources>
|
||||
</projectDescription>
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
eclipse.preferences.version=1
|
||||
encoding/<project>=UTF-8
|
||||
@@ -0,0 +1,7 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
|
||||
org.eclipse.jdt.core.compiler.compliance=1.8
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.8
|
||||
@@ -0,0 +1,4 @@
|
||||
activeProfiles=
|
||||
eclipse.preferences.version=1
|
||||
resolveWorkspaceProjects=true
|
||||
version=1
|
||||
@@ -0,0 +1,9 @@
|
||||
Manifest-Version: 1.0
|
||||
Bundle-ManifestVersion: 2
|
||||
Bundle-Name: Opal Notifier Widget plug-in
|
||||
Bundle-SymbolicName: org.eclipse.nebula.widgets.opal.notifier
|
||||
Bundle-Version: 1.0.0.qualifier
|
||||
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
|
||||
Require-Bundle: org.eclipse.nebula.widgets.opal.commons;bundle-version="1.0.0";visibility:=reexport
|
||||
Export-Package: org.eclipse.nebula.widgets.opal.notifier
|
||||
Automatic-Module-Name: org.eclipse.nebula.widgets.opal.notifier
|
||||
@@ -0,0 +1,4 @@
|
||||
source.. = src/
|
||||
output.. = bin/
|
||||
bin.includes = META-INF/,\
|
||||
.
|
||||
@@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<!--
|
||||
Copyright (C) 2018, Laurent CARON.
|
||||
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License v1.0
|
||||
which accompanies this distribution, and is available at
|
||||
http://www.eclipse.org/legal/epl-v10.html
|
||||
-->
|
||||
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.eclipse.nebula</groupId>
|
||||
<artifactId>notifier</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>org.eclipse.nebula.widgets.opal.notifier</artifactId>
|
||||
<packaging>eclipse-plugin</packaging>
|
||||
</project>
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package org.eclipse.nebula.widgets.opal.notifier;
|
||||
|
||||
public interface INotificationClickedListener {
|
||||
|
||||
void onNotificationClick();
|
||||
|
||||
}
|
||||
+325
@@ -0,0 +1,325 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2011 Laurent CARON All rights reserved. This program and the
|
||||
* accompanying materials are made available under the terms of the Eclipse
|
||||
* Public License v1.0 which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors: Laurent CARON (laurent.caron at gmail dot com) - initial API
|
||||
* and implementation
|
||||
*******************************************************************************/
|
||||
package org.eclipse.nebula.widgets.opal.notifier;
|
||||
|
||||
import org.eclipse.nebula.widgets.opal.commons.SWTGraphicUtil;
|
||||
import org.eclipse.nebula.widgets.opal.notifier.NotifierColorsFactory.NotifierTheme;
|
||||
import org.eclipse.swt.SWT;
|
||||
import org.eclipse.swt.custom.StyledText;
|
||||
import org.eclipse.swt.graphics.Color;
|
||||
import org.eclipse.swt.graphics.Font;
|
||||
import org.eclipse.swt.graphics.GC;
|
||||
import org.eclipse.swt.graphics.Image;
|
||||
import org.eclipse.swt.graphics.Rectangle;
|
||||
import org.eclipse.swt.layout.GridData;
|
||||
import org.eclipse.swt.layout.GridLayout;
|
||||
import org.eclipse.swt.widgets.Display;
|
||||
import org.eclipse.swt.widgets.Label;
|
||||
import org.eclipse.swt.widgets.Shell;
|
||||
|
||||
/**
|
||||
* This class provides a notifier window, which is a window that appears in the
|
||||
* bottom of the screen and slides.
|
||||
*/
|
||||
public class Notifier {
|
||||
private static final int FONT_SIZE = 10;
|
||||
private static final int MAX_DURATION_FOR_OPENING = 500;
|
||||
private static final int DISPLAY_TIME = 4500;
|
||||
|
||||
private static final int FADE_TIMER = 50;
|
||||
private static final int FADE_OUT_STEP = 8;
|
||||
|
||||
private static final int STEP = 5;
|
||||
|
||||
/**
|
||||
* Starts a notification. A window will appear in the bottom of the screen, then
|
||||
* will disappear after 4.5 s
|
||||
*
|
||||
* @param title the title of the popup window
|
||||
* @param text the text of the notification
|
||||
*
|
||||
*/
|
||||
public static void notify(final String title, final String text) {
|
||||
notify(null, title, text, NotifierTheme.YELLOW_THEME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts a notification. A window will appear in the bottom of the screen, then
|
||||
* will disappear after 4.5 s
|
||||
*
|
||||
* @param image the image to display (if <code>null</code>, a default image is
|
||||
* displayed)
|
||||
* @param title the title of the popup window
|
||||
* @param text the text of the notification
|
||||
*
|
||||
*/
|
||||
public static void notify(final Image image, final String title, final String text) {
|
||||
notify(image, title, text, NotifierTheme.YELLOW_THEME);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts a notification. A window will appear in the bottom of the screen, then
|
||||
* will disappear after 4.5 s
|
||||
*
|
||||
* @param title the title of the popup window
|
||||
* @param text the text of the notification
|
||||
* @param theme the graphical theme. If <code>null</code>, the yellow theme is
|
||||
* used
|
||||
*
|
||||
* @see NotifierTheme
|
||||
*/
|
||||
public static void notify(final String title, final String text, final NotifierTheme theme) {
|
||||
notify(null, title, text, theme);
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts a notification. A window will appear in the bottom of the screen, then
|
||||
* will disappear after 4.5 s
|
||||
*
|
||||
* @param image the image to display (if <code>null</code>, a default image is
|
||||
* displayed)
|
||||
* @param title the title of the popup window
|
||||
* @param text the text of the notification
|
||||
* @param theme the graphical theme. If <code>null</code>, the yellow theme is
|
||||
* used
|
||||
*
|
||||
* @see NotifierTheme
|
||||
*/
|
||||
public static void notify(final Image image, final String title, final String text, final NotifierTheme theme) {
|
||||
final Shell shell = createNotificationWindow(image, title, text, NotifierColorsFactory.getColorsForTheme(theme));
|
||||
makeShellAppears(shell);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a notification window
|
||||
*
|
||||
* @param image image. If <code>null</code>, a default image is used
|
||||
* @param title title, the title of the window
|
||||
* @param text text of the window
|
||||
* @param colors color set
|
||||
* @return the notification window as a shell object
|
||||
*/
|
||||
protected static Shell createNotificationWindow(final Image image, final String title, final String text, final NotifierColors colors) {
|
||||
final Shell shell = new Shell(Display.getDefault().getActiveShell(), SWT.NO_TRIM | SWT.NO_FOCUS | SWT.ON_TOP);
|
||||
shell.setLayout(new GridLayout(2, false));
|
||||
shell.setBackgroundMode(SWT.INHERIT_FORCE);
|
||||
|
||||
createTitle(shell, title, colors);
|
||||
createImage(shell, image);
|
||||
createText(shell, text, colors);
|
||||
createBackground(shell, colors);
|
||||
createCloseAction(shell);
|
||||
|
||||
shell.addListener(SWT.Dispose, event -> {
|
||||
colors.dispose();
|
||||
});
|
||||
|
||||
shell.pack();
|
||||
shell.setMinimumSize(320, 100);
|
||||
return shell;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the title part of the window
|
||||
*
|
||||
* @param shell the window
|
||||
* @param title the title
|
||||
* @param colors the color set
|
||||
*/
|
||||
private static void createTitle(final Shell shell, final String title, final NotifierColors colors) {
|
||||
final Label titleLabel = new Label(shell, SWT.NONE);
|
||||
final GridData gdLabel = new GridData(GridData.BEGINNING, GridData.BEGINNING, true, false, 2, 1);
|
||||
gdLabel.horizontalIndent = 40;
|
||||
titleLabel.setLayoutData(gdLabel);
|
||||
final Color titleColor = colors.titleColor;
|
||||
titleLabel.setForeground(titleColor);
|
||||
|
||||
final Font titleFont = SWTGraphicUtil.buildFontFrom(titleLabel, SWT.BOLD, FONT_SIZE);
|
||||
titleLabel.setFont(titleFont);
|
||||
titleLabel.setText(title);
|
||||
SWTGraphicUtil.addDisposer(shell, titleFont);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the image part of the window
|
||||
*
|
||||
* @param shell the window
|
||||
* @param image the image
|
||||
*/
|
||||
private static void createImage(final Shell shell, final Image image) {
|
||||
final Label labelImage = new Label(shell, SWT.NONE);
|
||||
final GridData gdImage = new GridData(GridData.CENTER, GridData.BEGINNING, false, true);
|
||||
gdImage.horizontalIndent = 10;
|
||||
labelImage.setLayoutData(gdImage);
|
||||
if (image == null) {
|
||||
final Image temp = SWTGraphicUtil.createImageFromFile("images/information.png");
|
||||
labelImage.setImage(temp);
|
||||
SWTGraphicUtil.addDisposer(shell, temp);
|
||||
} else {
|
||||
labelImage.setImage(image);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the text part of the window
|
||||
*
|
||||
* @param shell the window
|
||||
* @param text the text
|
||||
* @param colors the color set
|
||||
*/
|
||||
private static void createText(final Shell shell, final String text, final NotifierColors colors) {
|
||||
final StyledText textLabel = new StyledText(shell, SWT.WRAP | SWT.READ_ONLY);
|
||||
final GridData gdText = new GridData(GridData.FILL, GridData.FILL, true, true);
|
||||
gdText.horizontalIndent = 15;
|
||||
textLabel.setLayoutData(gdText);
|
||||
textLabel.setEnabled(false);
|
||||
final Font textFont = SWTGraphicUtil.buildFontFrom(textLabel, SWT.NONE, 10);
|
||||
textLabel.setFont(textFont);
|
||||
|
||||
final Color textColor = colors.textColor;
|
||||
textLabel.setForeground(textColor);
|
||||
|
||||
textLabel.setText(text);
|
||||
SWTGraphicUtil.applyHTMLFormating(textLabel);
|
||||
|
||||
SWTGraphicUtil.addDisposer(shell, textFont);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the background of the window
|
||||
*
|
||||
* @param shell the window
|
||||
* @param colors the color set of the window
|
||||
*/
|
||||
private static void createBackground(final Shell shell, final NotifierColors colors) {
|
||||
shell.addListener(SWT.Resize, event -> {
|
||||
final Rectangle rect = shell.getClientArea();
|
||||
final Image newImage = new Image(Display.getDefault(), Math.max(1, rect.width), rect.height);
|
||||
final GC gc = new GC(newImage);
|
||||
gc.setAntialias(SWT.ON);
|
||||
|
||||
final Color borderColor = colors.borderColor;
|
||||
final Color fillColor1 = colors.leftColor;
|
||||
final Color fillColor2 = colors.rightColor;
|
||||
|
||||
gc.setBackground(borderColor);
|
||||
gc.fillRoundRectangle(0, 0, rect.width, rect.height, 8, 8);
|
||||
|
||||
gc.setBackground(fillColor1);
|
||||
gc.fillRoundRectangle(1, 1, rect.width - 2, rect.height - 2, 8, 8);
|
||||
|
||||
gc.setBackground(fillColor2);
|
||||
gc.fillRoundRectangle(30, 1, rect.width - 32, rect.height - 2, 8, 8);
|
||||
gc.fillRectangle(30, 1, 10, rect.height - 2);
|
||||
|
||||
final Image closeImage = SWTGraphicUtil.createImageFromFile("images/close.png");
|
||||
gc.drawImage(closeImage, rect.width - 21, 13);
|
||||
|
||||
gc.dispose();
|
||||
closeImage.dispose();
|
||||
|
||||
shell.setBackgroundImage(newImage);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param shell shell that will appear
|
||||
*/
|
||||
protected static void makeShellAppears(final Shell shell) {
|
||||
if (shell == null || shell.isDisposed()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final Rectangle clientArea = Display.getDefault().getPrimaryMonitor().getClientArea();
|
||||
final int startX = clientArea.x + clientArea.width - shell.getSize().x;
|
||||
|
||||
final int stepForPosition = MAX_DURATION_FOR_OPENING / shell.getSize().y * STEP;
|
||||
final int stepForAlpha = STEP * 255 / shell.getSize().y;
|
||||
|
||||
final int lastPosition = clientArea.y + clientArea.height - shell.getSize().y;
|
||||
|
||||
shell.setAlpha(0);
|
||||
shell.setLocation(startX, clientArea.y + clientArea.height);
|
||||
shell.open();
|
||||
|
||||
shell.getDisplay().timerExec(stepForPosition, new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
if (shell == null || shell.isDisposed()) {
|
||||
return;
|
||||
}
|
||||
|
||||
shell.setLocation(startX, shell.getLocation().y - STEP);
|
||||
shell.setAlpha(shell.getAlpha() + stepForAlpha);
|
||||
if (shell.getLocation().y >= lastPosition) {
|
||||
shell.getDisplay().timerExec(stepForPosition, this);
|
||||
} else {
|
||||
shell.setAlpha(255);
|
||||
Display.getDefault().timerExec(DISPLAY_TIME, fadeOut(shell, false));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param shell shell that will disappear
|
||||
* @param fast if true, the fading is much faster
|
||||
* @return a runnable
|
||||
*/
|
||||
private static Runnable fadeOut(final Shell shell, final boolean fast) {
|
||||
return new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (shell == null || shell.isDisposed()) {
|
||||
return;
|
||||
}
|
||||
|
||||
int currentAlpha = shell.getAlpha();
|
||||
currentAlpha -= FADE_OUT_STEP * (fast ? 8 : 1);
|
||||
|
||||
if (currentAlpha <= 0) {
|
||||
shell.setAlpha(0);
|
||||
shell.dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
shell.setAlpha(currentAlpha);
|
||||
|
||||
Display.getDefault().timerExec(FADE_TIMER, this);
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a listener to the shell in order to handle the clicks on the close button
|
||||
*
|
||||
* @param shell associated shell
|
||||
*/
|
||||
private static void createCloseAction(final Shell shell) {
|
||||
shell.addListener(SWT.MouseUp, event -> {
|
||||
final Rectangle rect = shell.getClientArea();
|
||||
final int xUpperLeftCorner = rect.width - 21;
|
||||
final int yUpperLeftCorner = 13;
|
||||
|
||||
if (event.x >= xUpperLeftCorner && event.x <= xUpperLeftCorner + 8 && event.y >= yUpperLeftCorner && event.y <= yUpperLeftCorner + 8) {
|
||||
Display.getDefault().timerExec(0, fadeOut(shell, true));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2011 Laurent CARON All rights reserved. This program and the
|
||||
* accompanying materials are made available under the terms of the Eclipse
|
||||
* Public License v1.0 which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors: Laurent CARON (laurent.caron at gmail dot com) - initial API
|
||||
* and implementation
|
||||
*******************************************************************************/
|
||||
package org.eclipse.nebula.widgets.opal.notifier;
|
||||
|
||||
import org.eclipse.nebula.widgets.opal.commons.SWTGraphicUtil;
|
||||
import org.eclipse.swt.graphics.Color;
|
||||
|
||||
/**
|
||||
* This class is a simple POJO that holds colors used by the Notifier widget
|
||||
*
|
||||
*/
|
||||
class NotifierColors {
|
||||
Color titleColor;
|
||||
Color textColor;
|
||||
Color borderColor;
|
||||
Color leftColor;
|
||||
Color rightColor;
|
||||
|
||||
void dispose() {
|
||||
SWTGraphicUtil.safeDispose(titleColor);
|
||||
SWTGraphicUtil.safeDispose(borderColor);
|
||||
SWTGraphicUtil.safeDispose(leftColor);
|
||||
SWTGraphicUtil.safeDispose(rightColor);
|
||||
SWTGraphicUtil.safeDispose(textColor);
|
||||
}
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2011 Laurent CARON All rights reserved. This program and the
|
||||
* accompanying materials are made available under the terms of the Eclipse
|
||||
* Public License v1.0 which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* Laurent CARON (laurent.caron at gmail dot com) - initial API and implementation
|
||||
*******************************************************************************/
|
||||
|
||||
package org.eclipse.nebula.widgets.opal.notifier;
|
||||
|
||||
import org.eclipse.swt.graphics.Color;
|
||||
import org.eclipse.swt.widgets.Display;
|
||||
|
||||
/**
|
||||
* This class creates the colors associated to a given theme
|
||||
*
|
||||
*/
|
||||
public class NotifierColorsFactory {
|
||||
|
||||
public enum NotifierTheme {
|
||||
YELLOW_THEME, GRAY_THEME, BLUE_THEME
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
private NotifierColorsFactory() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param theme a theme for the notifier widget
|
||||
* @return the color set for the given theme
|
||||
*/
|
||||
static NotifierColors getColorsForTheme(final NotifierTheme theme) {
|
||||
final NotifierColors colors = new NotifierColors();
|
||||
Display display = Display.getDefault();
|
||||
switch (theme) {
|
||||
case BLUE_THEME:
|
||||
colors.textColor = new Color(display, 4, 64, 140);
|
||||
colors.titleColor = new Color(display, 0, 0, 0);
|
||||
colors.borderColor = new Color(display, 153, 188, 232);
|
||||
colors.leftColor = new Color(display, 210, 225, 244);
|
||||
colors.rightColor = new Color(display, 182, 207, 238);
|
||||
break;
|
||||
case GRAY_THEME:
|
||||
colors.textColor = new Color(display, 0, 0, 0);
|
||||
colors.titleColor = new Color(display, 255, 20, 20);
|
||||
colors.borderColor = new Color(display, 208, 208, 208);
|
||||
colors.leftColor = new Color(display, 255, 255, 255);
|
||||
colors.rightColor = new Color(display, 208, 208, 208);
|
||||
break;
|
||||
default:
|
||||
colors.textColor = new Color(display, 0, 0, 0);
|
||||
colors.titleColor = new Color(display, 0, 0, 0);
|
||||
colors.borderColor = new Color(display, 218, 178, 85);
|
||||
colors.leftColor = new Color(display, 220, 220, 160);
|
||||
colors.rightColor = new Color(display, 255, 255, 191);
|
||||
break;
|
||||
}
|
||||
return colors;
|
||||
}
|
||||
|
||||
}
|
||||
+382
@@ -0,0 +1,382 @@
|
||||
package org.eclipse.nebula.widgets.opal.notifier;
|
||||
|
||||
import org.eclipse.nebula.widgets.opal.commons.SWTGraphicUtil;
|
||||
import org.eclipse.nebula.widgets.opal.notifier.NotifierColorsFactory.NotifierTheme;
|
||||
import org.eclipse.swt.SWT;
|
||||
import org.eclipse.swt.custom.StyledText;
|
||||
import org.eclipse.swt.graphics.Color;
|
||||
import org.eclipse.swt.graphics.Font;
|
||||
import org.eclipse.swt.graphics.GC;
|
||||
import org.eclipse.swt.graphics.Image;
|
||||
import org.eclipse.swt.graphics.Rectangle;
|
||||
import org.eclipse.swt.layout.GridData;
|
||||
import org.eclipse.swt.layout.GridLayout;
|
||||
import org.eclipse.swt.widgets.Display;
|
||||
import org.eclipse.swt.widgets.Label;
|
||||
import org.eclipse.swt.widgets.Shell;
|
||||
|
||||
/**
|
||||
* This class provides a notifier window, which is a window that appears in the
|
||||
* bottom of the screen and slides.
|
||||
*/
|
||||
public class WNotifier {
|
||||
|
||||
private static final int FONT_SIZE = 10;
|
||||
private static final int MAX_DURATION_FOR_OPENING = 500;
|
||||
|
||||
private static final int FADE_TIMER = 50;
|
||||
private static final int FADE_OUT_STEP = 8;
|
||||
|
||||
private static final int STEP = 5;
|
||||
|
||||
private int displayTime = 4500;
|
||||
private INotificationClickedListener clickListener;
|
||||
private boolean closeAfterClick = false;
|
||||
|
||||
private final String title;
|
||||
private final String text;
|
||||
private final Image image;
|
||||
private final NotifierTheme theme;
|
||||
|
||||
public static NotifierTheme Themes;
|
||||
|
||||
|
||||
/**
|
||||
* Starts a notification. A window will appear in the bottom of the screen, then
|
||||
* will disappear after 4.5 s
|
||||
*
|
||||
* @param title the title of the popup window
|
||||
* @param text the text of the notification
|
||||
*
|
||||
*/
|
||||
public WNotifier(final String title, final String text) {
|
||||
this.image = null;
|
||||
this.text = text;
|
||||
this.title = title;
|
||||
this.theme = NotifierTheme.BLUE_THEME;
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts a notification. A window will appear in the bottom of the screen, then
|
||||
* will disappear after 4.5 s
|
||||
*
|
||||
* @param image the image to display (if <code>null</code>, a default image is
|
||||
* displayed)
|
||||
* @param title the title of the popup window
|
||||
* @param text the text of the notification
|
||||
*
|
||||
*/
|
||||
public WNotifier(final Image image, final String title, final String text) {
|
||||
this.image = image;
|
||||
this.text = text;
|
||||
this.title = title;
|
||||
this.theme = NotifierTheme.BLUE_THEME;
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts a notification. A window will appear in the bottom of the screen, then
|
||||
* will disappear after 4.5 s
|
||||
*
|
||||
* @param title the title of the popup window
|
||||
* @param text the text of the notification
|
||||
* @param theme the graphical theme. If <code>null</code>, the yellow theme is
|
||||
* used
|
||||
*
|
||||
* @see NotifierTheme
|
||||
*/
|
||||
public WNotifier(final String title, final String text, final NotifierTheme theme) {
|
||||
this.image = null;
|
||||
this.text = text;
|
||||
this.title = title;
|
||||
this.theme = theme;
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts a notification. A window will appear in the bottom of the screen, then
|
||||
* will disappear after 4.5 s
|
||||
*
|
||||
* @param image the image to display (if <code>null</code>, a default image is
|
||||
* displayed)
|
||||
* @param title the title of the popup window
|
||||
* @param text the text of the notification
|
||||
* @param theme the graphical theme. If <code>null</code>, the yellow theme is
|
||||
* used
|
||||
*
|
||||
* @see NotifierTheme
|
||||
*/
|
||||
public WNotifier(final Image image, final String title, final String text, final NotifierTheme theme) {
|
||||
this.image = image;
|
||||
this.text = text;
|
||||
this.title = title;
|
||||
this.theme = theme;
|
||||
}
|
||||
|
||||
|
||||
public void send() {
|
||||
final Shell shell = createNotificationWindow(image, title, text, NotifierColorsFactory.getColorsForTheme(theme));
|
||||
makeShellAppears(shell);
|
||||
}
|
||||
|
||||
public WNotifier withTimeout(double seconds) {
|
||||
displayTime = (int) (seconds * 1000);
|
||||
return this;
|
||||
}
|
||||
|
||||
public WNotifier withCloseOnClick(boolean x) {
|
||||
onClick(null,x);
|
||||
return this;
|
||||
}
|
||||
|
||||
public WNotifier onClick(INotificationClickedListener listener) {
|
||||
return onClick(listener, false);
|
||||
}
|
||||
|
||||
public WNotifier onClick(INotificationClickedListener listener, boolean close) {
|
||||
clickListener = listener;
|
||||
closeAfterClick = close;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a notification window
|
||||
*
|
||||
* @param image image. If <code>null</code>, a default image is used
|
||||
* @param title title, the title of the window
|
||||
* @param text text of the window
|
||||
* @param colors color set
|
||||
* @return the notification window as a shell object
|
||||
*/
|
||||
protected Shell createNotificationWindow(final Image image, final String title, final String text, final NotifierColors colors) {
|
||||
final Shell shell = new Shell(Display.getDefault().getActiveShell(), SWT.NO_TRIM | SWT.NO_FOCUS | SWT.ON_TOP);
|
||||
shell.setLayout(new GridLayout(2, false));
|
||||
shell.setBackgroundMode(SWT.INHERIT_FORCE);
|
||||
|
||||
createTitle(shell, title, colors);
|
||||
createImage(shell, image);
|
||||
createText(shell, text, colors);
|
||||
createBackground(shell, colors);
|
||||
createCloseAction(shell);
|
||||
createClickAction(shell);
|
||||
|
||||
shell.addListener(SWT.Dispose, event -> {
|
||||
colors.dispose();
|
||||
});
|
||||
|
||||
shell.pack();
|
||||
shell.setMinimumSize(320, 100);
|
||||
return shell;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the title part of the window
|
||||
*
|
||||
* @param shell the window
|
||||
* @param title the title
|
||||
* @param colors the color set
|
||||
*/
|
||||
private void createTitle(final Shell shell, final String title, final NotifierColors colors) {
|
||||
final Label titleLabel = new Label(shell, SWT.NONE);
|
||||
final GridData gdLabel = new GridData(GridData.BEGINNING, GridData.BEGINNING, true, false, 2, 1);
|
||||
gdLabel.horizontalIndent = 40;
|
||||
titleLabel.setLayoutData(gdLabel);
|
||||
final Color titleColor = colors.titleColor;
|
||||
titleLabel.setForeground(titleColor);
|
||||
|
||||
final Font titleFont = SWTGraphicUtil.buildFontFrom(titleLabel, SWT.BOLD, FONT_SIZE);
|
||||
titleLabel.setFont(titleFont);
|
||||
titleLabel.setText(title);
|
||||
SWTGraphicUtil.addDisposer(shell, titleFont);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the image part of the window
|
||||
*
|
||||
* @param shell the window
|
||||
* @param image the image
|
||||
*/
|
||||
private void createImage(final Shell shell, final Image image) {
|
||||
final Label labelImage = new Label(shell, SWT.NONE);
|
||||
final GridData gdImage = new GridData(GridData.CENTER, GridData.BEGINNING, false, true);
|
||||
gdImage.horizontalIndent = 10;
|
||||
labelImage.setLayoutData(gdImage);
|
||||
if (image == null) {
|
||||
final Image temp = SWTGraphicUtil.createImageFromFile("images/information.png");
|
||||
labelImage.setImage(temp);
|
||||
SWTGraphicUtil.addDisposer(shell, temp);
|
||||
} else {
|
||||
labelImage.setImage(image);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the text part of the window
|
||||
*
|
||||
* @param shell the window
|
||||
* @param text the text
|
||||
* @param colors the color set
|
||||
*/
|
||||
private void createText(final Shell shell, final String text, final NotifierColors colors) {
|
||||
final StyledText textLabel = new StyledText(shell, SWT.WRAP | SWT.READ_ONLY);
|
||||
final GridData gdText = new GridData(GridData.FILL, GridData.FILL, true, true);
|
||||
gdText.horizontalIndent = 15;
|
||||
textLabel.setLayoutData(gdText);
|
||||
textLabel.setEnabled(false);
|
||||
final Font textFont = SWTGraphicUtil.buildFontFrom(textLabel, SWT.NONE, 10);
|
||||
textLabel.setFont(textFont);
|
||||
|
||||
final Color textColor = colors.textColor;
|
||||
textLabel.setForeground(textColor);
|
||||
|
||||
textLabel.setText(text);
|
||||
SWTGraphicUtil.applyHTMLFormating(textLabel);
|
||||
|
||||
SWTGraphicUtil.addDisposer(shell, textFont);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the background of the window
|
||||
*
|
||||
* @param shell the window
|
||||
* @param colors the color set of the window
|
||||
*/
|
||||
private void createBackground(final Shell shell, final NotifierColors colors) {
|
||||
shell.addListener(SWT.Resize, event -> {
|
||||
final Rectangle rect = shell.getClientArea();
|
||||
final Image newImage = new Image(Display.getDefault(), Math.max(1, rect.width), rect.height);
|
||||
final GC gc = new GC(newImage);
|
||||
gc.setAntialias(SWT.ON);
|
||||
|
||||
final Color borderColor = colors.borderColor;
|
||||
final Color fillColor1 = colors.leftColor;
|
||||
final Color fillColor2 = colors.rightColor;
|
||||
|
||||
gc.setBackground(borderColor);
|
||||
gc.fillRoundRectangle(0, 0, rect.width, rect.height, 8, 8);
|
||||
|
||||
gc.setBackground(fillColor1);
|
||||
gc.fillRoundRectangle(1, 1, rect.width - 2, rect.height - 2, 8, 8);
|
||||
|
||||
gc.setBackground(fillColor2);
|
||||
gc.fillRoundRectangle(30, 1, rect.width - 32, rect.height - 2, 8, 8);
|
||||
gc.fillRectangle(30, 1, 10, rect.height - 2);
|
||||
|
||||
final Image closeImage = SWTGraphicUtil.createImageFromFile("images/close.png");
|
||||
gc.drawImage(closeImage, rect.width - 21, 13);
|
||||
|
||||
gc.dispose();
|
||||
closeImage.dispose();
|
||||
|
||||
shell.setBackgroundImage(newImage);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param shell shell that will appear
|
||||
*/
|
||||
protected void makeShellAppears(final Shell shell) {
|
||||
if (shell == null || shell.isDisposed()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final Rectangle clientArea = Display.getDefault().getPrimaryMonitor().getClientArea();
|
||||
final int startX = clientArea.x + clientArea.width - shell.getSize().x;
|
||||
|
||||
final int stepForPosition = MAX_DURATION_FOR_OPENING / shell.getSize().y * STEP;
|
||||
final int stepForAlpha = STEP * 255 / shell.getSize().y;
|
||||
|
||||
final int lastPosition = clientArea.y + clientArea.height - shell.getSize().y;
|
||||
|
||||
shell.setAlpha(0);
|
||||
shell.setLocation(startX, clientArea.y + clientArea.height);
|
||||
shell.open();
|
||||
|
||||
shell.getDisplay().timerExec(stepForPosition, new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
if (shell == null || shell.isDisposed()) {
|
||||
return;
|
||||
}
|
||||
|
||||
shell.setLocation(startX, shell.getLocation().y - STEP);
|
||||
shell.setAlpha(shell.getAlpha() + stepForAlpha);
|
||||
if (shell.getLocation().y >= lastPosition) {
|
||||
shell.getDisplay().timerExec(stepForPosition, this);
|
||||
} else {
|
||||
shell.setAlpha(255);
|
||||
Display.getDefault().timerExec(displayTime, fadeOut(shell, false));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param shell shell that will disappear
|
||||
* @param fast if true, the fading is much faster
|
||||
* @return a runnable
|
||||
*/
|
||||
private Runnable fadeOut(final Shell shell, final boolean fast) {
|
||||
return new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (shell == null || shell.isDisposed()) {
|
||||
return;
|
||||
}
|
||||
|
||||
int currentAlpha = shell.getAlpha();
|
||||
currentAlpha -= FADE_OUT_STEP * (fast ? 8 : 1);
|
||||
|
||||
if (currentAlpha <= 0) {
|
||||
shell.setAlpha(0);
|
||||
shell.dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
shell.setAlpha(currentAlpha);
|
||||
|
||||
Display.getDefault().timerExec(FADE_TIMER, this);
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
private void createClickAction(final Shell shell) {
|
||||
shell.addListener(SWT.MouseDown, event -> {
|
||||
|
||||
if(clickListener != null)
|
||||
clickListener.onNotificationClick();
|
||||
|
||||
if(closeAfterClick)
|
||||
Display.getDefault().timerExec(0, fadeOut(shell, true));
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a listener to the shell in order to handle the clicks on the close button
|
||||
*
|
||||
* @param shell associated shell
|
||||
*/
|
||||
private void createCloseAction(final Shell shell) {
|
||||
shell.addListener(SWT.MouseUp, event -> {
|
||||
final Rectangle rect = shell.getClientArea();
|
||||
final int xUpperLeftCorner = rect.width - 21;
|
||||
final int yUpperLeftCorner = 13;
|
||||
|
||||
if (event.x >= xUpperLeftCorner && event.x <= xUpperLeftCorner + 8 && event.y >= yUpperLeftCorner && event.y <= yUpperLeftCorner + 8) {
|
||||
Display.getDefault().timerExec(0, fadeOut(shell, true));
|
||||
} else if(event.button == 3) {
|
||||
Display.getDefault().timerExec(0, fadeOut(shell, true));
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user