-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hello, I want to create a global menu visible to all clients. Any click on menu change the model and this change should be visible to all clients. It's work fine for current client, but other clients don't get change. Thank You
index.zul <zk> <window id="menu" border="normal" height="100%" apply="org.zkoss.bind.BindComposer" viewmodel="@id('vm') @init('com.example.menu.MenuModel') "> <borderlayout hflex="1" vflex="1" id="border"> <west width="25%" border="none">
public class MenuModel {
private static SiteMode siteMode = SiteMode.MAIN;
private static String buttonLabel = Labels.switchToBackup;
private static State a1 = State.NOTACTIVE;
private static State a2 = State.NOTACTIVE;
private static State a3 = State.NOTACTIVE;
private static State a4 = State.NOTACTIVE;
private static String sm1 = Labels.activateA1;
private static String sm2 = Labels.activateA2;
private static String sm3 = Labels.activateA3;
private static String sm4 = Labels.activateA4;
private String source = "";
private Timer timer;
Window menu;
public SiteMode getSiteMode() {
return siteMode;
}
public String getButtonLabel() {
return buttonLabel;
}
public void setButtonLabel(String buttonLabel) {
MenuModel.buttonLabel = buttonLabel;
}
public State getA1() {
return a1;
}
public void setA1(State a1) {
MenuModel.a1 = a1;
}
public State getA2() {
return a2;
}
public void setA2(State a2) {
MenuModel.a2 = a2;
}
public State getA3() {
return a3;
}
public void setA3(State a3) {
MenuModel.a3 = a3;
}
public State getA4() {
return a4;
}
public void setA4(State a4) {
MenuModel.a4 = a4;
}
public String getSm1() {
return sm1;
}
public void setSm1(String sm1) {
MenuModel.sm1 = sm1;
}
public String getSm2() {
return sm2;
}
public void setSm2(String sm2) {
MenuModel.sm2 = sm2;
}
public String getSm3() {
return sm3;
}
public void setSm3(String sm3) {
MenuModel.sm3 = sm3;
}
public String getSm4() {
return sm4;
}
public void setSm4(String sm4) {
MenuModel.sm4 = sm4;
}
public void setSiteMode(SiteMode siteMode) {
MenuModel.siteMode = siteMode;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
@Command
@NotifyChange({ "*" })
public void switchMode() {
if (siteMode.equals(SiteMode.MAIN)) {
siteMode = SiteMode.BACKUP;
buttonLabel = Labels.switchToMain;
} else {
siteMode = SiteMode.MAIN;
buttonLabel = Labels.switchToBackup;
}
a1 = State.NOTACTIVE;
a2 = State.NOTACTIVE;
a3 = State.NOTACTIVE;
a4 = State.NOTACTIVE;
sm1 = Labels.activateA1;
sm2 = Labels.activateA2;
sm3 = Labels.activateA3;
sm4 = Labels.activateA4;
}
@Command
@NotifyChange({ "a1", "sm1", "source" })
public void switchA1() {
if (a1.equals(State.ACTIVE)) {
sm1 = Labels.activateA1;
a1 = State.NOTACTIVE;
} else {
sm1 = Labels.deactivateA1;
a1 = State.ACTIVE;
}
}
@Command
@NotifyChange({ "a2", "sm2", "source" })
public void switchA2() {
if (a2.equals(State.ACTIVE)) {
sm2 = Labels.activateA2;
a2 = State.NOTACTIVE;
} else {
sm2 = Labels.deactivateA2;
a2 = State.ACTIVE;
}
}
@Command
@NotifyChange({ "a3", "sm3", "source" })
public void switchA3() {
if (a3.equals(State.ACTIVE)) {
sm3 = Labels.activateA3;
a3 = State.NOTACTIVE;
} else {
sm3 = Labels.deactivateA3;
a3 = State.ACTIVE;
}
}
@Command
@NotifyChange({ "a4", "sm4", "source" })
public void switchA4() {
if (a4.equals(State.ACTIVE)) {
sm4 = Labels.activateA4;
a4 = State.NOTACTIVE;
} else {
sm4 = Labels.deactivateA4;
a4 = State.ACTIVE;
}
}
@Init
public void init(@SelectorParam("#menu") final Window menu) {
System.out.println("INIT");
this.menu = menu;
final Desktop desktop = Executions.getCurrent().getDesktop();
if (!desktop.isServerPushEnabled()) {
desktop.enableServerPush(true);
timer = new Timer();
timer.schedule(getTask(), 0, 1000);
}
}
// task to be scheduled
public TimerTask getTask() {
return new TimerTask() {
public void run() {
update();
}
};
}
// update value of intbox
public void update() {
Desktop desktop = menu.getDesktop();
try {
if (desktop == null) {
timer.cancel();
return;
}
try {
// active desktop
Executions.activate(desktop);
// update UI
menu.invalidate();
} finally {
// deactive desktop
Executions.deactivate(desktop);
}
} catch (Exception ignore) {
/* ignore */
}
}
}
public class Labels {
public static final String switchToMain = "Switch to Main";
public static final String switchToBackup = "Switch to Backup";
public static final String activateA1 = "Activate A1";
public static final String activateA2 = "Activate A2";
public static final String activateA3 = "Activate A3";
public static final String activateA4 = "Activate A4";
public static final String deactivateA1 = "Deactivate A1";
public static final String deactivateA2 = "Deactivate A2";
public static final String deactivateA3 = "Deactivate A3";
public static final String deactivateA4 = "Deactivate A4";
}
public enum SiteMode {
MAIN,
BACKUP;
}
public enum State { ACTIVE, NOTACTIVE; }
You have to put the global command in the application scope.
Like this all clients will be updated (server push is automaticly done, so no need of timer)
See this small talk how to put a global command in higher scope.
Greetz Chill.
Asked: 2015-06-22 07:15:22 +0800
Seen: 13 times
Last updated: Jun 22 '15
How to stop combining commands in an ajax call ?
zk developmet is esay [closed]
Need help with scrolling horizontal tabs in Right-to-Left (Arabic)
Create tab dynamically, multitab application
Radio button value change on database value
How to implement, each user has its own session timeout time
Thank You very match
vtager ( 2015-06-22 16:16:23 +0800 )edit