package com.ngMAT.appl.genericservlet; import java.util.Hashtable; import java.util.Vector; import com.ngMAT.HTML.Anchor; import com.ngMAT.HTML.HTMLComponent; import com.ngMAT.HTML.HTMLComponentVector; import com.ngMAT.HTML.HTMLSpace; import com.ngMAT.HTML.HTMLTemplate; import com.ngMAT.HTML.InsertionTag; import com.ngMAT.HTML.ParsedTable; public abstract class PartitionedListServlet extends com.ngMAT.appl.genericservlet.GenericServlet { public HTMLComponent process (GenericServletContext context) { PartitionedListServletConfig config = (PartitionedListServletConfig)context.getConfig(); HTMLTemplate template = context.getTemplate (config.getTemplateName (context)); ParsedTable pt = (ParsedTable)template.getParsedTable (config.getListName()); PartitionedData sd = (PartitionedData)context.getSessionValue (config.getSessionDataName (context)); if (sd == null) { sd = new PartitionedData (context, pt); sd.refresh_required = true; } _processCommand (context, template, sd); if (sd.refresh_required) { String _lpp = pt.getParameter ("lines_per_page"); if (_lpp != null) sd.lines_per_page = Integer.parseInt (_lpp); refreshData (context, sd); sd.refresh(); sd.refresh_required = false; context.setSessionValue (config.getSessionDataName (context), sd); } if (sd.page_top_index >= sd.data.size()) { sd.page_top_index = Math.max (sd.data.size() - sd.lines_per_page, 0); } else if (sd.page_top_index < 0) sd.page_top_index = 0; _processSurrounding (context, sd, template); int index = sd.page_top_index; for (int i = 0; i < sd.lines_per_page && index < sd.data.size(); i++, index++) { HTMLTemplate row = pt.getRowTemplate(); processRowFilling (context, sd, index, row); pt.addRow (row); } return template; } private void _processCommand (GenericServletContext context, HTMLTemplate template, PartitionedData sd) { String command = context.getParameter ("command"); if (command == null) return; if (command.equalsIgnoreCase ("refresh")) sd.refresh_required = true; else if (command.equalsIgnoreCase ("prev")) sd.page_top_index -= sd.lines_per_page; else if (command.equalsIgnoreCase ("next")) sd.page_top_index += sd.lines_per_page; else if (command.equalsIgnoreCase ("jump")) processJump (context, template, sd); else processCommand (context, template, sd); } private void processJump (GenericServletContext context, HTMLTemplate template, PartitionedData sd) { int page = context.getIntParameter ("page", 1) - 1; sd.page_top_index = sd.lines_per_page * page; } public void processCommand (GenericServletContext context, HTMLTemplate template, PartitionedData sd) { } public void _processSurrounding (GenericServletContext context, PartitionedData data, HTMLTemplate template) { PartitionedListServletConfig config = (PartitionedListServletConfig)context.getConfig(); InsertionTag tag = template.getInsertionTag (config.getListName() + ".pages"); if (tag != null) { tag.setInsertedComponent (createPageJumpAnchors (context, data)); } processSurrounding (context, data, template); } private HTMLComponent createPageJumpAnchors (GenericServletContext context, PartitionedData data) { PartitionedListServletConfig config = (PartitionedListServletConfig)context.getConfig(); int max_page_number = data.data.size() / data.lines_per_page; if (data.lines_per_page * max_page_number < data.data.size()) max_page_number++; HTMLComponentVector hcv = new HTMLComponentVector(); String _uri = config.getUriForHere (context); _uri += (_uri.indexOf ("?") < 0 ? "?" : "&") + "command=jump&page="; HTMLSpace spc = new HTMLSpace (2); for (int i = 1; i <= max_page_number; i++) { if (i > 1) hcv.addElement (spc); Anchor a = new Anchor (_uri + i, "" + i); hcv.addElement (a); } return hcv; } public abstract void refreshData (GenericServletContext context, PartitionedData data); public abstract void processSurrounding (GenericServletContext context, PartitionedData data, HTMLTemplate template); public abstract void processRowFilling (GenericServletContext context, PartitionedData data, int index, HTMLTemplate row); public class PartitionedData { public Vector data; public int lines_per_page = 10; public int page_top_index = 0; public boolean refresh_required = false; public Hashtable optional_data = new Hashtable(); private PartitionedData (GenericServletContext context, ParsedTable pt) { PartitionedListServletConfig config = (PartitionedListServletConfig)context.getConfig(); String s = pt.getParameter ("maxrows"); if (s != null) lines_per_page = Integer.parseInt (s); else lines_per_page = config.getDefaultLinesPerPage (context); } public void refresh() { page_top_index = 0; } } }