KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: ScrollBarCG.java,v 1.14 2005/06/07 12:55:59 neurolabs Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings.plaf.css;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.wings.*;
19 import org.wings.io.Device;
20
21 import java.io.IOException JavaDoc;
22
23 /**
24  * CG for a scrollbar.
25  *
26  * @author holger
27  */

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

256     protected void writeButtonStart(Device device, SScrollBar scrollBar, String JavaDoc event) throws IOException JavaDoc {
257         device.print("<button");
258     }
259 }
260
Popular Tags