0

Call java method through zscript function ?

asked 2014-07-22 08:53:49 +0800

sitansu gravatar image sitansu
2254 13
http://java91.blogspot.in...

updated 2014-07-22 09:29:17 +0800

HI there ,

I have an issue to call java method through zscript function in zul .I am trying to call somthing like this but its not working for me .Please help me how can i resolve this issue.

  <zk xmlns:x="xhtml" xmlns:zk="zk" xmlns:c="client">
        <window  width="350px"  contentStyle="background: #a6c9f0"  id="btnCloseWindow"
            height="450px" title="@load(vm.getText('rtrtr'))" border="normal" mode="modal"  apply="org.zkoss.bind.BindComposer"
            closable="true"  viewModel="@id('vm') @init('com.viewmodel.ViewModel')">
              <script type="text/javascript">
              <![CDATA[

                var posX;
                var posY;
                var validater=null;
                   function GetOffset (object, offset) {
                       if (!object)
                           return;
                       offset.x += object.offsetLeft;
                       offset.y += object.offsetTop;

                       GetOffset (object.offsetParent, offset);
                   }

                   function GetScrolled (object, scrolled) {
                       if (!object)
                           return;
                       scrolled.x += object.scrollLeft;
                       scrolled.y += object.scrollTop;

                       if (object.tagName.toLowerCase () != "html") {
                           GetScrolled (object.parentNode, scrolled);
                       }
                   }
                   function GetTopLeft() {
                       var div = document.getElementById ("body");

                       var offset = {x : 0, y : 0};
                       GetOffset (div, offset);

                      var scrolled = {x : 0, y : 0};
                       GetScrolled (div.parentNode, scrolled);

                        posX = (offset.x - scrolled.x)-div.offsetLeft;
                        posY = (offset.y - scrolled.y)-div.offsetTop;

                   }
                    function callJavaMethod(var1){
                        if(validater!=null){
                       // zAu.send(new zk.Event(zk.Widget.$(this), 'onSave',var1));
                        zAu.send(new zk.Event(zk.Widget.$('$btnCloseWindow'), 'onSave', var1, {toServer:true}));
                        }else{
                        //  zAu.send(new zk.Event(zk.Widget.$(this), 'onSave',validater));
                            zAu.send(new zk.Event(zk.Widget.$('$btnCloseWindow'), 'onSave', validater, {toServer:true}));
                        }
                      }
                    function onCancel(){
                        zAu.send(new zk.Event(zk.Widget.$('$btnCloseWindow'), 'onCancel', null, {toServer:true}));
                        //zAu.send(new zk.Event(zk.Widget.$(this), 'onCancel', null));
                      }
                    zk.afterMount(function()  {

                        var bMouseIsDown = false;

                        var oCanvas = document.getElementById("thecanvas");
                        if(oCanvas!=null){
                        var oCtx = oCanvas.getContext("2d");

                        var iWidth = oCanvas.width;
                        var iHeight = oCanvas.height;
                        oCtx.fillStyle="#ffffff";
                        oCtx.fillRect(0,0,iWidth,iHeight);
                        oCtx.beginPath();
                        //oCtx.strokeStyle = "rgb(255,0,255)";
                        oCtx.strokeWidth = "4px";

                        oCanvas.onmousedown = function(e) {
                            bMouseIsDown = true;
                            //iLastX = e.clientX-20 - oCanvas.offsetLeft;
                            //iLastY = e.clientY-120 - oCanvas.offsetTop;
                            GetTopLeft();
                            iLastX = e.clientX-18-posX;
                            iLastY = e.clientY-120-posY;
                        }
                        oCanvas.onmouseup = function() {
                            bMouseIsDown = false;
                            iLastX = -1;
                            iLastY = -1;
                        }
                        oCanvas.onmousemove = function(e) {
                            if (bMouseIsDown) {
                                //var iX = e.clientX -20 - oCanvas.offsetLeft;
                                //var iY = e.clientY -120 - oCanvas.offsetTop;
                                var iX =e.clientX-18-posX;
                                var iY = e.clientY-120-posY;
                                oCtx.moveTo(iLastX, iLastY);
                                oCtx.lineTo(iX, iY);
                                oCtx.stroke();
                                iLastX = iX;
                                iLastY = iY;
                                validater=iX+iY;
                            }
                        }



                        document.getElementById("save").onclick = function() {
                        var strDataURI = oCanvas.toDataURL();
                        var strDataURI = oCanvas.toDataURL("image/jpeg"); 
                        callJavaMethod(strDataURI)

                        }
                        document.getElementById("reset").onclick = function() {
                            oCtx.clearRect(0, 0, oCanvas.width, oCanvas.height);
                            oCtx.fillStyle="#ffffff";
                            oCtx.fillRect(0,0,oCanvas.width,oCanvas.height);
                            oCtx.beginPath();
                            validater=null;
                        }
                        }
                    }
                    );
                ]]>
                </script> 
              <x:div id="body" >
            <x:table border="0" cellpadding="4px">
                    <x:tr>
                        <x:td colspan="3">
                            <label style="font-weight: bold"  value="@load(vm.name)"></label>
                        </x:td>
                    </x:tr>
                    <x:tr>
                        <x:td colspan="3">
                            <textbox width="300px" mandatory="true"
                                value="@load(vm.signatureNameTextbox)" readonly="@load(vm.readonlySignatureNameTextbox)">
                            </textbox>
                        </x:td>
                    </x:tr>
                    <x:tr>
                        <x:td colspan="3">
                            <label style="font-weight: bold"
                                value="@load(vm.lablesign)">
                            </label>
                        </x:td>
                    </x:tr>
                    <x:tr>
                        <x:td colspan="3">
                            <div visible="@load(vm.divCanvas)">
                                <x:canvas width="300px" height="200px"
                                    visible="true" style="border:1px solid black;"
                                    id="thecanvas">
                                </x:canvas>
                            </div>
                            <image width="300px" height="200px" src="" content="@load(vm.signature)"
                                visible="@load(vm.showSignature)">
                            </image>
                        </x:td>
                    </x:tr>
                    <x:tr>
                        <x:td colspan="3">
                            <label value="@load(vm.message)" style="color:red"></label>
                        </x:td>
                    </x:tr>
                    <x:tr>
                        <div visible="@load(vm.canvasMode)">
                            <x:td align="center" colspan="3">
                                <x:table cellpadding="4px">
                                    <x:tr>
                                        <x:td>
                                            <x:input type="button"
                                                id="save" value="@load(vm.getText('save'))" />
                                        </x:td>
                                        <x:td align="center">
                                            <x:input type="button"
                                                id="reset" value="@load(vm.getText('reset_'))" />

                                        </x:td>
                                        <x:td align="left">
                                            <x:input type="button" 
                                                value="@load(vm.getText('cancel_'))" onClick="onCancel()" />
                                        </x:td>
                                    </x:tr>
                                </x:table>
                            </x:td>
                        </div>
                        <div visible="@load(vm.imageMode)">
                            <x:td align="right">
                                <x:input type="button"   value="@load(vm.getText('dsdsdsd_'))"
                                    onClick="onCancel()" align="left" />
                            </x:td>
                        </div>
                    </x:tr>
                </x:table>
                </x:div>
        </window>
    </zk>

I just try to call method in java class somthing like this:

@Listen("onCancel = #btnCloseWindow")
public void onCancel(Event event,@ContextParam(ContextType.VIEW) Component view) {
    LOGGER.debug("inside cancel");
    view.detach();
}

Thanks

Sitansu

delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2014-08-28 09:14:33 +0800

cor3000 gravatar image cor3000
4406 2 7
ZK Team

I assume you found the solution already using command binding

zul:

<x:input type="button" value="@load(vm.getText('dsdsdsd_'))"
  onClick="@command('cancel')" align="left" />

ViewModel:

@Command("cancel")
public void doCancel(@ContextParam(ContextType.VIEW) Component view) {
   //do what you need to do inside
}

The idea of MVVM is to separate the View from the ViewModel and not call the methods directly!

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: 2014-07-22 08:53:49 +0800

Seen: 67 times

Last updated: Aug 28 '14

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