Skip to content

Commit 137d476

Browse files
committed
Added text color themeing
1 parent 50f89c0 commit 137d476

File tree

6 files changed

+91
-13
lines changed

6 files changed

+91
-13
lines changed

src/com/demo/custom/CustomNotification.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,62 @@
11
package com.demo.custom;
22

33
import java.awt.BorderLayout;
4+
import java.awt.event.ActionEvent;
5+
import java.awt.event.ActionListener;
46

7+
import javax.swing.JButton;
58
import javax.swing.JLabel;
9+
import javax.swing.JProgressBar;
10+
import javax.swing.Timer;
611

712
import com.notification.Notification;
813
import com.notification.NotificationBuilder;
914
import com.notification.types.BorderLayoutNotification;
1015
import com.theme.TextTheme;
1116
import com.theme.ThemePackage;
17+
import com.theme.WindowTheme;
1218

1319
/**
1420
* This is a CustomNotification which will only have one line of text.
1521
*/
1622
public class CustomNotification extends BorderLayoutNotification {
1723
private JLabel m_label;
24+
private JButton m_button;
25+
private JProgressBar m_progress;
26+
27+
private TextTheme m_theme;
1828

1929
public CustomNotification() {
2030
m_label = new JLabel();
31+
m_button = new JButton("Click me!");
32+
m_progress = new JProgressBar();
33+
34+
m_button.addActionListener(new ActionListener() {
35+
@Override
36+
public void actionPerformed(ActionEvent e) {
37+
removeComponent(m_button);
38+
addComponent(m_progress, BorderLayout.SOUTH);
39+
final Timer timer = new Timer(100, null);
40+
41+
timer.addActionListener(new ActionListener() {
42+
@Override
43+
public void actionPerformed(ActionEvent e) {
44+
m_progress.setValue(m_progress.getValue() + 1);
45+
m_progress.repaint();
46+
47+
if (m_progress.getValue() == 100) {
48+
timer.stop();
49+
hide();
50+
}
51+
}
52+
});
53+
54+
timer.start();
55+
}
56+
});
57+
2158
this.addComponent(m_label, BorderLayout.CENTER);
59+
this.addComponent(m_button, BorderLayout.SOUTH);
2260
}
2361

2462
/**
@@ -27,7 +65,13 @@ public CustomNotification() {
2765
* @param theme
2866
*/
2967
public void setTextTeme(TextTheme theme) {
68+
System.out.println("Text theme set");
3069
m_label.setFont(theme.title);
70+
m_label.setForeground(theme.titleColor);
71+
m_button.setFont(theme.subtitle);
72+
m_button.setForeground(theme.subtitleColor);
73+
74+
m_theme = theme;
3175
}
3276

3377
public String getText() {
@@ -38,6 +82,19 @@ public void setText(String text) {
3882
m_label.setText(text);
3983
}
4084

85+
@Override
86+
protected void themeSet(WindowTheme theme) {
87+
super.themeSet(theme);
88+
89+
if (m_theme != null) {
90+
// the WindowNotification is going to automatically give all our labels with the set foreground color, but
91+
// we
92+
// want to change this to the title color of the font
93+
m_label.setForeground(m_theme.titleColor);
94+
m_button.setForeground(m_theme.subtitleColor);
95+
}
96+
}
97+
4198
public static class CustomBuilder implements NotificationBuilder {
4299
@Override
43100
public Notification buildNotification(ThemePackage pack, Object[] args) {

src/com/demo/custom/CustomNotificationDemo.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
import com.manager.NotificationManager;
44
import com.manager.SimpleManager;
55
import com.notification.NotificationFactory;
6+
import com.notification.NotificationFactory.Location;
67
import com.theme.ThemePackagePresets;
78
import com.utils.Time;
89

910
public class CustomNotificationDemo {
10-
public static void main(String[] args) {
11+
public static void main(String[] args) throws Exception {
12+
// UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
1113
// register the custom builder with the factory
1214
NotificationFactory factory = new NotificationFactory(ThemePackagePresets.cleanLight());
1315
factory.addBuilder("custom", new CustomNotification.CustomBuilder());
1416

1517
// add the Notification
16-
NotificationManager manager = new SimpleManager();
17-
manager.addNotification(factory.build("custom", "this is test text"), Time.seconds(5));
18+
NotificationManager manager = new SimpleManager(Location.NORTH);
19+
manager.addNotification(factory.build("custom", "this is test text"), Time.infinite());
1820
}
1921
}

src/com/notification/types/IconNotification.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public void setTextTheme(TextTheme theme) {
6262
m_theme = theme;
6363
m_titleLabel.setFont(theme.title);
6464
m_subtitleLabel.setFont(theme.subtitle);
65+
m_titleLabel.setForeground(theme.titleColor);
66+
m_subtitleLabel.setForeground(theme.subtitleColor);
6567
}
6668

6769
public String getTitle() {
@@ -85,10 +87,10 @@ public void setSubtitle(String subtitle) {
8587
@Override
8688
public void themeSet(WindowTheme theme) {
8789
super.themeSet(theme);
88-
m_titleLabel.setBackground(theme.background);
89-
m_subtitleLabel.setBackground(theme.background);
9090

91-
m_titleLabel.setForeground(theme.foreground);
92-
m_subtitleLabel.setForeground(theme.foreground);
91+
if (m_theme != null) {
92+
m_titleLabel.setForeground(m_theme.titleColor);
93+
m_subtitleLabel.setForeground(m_theme.subtitleColor);
94+
}
9395
}
9496
}

src/com/notification/types/TextNotification.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import javax.swing.JLabel;
66

77
import com.theme.TextTheme;
8+
import com.theme.WindowTheme;
89

910
/**
1011
* A text notification which will display a title and a subtitle.
@@ -35,6 +36,8 @@ public void setTextTheme(TextTheme theme) {
3536
m_theme = theme;
3637
m_titleLabel.setFont(theme.title);
3738
m_subtitleLabel.setFont(theme.subtitle);
39+
m_titleLabel.setForeground(theme.titleColor);
40+
m_subtitleLabel.setForeground(theme.subtitleColor);
3841
}
3942

4043
public String getTitle() {
@@ -52,4 +55,14 @@ public String getSubtitle() {
5255
public void setSubtitle(String subtitle) {
5356
m_subtitleLabel.setText(subtitle);
5457
}
58+
59+
@Override
60+
protected void themeSet(WindowTheme theme) {
61+
super.themeSet(theme);
62+
63+
if (m_theme != null) {
64+
m_titleLabel.setForeground(m_theme.titleColor);
65+
m_subtitleLabel.setForeground(m_theme.subtitleColor);
66+
}
67+
}
5568
}

src/com/theme/TextTheme.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.theme;
22

3+
import java.awt.Color;
34
import java.awt.Font;
45

56
public class TextTheme {
67
public Font title;
78
public Font subtitle;
9+
public Color titleColor;
10+
public Color subtitleColor;
811
}

src/com/theme/ThemePackagePresets.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ public static ThemePackage cleanLight() {
1212

1313
WindowTheme window = new WindowTheme();
1414
window.background = new Color(255, 255, 255);
15-
window.foreground = new Color(10, 10, 10);
15+
window.foreground = new Color(160, 205, 250);
1616
window.opacity = 0.8f;
1717
window.width = 300;
1818
window.height = 100;
1919

2020
TextTheme text = new TextTheme();
2121
text.title = new Font("Arial", Font.BOLD, 25);
2222
text.subtitle = new Font("Arial", Font.PLAIN, 20);
23+
text.titleColor = new Color(10, 10, 10);
24+
text.subtitleColor = new Color(10, 10, 10);
2325

2426
pack.windowTheme = window;
2527
pack.textTheme = text;
@@ -31,18 +33,17 @@ public static ThemePackage cleanDark() {
3133
ThemePackage pack = new ThemePackage();
3234

3335
WindowTheme window = new WindowTheme();
34-
window.background = new Color(0, 0, 50);
35-
// JLABEL DOES NOT LIKE FOREGROUND COLOR BEING PURE WHITE, FOR SOME
36-
// REASON. IT SETS IT TO BLACK. THIS WORKS,
37-
// HOWEVER.
38-
window.foreground = new Color(254, 254, 254);
36+
window.background = new Color(0, 0, 0);
37+
window.foreground = new Color(16, 124, 162);
3938
window.opacity = 0.8f;
4039
window.width = 300;
4140
window.height = 100;
4241

4342
TextTheme text = new TextTheme();
4443
text.title = new Font("Arial", Font.BOLD, 25);
4544
text.subtitle = new Font("Arial", Font.PLAIN, 20);
45+
text.titleColor = new Color(200, 200, 200);
46+
text.subtitleColor = new Color(200, 200, 200);
4647

4748
pack.windowTheme = window;
4849
pack.textTheme = text;

0 commit comments

Comments
 (0)