KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > plaf > css > PageScrollerCG


1 /*
2  * Copyright (c) 2005 Your Corporation. All Rights Reserved.
3  */

4 package org.wings.plaf.css;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.wings.*;
9 import org.wings.io.Device;
10
11 import java.io.IOException JavaDoc;
12
13 /**
14  * CG for a pagescroller.
15  *
16  * @author holger
17  */

18 public class PageScrollerCG extends AbstractComponentCG implements org.wings.plaf.PageScrollerCG {
19     private final static transient Log log = LogFactory.getLog(PageScrollerCG.class);
20
21     public static final int FORWARD = 0;
22     public static final int BACKWARD = 1;
23     public static final int FORWARD_BLOCK = 2;
24     public static final int BACKWARD_BLOCK = 3;
25     public static final int FIRST = 4;
26     public static final int LAST = 5;
27     private final static SIcon[][][] DEFAULT_ICONS = new SIcon[2][6][2];
28
29     static {
30         String JavaDoc[] postfixes = new String JavaDoc[6];
31         String JavaDoc[] prefixes = new String JavaDoc[6];
32         for (int orientation = 0; orientation < 2; orientation++) {
33             prefixes[BACKWARD] = "";
34             prefixes[FORWARD] = "";
35             prefixes[FIRST] = "Margin";
36             prefixes[LAST] = "Margin";
37             prefixes[FORWARD_BLOCK] = "Block";
38             prefixes[BACKWARD_BLOCK] = "Block";
39             if (orientation == SConstants.VERTICAL) {
40                 postfixes[BACKWARD] = "Up";
41                 postfixes[FORWARD] = "Down";
42                 postfixes[FIRST] = "Up";
43                 postfixes[LAST] = "Down";
44                 postfixes[BACKWARD_BLOCK] = "Up";
45                 postfixes[FORWARD_BLOCK] = "Down";
46             } else {
47                 postfixes[BACKWARD] = "Left";
48                 postfixes[FORWARD] = "Right";
49                 postfixes[FIRST] = "Left";
50                 postfixes[LAST] = "Right";
51                 postfixes[BACKWARD_BLOCK] = "Left";
52                 postfixes[FORWARD_BLOCK] = "Right";
53             }
54
55             for (int direction = 0; direction < postfixes.length; direction++) {
56                 DEFAULT_ICONS[orientation][direction][0] =
57                         new SResourceIcon("org/wings/icons/"
58                         + prefixes[direction]
59                         + "Scroll"
60                         + postfixes[direction] + ".gif");
61                 DEFAULT_ICONS[orientation][direction][1] =
62                         new SResourceIcon("org/wings/icons/Disabled"
63                         + prefixes[direction]
64                         + "Scroll"
65                         + postfixes[direction] + ".gif");
66             }
67         }
68     }
69
70     public void writeContent(Device d, SComponent c)
71             throws IOException JavaDoc {
72         log.debug("write = " + c);
73         SPageScroller sb = (SPageScroller) c;
74
75         if (sb.getLayoutMode() == SConstants.VERTICAL)
76             writeVerticalPageScroller(d, sb);
77         else
78             writeHorizontalPageScroller(d, sb);
79     }
80
81     private void writeVerticalPageScroller(Device d, SPageScroller sb) throws IOException JavaDoc {
82         int value = sb.getValue();
83         int extent = sb.getExtent();
84         int minimum = sb.getMinimum();
85         int maximum = sb.getMaximum();
86         boolean backEnabled = value > minimum;
87         boolean forwardEnabled = value < maximum - extent;
88
89         d.print("<table orientation=\"vertical\" class=\"SLayout\"><tbody>\n")
90                 .print("<tr height=\"1%\">\n")
91                 .print("<td height=\"1%\" class=\"SLayout\"><table area=\"buttons\" class=\"SLayout\"><tbody>\n");
92
93         d.print("<tr><td class=\"SLayout\">");
94         writeButton(d, sb, DEFAULT_ICONS[SConstants.VERTICAL][FIRST][0], "" + minimum);
95         d.print("</td></tr>\n");
96         d.print("<tr><td class=\"SLayout\">");
97         writeButton(d, sb, DEFAULT_ICONS[SConstants.VERTICAL][BACKWARD][0], "" + (value - extent));
98         d.print("</td></tr>\n");
99
100         d.print("</tbody></table></td>\n")
101                 .print("</tr>\n")
102                 .print("<tr height=\"100%\">\n")
103                 .print("<td class=\"SLayout\"><table area=\"pages\" height=\"100%\" class=\"SLayout\"><tbody>\n");
104
105         int firstDirectPage = sb.getCurrentPage() - (sb.getDirectPages() - 1) / 2;
106         firstDirectPage = Math.min(firstDirectPage, sb.getPageCount() - sb.getDirectPages());
107         firstDirectPage = Math.max(firstDirectPage, 0);
108
109         for (int i = 0; i < Math.min(sb.getDirectPages(), sb.getPageCount() - firstDirectPage); i++) {
110             int page = firstDirectPage + i;
111             d.print("<tr><td class=\"SLayout\">");
112             writePage(d, sb, page);
113             d.print("</td></tr>\n");
114         }
115
116         d.print("</tbody></table></td>\n")
117                 .print("</tr>\n")
118                 .print("<tr height=\"1%\">\n")
119                 .print("<td height=\"1%\" class=\"SLayout\"><table area=\"buttons\" class=\"SLayout\"><tbody>\n");
120
121         d.print("<tr><td class=\"SLayout\">");
122         writeButton(d, sb, DEFAULT_ICONS[SConstants.VERTICAL][FORWARD][0], "" + (value + extent));
123         d.print("</td></tr>\n");
124         d.print("<tr><td class=\"SLayout\">");
125         writeButton(d, sb, DEFAULT_ICONS[SConstants.VERTICAL][LAST][0], "" + (maximum + 1 - extent));
126         d.print("</td></tr>\n");
127
128         d.print("</tbody></table></td>\n")
129                 .print("</tr>\n")
130                 .print("</tbody></table>");
131     }
132
133     private void verticalArea(Device d, String JavaDoc s, int v) throws IOException JavaDoc {
134         d.print("<tr><td class=\"SLayout\" style=\"background-color: ");
135         d.print(s);
136         d.print("\" height=\"");
137         d.print(v + "%");
138         d.print("\"></td></tr>\n");
139     }
140
141     private void writeHorizontalPageScroller(Device d, SPageScroller sb) throws IOException JavaDoc {
142         int value = sb.getValue();
143         int extent = sb.getExtent();
144         int minimum = sb.getMinimum();
145         int maximum = sb.getMaximum();
146         boolean backEnabled = value > minimum;
147         boolean forwardEnabled = value < maximum - extent;
148
149         d.print("<table class=\"SLayout\" orientation=\"horizontal\"><tbody><tr>\n")
150                 .print("<td class=\"SLayout\" width=\"1%\"><table area=\"buttons\"><tbody><tr>\n");
151
152         d.print("<td class=\"SLayout\">");
153         writeButton(d, sb, DEFAULT_ICONS[SConstants.HORIZONTAL][FIRST][0], "" + minimum);
154         d.print("</td>\n");
155         d.print("<td class=\"SLayout\">");
156         writeButton(d, sb, DEFAULT_ICONS[SConstants.HORIZONTAL][BACKWARD][0], "" + (value - extent));
157         d.print("</td>\n");
158
159         d.print("</tr></tbody></table></td>\n")
160                 .print("<td class=\"SLayout\" width=\"100%\"><table area=\"pages\" width=\"100%\"><tbody><tr>\n");
161
162         int firstDirectPage = sb.getCurrentPage() - (sb.getDirectPages() - 1) / 2;
163         firstDirectPage = Math.min(firstDirectPage, sb.getPageCount() - sb.getDirectPages());
164         firstDirectPage = Math.max(firstDirectPage, 0);
165
166         for (int i = 0; i < Math.min(sb.getDirectPages(), sb.getPageCount() - firstDirectPage); i++) {
167             int page = firstDirectPage + i;
168             d.print("<td class=\"SLayout\">");
169             writePage(d, sb, page);
170             d.print("</td>\n");
171         }
172
173         d.print("</tr></tbody></table></td>\n")
174                 .print("<td class=\"SLayout\" width=\"1%\"><table area=\"buttons\"><tbody><tr>\n");
175
176         d.print("<td class=\"SLayout\">");
177         writeButton(d, sb, DEFAULT_ICONS[SConstants.HORIZONTAL][FORWARD][0], "" + (value + extent));
178         d.print("</td>\n");
179         d.print("<td class=\"SLayout\">");
180         writeButton(d, sb, DEFAULT_ICONS[SConstants.HORIZONTAL][LAST][0], "" + (maximum + 1 - extent));
181         d.print("</td>\n");
182
183         d.print("</tr></tbody></table></td>\n")
184                 .print("</tr></tbody></table>");
185     }
186
187     private void writePage(Device device, SPageScroller pageScroller, int page) throws IOException JavaDoc {
188         boolean childSelectorWorkaround = !pageScroller.getSession().getUserAgent().supportsCssChildSelector();
189         boolean showAsFormComponent = pageScroller.getShowAsFormComponent();
190
191         if (showAsFormComponent) {
192             writeButtonStart(device, pageScroller, String.valueOf(page * pageScroller.getExtent()));
193             device.print(" type=\"submit\" name=\"")
194                     .print(Utils.event(pageScroller))
195                     .print("\" value=\"")
196                     .print(String.valueOf(page * pageScroller.getExtent()))
197                     .print("\"");
198         } else {
199             device.print("<a HREF=\"")
200                     .print(pageScroller.getRequestURL()
201                     .addParameter(Utils.event(pageScroller) + "=" + page * pageScroller.getExtent()).toString())
202                     .print("\"");
203         }
204         device.print(">");
205
206         device.print(Integer.toString(page + 1));
207
208         if (showAsFormComponent)
209             device.print("</button>\n");
210         else
211             device.print("</a>\n");
212     }
213
214     private void writeButton(Device device, SPageScroller pageScroller, SIcon icon, String JavaDoc event) throws IOException JavaDoc {
215         boolean childSelectorWorkaround = !pageScroller.getSession().getUserAgent().supportsCssChildSelector();
216         boolean showAsFormComponent = pageScroller.getShowAsFormComponent();
217
218         if (showAsFormComponent) {
219             writeButtonStart(device, pageScroller, event);
220             device.print(" type=\"submit\" name=\"")
221                     .print(Utils.event(pageScroller))
222                     .print("\" value=\"")
223                     .print(event)
224                     .print("\"");
225         } else {
226             device.print("<a HREF=\"")
227                     .print(pageScroller.getRequestURL()
228                     .addParameter(Utils.event(pageScroller) + "=" + event).toString())
229                     .print("\"");
230         }
231         device.print(">");
232
233         device.print("<img");
234         Utils.optAttribute(device, "src", icon.getURL());
235         Utils.optAttribute(device, "width", icon.getIconWidth());
236         Utils.optAttribute(device, "height", icon.getIconHeight());
237         device.print(" alt=\"");
238         device.print(icon.getIconTitle());
239         device.print("\"/>");
240
241         if (showAsFormComponent)
242             device.print("</button>\n");
243         else
244             device.print("</a>\n");
245     }
246
247     /* (non-Javadoc)
248      * @see org.wings.plaf.css.ScrollBarCG#writeButtonStart(org.wings.io.Device, org.wings.SScrollBar, java.lang.String)
249      */

250     protected void writeButtonStart(Device device, SPageScroller pageScroller, String JavaDoc event) throws IOException JavaDoc {
251         device.print("<button");
252     }
253 }
254
Popular Tags