0

ZK Button is caled Twice

asked 2011-03-10 18:00:00 +0800

vradiya gravatar image vradiya
33 1

updated 2011-03-10 20:14:18 +0800

Hello all, Can you please take a look at below code and let me know what thing I am doing wrong?. Whenever I am trying to update/delete the account it's being called twice..:( So method onClick$delete and onClick$update is called twice..it's a simple button type..

account.zul

<?xml version="1.0" encoding="UTF-8"?>
<?page id="zkAccount"?>
<?taglib uri="http://www.zkoss.org/dsp/web/core" prefix="c"?>
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" arg0="./cAcct"?>
<?taglib uri="http://www.zkoss.org/zkspring/security" prefix="sec"?>
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>

<zk>
<window border="none" width="100%" id="cAcct" apply="com.zebra.zls.controller.AccountController" xmlns:zk="http://www.zkoss.org/2005/zul">
<h:form xmlns:h="http://www.w3.org/1999/xhtml">
		<toolbar align="end">
	<div height="23px">
	
	<zk if="${sec:isAllGranted('ROLE_ADMIN')}">
   		<button id="new" type="button" label="New"></button><space width="10px"></space><button id="edit" type="button" label="Edit"></button>
		<space width="10px"></space><button id="password" type="button" label="Password"></button><space width="10px"></space><button id="delete" type="button" label="Delete"></button>
	</zk>	
	
	</div>
	</toolbar>
		
		<zk:listbox id="accountsList" model="@{cAcct$composer.accounts}" selectedItem="@{selected}" style="height:400px">
		         <zk:listhead sizable="true">
		                  <zk:listheader label="User Name" />             
		                  <zk:listheader label="Last Name" />             
		                  <zk:listheader label="First Name" />
		                  <zk:listheader label="Email"  />
		                  <zk:listheader label="Role"  />
		                  
		         </zk:listhead>
		                         
                 <zk:listitem self="@{each=account}">
                              <zk:listcell style="text-align: left" label="@{account.userName}" value="@{account.userName}"/>
                              <zk:listcell style="text-align: left" label="@{account.lastName}" value="@{account.lastName}"/>
                              <zk:listcell style="text-align: left" label="@{account.firstName}" value="@{account.firstName}"/>
                              <zk:listcell style="text-align: left" label="@{account.email}" value="@{account.email}"/>
                              <zk:listcell style="text-align: left" label="@{account.role}" value="@{account.role}"/>
                 </zk:listitem>     
		</zk:listbox>  

		<window title="Updating User Account" id="popup" apply="com.zebra.zls.controller.AccountController" border="normal" position="center">
			<h:form xmlns:h="http://www.w3.org/1999/xhtml">
				<grid width="400px">
				  	<rows>
					    <row>User Name <textbox id="usrName" name="usrName" value=""/></row>
					    <row>Password <textbox id="pass" type="password" name="pass" value=""/></row>
					    <row>Confirm Password <textbox type="password" id="confPass" name="confPass" value=""/></row>
					    <row>Last Name <textbox id="lstName" name="lastName" value=""/></row>
					    <row>First Name <textbox id="frstName" name="firstName" value=""/></row>
					    <row>Email <textbox id="email" name="email" value=""/></row>
					    <row>Role
					    <radiogroup id="userSelection">
					                    <radio value="ROLE_ADMIN" label="Admin" />
					                    <radio value="ROLE_USER" label="User" />
					     </radiogroup>    	       
					    </row>
					    <row spans="2" style="text-align: right;">
					  	  <button style="display:none" type="button" id="adduser" label="Add User"/>
					  	</row>
					    <row spans="2" style="text-align: right;">
					  	  <button style="display:none;" type="button" id="update" label="Update User"/>
					  	</row>
					</rows>
				</grid>
			</h:form>
		</window>
</h:form>		
</window>

</zk>

AccountController.java




import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.bind.JAXBException;

import org.zkoss.zhtml.Messagebox;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.databind.AnnotateDataBinder;
import org.zkoss.zul.Button;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Radio;
import org.zkoss.zul.Radiogroup;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;


public class AccountController extends GenericForwardComposer { 

	
	private static final long serialVersionUID = 1L;
	private List<AccountBean> _accounts = new ArrayList<AccountBean>();
	private AnnotateDataBinder binder;
	private Listbox accountsList;
	private Window popup;
	private Textbox usrName;
	private Textbox lstName;
	private Textbox frstName;
	private Textbox email;
	private Textbox confPass;
	private Textbox pass;
	private Button update;
	private Button adduser;
	private Radiogroup userSelection;
	private Window cAcct;
	
