-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from DJ-Raven/alerts
Add Alerts
- Loading branch information
Showing
49 changed files
with
1,175 additions
and
38 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package raven.alerts; | ||
|
||
import com.formdev.flatlaf.extras.FlatSVGIcon; | ||
import raven.swing.AnimateIcon; | ||
import raven.swing.animator.EasingInterpolator; | ||
import raven.swing.animator.KeyFrames; | ||
|
||
import javax.swing.*; | ||
import java.awt.*; | ||
|
||
public class AlertsOption { | ||
|
||
protected Icon icon; | ||
protected Color baseColor; | ||
|
||
protected boolean loopAnimation; | ||
|
||
protected EffectOption effectOption; | ||
|
||
public AlertsOption(Icon icon, Color baseColor) { | ||
this.icon = icon; | ||
this.baseColor = baseColor; | ||
} | ||
|
||
public AlertsOption setEffectOption(EffectOption effectOption) { | ||
this.effectOption = effectOption; | ||
return this; | ||
} | ||
|
||
public AlertsOption setLoopAnimation(boolean loopAnimation) { | ||
this.loopAnimation = loopAnimation; | ||
return this; | ||
} | ||
|
||
public static class EffectOption { | ||
|
||
protected float effectAlpha = 1f; | ||
protected boolean effectFadeOut = false; | ||
protected Icon[] randomEffect; | ||
|
||
public EffectOption setEffectAlpha(float effectAlpha) { | ||
this.effectAlpha = effectAlpha; | ||
return this; | ||
} | ||
|
||
public EffectOption setEffectFadeOut(boolean effectFadeOut) { | ||
this.effectFadeOut = effectFadeOut; | ||
return this; | ||
} | ||
|
||
public EffectOption setRandomEffect(Icon[] randomEffect) { | ||
this.randomEffect = randomEffect; | ||
return this; | ||
} | ||
} | ||
|
||
|
||
protected static AlertsOption getAlertsOption(MessageAlerts.MessageType messageType) { | ||
if (messageType == MessageAlerts.MessageType.SUCCESS) { | ||
Icon effects[] = new Icon[]{ | ||
new FlatSVGIcon("raven/alerts/effect/check.svg"), | ||
new FlatSVGIcon("raven/alerts/effect/star.svg"), | ||
new FlatSVGIcon("raven/alerts/effect/firework.svg"), | ||
new FlatSVGIcon("raven/alerts/effect/balloon.svg") | ||
}; | ||
return getDefaultOption("raven/alerts/icon/success.svg", Color.decode("#10b981"), effects); | ||
} else if (messageType == MessageAlerts.MessageType.WARNING) { | ||
Icon effects[] = new Icon[]{ | ||
new FlatSVGIcon("raven/alerts/effect/disclaimer.svg"), | ||
new FlatSVGIcon("raven/alerts/effect/warning.svg"), | ||
new FlatSVGIcon("raven/alerts/effect/query.svg"), | ||
new FlatSVGIcon("raven/alerts/effect/mark.svg") | ||
}; | ||
return getDefaultOption("raven/alerts/icon/warning.svg", Color.decode("#f59e0b"), effects); | ||
} else if (messageType == MessageAlerts.MessageType.ERROR) { | ||
Icon effects[] = new Icon[]{ | ||
new FlatSVGIcon("raven/alerts/effect/error.svg"), | ||
new FlatSVGIcon("raven/alerts/effect/sad.svg"), | ||
new FlatSVGIcon("raven/alerts/effect/shield.svg"), | ||
new FlatSVGIcon("raven/alerts/effect/nothing.svg") | ||
}; | ||
return getDefaultOption("raven/alerts/icon/error.svg", Color.decode("#ef4444"), effects); | ||
} else { | ||
return getDefaultOption("raven/alerts/icon/information.svg", null); | ||
} | ||
} | ||
|
||
private static AlertsOption getDefaultOption(String icon, Color color, Icon[] effects) { | ||
AnimateIcon.AnimateOption option = new AnimateIcon.AnimateOption() | ||
.setInterpolator(EasingInterpolator.EASE_OUT_BOUNCE) | ||
.setScaleInterpolator(new KeyFrames(1f, 1.5f, 1f)) | ||
.setRotateInterpolator(new KeyFrames(0f, (float) Math.toRadians(-30f), 0f)); | ||
return new AlertsOption(new AnimateIcon(icon, 4f, option), color) | ||
.setEffectOption(new EffectOption() | ||
.setEffectAlpha(0.9f) | ||
.setEffectFadeOut(true) | ||
.setRandomEffect(effects)) | ||
.setLoopAnimation(true); | ||
} | ||
|
||
public static AlertsOption getDefaultOption(String icon, Color color) { | ||
AnimateIcon.AnimateOption option = new AnimateIcon.AnimateOption() | ||
.setScaleInterpolator(new KeyFrames(1f, 1.2f, 1f)) | ||
.setRotateInterpolator(new KeyFrames(0f, (float) Math.toRadians(-30), (float) Math.toRadians(30), 0f)); | ||
return new AlertsOption(new AnimateIcon(icon, 4f, option), color) | ||
.setLoopAnimation(true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package raven.alerts; | ||
|
||
import com.formdev.flatlaf.FlatClientProperties; | ||
import net.miginfocom.swing.MigLayout; | ||
import raven.popup.GlassPanePopup; | ||
import raven.popup.component.PopupCallbackAction; | ||
|
||
import javax.swing.*; | ||
import javax.swing.text.SimpleAttributeSet; | ||
import javax.swing.text.StyleConstants; | ||
import javax.swing.text.StyledDocument; | ||
import java.awt.*; | ||
|
||
public class MessageAlerts { | ||
|
||
private static MessageAlerts instance; | ||
|
||
public static final int DEFAULT_OPTION = -1; | ||
public static final int YES_NO_OPTION = 0; | ||
public static final int YES_NO_CANCEL_OPTION = 1; | ||
public static final int OK_CANCEL_OPTION = 2; | ||
|
||
// Return values | ||
public static final int YES_OPTION = 0; | ||
public static final int NO_OPTION = 1; | ||
public static final int CANCEL_OPTION = 2; | ||
public static final int OK_OPTION = 0; | ||
public static final int CLOSED_OPTION = -1; | ||
|
||
public static MessageAlerts getInstance() { | ||
if (instance == null) { | ||
instance = new MessageAlerts(); | ||
} | ||
return instance; | ||
} | ||
|
||
private MessageAlerts() { | ||
} | ||
|
||
public void showMessage(String title, String message) { | ||
showMessage(title, message, MessageType.DEFAULT); | ||
} | ||
|
||
public void showMessage(String title, String message, MessageType messageType) { | ||
showMessage(title, message, messageType, DEFAULT_OPTION, null); | ||
} | ||
|
||
public void showMessage(String title, String message, MessageType messageType, int option, PopupCallbackAction callback) { | ||
AlertsOption alertsOption = AlertsOption.getAlertsOption(messageType); | ||
GlassPanePopup.showPopup(new SimpleAlerts(createSimpleMessage(alertsOption, title, message), alertsOption, option, callback)); | ||
} | ||
|
||
private Component createSimpleMessage(AlertsOption option, String title, String message) { | ||
JPanel panel = new JPanel(new MigLayout("wrap,insets 0,center", "center")); | ||
panel.setOpaque(false); | ||
JLabel labelTitle = new JLabel(title); | ||
JTextPane textPane = new JTextPane(); | ||
textPane.setOpaque(false); | ||
StyledDocument doc = textPane.getStyledDocument(); | ||
SimpleAttributeSet center = new SimpleAttributeSet(); | ||
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER); | ||
doc.setParagraphAttributes(0, doc.getLength(), center, false); | ||
textPane.setEditable(false); | ||
textPane.setText(message); | ||
textPane.putClientProperty(FlatClientProperties.STYLE, "" + | ||
"border:5,25,5,25;" + | ||
"[light]foreground:lighten(@foreground,30%);" + | ||
"[dark]foreground:darken(@foreground,30%)"); | ||
labelTitle.putClientProperty(FlatClientProperties.STYLE, "" + | ||
"font:bold +5"); | ||
if (option.baseColor != null) { | ||
labelTitle.setForeground(option.baseColor); | ||
} | ||
panel.add(labelTitle); | ||
JScrollPane scrollPane = new JScrollPane(textPane); | ||
if (option.effectOption != null) { | ||
scrollPane.setOpaque(false); | ||
scrollPane.getViewport().setOpaque(false); | ||
} | ||
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); | ||
scrollPane.setBorder(BorderFactory.createEmptyBorder()); | ||
applyScrollStyle(scrollPane.getVerticalScrollBar()); | ||
applyScrollStyle(scrollPane.getHorizontalScrollBar()); | ||
panel.add(scrollPane); | ||
SwingUtilities.invokeLater(() -> scrollPane.getVerticalScrollBar().setValue(0)); | ||
return panel; | ||
} | ||
|
||
private void applyScrollStyle(JScrollBar scrollBar) { | ||
scrollBar.setUnitIncrement(10); | ||
scrollBar.putClientProperty(FlatClientProperties.STYLE, "" + | ||
"width:10;" + | ||
"trackArc:999;" + | ||
"thumbInsets:0,3,0,3;" + | ||
"trackInsets:0,3,0,3;"); | ||
} | ||
|
||
public enum MessageType { | ||
SUCCESS, ERROR, WARNING, DEFAULT | ||
} | ||
} |
Oops, something went wrong.