Skip to content

Commit

Permalink
Merge pull request #2 from DJ-Raven/alerts
Browse files Browse the repository at this point in the history
Add Alerts
  • Loading branch information
DJ-Raven committed Nov 25, 2023
2 parents 19bcebf + 874651e commit 2413eca
Show file tree
Hide file tree
Showing 49 changed files with 1,175 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 76 additions & 7 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file removed jar library/swing-glasspane-popup-1.1.0.jar
Binary file not shown.
Binary file added jar library/swing-glasspane-popup-1.2.0.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>raven.popup</groupId>
<artifactId>swing-glasspane-popup</artifactId>
<version>1.1.0</version>
<version>1.2.0</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
Expand Down
108 changes: 108 additions & 0 deletions src/main/java/raven/alerts/AlertsOption.java
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);
}
}
101 changes: 101 additions & 0 deletions src/main/java/raven/alerts/MessageAlerts.java
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
}
}
Loading

0 comments on commit 2413eca

Please sign in to comment.