Source code for /src/dynamic_section.jsp.txt

//======================================================================//
// Author: Mark Qian <markqian@hotmail.com>                             //
// WWW: http://www.coolshare.com                                        //
// Copyright (c) 2006, Mark Qian                                        //
//                                                                      //
// You must contact Mark Qian to get a permission of use                //
// in case you want to make any use of the codes except viewing it     //
// on Mark's site.                                                      //
//======================================================================//


<%@ page import="com.coolshare.remotescript.common.Util" %>
<%@ page import="java.lang.StringBuffer"%>
<%
String sectionName2 = request.getParameter("sName");
boolean isInclude = sectionName2==null;
sectionName2 = sectionName2==null?"A":sectionName2;
String sectionNumber = request.getParameter("sNumber");
sectionNumber = sectionNumber==null?"0":sectionNumber;
String nextSectionName = Util.getNextSection(sectionName2);
String pName = request.getParameter("pName");
pName = pName==null?"":" depending on the key field in section "+pName;
String numStr = (String)request.getSession().getAttribute(sectionName2+"_"+sectionNumber);
int numInput;
if (numStr==null) {
	numInput = (int)(Math.random()*9+1);
	request.getSession().setAttribute(sectionName2+"_"+sectionNumber, ""+numInput);
} else {
	numInput = Integer.parseInt(numStr);
}
String cId = request.getParameter("cId");

String a = request.getParameter("a");
if (a==null)
  a = (String)request.getAttribute("a");
a=a==null?"AJAX":a;

boolean isTable = numInput>2 && numInput<6;  
String colspan = " colspan=5";
if (isTable) 
	colspan = "";
StringBuffer sb = new StringBuffer();
sb.append("<br><hr><h3>This is section "+sectionName2 +"_"+sectionNumber +pName +"</h3>\n");
sb.append("<table><tr><th>Key Field</th><td"+colspan +"><select id=\""+sectionName2 +"_"+sectionNumber +"\" onchange=\"doChange(this)\" style=\"width:400\">\n");
sb.append("<option value=\"\">Select the next depending section</option>\n");
for (int i=0; i<20; i++) {
  sb.append("  <option value=\""+nextSectionName + "|" + i + "\">" + nextSectionName +"_"+i +"</option>\n");
}
sb.append("</select></td></tr>\n");
sb.append("<tr>\n");
if (isTable) {
  sb.append("<td colspan=2><table><tr>\n");
  for (int i=0; i<numInput; i++) {
    sb.append("  <th>Column "+i +"</th>\n");
  }
  sb.append("</tr><tr>\n");

  for (int k=0; k<numInput; k++) {
    for (int i=0; i<numInput; i++) {
      String vv = "";
      if (Math.random()*100>30)
    	  vv = ""+Math.random()*100;
      sb.append("    <td><input type=text value=\""+ vv +"\"></td>\n");
    }    

    sb.append("   </tr><tr>\n");
  }

  sb.append("</tr></table></td>\n");
} else {
  int j=0; 
  for (int i=0; i<numInput; i++) {
    sb.append("  <td>Field_"+i +"</td><td><input id =\"Field_"+sectionName2 +"|"+sectionNumber +"\" value=\""+Math.random()*100 +"\"></td>\n");
    if (++j>3) {
	  j = 0;
      sb.append("  </tr><tr>\n"); 
    }
  }
}
sb.append("</tr></table>\n");
String res = sb.toString();

if (a.equals("AJAX") || isInclude) {
	  %>
	  <%=res%>
	  <%
	  } else if (a.equals("DS")) {
	  %>
	  var comm = cm.findCommunicator('<%=cId %>');
	  comm.res = unescape("<%=Util.doEncode(res)%>");
	  comm.process();
	  <%
	  } else if (a.equals("Iframe")) {
	  	%>
	  	<html>
	  	  <script>
	  	  function doOnload() {
	  	    try {
	  		  var comm = parent.cm.findCommunicator('<%=cId %>');
	  		  comm.res = unescape("<%=Util.doEncode(res)%>");
	  		  comm.process(window);
	  		  //window.location.replace('about:blank'); 
	  		} catch (e) {
	  		  var msg = e.description?e.description:e;
	            alert(msg);
	            if (msg=="Permission denied")
	              alert("Sorry, I, the code from RemoteScript.com, can not close the progress dialog\nsince it is popped up codes from coolshare.com .");
	          }
	  	  }
	  	</script>
	  	<Body onload="doOnload()">
	  	</Body>
	  	</html>
	  <%
	  }
	  else if (a.equals("Cookie")) {
	    Cookie setCookie = new Cookie("RSLite", Util.doEncode(res));
	    response.addCookie(setCookie);	
	  }
	  else if (a.equals("Frame")) {
	  	%>
	  	 <html>
	    <script>
	    function doOnload() {
	      try {
	  	  var comm = top.edit.cm.findCommunicator('<%=cId %>');
	  	  comm.res = unescape("<%=Util.doEncode(res)%>");
	  	  comm.process(window);
	  	  //window.location.replace('about:blank'); 
	      } catch (e) {
	         alert(e.description?e.description:e);
	         alert("Sorry, I, the code from RemoteScript.com, can not close the progress dialog\nsince it is popped up codes from coolshare.com .");
	      }
	    }
	  </script>
	  <Body onload="doOnload()">
	  </Body>
	  </html>
	  <%
	  	}
	%>