You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Filip Balas <fb...@IMVProjects.com> on 2004/11/12 02:46:32 UTC

RE: How to: Pass a model and listener into a component [additional info]


Couldn't attach files so here they are
in text:
-------------------------------------------
---------Discipline.page-------------------
-------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE page-specification PUBLIC
  "-//Apache Software Foundation//Tapestry Specification 3.0//EN"
  "http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">
<!-- generated by Spindle, http://spindle.sourceforge.net -->

<page-specification class="com.imvprojects.forecast.pages.DisciplineList">

    <property-specification name="unassignedDisciplineListModel" type="org.apache.tapestry.form.IPropertySelectionModel"/>
    
    <component id="assignDisciplines" type="RelationshipCreator">
    	<binding name="optionsList" expression="unassignedDisciplineListModel"/>
      <binding name="selectionListener" expression="listeners.addDiscipline"/>
    </component>
      

</page-specification>


-------------------------------------------
---------Discipline.html-------------------
-------------------------------------------

<span jwcid="@Shell">
  <span jwcid="assignDisciplines"/>
</span>


-------------------------------------------
---------Discipline.java-------------------
-------------------------------------------

package com.imvprojects.forecast.pages;

import java.util.List;
import java.util.Vector;

import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.event.PageEvent;
import org.apache.tapestry.form.IPropertySelectionModel;
import org.objectstyle.cayenne.query.SelectQuery;

import com.imvprojects.forecast.LoginAccessor;
import com.imvprojects.forecast.components.RelationshipCreator;
import com.imvprojects.forecast.data.Discipline;
import com.imvprojects.forecast.data.Project;
import com.imvprojects.forecast.data.ProjectDiscipline_Relationship;
import com.imvprojects.forecast.pages.models.DisciplineModel;

public abstract class DisciplineList extends LoginAccessor {
	public abstract void setAProject(Project aProject);
	public abstract Project getAProject();
	
	public abstract void setUnassignedDisciplineListModel(IPropertySelectionModel aDisciplineList);
	public abstract IPropertySelectionModel getUnassignedDisciplineListModel();
	
	public void addDiscipline(IRequestCycle cycle) {
		Object o = ((RelationshipCreator)getComponent("assignDisciplines")).getItemToAdd();
  		System.err.println();
    		System.err.println();
    		System.err.println(((Discipline)o).getDescription());
    		System.err.println();
    		System.err.println();
	}
	
	private List getUnassignedDisciplines(){
		List assigned = ((DisciplineList)getPage()). getAProject().getAssignedDiscipline();
    
    		SelectQuery query = new SelectQuery(Discipline.class);
    		List allDisciplines =  getVisitDataContext().performQuery(query);
    
    		List unassigned = new Vector();
    		unassigned.addAll(allDisciplines);
    		unassigned.removeAll(assigned);
		
    		return assigned;
	}
	
	public void pageBeginRender(PageEvent event) {
		setUnassignedDisciplineListModel(new DisciplineModel(getUnassignedDisciplines()));
		
		super.pageBeginRender(event);
	}
	
}


-------------------------------------------
---------selector.jwc----------------------
-------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE component-specification PUBLIC
  "-//Apache Software Foundation//Tapestry Specification 3.0//EN"
  "http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">
<!-- generated by Spindle, http://spindle.sourceforge.net -->

<component-specification class="com.imvprojects.forecast.components.RelationshipCreator"
    allow-body="no"
    allow-informal-parameters="no">
    
    <description>
      Displays a selection list based
      on a model that is passed in as 
      a parameter.  It also calls an
      appropriate listener which is also
      passed in as a parameter.
    </description>
    
    <parameter name="optionsList" 
    		   type="org.apache.tapestry.form.IPropertySelectionModel" 
    	     required="yes"
          direction="form" />

    <parameter name="selectionListener" 
    		   type="org.apache.tapestry.IActionListener" 
    	     required="yes"
          direction="in" />
    
    <property-specification name="itemToAdd" type="java.lang.Object" persistent="yes"/>
    
    <component id="addDisciplineForm" type="Form">
      <binding name="listener" expression="selectionListener"/>
    </component>
   
    <component id="unassignedDiscipline" type="PropertySelection">
    	<binding name="value" expression="itemToAdd"/>
      <binding name="model" expression="optionsList"/>
    </component>
    
</component-specification>


-------------------------------------------
---------selector.html---------------------
-------------------------------------------

