001: package org.wingx.plaf.css;
002:
003: import org.wings.plaf.css.AbstractComponentCG;
004: import org.wings.plaf.css.Utils;
005: import org.wings.*;
006: import org.wings.io.Device;
007: import org.apache.commons.logging.Log;
008: import org.apache.commons.logging.LogFactory;
009:
010: import java.io.IOException;
011:
012: /**
013: * CG for a pagescroller.
014: *
015: * @author holger
016: */
017: public final class XPageScrollerCG extends AbstractComponentCG
018: implements org.wings.plaf.PageScrollerCG {
019: private static final long serialVersionUID = 1L;
020:
021: private final static Log log = LogFactory
022: .getLog(org.wingx.plaf.css.XPageScrollerCG.class);
023:
024: public static final int FORWARD = 0;
025: public static final int BACKWARD = 1;
026: public static final int FORWARD_BLOCK = 2;
027: public static final int BACKWARD_BLOCK = 3;
028: public static final int FIRST = 4;
029: public static final int LAST = 5;
030: private final static SIcon[][][] DEFAULT_ICONS = new SIcon[2][6][2];
031: public static SIcon TRANSPARENT_ICON = new SResourceIcon(
032: "org/wings/icons/transdot.gif");
033:
034: static {
035: String[] postfixes = new String[6];
036: String[] prefixes = new String[6];
037: for (int orientation = 0; orientation < 2; orientation++) {
038: prefixes[org.wingx.plaf.css.XPageScrollerCG.BACKWARD] = "navigate_";
039: prefixes[org.wingx.plaf.css.XPageScrollerCG.FORWARD] = "navigate_";
040: prefixes[org.wingx.plaf.css.XPageScrollerCG.FIRST] = "navigate_";
041: prefixes[org.wingx.plaf.css.XPageScrollerCG.LAST] = "navigate_";
042: prefixes[org.wingx.plaf.css.XPageScrollerCG.FORWARD_BLOCK] = "navigate_";
043: prefixes[org.wingx.plaf.css.XPageScrollerCG.BACKWARD_BLOCK] = "navigate_";
044: if (orientation == SConstants.VERTICAL) {
045: postfixes[org.wingx.plaf.css.XPageScrollerCG.BACKWARD] = "up";
046: postfixes[org.wingx.plaf.css.XPageScrollerCG.FORWARD] = "down";
047: postfixes[org.wingx.plaf.css.XPageScrollerCG.FIRST] = "top";
048: postfixes[org.wingx.plaf.css.XPageScrollerCG.LAST] = "bottom";
049: postfixes[org.wingx.plaf.css.XPageScrollerCG.BACKWARD_BLOCK] = "up2";
050: postfixes[org.wingx.plaf.css.XPageScrollerCG.FORWARD_BLOCK] = "down2";
051: } else {
052: postfixes[org.wingx.plaf.css.XPageScrollerCG.BACKWARD] = "left";
053: postfixes[org.wingx.plaf.css.XPageScrollerCG.FORWARD] = "right";
054: postfixes[org.wingx.plaf.css.XPageScrollerCG.FIRST] = "beginning";
055: postfixes[org.wingx.plaf.css.XPageScrollerCG.LAST] = "end";
056: postfixes[org.wingx.plaf.css.XPageScrollerCG.BACKWARD_BLOCK] = "left2";
057: postfixes[org.wingx.plaf.css.XPageScrollerCG.FORWARD_BLOCK] = "right2";
058: }
059:
060: for (int direction = 0; direction < postfixes.length; direction++) {
061: org.wingx.plaf.css.XPageScrollerCG.DEFAULT_ICONS[orientation][direction][0] = new SResourceIcon(
062: "org/wingx/table/images/" + prefixes[direction]
063: + postfixes[direction] + ".png");
064: org.wingx.plaf.css.XPageScrollerCG.DEFAULT_ICONS[orientation][direction][1] = new SResourceIcon(
065: "org/wingx/table/images/" + prefixes[direction]
066: + "disabled_" + postfixes[direction]
067: + ".png");
068: }
069: }
070: }
071:
072: public void writeInternal(Device d, SComponent c)
073: throws IOException {
074: SPageScroller sb = (SPageScroller) c;
075:
076: String style = sb.getStyle();
077: if (sb.getLayoutMode() == SConstants.VERTICAL) {
078: sb.setStyle(style + " SPageScroller_vertical");
079: writeVerticalPageScroller(d, sb);
080: } else {
081: sb.setStyle(style + " SPageScroller_horizontal");
082: writeHorizontalPageScroller(d, sb);
083: }
084: sb.setStyle(style);
085: }
086:
087: private void writeVerticalPageScroller(Device d, SPageScroller sb)
088: throws IOException {
089: int value = sb.getValue();
090: int extent = sb.getExtent();
091: int minimum = sb.getMinimum();
092: int maximum = sb.getMaximum();
093: boolean backEnabled = value > minimum;
094: boolean forwardEnabled = value < maximum - extent;
095: boolean firstPage = (value == minimum);
096: boolean lastPage = (value >= (maximum - extent));
097: boolean msie = isMSIE(sb);
098:
099: d.print("<table");
100: Utils.writeAllAttributes(d, sb);
101: Utils.writeEvents(d, sb, null);
102: d.print("><tbody><tr height=\"1%\">").print(
103: "<td height=\"1%\"><table class=\"buttons\"><tbody>");
104:
105: d.print("<tr><td");
106: Utils.printClickability(d, sb, "" + minimum, !firstPage, sb
107: .getShowAsFormComponent());
108: d.print(">");
109: writeIcon(
110: d,
111: XPageScrollerCG.DEFAULT_ICONS[SConstants.VERTICAL][XPageScrollerCG.FIRST][0],
112: msie);
113: d.print("</td></tr>");
114:
115: d.print("<tr><td");
116: Utils.printClickability(d, sb, "" + (value - extent),
117: backEnabled, sb.getShowAsFormComponent());
118: d.print(">");
119: writeIcon(
120: d,
121: XPageScrollerCG.DEFAULT_ICONS[SConstants.VERTICAL][XPageScrollerCG.BACKWARD][0],
122: msie);
123: d.print("</td></tr>");
124:
125: d.print("</tbody></table></td>").print("</tr>").print("<tr>")
126: .print("<td><table class=\"pages\"><tbody>");
127:
128: int firstDirectPage = sb.getCurrentPage()
129: - (sb.getDirectPages() - 1) / 2;
130: firstDirectPage = Math.min(firstDirectPage, sb.getPageCount()
131: - sb.getDirectPages());
132: firstDirectPage = Math.max(firstDirectPage, 0);
133:
134: for (int i = 0; i < Math.min(sb.getDirectPages(), sb
135: .getPageCount()
136: - firstDirectPage); i++) {
137: int page = firstDirectPage + i;
138: d.print("<tr><td");
139: boolean isCurrentPage = (sb.getCurrentPage() == page);
140: Utils.optAttribute(d, "class",
141: isCurrentPage ? "page_selected" : null);
142: Utils.printClickability(d, sb, String.valueOf(page
143: * sb.getExtent()), !isCurrentPage, sb
144: .getShowAsFormComponent());
145: d.print(">");
146: d.print(Integer.toString(page + 1));
147: d.print("</td></tr>");
148: }
149:
150: d.print("</tbody></table></td>").print("</tr>").print(
151: "<tr height=\"1%\">").print(
152: "<td height=\"1%\"><table class=\"buttons\"><tbody>");
153:
154: d.print("<tr><td");
155: Utils.printClickability(d, sb, "" + (value + extent),
156: forwardEnabled, sb.getShowAsFormComponent());
157: d.print(">");
158: writeIcon(
159: d,
160: XPageScrollerCG.DEFAULT_ICONS[SConstants.VERTICAL][XPageScrollerCG.FORWARD][0],
161: msie);
162: d.print("</td></tr>");
163:
164: d.print("<tr><td");
165: Utils.printClickability(d, sb, "" + (maximum + 1 - extent),
166: !lastPage, sb.getShowAsFormComponent());
167: d.print(">");
168: writeIcon(
169: d,
170: XPageScrollerCG.DEFAULT_ICONS[SConstants.VERTICAL][XPageScrollerCG.LAST][0],
171: msie);
172: d.print("</td></tr>");
173:
174: d.print("</tbody></table></td>").print("</tr>").print(
175: "</tbody></table>");
176: }
177:
178: private void writeHorizontalPageScroller(Device d, SPageScroller sb)
179: throws IOException {
180: int value = sb.getValue();
181: int extent = sb.getExtent();
182: int minimum = sb.getMinimum();
183: int maximum = sb.getMaximum();
184: boolean backEnabled = value > minimum;
185: boolean forwardEnabled = value < maximum - extent;
186: boolean firstPage = (value == minimum);
187: boolean lastPage = (value >= (maximum - extent));
188: boolean msie = isMSIE(sb);
189:
190: d.print("<table");
191: Utils.writeAllAttributes(d, sb);
192: Utils.writeEvents(d, sb, null);
193: d.print("><tbody><tr>").print(
194: "<td><table class=\"buttons\"><tbody><tr>");
195:
196: d.print("<td valign=\"top\"");
197: SIcon icon3 = XPageScrollerCG.DEFAULT_ICONS[SConstants.HORIZONTAL][XPageScrollerCG.FIRST][firstPage ? 1
198: : 0];
199: Utils.printClickability(d, sb, "" + minimum, !firstPage, sb
200: .getShowAsFormComponent());
201: d.print(">");
202: writeIcon(d, icon3, msie);
203: d.print("</td>\n");
204:
205: d.print("<td valign=\"top\"");
206: SIcon icon2 = XPageScrollerCG.DEFAULT_ICONS[SConstants.HORIZONTAL][XPageScrollerCG.BACKWARD][firstPage ? 1
207: : 0];
208: Utils.printClickability(d, sb, "" + (value - extent),
209: backEnabled, sb.getShowAsFormComponent());
210: d.print(">");
211: writeIcon(d, icon2, msie);
212: d.print("</td>\n");
213:
214: d.print("</tr></tbody></table></td>").print(
215: "<td><table class=\"pages\"><tbody><tr>");
216:
217: int firstDirectPage = sb.getCurrentPage()
218: - (sb.getDirectPages() - 1) / 2;
219: firstDirectPage = Math.min(firstDirectPage, sb.getPageCount()
220: - sb.getDirectPages());
221: firstDirectPage = Math.max(firstDirectPage, 0);
222:
223: int count = Math.min(sb.getDirectPages(), sb.getPageCount()
224: - firstDirectPage);
225: for (int i = 0; i < count; i++) {
226: int page = firstDirectPage + i;
227: d.print("<td valign=\"top\"");
228: boolean isCurrentPage = (sb.getCurrentPage() == page);
229: Utils.optAttribute(d, "class",
230: isCurrentPage ? "page_selected" : null);
231: Utils.printClickability(d, sb, String.valueOf(page
232: * sb.getExtent()), !isCurrentPage, sb
233: .getShowAsFormComponent());
234: d.print(">");
235: d.print(Integer.toString(page + 1));
236: d.print("</td>");
237: }
238:
239: d.print("</tr></tbody></table></td>").print(
240: "<td><table class=\"buttons\"><tbody><tr>");
241:
242: d.print("<td valign=\"top\"");
243: SIcon icon1 = XPageScrollerCG.DEFAULT_ICONS[SConstants.HORIZONTAL][XPageScrollerCG.FORWARD][lastPage ? 1
244: : 0];
245: Utils.printClickability(d, sb, "" + (value + extent),
246: forwardEnabled, sb.getShowAsFormComponent());
247: d.print(">");
248: writeIcon(d, icon1, msie);
249: d.print("</td>\n");
250:
251: d.print("<td valign=\"top\"");
252: SIcon icon = XPageScrollerCG.DEFAULT_ICONS[SConstants.HORIZONTAL][XPageScrollerCG.LAST][lastPage ? 1
253: : 0];
254: String event = "" + (sb.getPageCount() - 1) * extent;
255: Utils.printClickability(d, sb, event, !lastPage, sb
256: .getShowAsFormComponent());
257: d.print(">");
258: writeIcon(d, icon, msie);
259: d.print("</td>\n");
260:
261: d.print("</tr></tbody></table></td>").print(
262: "</tr></tbody></table>");
263: }
264:
265: private void writeIcon(Device device, SIcon icon, boolean ie)
266: throws IOException {
267: device.print("<img");
268: if (ie && icon.getURL().toString().endsWith(".png")
269: && icon.getIconWidth() > 0 && icon.getIconHeight() > 0) {
270: Utils.optAttribute(device, "style",
271: "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"
272: + icon.getURL()
273: + "', sizingMethod='scale')");
274: Utils.optAttribute(device, "src",
275: XPageScrollerCG.TRANSPARENT_ICON.getURL());
276: } else
277: Utils.optAttribute(device, "src", icon.getURL());
278: Utils.optAttribute(device, "width", icon.getIconWidth());
279: Utils.optAttribute(device, "height", icon.getIconHeight());
280: device.print(" alt=\"");
281: device.print(icon.getIconTitle());
282: device.print("\"/>");
283: }
284: }
|