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;
import java.util.stream.IntStream;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
@ -29,22 +31,17 @@ import javafx.event.EventType;
import javafx.event.WeakEventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.image.*;
import javafx.scene.input.*;
import javafx.scene.layout.*;
import javafx.stage.Popup;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;
import java.util.stream.IntStream;
/**
* Created by
@ -66,9 +63,11 @@ public class Notification {
public Notification(final String TITLE, final String MESSAGE) {
this(TITLE, MESSAGE, null);
}
public Notification(final String MESSAGE, final Image IMAGE) {
this("", MESSAGE, IMAGE);
}
public Notification(final String TITLE, final String MESSAGE, final Image IMAGE) {
this.TITLE = TITLE;
this.MESSAGE = MESSAGE;
@ -122,6 +121,7 @@ public class Notification {
// ******************** Methods *******************************************
/**
* @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.
@ -140,6 +140,7 @@ public class Notification {
* stage is closed Notifications will be shut down as well.<br>
* This is only needed if <code>setPopupLocation</code> is called
* <u>without</u> a stage reference.
*
* @param 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
* will fade out. The default is 5000 ms
*
* @return the Duration the popup notification will stay on screen
*/
public Duration getPopupLifetime() {
@ -201,6 +203,7 @@ public class Notification {
/**
* 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.
*
* @param 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
* The parameter is limited to values between 0 and 1000 ms
*
* @return the Duration that it takes to fade out the notification
*/
public Duration getPopupAnimationTime() {
@ -220,6 +224,7 @@ public class Notification {
* Defines the Duration that it takes to fade out the notification
* The parameter is limited to values between 0 and 1000 ms
* Default value is 500 ms
*
* @param 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
*
* @param NOTIFICATION
*/
public void notify(final Notification NOTIFICATION) {
@ -237,6 +243,7 @@ public class Notification {
/**
* Show a Notification with the given parameters on the screen
*
* @param TITLE
* @param MESSAGE
* @param IMAGE
@ -247,6 +254,7 @@ public class Notification {
/**
* Show a Notification with the given title and message and an Info icon
*
* @param TITLE
* @param MESSAGE
*/
@ -256,6 +264,7 @@ public class Notification {
/**
* Show a Notification with the given title and message and a Warning icon
*
* @param TITLE
* @param MESSAGE
*/
@ -265,6 +274,7 @@ public class Notification {
/**
* Show a Notification with the given title and message and a Checkmark icon
*
* @param TITLE
* @param MESSAGE
*/
@ -274,6 +284,7 @@ public class Notification {
/**
* Show a Notification with the given title and message and an Error icon
*
* @param TITLE
* @param MESSAGE
*/
@ -283,6 +294,7 @@ public class Notification {
/**
* Makes sure that the given VALUE is within the range of MIN to MAX
*
* @param MIN
* @param MAX
* @param VALUE
@ -302,11 +314,15 @@ public class Notification {
IntStream.range(0, popups.size()).parallel().forEachOrdered(
i -> {
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);
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);
break;
@ -320,6 +336,7 @@ public class Notification {
/**
* Creates and shows a popup with the data from the given Notification object
*
* @param NOTIFICATION
*/
private void showPopup(final Notification NOTIFICATION) {
@ -348,7 +365,8 @@ public class Notification {
POPUP.setY(getY());
POPUP.getContent().add(popupContent);
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);
@ -364,17 +382,20 @@ public class Notification {
timeline.setOnFinished(actionEvent -> Platform.runLater(() -> {
POPUP.hide();
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()) {
stage.toFront();
} else {
}
else {
stage.show();
}
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();
}
@ -383,6 +404,7 @@ public class Notification {
return calcX(stageRef.getX(), stageRef.getWidth());
}
private double getY() {
if (null == stageRef) return calcY(0.0, Screen.getPrimary().getBounds().getHeight());
return calcY(stageRef.getY(), stageRef.getHeight());
@ -390,45 +412,117 @@ public class Notification {
private double calcX(final double LEFT, final double TOTAL_WIDTH) {
switch (popupLocation) {
case TOP_LEFT : case CENTER_LEFT : case BOTTOM_LEFT : 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;
case TOP_LEFT:
case CENTER_LEFT:
case BOTTOM_LEFT:
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) {
switch (popupLocation) {
case TOP_LEFT : case TOP_CENTER : case TOP_RIGHT : 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;
case TOP_LEFT:
case TOP_CENTER:
case TOP_RIGHT:
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 ********************************
public final ObjectProperty<EventHandler<NotificationEvent>> onNotificationPressedProperty() { return onNotificationPressed; }
public final void setOnNotificationPressed(EventHandler<NotificationEvent> value) { 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>> onNotificationPressedProperty() {
return onNotificationPressed;
}
public final void setOnNotificationPressed(EventHandler<NotificationEvent> value) {
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 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>> 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 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";
}
};
@ -437,11 +531,14 @@ public class Notification {
final EventHandler<NotificationEvent> HANDLER;
if (NotificationEvent.NOTIFICATION_PRESSED == TYPE) {
HANDLER = getOnNotificationPressed();
} else if (NotificationEvent.SHOW_NOTIFICATION == TYPE) {
}
else if (NotificationEvent.SHOW_NOTIFICATION == TYPE) {
HANDLER = getOnShowNotification();
} else if (NotificationEvent.HIDE_NOTIFICATION == TYPE) {
}
else if (NotificationEvent.HIDE_NOTIFICATION == TYPE) {
HANDLER = getOnHideNotification();
} else {
}
else {
HANDLER = null;
}
if (null == HANDLER) return;