0

passing parameter using hashmap, I'm really confused

asked 2010-02-11 04:09:12 +0800

JoH gravatar image JoH
105 2 3

Hallo,

I read the code from terrytornado how to pass parameter to another zul window.

But I couldn't follow correctly the code. Ok the hashmap put the parameter I selected form the listitem.
But how can my other window, which contains a textbox, say test.zul, recognizes my passed parameter?

code:

public void onDoubleClick(Event event) throws Exception {

Listbox lb = (Listbox)this.getFellow("items");
Listitem item = lb.getSelectedItem();
String energy = ((de.qcdb.lmu.domain.Item)item.getValue()).getEnergy();

/*This part I don't get it*/
HashMap map = new HashMap();

map.put("energy", energy);

Window guestWnd = (Window)Executions.createComponents("image.zul", null, map);
guestWnd.doModal();
}

test.zul
<window id="imWnd" title="Image" border="normal" use="de.qcdb.lmu.window.FrontWindow" closable="true">

<textbox id="energy" width="99%" value="????" />
</window>
</zk>

delete flag offensive retag edit

20 Replies

Sort by ยป oldest newest

answered 2010-02-11 05:46:04 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2010-02-11 05:58:08 +0800

Hi Joh,

Let us imagine that your zul-controllers are extending from GenericForwardComposer

In your created Dialog (image.zul) you can get back these 'createEvent-params' in the following way:

This is our new methode for getting the overhanded params.
This method is in our BaseController classes.

/**
	 * Get the params map that are overhanded at creation time. <br>
	 * Reading the params that are binded to the createEvent.<br>
	 * 
	 * @param event
	 * @return params map
	 */
	@SuppressWarnings("unchecked")
	public Map<String, Object> getCreationArgsMap(Event event) {
		CreateEvent ce = (CreateEvent) ((ForwardEvent) event).getOrigin();
		return ce.getArg();
	}

