KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: CheckBoxCG.java,v 1.17 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.session.SessionManager;
20
21 import java.io.IOException JavaDoc;
22
23 public class CheckBoxCG extends ButtonCG implements org.wings.plaf.CheckBoxCG {
24     private static final SIcon ICON_DISABLEDSELECTED = (SIcon) SessionManager
25             .getSession().getCGManager().getObject("SCheckBox.disabledSelectedIcon", SIcon.class);
26
27     private static final SIcon ICON_DISABLED = (SIcon) SessionManager
28             .getSession().getCGManager().getObject("SCheckBox.disabledIcon", SIcon.class);
29
30     private static final SIcon ICON_PRESSED = (SIcon) SessionManager
31             .getSession().getCGManager().getObject("SCheckBox.pressedIcon", SIcon.class);
32
33     private static final SIcon ICON_ROLLOVERSELECTED = (SIcon) SessionManager
34             .getSession().getCGManager().getObject("SCheckBox.rolloverSelectedIcon", SIcon.class);
35
36     private static final SIcon ICON_ROLLOVER = (SIcon) SessionManager
37             .getSession().getCGManager().getObject("SCheckBox.rolloverIcon", SIcon.class);
38
39     private static final SIcon ICON_DEFAULT = (SIcon) SessionManager
40             .getSession().getCGManager().getObject("SCheckBox.icon", SIcon.class);
41
42     private static final SIcon ICON_SELECTED = (SIcon) SessionManager
43                 .getSession().getCGManager().getObject("SCheckBox.selectedIcon", SIcon.class);
44     
45     protected boolean useIconsInForms = false;
46     
47
48     public boolean isUseIconsInForm() {
49         return useIconsInForms;
50     }
51
52     public void setUseIconsInForm(boolean useIconsInForm) {
53         this.useIconsInForms = useIconsInForm;
54     }
55
56     public void installCG(SComponent component) {
57         super.installCG(component);
58         final SAbstractButton button = (SAbstractButton) component;
59         installIcons(button);
60     }
61
62     protected void installIcons(final SAbstractButton button) {
63         org.wings.plaf.CGManager manager = button.getSession().getCGManager();
64         button.setIcon(ICON_DEFAULT);
65         button.setSelectedIcon(ICON_SELECTED);
66         button.setRolloverIcon(ICON_ROLLOVER);
67         button.setRolloverSelectedIcon(ICON_ROLLOVERSELECTED);
68         button.setPressedIcon(ICON_PRESSED);
69         button.setDisabledIcon(ICON_DISABLED);
70         button.setDisabledSelectedIcon(ICON_DISABLEDSELECTED);
71     }
72
73     public void writeContent(final Device device, final SComponent component)
74             throws IOException JavaDoc {
75         final SAbstractButton button = (SAbstractButton) component;
76
77         final boolean showAsFormComponent = button.getShowAsFormComponent();
78         final String JavaDoc text = button.getText();
79         final SIcon icon = getIcon(button);
80
81         /* TODO for the button support in IE hack to be working, this component
82          * needs to always or never use buttons when rendered as form component.
83          * Therefore best would be to drop button support on this component, since
84          * one probably wants to change CheckBox state without submitting.
85          * Therefore replace button with table...
86          * At this time it probably never uses buttons, since useIconsInForms is false
87          * by default (and probably never set). useIconsInForms should be dropped!
88          * Try #setShowAsFormComponent(false) if you want icon checkboxes in your
89          * application.
90          * (OL)
91          */

92         
93         if (showAsFormComponent && useIconsInForms) {
94             writeButtonStart(device, button);
95             device.print(" type=\"submit\" name=\"");
96             Utils.write(device, Utils.event(button));
97             device.print("\"");
98             Utils.optAttribute(device, "tabindex", button.getFocusTraversalIndex());
99             Utils.optAttribute(device, "accesskey", button.getMnemonic());
100             Utils.writeEvents(device, button);
101         } else if (showAsFormComponent && !useIconsInForms) {
102             device.print("<span");
103         } else {
104             RequestURL addr = button.getRequestURL();
105             addr.addParameter(button, button.getToggleSelectionParameter());
106             writeLinkStart(device, addr);
107
108             Utils.optAttribute(device, "accesskey", button.getMnemonic());
109             Utils.writeEvents(device, button);
110         }
111         Utils.printCSSInlineFullSize(device, component.getPreferredSize());
112
113         if (!button.isEnabled())
114             device.print(" disabled=\"true\"");
115         if (button.isSelected())
116             device.print(" checked=\"true\"");
117         if (component.isFocusOwner())
118             Utils.optAttribute(device, "focus", component.getName());
119
120         device.print(">");
121
122         if (showAsFormComponent && !useIconsInForms && text == null)
123             inputTypeCheckbox(device, button);
124         else if (icon != null && text == null)
125             writeIcon(device, icon);
126         else if (text != null && icon == null)
127             writeText(device, text);
128         else if (text != null) {
129             new IconTextCompound() {
130                 protected void text(Device device) throws IOException JavaDoc {
131                     writeText(device, text);
132                 }
133
134                 protected void icon(Device device) throws IOException JavaDoc {
135                     if (showAsFormComponent && !useIconsInForms)
136                         inputTypeCheckbox(device, button);
137                     else
138                         writeIcon(device, icon);
139                 }
140             }.writeCompound(device, component, button.getHorizontalTextPosition(), button.getVerticalTextPosition());
141         }
142
143         if (showAsFormComponent && useIconsInForms)
144             device.print("</button>");
145         else if (showAsFormComponent && !useIconsInForms)
146             device.print("</span>");
147         else
148             device.print("</a>");
149     }
150
151     /**
152      * @param device
153      * @throws IOException
154      */

155     protected void writeButtonStart(final Device device, final SAbstractButton comp) throws IOException JavaDoc {
156         device.print("<button");
157     }
158
159     /**
160      * Convenience method to keep differences between default and msie
161      * implementations small
162      * @param device
163      * @param addr
164      * @throws IOException
165      */

166     protected void writeLinkStart(final Device device, RequestURL addr) throws IOException JavaDoc {
167         device.print("<a HREF=\"");
168         addr.write(device);
169         device.print("\"");
170     }
171
172     protected void inputTypeCheckbox(Device device, SAbstractButton button) throws IOException JavaDoc {
173         device.print("<input type=\"hidden\" name=\"");
174         Utils.write(device, Utils.event(button));
175         device.print("\" value=\"hidden_reset\"/>");
176
177         device.print("<input type=\"checkbox\" name=\"");
178         Utils.write(device, Utils.event(button));
179         device.print("\"");
180         Utils.optAttribute(device, "tabindex", button.getFocusTraversalIndex());
181
182         if (!button.isEnabled())
183             device.print(" disabled=\"true\"");
184         if (button.isSelected())
185             device.print(" checked=\"true\"");
186
187         Utils.writeEvents(device, button);
188         device.print("/>");
189     }
190 }
191
Popular Tags