package e4s.tutorial; import java.util.Hashtable; import e4s.application.CountryCode; import e4s.html.*; import e4s.html.input.extended.E4InputFieldName; import e4s.html.input.extended.LONGFIELD; import e4s.html.input.grid.E4Grid; import e4s.html.input.grid.E4GridSize; import e4s.servlet.E4ModuleImplementation; /** * A grid example (editing several rows/columns of data). * * This example shall demonstrate a grid which holds data for each * country (vertically) within 2 years (horizontally). This could * be some comercial data about sales figures. What we what we want to * do here is a input of this data (but it would be too huge to be * edited on one screen completley, so we use the Grid element). * * {@tutorial Example_Grid} */ public class Example_Grid extends E4ModuleImplementation { public static E4Method start = null; /** * This is the class providing data and painting on the screen. . */ public class MyData extends E4Grid { /** * Hold the actuals but changed values */ private Hashtable m_hash = new Hashtable(); public MyData() { // create a grid with 2 label cells each for vertical and horizontal headers and use method refresh for super(2,2); // set captions to be displayed super.setCaption_vertical("Country"); super.setCaption_horizontal("Period"); // define some alternative sizes the user can choose from super.addDisplaySize(new E4GridSize(25,12)); super.addDisplaySize(new E4GridSize(15,15)); super.addDisplaySize(new E4GridSize(20,20)); super.addDisplaySize(new E4GridSize(30,30)); //populate some random test data for( int col = 0; col < horizontalSize(); col++ ) for( int row = 0; row < verticalSize(); row++ ) { int rnd = (int)(Math.random() * 1000.0f); setData(Integer.toString(rnd),row,col); } } /* * Get the data associated with row and col. * Typically this could be a database selection, but in this case data is stored in a Hashtable. */ private String getData( int row, int col ) { String key = Integer.toString(row) + "/" + Integer.toString(col); return (String)m_hash.get(key); } /* * set the data associated with row and col. * Typically this could be a database update, but in this case data is stored in a Hashtable. */ private void setData( String data, int row, int col ) { String key = Integer.toString(row) + "/" + Integer.toString(col); m_hash.remove(key); if (data != null) m_hash.put(key,data); } /** * This are 24 month. * @return 24. */ public int horizontalSize() { return 24; } /** * Draw one header for the horizontal row. * @param td contains an TD element for each header-column specified on construction (we expect 2 here) * @param row the actual row */ public void drawHorizontalHeader_Screen( TD td[], int row) throws Exception { CountryCode.COUNTRY[] cc = CountryCode.getCountries(); td[0].print(cc[row].getCode()); td[1].NOBR().print(cc[row].getName()); } /** * This are all countries. * @return the size of the country array. */ public int verticalSize() { CountryCode.COUNTRY[] cc = CountryCode.getCountries(); return cc.length; } /** * Draw one header for the vertical row. * @param tr contains an TR element for each header-row (we expect 2 here) * @param row_from the first actual row * @param row_end the last actual row (including) */ public void drawVerticalHeaders_Screen(TR tr[], int col_start, int col_end) throws Exception { final String LABELS[] = { "Jan","Feb","Mar","Apr","Mai","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; for( int col = col_start; col <= col_end; col++ ) { int month = col % 12; int year = 2007 + col / 12; tr[0].TD().print(year); tr[1].TD().print(LABELS[month]); } } /** * Draw a cell on the screen: add some input field (without any validation in this example). */ public void drawCell_Screen(TD td, E4InputFieldName fieldname, int row, int col) throws Exception { LONGFIELD field = new LONGFIELD(fieldname,(String)null,3); field.setValue(getData(row,col)); td.addElement(field); } /** * Draw a data cell in printing mode. */ public void drawCell_Printer(TD td, int row, int col) throws Exception { td.print(getData(row,col)); td.setAlign(Align.RIGHT); } /** * Save the updated value, stored in the params and named by fieldname, associated by row/col. */ public boolean saveValue(E4InputFieldName fieldname, E4CgiParams params, int row, int col) throws Exception { String value = params.get(fieldname); setData(value,row,col); return true; } } /** * This is our start method, it will be initially called. */ public void start( HTML html, E4CgiParams params ) throws Exception { // create the data object MyData data = new MyData(); // save it for later usage as session persistant object setSessionObject(data); // add the data element to the body data.add2body(html.BODY(),getServlet()); } }