So we call this method in our onCreate event of the DialogWindow --> onCreate$guestWnd(Event event).
You must adapt it to the correct window ID. I don't see the code of your image.zul.


	public void onCreate$imageDialogWindow(Event event) throws Exception {

                . . .
		// get the params map that are overhanded by creation.
		Map<String, Object> args = getCreationArgsMap(event);

		if (args.containsKey("energy")) {
			String energy  = (String) args.get("energy");
			System.out.println(energy);
		} else {
			//
		}
               . . .

best
Stephan

PS:
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("energy", energy);

link publish delete flag offensive edit

answered 2010-02-11 06:52:25 +0800

JoH gravatar image JoH
105 2 3

Hi, first thanx for the reply.

In principle one could write all of them in class right?
so these are the methods in my class FrontWindow,
Sorry I meant before image.zul not test.zul

It should show me the String energy right if double click my listitem, but somehow it's still empty. Or what did you mean by adapting window ID

code:

public class FrontWindow extends Window {
protected Map<String, Object> args;

@SuppressWarnings("unchecked")

public Map<String, Object> getCreationArgsMap(Event event) {
CreateEvent ce = (CreateEvent) ((ForwardEvent) event).getOrigin();
return ce.getArg();
}

public void onCreate$imageDialogWindow(Event event) throws Exception {



Map<String, Object> args = getCreationArgsMap(event);

if (args.containsKey("energy")) {
String energy = (String) args.get("energy");
System.out.println(energy);
} else {
//
}
}


public void onDoubleClick(Event event) throws Exception {

Listbox lb = (Listbox)this.getFellow("items");
Listitem item = lb.getSelectedItem();
String energy = ((de.qcdb.lmu.domain.Item)item.getValue()).getEnergy();

HashMap<String, Object> map = new HashMap<String, Object>();

map.put("energy", energy);

Window imWnd = (Window)Executions.createComponents("image.zul", null, map);
imWnd.doModal();
}
}

image.zul:
<zk xmlns="http://www.zkoss.org/2005/zul" xmlns:h="http://www.w3.org/1999/xhtml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.zkoss.org/2005/zul http://www.zkoss.org/2005/zul/zul.xsd">
<window id="imWnd" title="Image" border="normal" use="de.qcdb.lmu.window.FrontWindow" closable="true">


</window>
</zk>

link publish delete flag offensive edit

answered 2010-02-11 08:14:00 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2010-02-11 08:15:37 +0800

Hi Joh,

sorry it's very confusing.

1. Please use CODE tags for posting code
2. Have you ever have a look on my sample codes.
3. What will you exactly do?

You must adapt it to your Component IDs like this:

   public void onCreate$imWnd(Event event) throws Exception {

          Map<String, Object> args = getCreationArgsMap(event);

          if (args.containsKey("energy")) {
              String energy = (String) args.get("energy");
              System.out.println(energy);
          } else {
             //
          }
}

best
Stephan

link publish delete flag offensive edit

answered 2010-02-11 08:38:53 +0800

JoH gravatar image JoH
105 2 3

Hi terry,

What I want to do is so simple:
1. I have a listitem from my database. Yesterday I succeed to add doubleclick event and open a new window named image.zul. I want to transfer the value (energy) from my listitem to that window, which contains a textbox. The value that I passed should appear there in the textbox.
2. So I read your code, and you're using hash map. Truly I don't have experience in these things, since I'm not real java programmer, but fortran. So even the more deeper I read your code, the more confused I'm.

so again the code:

@SuppressWarnings("unchecked")
	public Map<String, Object> getCreationArgsMap(Event event) {
		CreateEvent ce = (CreateEvent) ((ForwardEvent) event).getOrigin();
		return ce.getArg();
		}

	public void onCreate$imWnd(Event event) throws Exception {

	
		// get the params map that are overhanded by creation.
		Map<String, Object> args = getCreationArgsMap(event);

		if (args.containsKey("energy")) {
			String energy  = (String) args.get("energy");
			System.out.println(energy);
		} else {
	//
		}
	} 

	
	public void onDoubleClick(Event event) throws Exception {
		

		Listbox lb = (Listbox)getFellow("items");
		Listitem item = lb.getSelectedItem();
		String energy = ((de.qcdb.lmu.domain.Item)item.getValue()).getEnergy();
		//Textbox tb_e = (Textbox)Path.getComponent("/imWnd/tb_energy");
		//tb_e.setValue(energy);
		HashMap<String, Object> map = new HashMap<String, Object>();
		

		
		map.put("energy", energy);
        	
		

		Window imWnd = (Window)Executions.createComponents("image.zul", null, map);
		imWnd.doModal();
	}

and my image.zul

<zk xmlns="http://www.zkoss.org/2005/zul" xmlns:h="http://www.w3.org/1999/xhtml"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.zkoss.org/2005/zul http://www.zkoss.org/2005/zul/zul.xsd">
<window id="imWnd" title="Image" border="normal" use="de.qcdb.lmu.window.FrontWindow"  closable="true">
<!--<image src="/images/info.png" />--> 
<textbox id="tb_energy" width="99%" />
</window>
</zk>

link publish delete flag offensive edit

answered 2010-02-11 09:45:52 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2010-02-11 09:48:23 +0800

Joh,

you use cutted pieces of my code. I'm working with a BaseController from where i extend the controllers.
And at no time i have worked with several zul-files and ONE controller for it.

I don't know which version you have from the sample app. zk_sample_gui or Zksample2
If you will have access to the components you must let the controllers know of that. Therefore are so called composers.
Implement the Interface afterCompose or extend from a xxxComposer.
Please read the docs about Composers.

public class FrontWindow extends Window implements AfterCompose {

	private transient static final Logger logger = Logger.getLogger(FrontWindow.class);

	@Override
	public void afterCompose() {
	     Components.wireVariables(this, this); // auto wire variables
	     Components.addForwards(this, this); // auto forward
	}

Stephan

PS: Can you debug through your events? Set breakpoints

	/**
	 * default constructor.<br>
	 */
	public FrontWindow() {
		super();

		if (logger.isDebugEnabled()) {
			logger.debug("--> super()");
		}
	}

link publish delete flag offensive edit

answered 2010-02-11 10:13:55 +0800

JoH gravatar image JoH
105 2 3

This is my logger

16:59:40,033 INFO FrontWindow:546 - --> [MouseEvent onDoubleClick <FrontWindow guestWnd>]
HF=-630.434123

it seems that the System.out.println(energy) works. But as the image.zul shows up. The number doesn't show up in the textbox

link publish delete flag offensive edit

answered 2010-02-11 13:56:10 +0800

JoH gravatar image JoH
105 2 3

I took your code from
http://www.zkoss.org/forum/listComment/6827
So you have BranchDialogCtrl which extends your BaseCtrl.
In your BaseCtrl you did autowire the variables using afterCompose.
In the BranchDialogCtrl you defined the components that have been autowired.
What does doOnCreateCommon do? You wrote it autowired the variables. So, is this also necessary for the doubleclick event you wrote in BranchListCtrl?

public class BranchDialogCtrl extends BaseCtrl {
// Components from the zul-file that are autowired
	private Window branchDialogWindow; // the window 'id' from the zul-file
	private Textbox braNr; // autowire
	private Textbox braBezeichnung; // autowire

        private Listbox lbBranch; // overhanded per param
	private Branche branche; // overhanded per param
	private BranchListCtrl branchCtrl; // overhanded per param
	private BrancheService brancheService;

public void onCreate$branchDialogWindow(Event event) throws Exception {

		if (logger.isInfoEnabled()) {
			logger.info("--> " + event.toString());
		}

		doOnCreateCommon(branchDialogWindow, event); // autowire the vars

		if (args.containsKey("branche")) {
			branche = (Branche) args.get("branche");
			setBranche(branche);
		} else {
			setBranche(null);
		}

		// we get the listBox Object for the branch list. So we have access
		// to it and can synchronize the shown data when we do insert, edit or
		// delete branches here.
		if (args.containsKey("lbBranch")) {
			lbBranch = (Listbox) args.get("lbBranch");
		} else {
			lbBranch = null;
		}

		if (args.containsKey("branchCtrl")) {
			branchCtrl = (BranchListCtrl) args.get("branchCtrl");
		} else {
			branchCtrl = null;
		}

		showBranchDialog(getBranche());

	}

link publish delete flag offensive edit

answered 2010-02-11 15:14:18 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2010-02-11 15:17:45 +0800

doOnCreateCommon() is older stuff and works like an AllInThere; used before 3.5.x . Because the newer zk version have Composers.

The best is to checkout the whole new ZkSample2 project. There we only using the getCreationArgsMap() method.

 It seems that the System.out.println(energy) works. But as the image.zul shows up. The number doesn't show up in the textbox.

tb_energy.setValue(energie);

Make a own Controller for the image.zul and all is OK !

In the onCreate$imageZulControllerWindow() you can read back from the overhanded params and shows it in the textbox.
I do the same in all Dialogs and it works.

best
Stephan

link publish delete flag offensive edit

answered 2010-02-12 05:16:18 +0800

JoH gravatar image JoH
105 2 3

thanx it works now

link publish delete flag offensive edit

answered 2010-02-12 08:24:58 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

Good. :-)

Do you work now with two controllers?

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: 2010-02-11 04:09:12 +0800

Seen: 2,733 times

Last updated: Feb 17 '11

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