KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: ListCG.java,v 1.23 2005/05/26 13:18:09 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
17 import org.wings.*;
18 import org.wings.io.Device;
19 import org.wings.plaf.CGManager;
20 import org.wings.script.JavaScriptEvent;
21 import org.wings.script.JavaScriptListener;
22
23 import java.io.IOException JavaDoc;
24
25 public class ListCG extends AbstractComponentCG implements
26         org.wings.plaf.ListCG {
27
28     public void installCG(final SComponent comp) {
29         super.installCG(comp);
30         final SList component = (SList) comp;
31         final CGManager manager = component.getSession().getCGManager();
32         Object JavaDoc value;
33         value = manager.getObject("SList.cellRenderer", SDefaultListCellRenderer.class);
34         if (value != null) {
35             component.setCellRenderer((SDefaultListCellRenderer) value);
36         }
37     }
38
39     private static final JavaScriptListener selectListener = new JavaScriptListener(JavaScriptEvent.ON_CHANGE, "submit()");
40
41     protected void writeFormList(Device device, SList list) throws IOException JavaDoc {
42
43         device.print("<select");
44         Utils.optAttribute(device, "name", Utils.event(list));
45         Utils.optAttribute(device, "tabindex", list.getFocusTraversalIndex());
46         Utils.optAttribute(device, "size", list.getVisibleRowCount());
47         Utils.optAttribute(device, "multiple", (list.getSelectionMode() == SList.MULTIPLE_SELECTION) ? "multiple" : null);
48
49         if (!list.isEnabled())
50             device.print(" disabled=\"true\"");
51         if (list.isFocusOwner())
52             Utils.optAttribute(device, "focus", list.getName());
53
54         list.removeScriptListener(selectListener);
55         if (list.getListSelectionListeners().length > 0) {
56             list.addScriptListener(selectListener);
57         }
58
59         Utils.printCSSInlineFullSize(device, list.getPreferredSize());
60         Utils.writeEvents(device, list);
61
62         device.print(">");
63         javax.swing.ListModel JavaDoc model = list.getModel();
64         int size = model.getSize();
65
66         SListCellRenderer cellRenderer = list.getCellRenderer();
67
68         for (int i = 0; i < size; i++) {
69             SComponent renderer = null;
70             if (cellRenderer != null) {
71                 renderer = cellRenderer.getListCellRendererComponent(list, model.getElementAt(i), false, i);
72             }
73
74             device.print("\n<option");
75             Utils.optAttribute(device, "value", list.getSelectionParameter(i));
76             if (list.isSelectedIndex(i)) {
77                 device.print(" selected=\"selected\"");
78             }
79
80             org.wings.io.StringBufferDevice stringBufferDevice = getStringBufferDevice();
81
82             Utils.printCSSInlineStyleAttributes(stringBufferDevice, renderer);
83
84             String JavaDoc styleString = stringBufferDevice.toString();
85             if (styleString != null && styleString.length() > 0) {
86                 device.print(" style=\"");
87                 Utils.write(device, styleString);
88                 device.print("\"");
89             }
90             device.print(">");
91
92             if (renderer != null) {
93                 // Hack: remove all tags, because in form selections, looks ugly.
94
org.wings.io.StringBufferDevice string = getStringBufferDevice();
95                 renderer.write(string);
96                 char[] chars = string.toString().toCharArray();
97                 int pos = 0;
98                 for (int c = 0; c < chars.length; c++) {
99                     switch (chars[c]) {
100                         case '<':
101                             device.print(chars, pos, c - pos);
102                             break;
103                         case '>':
104                             pos = c + 1;
105                     }
106                 }
107                 device.print(chars, pos, chars.length - pos);
108             } else {
109                 device.print(model.getElementAt(i).toString());
110             }
111
112             device.print("</option>");
113         }
114
115         device.print("</select>");
116         device.print("<input type=\"hidden\"");
117         Utils.optAttribute(device, "name", Utils.event(list));
118         Utils.optAttribute(device, "value", -1);
119         device.print("/>");
120     }
121
122     private org.wings.io.StringBufferDevice stringBufferDevice = null;
123
124     protected org.wings.io.StringBufferDevice getStringBufferDevice() {
125         if (stringBufferDevice == null) {
126             stringBufferDevice = new org.wings.io.StringBufferDevice();
127         }
128         stringBufferDevice.reset();
129         return stringBufferDevice;
130     }
131
132     public void writeAnchorList(Device device, SList list)
133             throws IOException JavaDoc {
134         boolean showAsFormComponent = list.getShowAsFormComponent();
135         boolean renderSelection = list.getSelectionMode() != SList.NO_SELECTION;
136
137         device.print("<");
138         device.print(list.getType());
139         Utils.optAttribute(device, "type", list.getOrderType());
140         Utils.optAttribute(device, "start", list.getStart());
141
142         Utils.printCSSInlineFullSize(device, list.getPreferredSize());
143         device.print(">");
144
145         javax.swing.ListModel JavaDoc model = list.getModel();
146         SListCellRenderer cellRenderer = list.getCellRenderer();
147         SCellRendererPane rendererPane = list.getCellRendererPane();
148
149         int start = 0;
150         int end = model.getSize();
151
152         java.awt.Rectangle JavaDoc viewport = list.getViewportSize();
153         if (viewport != null) {
154             start = viewport.y;
155             end = start + viewport.height;
156         }
157
158         for (int i = start; i < end; i++) {
159             boolean selected = list.isSelectedIndex(i);
160
161             if (renderSelection && selected)
162                 device.print("<li class=\"SList_selected\"");
163             else
164                 device.print("<li>");
165
166             SComponent renderer = cellRenderer.getListCellRendererComponent(list, model.getElementAt(i), selected, i);
167
168             if (renderSelection) {
169                 if (showAsFormComponent) {
170                     writeButtonStart(device, list, list.getToggleSelectionParameter(i));
171                     device.print(" name=\"");
172                     Utils.write(device, Utils.event(list));
173                     device.print("\" value=\"");
174                     Utils.write(device, list.getToggleSelectionParameter(i));
175                     device.print("\"");
176                 } else {
177                     RequestURL selectionAddr = list.getRequestURL();
178                     selectionAddr.addParameter(Utils.event(list), list.getToggleSelectionParameter(i));
179
180                     writeLinkStart(device, selectionAddr);
181                 }
182                 Utils.optAttribute(device, "focus", renderer.getName());
183                 device.print(">");
184             } else
185                 device.print("<span>");
186
187             rendererPane.writeComponent(device, renderer, list);
188
189             if (renderSelection) {
190                 if (showAsFormComponent)
191                     device.print("</button>");
192                 else
193                     device.print("</a>");
194             } else
195                 device.print("</span>");
196
197             device.print("</li>\n");
198         }
199
200         device.print("</");
201         Utils.write(device, list.getType());
202         device.print(">");
203     }
204
205     /**
206      * @param device
207      * @throws IOException
208      */

209     protected void writeButtonStart(Device device, SList list, String JavaDoc value) throws IOException JavaDoc {
210         device.print("<button");
211     }
212
213     /**
214      * Convenience method to keep differences between default and msie
215      * implementations small
216      * @param device
217      * @param selectionAddr
218      * @throws IOException
219      */

220     protected void writeLinkStart(Device device, RequestURL selectionAddr) throws IOException JavaDoc {
221         device.print("<a HREF=\"");
222         Utils.write(device, selectionAddr.toString());
223         device.print("\"");
224     }
225
226
227     public void writeContent(final Device device,
228                              final SComponent _c)
229             throws IOException JavaDoc {
230         final SList component = (SList) _c;
231
232         SList list = (SList) component;
233         if (list.getShowAsFormComponent()) {
234             writeFormList(device, list);
235         } else {
236             writeAnchorList(device, list);
237         }
238     }
239 }
240
Popular Tags