Skip to content

Commit 7679e8e

Browse files
committed
added notification-center demo.
1 parent d41f37a commit 7679e8e

File tree

1 file changed

+268
-0
lines changed

1 file changed

+268
-0
lines changed
Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
package com.openfin.desktop.demo;
2+
3+
import java.awt.BorderLayout;
4+
import java.awt.Color;
5+
import java.awt.Dimension;
6+
import java.awt.FlowLayout;
7+
import java.awt.GridBagConstraints;
8+
import java.awt.GridBagLayout;
9+
import java.awt.Insets;
10+
import java.awt.event.WindowAdapter;
11+
import java.awt.event.WindowEvent;
12+
import java.io.IOException;
13+
import java.util.Date;
14+
import java.util.UUID;
15+
16+
import javax.swing.BorderFactory;
17+
import javax.swing.JButton;
18+
import javax.swing.JComboBox;
19+
import javax.swing.JFrame;
20+
import javax.swing.JLabel;
21+
import javax.swing.JPanel;
22+
import javax.swing.JTextField;
23+
import javax.swing.SwingUtilities;
24+
25+
import org.json.JSONObject;
26+
27+
import com.openfin.desktop.DesktopConnection;
28+
import com.openfin.desktop.DesktopException;
29+
import com.openfin.desktop.DesktopIOException;
30+
import com.openfin.desktop.DesktopStateListener;
31+
import com.openfin.desktop.RuntimeConfiguration;
32+
import com.openfin.desktop.notifications.ButtonOptions;
33+
import com.openfin.desktop.notifications.NotificationActionResult;
34+
import com.openfin.desktop.notifications.NotificationIndicator;
35+
import com.openfin.desktop.notifications.NotificationOptions;
36+
import com.openfin.desktop.notifications.Notifications;
37+
import com.openfin.desktop.notifications.events.NotificationActionEvent;
38+
39+
public class NotificationServiceDemo {
40+
41+
private JFrame demoWindow;
42+
private DesktopConnection desktopConnection;
43+
private JPanel glassPane;
44+
private Notifications notifications;
45+
46+
public NotificationServiceDemo() {
47+
this.demoWindow = new JFrame("OpenFin Notification Service Demo");
48+
this.demoWindow.setContentPane(this.createContentPanel());
49+
this.demoWindow.setGlassPane(this.createGlassPane());
50+
this.demoWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
51+
this.demoWindow.addWindowListener(new WindowAdapter() {
52+
@Override
53+
public void windowClosing(WindowEvent e) {
54+
if (desktopConnection != null) {
55+
try {
56+
desktopConnection.disconnect();
57+
}
58+
catch (DesktopException e1) {
59+
e1.printStackTrace();
60+
}
61+
}
62+
}
63+
});
64+
65+
this.demoWindow.pack();
66+
this.demoWindow.setLocationRelativeTo(null);
67+
this.demoWindow.setVisible(true);
68+
this.glassPane.setVisible(true);
69+
this.initOpenFin();
70+
}
71+
72+
private void initOpenFin() {
73+
try {
74+
this.desktopConnection = new DesktopConnection("OpenFin Notification Service Demo");
75+
RuntimeConfiguration config = new RuntimeConfiguration();
76+
config.setRuntimeVersion("stable");
77+
this.desktopConnection.connect(config, new DesktopStateListener() {
78+
79+
@Override
80+
public void onReady() {
81+
notifications = new Notifications(desktopConnection);
82+
83+
84+
notifications.addEventListener(Notifications.EVENT_TYPE_ACTION, ne ->{
85+
NotificationActionEvent actionEvent = (NotificationActionEvent) ne;
86+
NotificationActionResult actionResult = actionEvent.getResult();
87+
System.out.println("actionResult: notificationId: " + actionEvent.getNotificationOptions().getId() + ", user clicked on btn: " + actionResult.getString("btn"));
88+
});
89+
90+
SwingUtilities.invokeLater(()->{
91+
glassPane.setVisible(false);
92+
});
93+
}
94+
95+
@Override
96+
public void onClose(String error) {
97+
System.exit(0);
98+
}
99+
100+
@Override
101+
public void onError(String reason) {
102+
103+
}
104+
105+
@Override
106+
public void onMessage(String message) {
107+
108+
}
109+
110+
@Override
111+
public void onOutgoingMessage(String message) {
112+
113+
}
114+
115+
}, 60);
116+
117+
}
118+
catch (DesktopException | DesktopIOException | IOException e) {
119+
e.printStackTrace();
120+
}
121+
finally {
122+
123+
}
124+
}
125+
126+
private JPanel createGlassPane() {
127+
this.glassPane = new JPanel(new BorderLayout());
128+
JLabel l = new JLabel("Loading, please wait......");
129+
l.setHorizontalAlignment(JLabel.CENTER);
130+
this.glassPane.add(l, BorderLayout.CENTER);
131+
this.glassPane.setBackground(Color.LIGHT_GRAY);
132+
return this.glassPane;
133+
}
134+
135+
136+
private JPanel createToggleNotificationCenterPanel() {
137+
JPanel pnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
138+
pnl.setBorder(BorderFactory.createTitledBorder("Notification Center"));
139+
JButton btnToggleNotificationCenter = new JButton("Toggle Notification Center");
140+
btnToggleNotificationCenter.addActionListener(e->{
141+
this.notifications.toggleNotificationCenter();
142+
});
143+
JButton btnClearAll = new JButton("Clear All");
144+
btnClearAll.addActionListener(e->{
145+
this.notifications.clearAll();
146+
});
147+
pnl.add(btnToggleNotificationCenter);
148+
pnl.add(btnClearAll);
149+
return pnl;
150+
}
151+
152+
private JPanel createCreateNotificationPanel() {
153+
JPanel pnl = new JPanel(new BorderLayout());
154+
pnl.setBorder(BorderFactory.createTitledBorder("Notification"));
155+
156+
JTextField tfTitle = new JTextField("Title");
157+
JTextField tfBody = new JTextField("Body");
158+
JTextField tfCategory = new JTextField("Category");
159+
JTextField tfIcon = new JTextField("https://openfin.co/favicon-32x32.png");
160+
JTextField tfIndicatorText = new JTextField("");
161+
JTextField tfExpiresInSecs = new JTextField("5");
162+
163+
JComboBox<String> cbSticky = new JComboBox<>();
164+
cbSticky.addItem(NotificationOptions.STICKY_STICKY);
165+
cbSticky.addItem(NotificationOptions.STICKY_TRANSIENT);
166+
167+
JComboBox<String> cbIndicator = new JComboBox<>();
168+
cbIndicator.addItem(NotificationIndicator.TYPE_FAILURE);
169+
cbIndicator.addItem(NotificationIndicator.TYPE_WARNING);
170+
cbIndicator.addItem(NotificationIndicator.TYPE_SUCCESS);
171+
172+
JPanel pnlCenter = new JPanel(new GridBagLayout());
173+
GridBagConstraints gbConst = new GridBagConstraints();
174+
gbConst.gridx = 0;
175+
gbConst.gridy = 0;
176+
gbConst.weightx = 0;
177+
gbConst.insets = new Insets(5, 5, 5, 5);
178+
gbConst.anchor = GridBagConstraints.EAST;
179+
pnlCenter.add(new JLabel("Title"), gbConst);
180+
gbConst.gridy++;
181+
pnlCenter.add(new JLabel("Body"), gbConst);
182+
gbConst.gridy++;
183+
pnlCenter.add(new JLabel("Category"), gbConst);
184+
gbConst.gridy++;
185+
pnlCenter.add(new JLabel("Icon"), gbConst);
186+
gbConst.gridy++;
187+
pnlCenter.add(new JLabel("Sticky"), gbConst);
188+
gbConst.gridy++;
189+
pnlCenter.add(new JLabel("Indicator"), gbConst);
190+
gbConst.gridy++;
191+
pnlCenter.add(new JLabel("Indicator Text"), gbConst);
192+
gbConst.gridy++;
193+
pnlCenter.add(new JLabel("Expires (in seconds)"), gbConst);
194+
gbConst.gridx = 1;
195+
gbConst.gridy = 0;
196+
gbConst.weightx = 0.5;
197+
gbConst.insets = new Insets(5, 0, 5, 5);
198+
gbConst.fill = GridBagConstraints.BOTH;
199+
pnlCenter.add(tfTitle, gbConst);
200+
gbConst.gridy++;
201+
pnlCenter.add(tfBody, gbConst);
202+
gbConst.gridy++;
203+
pnlCenter.add(tfCategory, gbConst);
204+
gbConst.gridy++;
205+
pnlCenter.add(tfIcon, gbConst);
206+
gbConst.gridy++;
207+
pnlCenter.add(cbSticky, gbConst);
208+
gbConst.gridy++;
209+
pnlCenter.add(cbIndicator, gbConst);
210+
gbConst.gridy++;
211+
pnlCenter.add(tfIndicatorText, gbConst);
212+
gbConst.gridy++;
213+
pnlCenter.add(tfExpiresInSecs, gbConst);
214+
gbConst.weighty = 0.5;
215+
gbConst.gridy++;
216+
pnlCenter.add(new JLabel(), gbConst);
217+
218+
219+
JButton btnCreate = new JButton("Create Notification");
220+
btnCreate.addActionListener(e->{
221+
NotificationOptions opt = new NotificationOptions(tfTitle.getText(), tfBody.getText(), tfCategory.getText());
222+
String icon = tfIcon.getText().trim();
223+
if (!icon.isEmpty()) {
224+
opt.setIcon(icon);
225+
}
226+
opt.setSticky((String) cbSticky.getSelectedItem());
227+
NotificationIndicator indicatorOpts = new NotificationIndicator((String) cbIndicator.getSelectedItem());
228+
String indicatorText = tfIndicatorText.getText().trim();
229+
if (!indicatorText.isEmpty()) {
230+
indicatorOpts.setText(indicatorText);
231+
}
232+
opt.setIndicator(indicatorOpts);
233+
234+
String expires = tfExpiresInSecs.getText().trim();
235+
if (!expires.isEmpty()) {
236+
opt.setExpires(new Date(System.currentTimeMillis() + (1000 * (Integer.parseInt(expires)))));
237+
}
238+
239+
ButtonOptions bo1 = new ButtonOptions("Button 1");
240+
bo1.setOnClick(new NotificationActionResult(new JSONObject().put("btn", "btn1")));
241+
ButtonOptions bo2 = new ButtonOptions("Button 2");
242+
bo2.setOnClick(new NotificationActionResult(new JSONObject().put("btn", "btn2")));
243+
bo2.setCta(true);
244+
opt.setButtons(bo1, bo2);
245+
246+
this.notifications.create(opt);
247+
});
248+
JPanel pnlBottom = new JPanel(new FlowLayout(FlowLayout.RIGHT));
249+
pnlBottom.add(btnCreate);
250+
pnl.add(pnlCenter, BorderLayout.CENTER);
251+
pnl.add(pnlBottom, BorderLayout.SOUTH);
252+
return pnl;
253+
}
254+
255+
private JPanel createContentPanel() {
256+
JPanel p = new JPanel(new BorderLayout());
257+
p.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
258+
p.setPreferredSize(new Dimension(550, 400));
259+
p.add(this.createToggleNotificationCenterPanel(), BorderLayout.NORTH);
260+
p.add(this.createCreateNotificationPanel(), BorderLayout.CENTER);
261+
return p;
262+
}
263+
264+
public static void main(String[] args) {
265+
new NotificationServiceDemo();
266+
}
267+
268+
}

0 commit comments

Comments
 (0)