KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: ComboBoxCG.java,v 1.21 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
21 import java.io.IOException JavaDoc;
22
23 public class ComboBoxCG extends AbstractComponentCG implements
24         org.wings.plaf.ComboBoxCG {
25
26     public void installCG(final SComponent comp) {
27         super.installCG(comp);
28         final SComboBox component = (SComboBox) comp;
29         final CGManager manager = component.getSession().getCGManager();
30         Object JavaDoc value;
31         value = manager.getObject("SComboBox.renderer", SDefaultListCellRenderer.class);
32         if (value != null) {
33             component.setRenderer((SDefaultListCellRenderer) value);
34         }
35     }
36
37
38     protected void writeFormComboBox(Device device, SComboBox component) throws IOException JavaDoc {
39         device.print("<select size=\"1\"");
40         Utils.optAttribute(device, "name", Utils.event(component));
41         Utils.optAttribute(device, "tabindex", component.getFocusTraversalIndex());
42
43         Utils.printCSSInlineFullSize(device, component.getPreferredSize());
44
45         if (!component.isEnabled())
46             device.print(" disabled=\"true\"");
47         if (component.isFocusOwner())
48             Utils.optAttribute(device, "focus", component.getName());
49
50         Utils.writeEvents(device, component);
51
52         device.print(">\n");
53         javax.swing.ComboBoxModel JavaDoc model = component.getModel();
54         int size = model.getSize();
55         int selected = component.getSelectedIndex();
56
57         SListCellRenderer renderer = component.getRenderer();
58
59         for (int i = 0; i < size; i++) {
60             SComponent cellRenderer = null;
61             if (renderer != null) {
62                 cellRenderer = renderer.getListCellRendererComponent(component, model.getElementAt(i), false, i);
63             } else {
64                 device.print("<!--renderer==null-->");
65             }
66
67
68             device.print("<option");
69             Utils.optAttribute(device, "value", component.getSelectionParameter(i));
70             if (selected == i) {
71                 device.print(" selected=\"selected\"");
72             }
73
74             if (cellRenderer != null) {
75                 Utils.optAttribute(device, "title", cellRenderer.getToolTipText());
76                 org.wings.io.StringBufferDevice stringBufferDevice = getStringBufferDevice();
77                 Utils.printCSSInlineStyleAttributes(stringBufferDevice, cellRenderer);
78                 Utils.optAttribute(device, "style", stringBufferDevice.toString());
79             }
80
81             device.print(">\n"); //option
82

83             if (cellRenderer != null) {
84                 // Hack: remove all tags, because in form selections, looks ugly.
85
org.wings.io.StringBufferDevice string = getStringBufferDevice();
86                 cellRenderer.write(string);
87                 char[] chars = string.toString().toCharArray();
88                 int pos = 0;
89                 for (int c = 0; c < chars.length; c++) {
90                     switch (chars[c]) {
91                         case '<':
92                             device.print(chars, pos, c - pos);
93                             break;
94                         case '>':
95                             pos = c + 1;
96                     }
97                 }
98                 device.print(chars, pos, chars.length - pos);
99             } else {
100                 device.print("<!--cellrenderer==null, use toString-->");
101                 device.print(model.getElementAt(i).toString());
102             }
103
104             device.print("</option>");
105         }
106
107
108         device.print("</select>");
109         // util method
110

111         device.print("<input type=\"hidden\"");
112         Utils.optAttribute(device, "name", Utils.event(component));
113         Utils.optAttribute(device, "value", -1);
114
115         device.print("/>");
116     }
117
118     private org.wings.io.StringBufferDevice stringBufferDevice = null;
119
120     protected org.wings.io.StringBufferDevice getStringBufferDevice() {
121         if (stringBufferDevice == null) {
122             stringBufferDevice = new org.wings.io.StringBufferDevice();
123         }
124         stringBufferDevice.reset();
125         return stringBufferDevice;
126     }
127
128
129 //--- end code from common area in template.
130

131
132     public void writeContent(final Device device,
133                              final SComponent _c)
134             throws IOException JavaDoc {
135         final SComboBox component = (SComboBox) _c;
136
137 //--- code from write-template.
138
SComboBox comboBox = (SComboBox) component;
139         // TODO: implement anchor combobox
140
//if (comboBox.getShowAsFormComponent()) {
141
writeFormComboBox(device, comboBox);
142         //} else {
143
// writeAnchorComboBox(device, comboBox);
144
// }
145
device.print("\n");
146
147 //--- end code from write-template.
148
}
149 }
150
Popular Tags