0

Generate components using java??

asked 2009-11-16 17:17:52 +0800

nevyn gravatar image nevyn
159 2

Hi,

I've been having a look at zk and it looks brilliant. However, I can't seem to find any documentation on how to use Java to create components.

Say I have a set of "modules" which each user is assigned to. So a user may have admin rights to the system. If I only wanted to show the menu entry for administration to those users who are admins, then I'd like to do something along the lines of read from a database, and create a menu based upon what the user has rights to.

So assuming I do a read from the database and then for each of the items it reads, it creates a menu item. How do I get java to output an item for zuml? I'm only really looking for one line - something along the lines of:-
println("<menuitem>"+variableReadFromDatabase+"</menuitem>);

I realise this is probably a very simple problem - I just can't seem to find any documentation on it...

Regards,
Nevyn.

delete flag offensive retag edit

7 Replies

Sort by ยป oldest newest

answered 2009-11-16 19:10:23 +0800

PeterKuo gravatar image PeterKuo
481 2

You may refer to

http://docs.zkoss.org/wiki/Basic_concepts#Manipulate_DOM_by_Java

link publish delete flag offensive edit

answered 2009-11-16 19:23:09 +0800

nevyn gravatar image nevyn
159 2

Are there any good examples of this out there? I get ZUML which is what I've been playing around with. Easy as all hell and followed up with the great example of the demo. The problem is, the demo doesn't really show off anything else... at all. There's no examples of controls being used with java (fairly easy to figure out how to pass values to java functions from zk controls).

In otherwords, I don't understand DOM - where to use it, how to use it. There's a code snippet but no context as to where to use that code etc. Surely someone must have written some examples ...

link publish delete flag offensive edit

answered 2009-11-16 21:09:59 +0800

PeterKuo gravatar image PeterKuo
481 2

I would suggest developer guide
http://docs.zkoss.org/wiki/Developer%27s_Guide

and following smalltalk first.
http://docs.zkoss.org/wiki/ZK_MVC_Made_Easy

link publish delete flag offensive edit

answered 2009-11-17 22:56:34 +0800

nevyn gravatar image nevyn
159 2

Quick question - given that ZUML is a markup language, could I use something along the lines of php or whatever to generate zuml in the same way that you would html? Or is the issue likely to be that markup language that is generated by php is processed by the client rather than the server?

link publish delete flag offensive edit

answered 2009-11-18 01:42:01 +0800

PeterKuo gravatar image PeterKuo
481 2

zul is processed to be html at server side.
So I believe there is a way to generate zul by php.

link publish delete flag offensive edit

answered 2009-11-18 03:41:34 +0800

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

updated 2009-11-18 07:44:19 +0800

@nevyn

The easiest way to create the components from java code is:

1. create a zul.file with minimum the <window> tag.
2. use a composer for controlling .
3. call the zulfile with Executions.createComponents('myZulfile.zul', null, null),:

myZulFile.zul

<?page id="indexPage" title="Forsthaus SaaS" cacheable="false" 
	language="xul/html" zscriptLanguage="Java"?>
<?meta content="text/html; charset=UTF-8" 	pageEncoding="UTF-8"?>


<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="startWindow" border="none" width="100%" height="100%"
		use="de.forsthaus.InitApplication" />
</zk>

InitApplication.java
Only pieces of code.
With a composer you have full access to the components by their id. So it's easy to
create new Components and added they to the ParentComponent , in this case the first parent
is the window component.
Or take the ParentComponent and add a new Component with ParentCompionent.appendChild(yourNewComponent).
In both cases you can craete/modify your DOM tree with the components you need.

public class InitApplication extends WindowBaseCtrl implements Serializable {

	private transient final static Logger logger = Logger.getLogger(InitApplication.class);
	private static final long serialVersionUID = 1L;

	protected transient Window startWindow; // autowired
	protected transient North bl_north; // autowire
	protected transient South bl_south; // autowire
	protected transient Center bl_center; // autowire

	private transient Tablelayout tableLayout;
	private transient Tablechildren tableChildrenRecords;
	private transient Tablechildren tableChildrenStatistic;
	private transient Tablechildren tableChildrenButtons;
	private transient Div div_Buttons;
	private transient Vbox Vbox_Buttons;

	private transient Label label_RecordCountCustomer;

	private transient KundeService kundeService;
	private transient BrancheService brancheService;
        ...


	public InitApplication() {
		super();

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

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

		if (logger.isDebugEnabled()) {
			logger.debug("--> " + event.toString());
		}

		doOnCreateCommon(startWindow); // do the autowire
              ...

              showButtons();
         }

	private void createMainGrid() {

		Div div = new Div();
		div.setParent(bl_center);

		Hr hr = new Hr();
		hr.setParent(div);

		/*
		 * Borderlayout around the grid for make it scrollable to see all table
		 * records if the browser window are to small.
		 */
		Borderlayout bl = new Borderlayout();
		bl.setParent(div);
		Center ct = new Center();
		ct.setAutoscroll(true);
		ct.setStyle("background-color: #EBEBEB");
		ct.setBorder("none");
		ct.setFlex(true);
		ct.setParent(bl);
		Div divCt = new Div();
		divCt.setParent(ct);

		tableLayout = new Tablelayout();
		tableLayout.setColumns(3);
		tableLayout.setWidth("950px");
		tableLayout.setParent(divCt);

		tableChildrenRecords = new Tablechildren();
		tableChildrenRecords.setRowspan(1);
		tableChildrenRecords.setWidth("20%");
		tableChildrenRecords.setParent(tableLayout);

		tableChildrenStatistic = new Tablechildren();
		tableChildrenStatistic.setRowspan(1);
		tableChildrenStatistic.setWidth("50%");
		tableChildrenStatistic.setParent(tableLayout);

		tableChildrenButtons = new Tablechildren();
		tableChildrenButtons.setRowspan(1);
		tableChildrenButtons.setWidth("30%");
		tableChildrenButtons.setParent(tableLayout);

		Separator sep = new Separator();
		sep.setParent(divCt);
		Separator sep2 = new Separator();
		sep2.setParent(divCt);

		Div divFooter = new Div();
		divFooter.setAlign("center");
		divFooter.setParent(bl_south);

		Hr hr2 = new Hr();
		hr2.setParent(divFooter);

		Label footerLabel = new Label();
		footerLabel.setValue(" Help to prevent the global warming by writing cool software.");
		footerLabel
				.setStyle("align:center; padding-top:0px; font-family:Verdana; black: white; font-size: 0.6em");
		footerLabel.setParent(divFooter);

	}


private void showButtons() {

		Panel panel = new Panel();
		panel.setTitle("Demo Customers");
		panel.setWidth("220px");
		panel.setHeight("100%");
		panel.setBorder("none");
		panel.setStyle("padding-bottom:5px");
		panel.setParent(tableChildrenButtons);

		Panelchildren panelchildren = new Panelchildren();
		panelchildren.setParent(panel);

		Vbox_Buttons = new Vbox();
		Vbox_Buttons.setParent(panelchildren);

		div_Buttons = new Div();
		div_Buttons.setWidth("100%");
		div_Buttons.setHeight("100%");
		div_Buttons.setStyle("padding: 10px;");
		div_Buttons.setParent(Vbox_Buttons);

		/* 1000. Button */
		Div divBtn1 = new Div();
		divBtn1.setStyle("align: center");
		divBtn1.setParent(div_Buttons);

		Button btn = new Button();
		btn.setLabel("insert 1000");
		btn.setImage("/images/icons/database.gif");
		btn.setTooltiptext("Insert 1.000 randomly created customer records");
		btn.setParent(divBtn1);

		btn.addEventListener("onClick", new OnClick1000Eventlistener());

		/* Separator */
		Separator sep1 = new Separator();
		sep1.setParent(div_Buttons);
		Separator sep2 = new Separator();
		sep2.setParent(div_Buttons);


WindowBaseCtrl .java

public abstract class WindowBaseCtrl extends Window implements AfterCompose,
		Serializable {

	private static final long serialVersionUID = -2179229704315045689L;
	protected transient AnnotateDataBinder binder;

	// protected Workspace ws;
	protected transient Map<String, Object> args;

	public void doOnCreateCommon(Window w) throws Exception {
		binder = new AnnotateDataBinder(w);
		binder.loadAll();
		// ws = Workspace.getWorkspace();
	}

	public void doOnCreateCommon(Window w, Event fe) throws Exception {
		doOnCreateCommon(w);
		CreateEvent ce = (CreateEvent) ((ForwardEvent) fe).getOrigin();
		args = (Map<String, Object>) ce.getArg();
	}

	public WindowBaseCtrl() {
		super();
		this.setStyle("body {padding 0 0;}");
		// <style>
		// body {
		// padding: 0 0; /* 0 padding on top and bottom and 0 padding on right
		// and left */
		// }
		// </style>
	}

	@Override
	public void afterCompose() {
		processRecursive(this, this);

		// Components.wireVariables(this, this); // auto wire variables
		// Components.addForwards(this, this); // auto forward
	}

	/**
	 * Go recursive through all founded components and 
	 * wires all vars and added all forwarders for ALL <br>
	 * window components. <br>
	 * 
	 * @param main
	 * @param child
	 */
	private void processRecursive(Window main, Window child) {
		Components.wireVariables(main, child);
		Components.addForwards(main, this);
		List<Component> winList = (List<Component>) child.getChildren();
		for (Component window : winList) {
			if (window instanceof Window) {
				processRecursive(main, (Window) window);
			}
		}
	}

}

link publish delete flag offensive edit

answered 2009-11-18 16:26:29 +0800

nevyn gravatar image nevyn
159 2

Sweet. Thanks TerryTornado.

I'm now looking into EL to see what I can do with that. I had thought it was something specific to zk and have now learnt otherwise (really not a Java person - it feels counterintuitive to me). I might post up the code I come up with - it should be interesting once I've got something going...

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: 2009-11-16 17:17:52 +0800

Seen: 988 times

Last updated: Nov 18 '09

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