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
@ -66,9 +63,11 @@ public class Notification {
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;
@ -122,6 +121,7 @@ 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.
@ -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) {
@ -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
@ -302,11 +314,15 @@ public class Notification {
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:
case TOP_CENTER:
case TOP_RIGHT:
popups.get(i).setY(popups.get(i).getY() + height + spacingY); popups.get(i).setY(popups.get(i).getY() + height + spacingY);
break; break;
case BOTTOM_LEFT: case BOTTOM_CENTER: case BOTTOM_RIGHT: case BOTTOM_LEFT:
case BOTTOM_CENTER:
case BOTTOM_RIGHT:
popups.get(i).setY(popups.get(i).getY() - height - spacingY); popups.get(i).setY(popups.get(i).getY() - height - spacingY);
break; break;
@ -320,6 +336,7 @@ public class Notification {
/** /**
* 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,7 +365,8 @@ 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);
@ -364,17 +382,20 @@ public class Notification {
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();
} }
@ -383,6 +404,7 @@ public class Notification {
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());
@ -390,45 +412,117 @@ public class Notification {
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; } public final void setOnNotificationPressed(EventHandler<NotificationEvent> value) {
@Override public String getName() { return "onNotificationPressed";} onNotificationPressedProperty().set(value);
}
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 ObjectProperty<EventHandler<NotificationEvent>> onShowNotificationProperty() {
public final void setOnShowNotification(EventHandler<NotificationEvent> value) { onShowNotificationProperty().set(value); } return onShowNotification;
public final EventHandler<NotificationEvent> getOnShowNotification() { return onShowNotificationProperty().get(); } }
private ObjectProperty<EventHandler<NotificationEvent>> onShowNotification = new ObjectPropertyBase<EventHandler<NotificationEvent>>() {
@Override public Object getBean() { return this; } public final void setOnShowNotification(EventHandler<NotificationEvent> value) {
@Override public String getName() { return "onShowNotification";} 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 ObjectProperty<EventHandler<NotificationEvent>> onHideNotificationProperty() {
public final void setOnHideNotification(EventHandler<NotificationEvent> value) { onHideNotificationProperty().set(value); } return onHideNotification;
public final EventHandler<NotificationEvent> getOnHideNotification() { return onHideNotificationProperty().get(); } }
private ObjectProperty<EventHandler<NotificationEvent>> onHideNotification = new ObjectPropertyBase<EventHandler<NotificationEvent>>() {
@Override public Object getBean() { return this; } public final void setOnHideNotification(EventHandler<NotificationEvent> value) {
@Override public String getName() { return "onHideNotification";} 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";
}
}; };
@ -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;