KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > scroller > Scroller


1 package com.tonbeller.wcf.scroller;
2
3 import java.io.IOException JavaDoc;
4
5 import javax.servlet.jsp.JspWriter JavaDoc;
6
7 import com.tonbeller.wcf.controller.RequestContext;
8
9 /**
10  * Generates hidden input fields that are required to keep scrolling position.
11  * Provides static methods to enable scrolling.
12  */

13 public class Scroller {
14
15   private static String JavaDoc SCROLLER_KEY = Scroller.class.getName();
16   public static final String JavaDoc FORCE_SCROLLER = "forcescroller";
17
18   public void handleRequest(RequestContext context, JspWriter JavaDoc out) throws IOException JavaDoc {
19     // Aktuelle Scrollkoordinaten als versteckte Formparameter setzen
20
writeCoordParam(context, out, "wcfXCoord");
21     writeCoordParam(context, out, "wcfYCoord");
22   }
23
24   private void writeCoordParam(RequestContext context, JspWriter JavaDoc out, String JavaDoc coordName) throws IOException JavaDoc {
25     String JavaDoc coordVal = "0";
26     if(isScrollerEnabled(context)) {
27         String JavaDoc val = context.getParameter(coordName);
28         if (val != null)
29           coordVal = val;
30     }
31     out.print("<input type=\"hidden\" name=\""+coordName+"\" value=\""+coordVal+"\"/>");
32   }
33   
34   /**
35    * enables/disables scrolling for this request. Typically a request handler should
36    * decide whether the current position should be kept or not. A tree node
37    * expansion handler would want to keep the position a dialog close button
38    * handler not.
39    * <p>
40    * By default scrolling is disabled
41    */

42   public static void enableScroller(RequestContext context) {
43     if(isScrollerEnabled(context))
44       return;
45     context.getRequest().setAttribute(SCROLLER_KEY, "true");
46   }
47   
48   public static boolean isScrollerEnabled(RequestContext context) {
49     // set by url?
50
if(context.getParameter(FORCE_SCROLLER)!=null)
51       return true;
52     
53     // set by request listener?
54
return context.getRequest().getAttribute(SCROLLER_KEY)!=null;
55   }
56 }
57
Popular Tags