1

Menu update by server push

asked 2015-06-22 07:15:22 +0800

vtager gravatar image vtager
13 2

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">

<label value="@load(vm.siteMode)" style="color: red; font-style: oblique; align: center;"/> <button label="@load(vm.buttonLabel)" width="100%" onclick="@command('switchMode')"/> <separator/> <label value="A1" style="font-style: oblique; align: center;" visible="@load(vm.siteMode ne 'MAIN' ? 'false':'true')"/> <label value="@load(vm.a1)" id="a1" width="100%" visible="@load(vm.siteMode ne 'MAIN' ? 'false':'true')"/> <button label="@load(vm.sm1)" width="100%" onclick="@command('switchA1')" visible="@load(vm.siteMode ne 'MAIN' ? 'false':'true')"/> <label value="A2" style="font-style: oblique; align: center;" visible="@load(vm.siteMode eq 'MAIN' ? 'false':'true')"/> <label value="@load(vm.a2)" id="a2" width="100%" visible="@load(vm.siteMode eq 'MAIN' ? 'false':'true')"/> <button label="@load(vm.sm2)" width="100%" onclick="@command('switchA2')" visible="@load(vm.siteMode eq 'MAIN' ? 'false':'true')"/> <label value="A3" style="font-style: oblique; align: center;" visible="@load(vm.siteMode eq 'MAIN' ? 'false':'true')"/> <label value="@load(vm.a3)" id="a3" width="100%" visible="@load(vm.siteMode eq 'MAIN' ? 'false':'true')"/> <button label="@load(vm.sm3)" width="100%" onclick="@command('switchA3')" visible="@load(vm.siteMode eq 'MAIN' ? 'false':'true')"/> <label value="A4" style="font-style: oblique; align: center;" visible="@load(vm.siteMode eq 'MAIN' ? 'false':'true')"/> <label value="@load(vm.a4)" id="a4" width="100%" visible="@load(vm.siteMode eq 'MAIN' ? 'false':'true')"/> <button label="@load(vm.sm4)" width="100%" onclick="@command('switchA4')" visible="@load(vm.siteMode eq 'MAIN' ? 'false':'true')"/>
</west> </borderlayout> </window> </zk>

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; }

delete flag offensive retag edit

Comments

Thank You very match

vtager ( 2015-06-22 16:16:23 +0800 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2015-06-22 15:38:46 +0800

chillworld gravatar image chillworld flag of Belgium
5367 4 9
https://github.com/chillw...

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.

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2015-06-22 07:15:22 +0800

Seen: 13 times

Last updated: Jun 22 '15

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More