001: /*
002: * Copyright (c) 2005 Your Corporation. All Rights Reserved.
003: */
004: package org.wings.plaf.css;
005:
006: import org.apache.commons.logging.Log;
007: import org.apache.commons.logging.LogFactory;
008: import org.wings.*;
009: import org.wings.io.Device;
010:
011: import java.io.IOException;
012:
013: /**
014: * CG for a pagescroller.
015: *
016: * @author holger
017: */
018: public final class PageScrollerCG extends AbstractComponentCG implements
019: org.wings.plaf.PageScrollerCG {
020: private static final long serialVersionUID = 1L;
021:
022: private final static Log log = LogFactory
023: .getLog(PageScrollerCG.class);
024:
025: public static final int FORWARD = 0;
026: public static final int BACKWARD = 1;
027: public static final int FORWARD_BLOCK = 2;
028: public static final int BACKWARD_BLOCK = 3;
029: public static final int FIRST = 4;
030: public static final int LAST = 5;
031: private final static SIcon[][][] DEFAULT_ICONS = new SIcon[2][6][2];
032:
033: static {
034: String[] postfixes = new String[6];
035: String[] prefixes = new String[6];
036: for (int orientation = 0; orientation < 2; orientation++) {
037: prefixes[BACKWARD] = "";
038: prefixes[FORWARD] = "";
039: prefixes[FIRST] = "Margin";
040: prefixes[LAST] = "Margin";
041: prefixes[FORWARD_BLOCK] = "Block";
042: prefixes[BACKWARD_BLOCK] = "Block";
043: if (orientation == SConstants.VERTICAL) {
044: postfixes[BACKWARD] = "Up";
045: postfixes[FORWARD] = "Down";
046: postfixes[FIRST] = "Up";
047: postfixes[LAST] = "Down";
048: postfixes[BACKWARD_BLOCK] = "Up";
049: postfixes[FORWARD_BLOCK] = "Down";
050: } else {
051: postfixes[BACKWARD] = "Left";
052: postfixes[FORWARD] = "Right";
053: postfixes[FIRST] = "Left";
054: postfixes[LAST] = "Right";
055: postfixes[BACKWARD_BLOCK] = "Left";
056: postfixes[FORWARD_BLOCK] = "Right";
057: }
058:
059: for (int direction = 0; direction < postfixes.length; direction++) {
060: DEFAULT_ICONS[orientation][direction][0] = new SResourceIcon(
061: "org/wings/icons/" + prefixes[direction]
062: + "Scroll" + postfixes[direction]
063: + ".gif");
064: DEFAULT_ICONS[orientation][direction][1] = new SResourceIcon(
065: "org/wings/icons/Disabled"
066: + prefixes[direction] + "Scroll"
067: + postfixes[direction] + ".gif");
068: }
069: }
070: }
071:
072: public void writeInternal(Device d, SComponent c)
073: throws IOException {
074: log.debug("write = " + c);
075: SPageScroller sb = (SPageScroller) c;
076:
077: String style = sb.getStyle();
078: if (sb.getLayoutMode() == SConstants.VERTICAL) {
079: sb.setStyle(style + " SPageScroller_vertical");
080: writeVerticalPageScroller(d, sb);
081: } else {
082: sb.setStyle(style + " SPageScroller_horizontal");
083: writeHorizontalPageScroller(d, sb);
084: }
085: sb.setStyle(style);
086: }
087:
088: private void writeVerticalPageScroller(Device d, SPageScroller sb)
089: throws IOException {
090: int value = sb.getValue();
091: int extent = sb.getExtent();
092: int minimum = sb.getMinimum();
093: int maximum = sb.getMaximum();
094: boolean backEnabled = value > minimum;
095: boolean forwardEnabled = value < maximum - extent;
096: boolean firstPage = (value == minimum);
097: boolean lastPage = (value == (maximum - extent));
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: boolean enabled2 = !firstPage;
107: Utils.printClickability(d, sb, "" + minimum, enabled2, sb
108: .getShowAsFormComponent());
109: d.print(">");
110: writeIcon(d, DEFAULT_ICONS[SConstants.VERTICAL][FIRST][0]);
111: d.print("</td></tr>");
112:
113: d.print("<tr><td");
114: Utils.printClickability(d, sb, "" + (value - extent),
115: backEnabled, sb.getShowAsFormComponent());
116: d.print(">");
117: writeIcon(d, DEFAULT_ICONS[SConstants.VERTICAL][BACKWARD][0]);
118: d.print("</td></tr>");
119:
120: d.print("</tbody></table></td>").print("</tr>").print("<tr>")
121: .print("<td><table class=\"pages\"><tbody>");
122:
123: int firstDirectPage = sb.getCurrentPage()
124: - (sb.getDirectPages() - 1) / 2;
125: firstDirectPage = Math.min(firstDirectPage, sb.getPageCount()
126: - sb.getDirectPages());
127: firstDirectPage = Math.max(firstDirectPage, 0);
128:
129: for (int i = 0; i < Math.min(sb.getDirectPages(), sb
130: .getPageCount()
131: - firstDirectPage); i++) {
132: int page = firstDirectPage + i;
133: d.print("<tr><td");
134: boolean isCurrentPage = (sb.getCurrentPage() == page);
135: Utils.optAttribute(d, "class",
136: isCurrentPage ? "page_selected" : null);
137: boolean enabled = !isCurrentPage;
138: Utils.printClickability(d, sb, String.valueOf(page
139: * sb.getExtent()), enabled, sb
140: .getShowAsFormComponent());
141: d.print(">");
142: d.print(Integer.toString(page + 1));
143: d.print("</td></tr>");
144: }
145:
146: d.print("</tbody></table></td>").print("</tr>").print(
147: "<tr height=\"1%\">").print(
148: "<td height=\"1%\"><table class=\"buttons\"><tbody>");
149:
150: d.print("<tr><td");
151: Utils.printClickability(d, sb, "" + (value + extent),
152: forwardEnabled, sb.getShowAsFormComponent());
153: d.print(">");
154: writeIcon(d, DEFAULT_ICONS[SConstants.VERTICAL][FORWARD][0]);
155: d.print("</td></tr>");
156:
157: d.print("<tr><td");
158: boolean enabled1 = !lastPage;
159: Utils.printClickability(d, sb, "" + (maximum + 1 - extent),
160: enabled1, sb.getShowAsFormComponent());
161: d.print(">");
162: writeIcon(d, DEFAULT_ICONS[SConstants.VERTICAL][LAST][0]);
163: d.print("</td></tr>");
164:
165: d.print("</tbody></table></td>").print("</tr>").print(
166: "</tbody></table>");
167: }
168:
169: private void verticalArea(Device d, String s, int v)
170: throws IOException {
171: d.print("<tr><td style=\"background-color: ");
172: d.print(s);
173: d.print("\" height=\"");
174: d.print(v + "%");
175: d.print("\"></td></tr>");
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:
189: d.print("<table");
190: Utils.writeAllAttributes(d, sb);
191: Utils.writeEvents(d, sb, null);
192: d.print("><tbody><tr>").print(
193: "<td><table class=\"buttons\"><tbody><tr>");
194:
195: d.print("<td");
196: boolean enabled2 = !firstPage;
197: Utils.printClickability(d, sb, "" + minimum, enabled2, sb
198: .getShowAsFormComponent());
199: d.print(">");
200: writeIcon(d, DEFAULT_ICONS[SConstants.HORIZONTAL][FIRST][0]);
201: d.print("</td>");
202:
203: d.print("<td");
204: Utils.printClickability(d, sb, "" + (value - extent),
205: backEnabled, sb.getShowAsFormComponent());
206: d.print(">");
207: writeIcon(d, DEFAULT_ICONS[SConstants.HORIZONTAL][BACKWARD][0]);
208: d.print("</td>");
209:
210: d.print("</tr></tbody></table></td>").print(
211: "<td><table class=\"pages\"><tbody><tr>");
212:
213: int firstDirectPage = sb.getCurrentPage()
214: - (sb.getDirectPages() - 1) / 2;
215: firstDirectPage = Math.min(firstDirectPage, sb.getPageCount()
216: - sb.getDirectPages());
217: firstDirectPage = Math.max(firstDirectPage, 0);
218: for (int i = 0; i < Math.min(sb.getDirectPages(), sb
219: .getPageCount()
220: - firstDirectPage); i++) {
221: int page = firstDirectPage + i;
222: d.print("<td");
223: boolean isCurrentPage = (sb.getCurrentPage() == page);
224: Utils.optAttribute(d, "class",
225: isCurrentPage ? "page_selected" : null);
226: boolean enabled = !isCurrentPage;
227: Utils.printClickability(d, sb, String.valueOf(page
228: * sb.getExtent()), enabled, sb
229: .getShowAsFormComponent());
230: d.print(">");
231: d.print(Integer.toString(page + 1));
232: d.print("</td>");
233: }
234:
235: d.print("</tr></tbody></table></td>").print(
236: "<td><table class=\"buttons\"><tbody><tr>");
237:
238: d.print("<td");
239: Utils.printClickability(d, sb, "" + (value + extent),
240: forwardEnabled, sb.getShowAsFormComponent());
241: d.print(">");
242: writeIcon(d, DEFAULT_ICONS[SConstants.HORIZONTAL][FORWARD][0]);
243: d.print("</td>");
244:
245: d.print("<td");
246: boolean enabled1 = !lastPage;
247: Utils.printClickability(d, sb, "" + (sb.getPageCount() - 1)
248: * extent, enabled1, sb.getShowAsFormComponent());
249: d.print(">");
250:
251: writeIcon(d, DEFAULT_ICONS[SConstants.HORIZONTAL][LAST][0]);
252: d.print("</td>");
253:
254: d.print("</tr></tbody></table></td>").print(
255: "</tr></tbody></table>");
256: }
257:
258: private void writeIcon(Device device, SIcon icon)
259: throws IOException {
260: device.print("<img class=\"nopad\"");
261: Utils.optAttribute(device, "src", icon.getURL());
262: Utils.optAttribute(device, "width", icon.getIconWidth());
263: Utils.optAttribute(device, "height", icon.getIconHeight());
264: Utils.attribute(device, "alt", icon.getIconTitle());
265: device.print("/>");
266: }
267: }
|