Skip to content

Commit 5726642

Browse files
authored
added timer for all mainwindow examples; make refresh period configurable (#3)
1 parent 4f012b6 commit 5726642

File tree

7 files changed

+93
-8
lines changed

7 files changed

+93
-8
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,12 @@ One of these two classes can be used as the mainwinow through the following defi
129129

130130
You can also extend this screens, so that you can add your own (screen-) logic to the mainwindow.
131131

132-
132+
##### unread messages counter in predefined main windows screens
133+
134+
In order to display the messages that are marked as unread, the main windows will be refreshed in a particular interval.
135+
The logic is the same as the Count script which is available within the [Application Folders](https://doc.cuba-platform.com/manual-6.8/application_folder.html)
136+
feature of CUBA itself.
137+
138+
Therefore the refresh period can be adjusted by the existing application property:
139+
[cuba.web.appFoldersRefreshPeriodSec](https://doc.cuba-platform.com/manual-6.8/app_properties_reference.html#cuba.web.appFoldersRefreshPeriodSec)
140+
in the `web-app.properties` file.

modules/web/src/de/diedavids/cuba/userinbox/web-app.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,5 @@ cuba.availableLocales = English|en
4545
cuba.localeSelectVisible = false
4646
cuba.restApiUrl = http://localhost:8080/user-inbox-portal/api
4747
cuba.webAppUrl = http://localhost:8080/user-inbox
48+
49+
cuba.web.appFoldersRefreshPeriodSec = 5

modules/web/src/de/diedavids/cuba/userinbox/web-screens.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55

66
<!--
7+
78
<screen id="mainWindow"
89
template="de/diedavids/cuba/userinbox/web/screens/mainwindow-with-messages.xml"/>
910
10-
-->
11+
1112
<screen id="mainWindow"
1213
template="de/diedavids/cuba/userinbox/web/screens/side-mainwindow-with-messages.xml"/>
1314
15+
-->
1416
<screen id="user-inbox"
1517
template="de/diedavids/cuba/userinbox/web/message/user-inbox.xml"/>
1618
<screen id="send-message"
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,65 @@
11
package de.diedavids.cuba.userinbox.web.screens;
22

33
import com.haulmont.cuba.gui.WindowManager;
4+
import com.haulmont.cuba.gui.components.Action;
5+
import com.haulmont.cuba.gui.components.Timer;
6+
import com.haulmont.cuba.web.WebConfig;
47
import com.haulmont.cuba.web.app.mainwindow.AppMainWindow;
8+
import de.diedavids.cuba.userinbox.service.MessageService;
9+
10+
import javax.inject.Inject;
11+
import java.util.Map;
512

613
public class AppMainWindowWithMessages extends AppMainWindow {
714

815

16+
@Inject
17+
private Action openMessagesAction;
18+
19+
@Inject
20+
private MessageService messageService;
21+
22+
@Inject
23+
private Timer updateCountersTimer;
24+
25+
@Inject
26+
private WebConfig webConfig;
27+
928
public void openMessages() {
1029
openWindow("user-inbox", WindowManager.OpenType.DIALOG);
1130
}
1231

32+
@Override
33+
public void init(Map<String, Object> params) {
34+
initUpdateCounterTimerDelay();
35+
}
36+
37+
private void initUpdateCounterTimerDelay() {
38+
int period = webConfig.getAppFoldersRefreshPeriodSec() * 1000;
39+
updateCountersTimer.setDelay(period);
40+
}
41+
42+
@Override
43+
public void ready() {
44+
updateMessageCounter();
45+
}
46+
47+
public void updateCounters(Timer source) {
48+
updateMessageCounter();
49+
}
50+
51+
private void updateMessageCounter() {
52+
int messageCounter = getMessageCounter();
53+
String messageCounterCaption = "";
54+
55+
if (messageCounter > 0) {
56+
messageCounterCaption = "(" + messageCounter + ")";
57+
}
58+
59+
openMessagesAction.setCaption(messageCounterCaption);
60+
}
61+
62+
private int getMessageCounter() {
63+
return messageService.countUnreadMessagesForCurrentUser();
64+
}
1365
}

modules/web/src/de/diedavids/cuba/userinbox/web/screens/SideMainwindowWithMessages.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.haulmont.cuba.gui.components.Timer;
77
import com.haulmont.cuba.gui.components.mainwindow.FtsField;
88
import com.haulmont.cuba.gui.components.mainwindow.SideMenu;
9+
import com.haulmont.cuba.web.WebConfig;
910
import de.diedavids.cuba.userinbox.service.MessageService;
1011

1112
import javax.inject.Inject;
@@ -25,6 +26,13 @@ public class SideMainwindowWithMessages extends AbstractMainWindow {
2526
@Inject
2627
private MessageService messageService;
2728

29+
30+
@Inject
31+
private Timer updateCountersTimer;
32+
33+
@Inject
34+
private WebConfig webConfig;
35+
2836
@Override
2937
public void init(Map<String, Object> params) {
3038
super.init(params);
@@ -35,12 +43,22 @@ public void init(Map<String, Object> params) {
3543
initLogoImage(logoImage);
3644
initFtsField(ftsField);
3745

46+
initUpdateCounterTimerDelay();
3847
initMessagesMenuItem();
3948

4049
sideMenu.setSelectOnClick(true);
4150

51+
52+
4253
}
4354

55+
56+
private void initUpdateCounterTimerDelay() {
57+
int period = webConfig.getAppFoldersRefreshPeriodSec() * 1000;
58+
updateCountersTimer.setDelay(period);
59+
}
60+
61+
4462
private void initMessagesMenuItem() {
4563
SideMenu.MenuItem messagesMenuItem = sideMenu.createMenuItem("messages");
4664
messagesMenuItem.setCaption(messages.getMessage(this.getClass(), "messages"));

modules/web/src/de/diedavids/cuba/userinbox/web/screens/mainwindow-with-messages.xml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@
44
extends="/com/haulmont/cuba/web/app/mainwindow/mainwindow.xml"
55
messagesPack="de.diedavids.cuba.userinbox.web.screens"
66
xmlns:ext="http://schemas.haulmont.com/cuba/window-ext.xsd">
7-
<dialogMode height="600"
8-
width="800"/>
7+
<timers>
8+
<timer id="updateCountersTimer" delay="3000" autostart="true" repeating="true" onTimer="updateCounters"/>
9+
</timers>
910
<actions>
10-
<action id="openMessagesAction" invoke="openMessages"/>
11+
<action id="openMessagesAction"
12+
invoke="openMessages"
13+
icon="font-icon:ENVELOPE_O"
14+
/>
1115
</actions>
1216
<layout>
1317
<hbox id="titleBar">
1418
<button id="messagesBtn"
1519
ext:index="2"
16-
action="openMessagesAction"
17-
icon="font-icon:ENVELOPE_O"/>
20+
action="openMessagesAction"/>
1821
</hbox>
1922
</layout>
2023
</window>

modules/web/src/de/diedavids/cuba/userinbox/web/screens/side-mainwindow-with-messages.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
messagesPack="de.diedavids.cuba.userinbox.web.screens"
66
xmlns:main="http://schemas.haulmont.com/cuba/mainwindow.xsd">
77
<timers>
8-
<timer delay="3000" autostart="true" repeating="true" onTimer="updateCounters"/>
8+
<timer id="updateCountersTimer" delay="3000" autostart="true" repeating="true" onTimer="updateCounters"/>
99
</timers>
1010
<layout expand="horizontalWrap">
1111
<hbox id="horizontalWrap"

0 commit comments

Comments
 (0)