<form jwcid="addDisciplineForm">
  <select jwcid="unassignedDiscipline"/>
  <input type="submit" value="Assign" />
</form>


-------------------------------------------
---------selector.java---------------------
-------------------------------------------

package com.imvprojects.forecast.components;

public abstract class RelationshipCreator extends LoginAccessor {
	public abstract void setItemToAdd(Object aProjectList);
	public abstract Object getItemToAdd();
}


-------------------------------------------
-----ListSelectionModel.java---------------
-------------------------------------------

package com.imvprojects.forecast.pages.models;

import java.util.List;

import org.apache.tapestry.form.IPropertySelectionModel;

public abstract class ListSelectionModel implements IPropertySelectionModel {
	private List theList;
	
	protected abstract String convertToString(Object object);

	public ListSelectionModel(List aList){
		theList = aList;
	}

	public int getOptionCount() {
		return theList.size();
	}

	public Object getOption(int index) {
		return theList.get(index);
	}

	public String getLabel(int index) {
		return convertToString(theList.get(index));
	}

	public String getValue(int index) {
		return Integer.toString(index);
	}

	public Object translateValue(String value) {
		return theList.get(Integer.parseInt(value));
	}

}


-------------------------------------------
-----DisciplineModel.java------------------
-------------------------------------------

package com.imvprojects.forecast.pages.models;

import java.util.List;

import com.imvprojects.forecast.data.Discipline;

public class DisciplineModel extends ListSelectionModel {

	public DisciplineModel(List aList) {
		super(aList);
	}

	protected String convertToString(Object object) {
		if (object instanceof Discipline)
			return ((Discipline)object).getDescription();
		
		throw new RuntimeException("Non-Discipline in List : DisciplineModel only accepts lists of Disciplines.");
	}

}


Sorry about that,
Filip

Hello Everyone,

I've been scratching my head over this
for a few hours now.

I am trying to create a Property selection
component that is generic.  It is generic
in the sense that you pass it a model
and a listener and it will render everything
for you.

I have included a zip file with the bare
bones code that I am attempting.  The
idea works if I instantiate and use everything
from the page file.  However, when I try to 
make a component which accepts an IActionListener
and an IPropertySelectionModel, everything renders 
correctly at first but when I select an item from 
the displayed options, I get an exception:

java.lang.NullPointerException
Stack Trace: 
*	org.apache.tapestry.form.PropertySelection.renderComponent(PropertySelection.java:94) 
*	org.apache.tapestry.AbstractComponent.render(AbstractComponent.java:857) 
*	org.apache.tapestry.AbstractComponent.renderBody(AbstractComponent.java:624) 
*	org.apache.tapestry.form.Form.renderComponent(Form.java:362) 
*	org.apache.tapestry.AbstractComponent.render(AbstractComponent.java:857) 
*	org.apache.tapestry.form.Form.rewind(Form.java:568) 
*	org.apache.tapestry.engine.RequestCycle.rewindForm(RequestCycle.java:435) 
*	org.apache.tapestry.form.Form.trigger(Form.java:582) 
*	org.apache.tapestry.engine.DirectService.service(DirectService.java:169) 
*	org.apache.tapestry.engine.AbstractEngine.service(AbstractEngine.java:872) 
*	org.apache.tapestry.ApplicationServlet.doService(ApplicationServlet.java:197) 
*	org.apache.tapestry.ApplicationServlet.doPost(ApplicationServlet.java:326) 
*	javax.servlet.http.HttpServlet.service(HttpServlet.java:709) 
*	javax.servlet.http.HttpServlet.service(HttpServlet.java:802) 
*	org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237) 
*	org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157) 
*	org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214) 
*	org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104) 
*	org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520) 
*	org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198) 
*	org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152) 
*	org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104) 
*	org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520) 
*	org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137) 
*	org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104) 
*	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118) 
*	org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102) 
*	org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520) 
*	org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 
*	org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104) 
*	org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520) 
*	org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929) 
*	org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160) 
*	org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799) 
*	org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705) 
*	org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577) 
*	org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683) 
*	java.lang.Thread.run(Thread.java:534) 

The debugger tells me my model is null after the page is submitted
which means I'm loosing my model somewhere along the way.  I've tried
making the model property in the page persistent with no avail.

I'm obviously doing something wrong because the page displays correctly
once, so all the bindings were okay and the data got to the client. 
However, I'm loosing the model after the round trip from the browser.  
Does anyone have any suggestions as to what I'm doing wrong?

Thanks!
Filip





---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org