	public AccountController() throws Exception{
		this.returnAllAccounts();
	}
	
	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
	}
	
	public void onCreate$cAcct(ForwardEvent event) {
		binder = (AnnotateDataBinder) event.getTarget().getAttributeOrFellow("binder", true);
	}
	
	public void onCreate$popup(ForwardEvent event) {
		
		binder = (AnnotateDataBinder) event.getTarget().getAttributeOrFellow("binder", true);
		usrName = (Textbox) popup.getFellow("usrName");
		lstName = (Textbox) popup.getFellow("lstName");
		frstName = (Textbox) popup.getFellow("frstName");
		email = (Textbox) popup.getFellow("email");
		confPass = (Textbox) popup.getFellow("confPass");
		pass = (Textbox) popup.getFellow("pass");
		update = (Button)popup.getFellow("update");
		adduser = (Button) popup.getFellow("adduser");
		userSelection = (Radiogroup)popup.getFellow("userSelection");
	}
	public String fixString(String orgStr){
		if(orgStr == null)
			return "";
		else
			return orgStr;
	}
	public List<AccountBean> returnAllAccounts() throws Exception {
		
			return _accounts;
	}

	
	public void onClick$new(Event event){
		
		popup.doPopup();
		
		//Empty all Text boxes to fill New user data..
		usrName.setText("");
		pass.setText("");
		confPass.setText("");
		lstName.setText("");
		frstName.setText("");
		email.setText("");
		userSelection.setSelectedIndex(1);
		
		usrName.setReadonly(false);
		pass.setReadonly(false);
		confPass.setVisible(true);
		confPass.setReadonly(false);
		
		update.setStyle("display:none");
		adduser.setStyle("display:block");
		popup.setStyle("display:block");
		
	}
	
	public void onClick$edit(Event event) throws Exception{
		String username = null;
		if(accountsList.getSelectedItem() != null)
			username = accountsList.getSelectedItem().getLabel();
		else
			Messagebox.show("Please select any row from the list below and press edit.");
		
		if(username != null){
		
			popup.doPopup();
			usrName.setReadonly(true);
			pass.setReadonly(true);
			confPass.setVisible(false);
			
			AccountBean selectedItem = getAccountBean(accountsList.getSelectedItem().getLabel());
			
			usrName.setText(selectedItem.getUserName());
			pass.setText(selectedItem.getPassDigest());
			confPass.setText(selectedItem.getPassDigest());
			lstName.setText(selectedItem.getLastName());
			frstName.setText(selectedItem.getFirstName());
			email.setText(selectedItem.getEmail());
			
			for(Iterator<Radio> itRadio = userSelection.getItems().iterator(); itRadio.hasNext();){
				Radio radio = itRadio.next();
				if(radio.getValue().equalsIgnoreCase(selectedItem.getRole())){
					userSelection.setSelectedItem(radio);
				}
			}
			this.updateAccount();
			update.setStyle("display:block");
			adduser.setStyle("display:none");
			popup.setStyle("display:block");
		}	
	}
	
	public void onClick$delete(Event event) throws Exception{
		String username = null;
		if(accountsList.getSelectedItem() != null)
			username = accountsList.getSelectedItem().getLabel();
		else
			Messagebox.show("Please select any row from the list below and press delete.");
		
		if(username != null){
			if(delete(username).equalsIgnoreCase("SUCCESS")){
				this._accounts.clear();
				this.returnAllAccounts();
				binder = new AnnotateDataBinder(accountsList);
				binder.loadComponent(accountsList);
				Messagebox.show(username+" successfully deleted from Account");
			}else{
				Messagebox.show("Error Occurred while deleting account..."+username);
			}
		}
	}
	
	public void onClick$update(Event event){
		
		popup.setStyle("display:none");
	}
	
	public void onClick$adduser(Event event) throws Exception{
		
		this._accounts.clear();
		addAccount();
		this.returnAllAccounts();
		binder.loadAll();
		popup.setStyle("display:none");
	}
	
	public String delete(String username) throws FileNotFoundException, JAXBException {
		return "SUCCESS";
	}
	
	private String addAccount() throws FileNotFoundException, JAXBException {
		return "SUCCESS";
	}
	
	public String updateAccount() throws FileNotFoundException, JAXBException {
		// find account
		System.out.println("I am now going to Update Account");
		return "SUCCESS";
	}
}


delete flag offensive retag edit

2 Replies

Sort by ยป oldest newest

answered 2011-03-10 20:20:15 +0800

woodyki gravatar image woodyki
168

The problem is caused by the nested windows both called the same composer.
It will create two instances of the same composer.
Each composer instance contains the same button objects and response the click event.

link publish delete flag offensive edit

answered 2011-03-11 12:11:51 +0800

vradiya gravatar image vradiya
33 1

awesome..thanks a lot..Yep..that was the issue..:)

link publish delete flag offensive edit
Your reply
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

RSS

Stats

Asked: 2011-03-10 18:00:00 +0800

Seen: 527 times

Last updated: Mar 11 '11

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