Reformat enzo Notification per Bitsquare rules

This commit is contained in:
Chris Beams 2014-10-30 15:06:25 +01:00
parent 47d9693a07
commit 6435e2ab80
No known key found for this signature in database
GPG key ID: 3D214F8F5BC5ED73

View file

@ -16,6 +16,8 @@
package eu.hansolo.enzo.notification; package eu.hansolo.enzo.notification;
import java.util.stream.IntStream;
import javafx.animation.KeyFrame; import javafx.animation.KeyFrame;
import javafx.animation.KeyValue; import javafx.animation.KeyValue;
import javafx.animation.Timeline; import javafx.animation.Timeline;
@ -29,22 +31,17 @@ import javafx.event.EventType;
import javafx.event.WeakEventHandler; import javafx.event.WeakEventHandler;
import javafx.geometry.Insets; import javafx.geometry.Insets;
import javafx.geometry.Pos; import javafx.geometry.Pos;
import javafx.scene.Scene; import javafx.scene.*;
import javafx.scene.control.Label; import javafx.scene.control.*;
import javafx.scene.image.Image; import javafx.scene.image.*;
import javafx.scene.image.ImageView; import javafx.scene.input.*;
import javafx.scene.input.MouseEvent; import javafx.scene.layout.*;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Popup; import javafx.stage.Popup;
import javafx.stage.Screen; import javafx.stage.Screen;
import javafx.stage.Stage; import javafx.stage.Stage;
import javafx.stage.StageStyle; import javafx.stage.StageStyle;
import javafx.util.Duration; import javafx.util.Duration;
import java.util.stream.IntStream;
/** /**
* Created by * Created by
@ -53,26 +50,28 @@ import java.util.stream.IntStream;
* Time: 07:10 * Time: 07:10
*/ */
public class Notification { public class Notification {
public static final Image INFO_ICON = new Image(Notifier.class.getResourceAsStream("info.png")); public static final Image INFO_ICON = new Image(Notifier.class.getResourceAsStream("info.png"));
public static final Image WARNING_ICON = new Image(Notifier.class.getResourceAsStream("warning.png")); public static final Image WARNING_ICON = new Image(Notifier.class.getResourceAsStream("warning.png"));
public static final Image SUCCESS_ICON = new Image(Notifier.class.getResourceAsStream("success.png")); public static final Image SUCCESS_ICON = new Image(Notifier.class.getResourceAsStream("success.png"));
public static final Image ERROR_ICON = new Image(Notifier.class.getResourceAsStream("error.png")); public static final Image ERROR_ICON = new Image(Notifier.class.getResourceAsStream("error.png"));
public final String TITLE; public final String TITLE;
public final String MESSAGE; public final String MESSAGE;
public final Image IMAGE; public final Image IMAGE;
// ******************** Constructors ************************************** // ******************** Constructors **************************************
public Notification(final String TITLE, final String MESSAGE) { public Notification(final String TITLE, final String MESSAGE) {
this(TITLE, MESSAGE, null); this(TITLE, MESSAGE, null);
} }
public Notification(final String MESSAGE, final Image IMAGE) { public Notification(final String MESSAGE, final Image IMAGE) {
this("", MESSAGE, IMAGE); this("", MESSAGE, IMAGE);
} }
public Notification(final String TITLE, final String MESSAGE, final Image IMAGE) { public Notification(final String TITLE, final String MESSAGE, final Image IMAGE) {
this.TITLE = TITLE; this.TITLE = TITLE;
this.MESSAGE = MESSAGE; this.MESSAGE = MESSAGE;
this.IMAGE = IMAGE; this.IMAGE = IMAGE;
} }
@ -80,19 +79,19 @@ public class Notification {
public enum Notifier { public enum Notifier {
INSTANCE; INSTANCE;
private static final double ICON_WIDTH = 24; private static final double ICON_WIDTH = 24;
private static final double ICON_HEIGHT = 24; private static final double ICON_HEIGHT = 24;
private static double width = 300; private static double width = 300;
private static double height = 80; private static double height = 80;
private static double offsetX = 0; private static double offsetX = 0;
private static double offsetY = 25; private static double offsetY = 25;
private static double spacingY = 5; private static double spacingY = 5;
private static Pos popupLocation = Pos.TOP_RIGHT; private static Pos popupLocation = Pos.TOP_RIGHT;
private static Stage stageRef = null; private static Stage stageRef = null;
private Duration popupLifetime; private Duration popupLifetime;
private Duration popupAnimationTime; private Duration popupAnimationTime;
private Stage stage; private Stage stage;
private Scene scene; private Scene scene;
private ObservableList<Popup> popups; private ObservableList<Popup> popups;
@ -105,9 +104,9 @@ public class Notification {
// ******************** Initialization ************************************ // ******************** Initialization ************************************
private void init() { private void init() {
popupLifetime = Duration.millis(5000); popupLifetime = Duration.millis(5000);
popupAnimationTime = Duration.millis(500); popupAnimationTime = Duration.millis(500);
popups = FXCollections.observableArrayList(); popups = FXCollections.observableArrayList();
} }
private void initGraphics() { private void initGraphics() {
@ -122,10 +121,11 @@ public class Notification {
// ******************** Methods ******************************************* // ******************** Methods *******************************************
/** /**
* @param STAGE_REF The Notification will be positioned relative to the given Stage.<br> * @param STAGE_REF The Notification will be positioned relative to the given Stage.<br>
* If null then the Notification will be positioned relative to the primary Screen. * If null then the Notification will be positioned relative to the primary Screen.
* @param POPUP_LOCATION The default is TOP_RIGHT of primary Screen. * @param POPUP_LOCATION The default is TOP_RIGHT of primary Screen.
*/ */
public static void setPopupLocation(final Stage STAGE_REF, final Pos POPUP_LOCATION) { public static void setPopupLocation(final Stage STAGE_REF, final Pos POPUP_LOCATION) {
if (null != STAGE_REF) { if (null != STAGE_REF) {
@ -140,6 +140,7 @@ public class Notification {
* stage is closed Notifications will be shut down as well.<br> * stage is closed Notifications will be shut down as well.<br>
* This is only needed if <code>setPopupLocation</code> is called * This is only needed if <code>setPopupLocation</code> is called
* <u>without</u> a stage reference. * <u>without</u> a stage reference.
*
* @param OWNER * @param OWNER
*/ */
public static void setNotificationOwner(final Stage OWNER) { public static void setNotificationOwner(final Stage OWNER) {
@ -147,38 +148,38 @@ public class Notification {
} }
/** /**
* @param OFFSET_X The horizontal shift required. * @param OFFSET_X The horizontal shift required.
* <br> The default is 0 px. * <br> The default is 0 px.
*/ */
public static void setOffsetX(final double OFFSET_X) { public static void setOffsetX(final double OFFSET_X) {
Notifier.offsetX = OFFSET_X; Notifier.offsetX = OFFSET_X;
} }
/** /**
* @param OFFSET_Y The vertical shift required. * @param OFFSET_Y The vertical shift required.
* <br> The default is 25 px. * <br> The default is 25 px.
*/ */
public static void setOffsetY(final double OFFSET_Y) { public static void setOffsetY(final double OFFSET_Y) {
Notifier.offsetY = OFFSET_Y; Notifier.offsetY = OFFSET_Y;
} }
/** /**
* @param WIDTH The default is 300 px. * @param WIDTH The default is 300 px.
*/ */
public static void setWidth(final double WIDTH) { public static void setWidth(final double WIDTH) {
Notifier.width = WIDTH; Notifier.width = WIDTH;
} }
/** /**
* @param HEIGHT The default is 80 px. * @param HEIGHT The default is 80 px.
*/ */
public static void setHeight(final double HEIGHT) { public static void setHeight(final double HEIGHT) {
Notifier.height = HEIGHT; Notifier.height = HEIGHT;
} }
/** /**
* @param SPACING_Y The spacing between multiple Notifications. * @param SPACING_Y The spacing between multiple Notifications.
* <br> The default is 5 px. * <br> The default is 5 px.
*/ */
public static void setSpacingY(final double SPACING_Y) { public static void setSpacingY(final double SPACING_Y) {
Notifier.spacingY = SPACING_Y; Notifier.spacingY = SPACING_Y;
@ -192,6 +193,7 @@ public class Notification {
/** /**
* Returns the Duration that the notification will stay on screen before it * Returns the Duration that the notification will stay on screen before it
* will fade out. The default is 5000 ms * will fade out. The default is 5000 ms
*
* @return the Duration the popup notification will stay on screen * @return the Duration the popup notification will stay on screen
*/ */
public Duration getPopupLifetime() { public Duration getPopupLifetime() {
@ -201,6 +203,7 @@ public class Notification {
/** /**
* Defines the Duration that the popup notification will stay on screen before it * Defines the Duration that the popup notification will stay on screen before it
* will fade out. The parameter is limited to values between 2 and 20 seconds. * will fade out. The parameter is limited to values between 2 and 20 seconds.
*
* @param POPUP_LIFETIME * @param POPUP_LIFETIME
*/ */
public void setPopupLifetime(final Duration POPUP_LIFETIME) { public void setPopupLifetime(final Duration POPUP_LIFETIME) {
@ -210,6 +213,7 @@ public class Notification {
/** /**
* Returns the Duration that it takes to fade out the notification * Returns the Duration that it takes to fade out the notification
* The parameter is limited to values between 0 and 1000 ms * The parameter is limited to values between 0 and 1000 ms
*
* @return the Duration that it takes to fade out the notification * @return the Duration that it takes to fade out the notification
*/ */
public Duration getPopupAnimationTime() { public Duration getPopupAnimationTime() {
@ -220,6 +224,7 @@ public class Notification {
* Defines the Duration that it takes to fade out the notification * Defines the Duration that it takes to fade out the notification
* The parameter is limited to values between 0 and 1000 ms * The parameter is limited to values between 0 and 1000 ms
* Default value is 500 ms * Default value is 500 ms
*
* @param POPUP_ANIMATION_TIME * @param POPUP_ANIMATION_TIME
*/ */
public void setPopupAnimationTime(final Duration POPUP_ANIMATION_TIME) { public void setPopupAnimationTime(final Duration POPUP_ANIMATION_TIME) {
@ -228,6 +233,7 @@ public class Notification {
/** /**
* Show the given Notification on the screen * Show the given Notification on the screen
*
* @param NOTIFICATION * @param NOTIFICATION
*/ */
public void notify(final Notification NOTIFICATION) { public void notify(final Notification NOTIFICATION) {
@ -237,6 +243,7 @@ public class Notification {
/** /**
* Show a Notification with the given parameters on the screen * Show a Notification with the given parameters on the screen
*
* @param TITLE * @param TITLE
* @param MESSAGE * @param MESSAGE
* @param IMAGE * @param IMAGE
@ -247,6 +254,7 @@ public class Notification {
/** /**
* Show a Notification with the given title and message and an Info icon * Show a Notification with the given title and message and an Info icon
*
* @param TITLE * @param TITLE
* @param MESSAGE * @param MESSAGE
*/ */
@ -256,6 +264,7 @@ public class Notification {
/** /**
* Show a Notification with the given title and message and a Warning icon * Show a Notification with the given title and message and a Warning icon
*
* @param TITLE * @param TITLE
* @param MESSAGE * @param MESSAGE
*/ */
@ -265,6 +274,7 @@ public class Notification {
/** /**
* Show a Notification with the given title and message and a Checkmark icon * Show a Notification with the given title and message and a Checkmark icon
*
* @param TITLE * @param TITLE
* @param MESSAGE * @param MESSAGE
*/ */
@ -274,6 +284,7 @@ public class Notification {
/** /**
* Show a Notification with the given title and message and an Error icon * Show a Notification with the given title and message and an Error icon
*
* @param TITLE * @param TITLE
* @param MESSAGE * @param MESSAGE
*/ */
@ -283,6 +294,7 @@ public class Notification {
/** /**
* Makes sure that the given VALUE is within the range of MIN to MAX * Makes sure that the given VALUE is within the range of MIN to MAX
*
* @param MIN * @param MIN
* @param MAX * @param MAX
* @param VALUE * @param VALUE
@ -300,26 +312,31 @@ public class Notification {
private void preOrder() { private void preOrder() {
if (popups.isEmpty()) return; if (popups.isEmpty()) return;
IntStream.range(0, popups.size()).parallel().forEachOrdered( IntStream.range(0, popups.size()).parallel().forEachOrdered(
i -> { i -> {
switch (popupLocation) { switch (popupLocation) {
case TOP_LEFT: case TOP_CENTER: case TOP_RIGHT: case TOP_LEFT:
popups.get(i).setY(popups.get(i).getY() + height + spacingY); case TOP_CENTER:
break; case TOP_RIGHT:
popups.get(i).setY(popups.get(i).getY() + height + spacingY);
break;
case BOTTOM_LEFT: case BOTTOM_CENTER: case BOTTOM_RIGHT: case BOTTOM_LEFT:
popups.get(i).setY(popups.get(i).getY() - height - spacingY); case BOTTOM_CENTER:
break; case BOTTOM_RIGHT:
popups.get(i).setY(popups.get(i).getY() - height - spacingY);
break;
default: default:
popups.get(i).setY(popups.get(i).getY() - height - spacingY); popups.get(i).setY(popups.get(i).getY() - height - spacingY);
break; break;
}
} }
}
); );
} }
/** /**
* Creates and shows a popup with the data from the given Notification object * Creates and shows a popup with the data from the given Notification object
*
* @param NOTIFICATION * @param NOTIFICATION
*/ */
private void showPopup(final Notification NOTIFICATION) { private void showPopup(final Notification NOTIFICATION) {
@ -348,88 +365,165 @@ public class Notification {
POPUP.setY(getY()); POPUP.setY(getY());
POPUP.getContent().add(popupContent); POPUP.getContent().add(popupContent);
POPUP.addEventHandler(MouseEvent.MOUSE_PRESSED, new WeakEventHandler<>(event -> POPUP.addEventHandler(MouseEvent.MOUSE_PRESSED, new WeakEventHandler<>(event ->
fireNotificationEvent(new NotificationEvent(NOTIFICATION, Notifier.this, POPUP, NotificationEvent.NOTIFICATION_PRESSED)) fireNotificationEvent(new NotificationEvent(NOTIFICATION, Notifier.this, POPUP,
NotificationEvent.NOTIFICATION_PRESSED))
)); ));
popups.add(POPUP); popups.add(POPUP);
// Add a timeline for popup fade out // Add a timeline for popup fade out
KeyValue fadeOutBegin = new KeyValue(POPUP.opacityProperty(), 1.0); KeyValue fadeOutBegin = new KeyValue(POPUP.opacityProperty(), 1.0);
KeyValue fadeOutEnd = new KeyValue(POPUP.opacityProperty(), 0.0); KeyValue fadeOutEnd = new KeyValue(POPUP.opacityProperty(), 0.0);
KeyFrame kfBegin = new KeyFrame(Duration.ZERO, fadeOutBegin); KeyFrame kfBegin = new KeyFrame(Duration.ZERO, fadeOutBegin);
KeyFrame kfEnd = new KeyFrame(popupAnimationTime, fadeOutEnd); KeyFrame kfEnd = new KeyFrame(popupAnimationTime, fadeOutEnd);
Timeline timeline = new Timeline(kfBegin, kfEnd); Timeline timeline = new Timeline(kfBegin, kfEnd);
timeline.setDelay(popupLifetime); timeline.setDelay(popupLifetime);
timeline.setOnFinished(actionEvent -> Platform.runLater(() -> { timeline.setOnFinished(actionEvent -> Platform.runLater(() -> {
POPUP.hide(); POPUP.hide();
popups.remove(POPUP); popups.remove(POPUP);
fireNotificationEvent(new NotificationEvent(NOTIFICATION, Notifier.this, POPUP, NotificationEvent.HIDE_NOTIFICATION)); fireNotificationEvent(new NotificationEvent(NOTIFICATION, Notifier.this, POPUP,
NotificationEvent.HIDE_NOTIFICATION));
})); }));
if (stage.isShowing()) { if (stage.isShowing()) {
stage.toFront(); stage.toFront();
} else { }
else {
stage.show(); stage.show();
} }
POPUP.show(stage); POPUP.show(stage);
fireNotificationEvent(new NotificationEvent(NOTIFICATION, Notifier.this, POPUP, NotificationEvent.SHOW_NOTIFICATION)); fireNotificationEvent(new NotificationEvent(NOTIFICATION, Notifier.this, POPUP,
NotificationEvent.SHOW_NOTIFICATION));
timeline.play(); timeline.play();
} }
private double getX() { private double getX() {
if (null == stageRef) return calcX( 0.0, Screen.getPrimary().getBounds().getWidth() ); if (null == stageRef) return calcX(0.0, Screen.getPrimary().getBounds().getWidth());
return calcX(stageRef.getX(), stageRef.getWidth()); return calcX(stageRef.getX(), stageRef.getWidth());
} }
private double getY() { private double getY() {
if (null == stageRef) return calcY( 0.0, Screen.getPrimary().getBounds().getHeight() ); if (null == stageRef) return calcY(0.0, Screen.getPrimary().getBounds().getHeight());
return calcY(stageRef.getY(), stageRef.getHeight()); return calcY(stageRef.getY(), stageRef.getHeight());
} }
private double calcX(final double LEFT, final double TOTAL_WIDTH) { private double calcX(final double LEFT, final double TOTAL_WIDTH) {
switch (popupLocation) { switch (popupLocation) {
case TOP_LEFT : case CENTER_LEFT : case BOTTOM_LEFT : return LEFT + offsetX; case TOP_LEFT:
case TOP_CENTER: case CENTER : case BOTTOM_CENTER: return LEFT + (TOTAL_WIDTH - width) * 0.5 - offsetX; case CENTER_LEFT:
case TOP_RIGHT : case CENTER_RIGHT: case BOTTOM_RIGHT : return LEFT + TOTAL_WIDTH - width - offsetX; case BOTTOM_LEFT:
default: return 0.0; return LEFT + offsetX;
case TOP_CENTER:
case CENTER:
case BOTTOM_CENTER:
return LEFT + (TOTAL_WIDTH - width) * 0.5 - offsetX;
case TOP_RIGHT:
case CENTER_RIGHT:
case BOTTOM_RIGHT:
return LEFT + TOTAL_WIDTH - width - offsetX;
default:
return 0.0;
} }
} }
private double calcY(final double TOP, final double TOTAL_HEIGHT ) {
private double calcY(final double TOP, final double TOTAL_HEIGHT) {
switch (popupLocation) { switch (popupLocation) {
case TOP_LEFT : case TOP_CENTER : case TOP_RIGHT : return TOP + offsetY; case TOP_LEFT:
case CENTER_LEFT: case CENTER : case CENTER_RIGHT: return TOP + (TOTAL_HEIGHT- height)/2 - offsetY; case TOP_CENTER:
case BOTTOM_LEFT: case BOTTOM_CENTER: case BOTTOM_RIGHT: return TOP + TOTAL_HEIGHT - height - offsetY; case TOP_RIGHT:
default: return 0.0; return TOP + offsetY;
case CENTER_LEFT:
case CENTER:
case CENTER_RIGHT:
return TOP + (TOTAL_HEIGHT - height) / 2 - offsetY;
case BOTTOM_LEFT:
case BOTTOM_CENTER:
case BOTTOM_RIGHT:
return TOP + TOTAL_HEIGHT - height - offsetY;
default:
return 0.0;
} }
} }
// ******************** Event handling ******************************** // ******************** Event handling ********************************
public final ObjectProperty<EventHandler<NotificationEvent>> onNotificationPressedProperty() { return onNotificationPressed; } public final ObjectProperty<EventHandler<NotificationEvent>> onNotificationPressedProperty() {
public final void setOnNotificationPressed(EventHandler<NotificationEvent> value) { onNotificationPressedProperty().set(value); } return onNotificationPressed;
public final EventHandler<NotificationEvent> getOnNotificationPressed() { return onNotificationPressedProperty().get(); } }
private ObjectProperty<EventHandler<NotificationEvent>> onNotificationPressed = new ObjectPropertyBase<EventHandler<NotificationEvent>>() {
@Override public Object getBean() { return this; }
@Override public String getName() { return "onNotificationPressed";}
};
public final ObjectProperty<EventHandler<NotificationEvent>> onShowNotificationProperty() { return onShowNotification; } public final void setOnNotificationPressed(EventHandler<NotificationEvent> value) {
public final void setOnShowNotification(EventHandler<NotificationEvent> value) { onShowNotificationProperty().set(value); } onNotificationPressedProperty().set(value);
public final EventHandler<NotificationEvent> getOnShowNotification() { return onShowNotificationProperty().get(); } }
private ObjectProperty<EventHandler<NotificationEvent>> onShowNotification = new ObjectPropertyBase<EventHandler<NotificationEvent>>() {
@Override public Object getBean() { return this; }
@Override public String getName() { return "onShowNotification";}
};
public final ObjectProperty<EventHandler<NotificationEvent>> onHideNotificationProperty() { return onHideNotification; } public final EventHandler<NotificationEvent> getOnNotificationPressed() {
public final void setOnHideNotification(EventHandler<NotificationEvent> value) { onHideNotificationProperty().set(value); } return onNotificationPressedProperty().get();
public final EventHandler<NotificationEvent> getOnHideNotification() { return onHideNotificationProperty().get(); } }
private ObjectProperty<EventHandler<NotificationEvent>> onHideNotification = new ObjectPropertyBase<EventHandler<NotificationEvent>>() {
@Override public Object getBean() { return this; } private ObjectProperty<EventHandler<NotificationEvent>> onNotificationPressed = new
@Override public String getName() { return "onHideNotification";} ObjectPropertyBase<EventHandler<NotificationEvent>>() {
}; @Override
public Object getBean() {
return this;
}
@Override
public String getName() {
return "onNotificationPressed";
}
};
public final ObjectProperty<EventHandler<NotificationEvent>> onShowNotificationProperty() {
return onShowNotification;
}
public final void setOnShowNotification(EventHandler<NotificationEvent> value) {
onShowNotificationProperty().set(value);
}
public final EventHandler<NotificationEvent> getOnShowNotification() {
return onShowNotificationProperty().get();
}
private ObjectProperty<EventHandler<NotificationEvent>> onShowNotification = new
ObjectPropertyBase<EventHandler<NotificationEvent>>() {
@Override
public Object getBean() {
return this;
}
@Override
public String getName() {
return "onShowNotification";
}
};
public final ObjectProperty<EventHandler<NotificationEvent>> onHideNotificationProperty() {
return onHideNotification;
}
public final void setOnHideNotification(EventHandler<NotificationEvent> value) {
onHideNotificationProperty().set(value);
}
public final EventHandler<NotificationEvent> getOnHideNotification() {
return onHideNotificationProperty().get();
}
private ObjectProperty<EventHandler<NotificationEvent>> onHideNotification = new
ObjectPropertyBase<EventHandler<NotificationEvent>>() {
@Override
public Object getBean() {
return this;
}
@Override
public String getName() {
return "onHideNotification";
}
};
public void fireNotificationEvent(final NotificationEvent EVENT) { public void fireNotificationEvent(final NotificationEvent EVENT) {
@ -437,11 +531,14 @@ public class Notification {
final EventHandler<NotificationEvent> HANDLER; final EventHandler<NotificationEvent> HANDLER;
if (NotificationEvent.NOTIFICATION_PRESSED == TYPE) { if (NotificationEvent.NOTIFICATION_PRESSED == TYPE) {
HANDLER = getOnNotificationPressed(); HANDLER = getOnNotificationPressed();
} else if (NotificationEvent.SHOW_NOTIFICATION == TYPE) { }
else if (NotificationEvent.SHOW_NOTIFICATION == TYPE) {
HANDLER = getOnShowNotification(); HANDLER = getOnShowNotification();
} else if (NotificationEvent.HIDE_NOTIFICATION == TYPE) { }
else if (NotificationEvent.HIDE_NOTIFICATION == TYPE) {
HANDLER = getOnHideNotification(); HANDLER = getOnHideNotification();
} else { }
else {
HANDLER = null; HANDLER = null;
} }
if (null == HANDLER) return; if (null == HANDLER) return;