Skip to content

Commit a9db566

Browse files
committed
Add simple notification
1 parent fe6dcb1 commit a9db566

3 files changed

Lines changed: 121 additions & 31 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.temporaryteam.noticeditor.view;
2+
3+
import javafx.animation.KeyFrame;
4+
import javafx.animation.Timeline;
5+
import javafx.animation.TranslateTransition;
6+
import javafx.event.ActionEvent;
7+
import javafx.scene.control.Label;
8+
import javafx.scene.layout.VBox;
9+
import javafx.util.Duration;
10+
11+
/**
12+
* Simple notification.
13+
* @author aNNiMON
14+
*/
15+
public final class Notification {
16+
17+
public static final Duration DURATION_SHORT = new Duration(2000);
18+
public static final Duration DURATION_LONG = new Duration(5000);
19+
private static final Duration TRANSITION_DURATION = new Duration(300);
20+
21+
private static VBox notificationBox;
22+
private static Label notificationLabel;
23+
24+
private static Timeline hideTimer;
25+
private static TranslateTransition transitionIn, transitionOut;
26+
27+
public static void init(VBox vbox, Label label) {
28+
notificationBox = vbox;
29+
notificationLabel = label;
30+
31+
transitionIn = new TranslateTransition(TRANSITION_DURATION, notificationBox);
32+
transitionIn.setToY(0);
33+
34+
transitionOut = new TranslateTransition(TRANSITION_DURATION, notificationBox);
35+
transitionOut.setFromY(0);
36+
transitionOut.setOnFinished((e) -> notificationBox.setVisible(false));
37+
}
38+
39+
public static void show(String text) {
40+
show(text, DURATION_SHORT);
41+
}
42+
43+
public static void show(String text, Duration duration) {
44+
if (hideTimer != null) {
45+
// show new notification while previous exists
46+
hideTimer.stop();
47+
transitionOut.stop();
48+
}
49+
notificationLabel.setText(text);
50+
hideTimer = new Timeline(new KeyFrame(duration.add(TRANSITION_DURATION)));
51+
hideTimer.setOnFinished(Notification::hide);
52+
hideTimer.playFromStart();
53+
notificationBox.setVisible(true);
54+
55+
transitionIn.setFromY(notificationBox.getHeight());
56+
transitionIn.playFromStart();
57+
}
58+
59+
private static void hide(ActionEvent e) {
60+
if (hideTimer != null) {
61+
hideTimer.stop();
62+
hideTimer = null;
63+
}
64+
transitionOut.setToY(notificationBox.getHeight());
65+
transitionOut.playFromStart();
66+
}
67+
}

src/fxml/Main.fxml

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<?import javafx.scene.text.*?>
77
<?import javafx.scene.control.*?>
88
<?import java.lang.*?>
9+
<?import java.net.*?>
910
<?import javafx.scene.layout.*?>
1011

1112
<VBox prefWidth="800.0" prefHeight="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.temporaryteam.noticeditor.controller.NoticeController">
@@ -36,36 +37,48 @@
3637
</Menu>
3738
</menus>
3839
</MenuBar>
39-
<SplitPane dividerPositions="0.25" prefWidth="200.0" prefHeight="160.0" VBox.vgrow="ALWAYS">
40-
<items>
41-
<SplitPane SplitPane.resizableWithParent="false" dividerPositions="0.1, 0.8" orientation="VERTICAL">
42-
<items>
43-
<TextField fx:id="searchField" promptText="%search" focusTraversable="false" >
44-
<padding>
45-
<Insets bottom="3.0" left="3.0" right="3.0" top="3.0" />
46-
</padding>
47-
</TextField>
48-
<TreeView fx:id="noticeTreeView" editable="true" prefHeight="200.0" prefWidth="200.0" showRoot="false">
49-
<contextMenu>
50-
<ContextMenu>
51-
<items>
52-
<MenuItem fx:id="addBranchItem" onAction="#handleContextMenu" text="Add branch" />
53-
<MenuItem fx:id="addNoticeItem" onAction="#handleContextMenu" text="Add notice" />
54-
<MenuItem fx:id="deleteItem" onAction="#handleContextMenu" text="Delete" />
55-
</items>
56-
</ContextMenu>
57-
</contextMenu>
58-
</TreeView>
59-
<fx:include fx:id="noticeSettings" source="NoticeSettings.fxml" />
60-
</items>
61-
</SplitPane>
62-
<SplitPane fx:id="editorPanel" dividerPositions="0.5" prefWidth="390.0" prefHeight="400.0">
63-
<items>
64-
<TextArea fx:id="noticeArea" prefWidth="215.0" prefHeight="400.0" />
65-
<WebView fx:id="viewer" prefWidth="310.0" prefHeight="400.0" />
66-
</items>
67-
</SplitPane>
68-
</items>
69-
</SplitPane>
40+
<StackPane VBox.vgrow="ALWAYS">
41+
<children>
42+
<SplitPane dividerPositions="0.25" prefWidth="200.0" prefHeight="160.0" VBox.vgrow="ALWAYS">
43+
<items>
44+
<SplitPane SplitPane.resizableWithParent="false" dividerPositions="0.1, 0.8" orientation="VERTICAL">
45+
<items>
46+
<TextField fx:id="searchField" promptText="%search" focusTraversable="false" >
47+
<padding>
48+
<Insets bottom="3.0" left="3.0" right="3.0" top="3.0" />
49+
</padding>
50+
</TextField>
51+
<TreeView fx:id="noticeTreeView" editable="true" prefHeight="200.0" prefWidth="200.0" showRoot="false">
52+
<contextMenu>
53+
<ContextMenu>
54+
<items>
55+
<MenuItem fx:id="addBranchItem" onAction="#handleContextMenu" text="Add branch" />
56+
<MenuItem fx:id="addNoticeItem" onAction="#handleContextMenu" text="Add notice" />
57+
<MenuItem fx:id="deleteItem" onAction="#handleContextMenu" text="Delete" />
58+
</items>
59+
</ContextMenu>
60+
</contextMenu>
61+
</TreeView>
62+
<fx:include fx:id="noticeSettings" source="NoticeSettings.fxml" />
63+
</items>
64+
</SplitPane>
65+
<SplitPane fx:id="editorPanel" dividerPositions="0.5" prefWidth="390.0" prefHeight="400.0">
66+
<items>
67+
<TextArea fx:id="noticeArea" prefWidth="215.0" prefHeight="400.0" />
68+
<WebView fx:id="viewer" prefWidth="310.0" prefHeight="400.0" />
69+
</items>
70+
</SplitPane>
71+
</items>
72+
</SplitPane>
73+
<VBox fx:id="notificationBox" maxHeight="-Infinity" maxWidth="-Infinity" styleClass="notificationBox" visible="false" StackPane.alignment="BOTTOM_CENTER">
74+
<children>
75+
<Label fx:id="notificationLabel" styleClass="notificationLabel" />
76+
</children>
77+
</VBox>
78+
</children>
79+
</StackPane>
7080
</children>
81+
<stylesheets>
82+
<URL value="@/resources/styles/main.css" />
83+
</stylesheets>
7184
</VBox>

src/resources/styles/main.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.notificationBox {
2+
-fx-background-color: #000000AA;
3+
-fx-padding: 10;
4+
-fx-background-radius: 3 3 0 0;
5+
-fx-effect: dropshadow(two-pass-box, rgba(0,0,0,0.25), 4, 0.0, 0, 1);
6+
}
7+
.notificationLabel {
8+
-fx-text-fill: #FFF;
9+
-fx-font-size: 14;
10+
}

0 commit comments

Comments
 (0)