Wednesday, 7 August 2013

ZK Quick InputBox

Just to get String input before do some process. For example, we will normally ask the reason to delete any record.

<?xml version="1.0" encoding="UTF-8"?>
<zk>
<window id="inputstringbox" title="@load(vm.screenTitle)"
width="auto" height="auto" border="normal" minimizable="false"
sclass="mymodal" mode="modal" maximizable="false" closable="true"
action="hide: slideUp" apply="org.zkoss.bind.BindComposer"
onCancel="@command('closeThis')"
viewModel="@id('vm') @init('com.product.webapp.component.InputStringBoxVM')">
<ftextbox id="txtDescription" rows="4" cols="70"
value="@bind(vm.input)" />
<separator />
<div align="center">
<fbutton label="Ok" onClick="@command('onOk')" />
<fbutton label="Cancel" onClick="@command('closeThis')" />
</div>
</window>
</zk>

package com.product.webapp.component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.AfterCompose;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.ExecutionArgParam;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.select.Selectors;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Window;

import com.product.webapp.utilities.GlobalCommandValues;
import com.product.webapp.utilities.Libs;
import com.product.webapp.utilities.ShowWindow;
import com.product.webapp.utilities.ValidationMessage;

public class InputStringBoxVM {

@Wire("#inputstringbox")
private Window win;
private String input;
private String validateMessage;
private String screenTitle;

public String getInput() {
return input;
}

public String getScreenTitle() {
return screenTitle;
}

public void setScreenTitle(String screenTitle) {
this.screenTitle = screenTitle;
}

public String getValidateMessage() {
return validateMessage;
}

public void setValidateMessage(String validateMessage) {
this.validateMessage = validateMessage;
}

public void setInput(String input) {
this.input = input;
}

@AfterCompose
public void initSetup(@ContextParam(ContextType.VIEW) Component view,
@ExecutionArgParam("validateMessage") String validateMessage,
@ExecutionArgParam("screenTitle") String screenTitle) {
Selectors.wireComponents(view, this, false);
this.validateMessage = validateMessage;
this.screenTitle = screenTitle;
}

@SuppressWarnings({ "unchecked", "rawtypes" })
@Command
public void onOk() {
if (IsValidEntry() == false) {
return;
} else {
Map args = new HashMap();
args.put("input", this.input);
BindUtils.postGlobalCommand(null, null,
GlobalCommandValues.InputStringEntered, args);
win.detach();
}
}

public boolean IsValidEntry() {

List<ValidationMessage> vList = new ArrayList<ValidationMessage>();

if (Libs.isNotEmpty(this.input) == false) {
vList.add(new ValidationMessage(this.validateMessage));
}

if (vList.size() > 0) {
ShowWindow.ShowValidation(vList);
return false;
} else
return true;
}

@Command
public void closeThis() {
win.detach();
}
}

public static void showInputBox(String title, String validateMessage,
Window callingFrom) {
final HashMap<String, Object> map = new HashMap<String, Object>();
map.put("screenTitle", title);
map.put("validateMessage", validateMessage);
if (callingFrom.hasFellow("inputstringbox") == false) {
Executions.createComponents("/ZK/components/inputstringbox.zul",
null, map);
}
}

@Command
public void onResetPassword() {
ShowWindow.showInputBox("Reason",
"Pleas Enter the reason to reset the password.", win);
}

@GlobalCommand(GlobalCommandValues.InputStringEntered)
public void resetPassword() {
// do something here
}

No comments:

Post a Comment

CSS Classes and Styles