KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: ButtonCG.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.script.JavaScriptListener;
19 import org.wings.script.JavaScriptEvent;
20 import org.wings.io.Device;
21
22 import java.io.IOException JavaDoc;
23
24 public class ButtonCG extends LabelCG implements org.wings.plaf.ButtonCG {
25
26     /**
27      * Use this java script implementation to submit forms on button click
28      */

29     // TODO: Implement handling of formless submits
30
// TODO: Avoid triggering of enter key catchers
31
// TODO: Use Utils.loadScript("org/wings/plaf/css/xxx.js")
32
public final static String JavaDoc JS_FORM_SUBMIT_SCRIPT = "javascript:this.form.submit();";
33
34     /**
35      * Use i.e. {@link SButton#addScriptListener(org.wings.script.ScriptListener)} to add this scripts.
36      */

37     public final static JavaScriptListener JS_ON_CHANGE_SUBMIT = new JavaScriptListener(JavaScriptEvent.ON_CHANGE, JS_FORM_SUBMIT_SCRIPT);
38
39     private void writeDynamicIcons(final Device device, SAbstractButton abstractButton, SIcon origIcon,
40                                    String JavaDoc iconName, boolean renderNameAttribute)
41             throws IOException JavaDoc {
42         if (abstractButton.isEnabled() &&
43                 (abstractButton.getGroup() == null || !abstractButton.isSelected())) {
44             // render rollover
45
SIcon rolloverIcon = abstractButton.getRolloverIcon();
46             SIcon pressedIcon = abstractButton.getPressedIcon();
47
48             if (rolloverIcon != null || pressedIcon != null) {
49                 if (renderNameAttribute) {
50
51                     device.print(" name=\"");
52                     Utils.write(device, iconName);
53
54                     device.print("\"");
55                 } // end of if ()
56

57
58                 if (rolloverIcon != null) {
59
60                     device.print(" onMouseover=\"if(document.images){this.src='");
61                     Utils.write(device, rolloverIcon.getURL());
62
63                     device.print("';}\" onmouseout=\"if(document.images){this.src='");
64                     Utils.write(device, origIcon.getURL());
65
66                     device.print("';}\"");
67                 }
68
69                 if (pressedIcon != null) {
70
71                     device.print(" onMousedown=\"if(document.images){this.src='");
72                     Utils.write(device, pressedIcon.getURL());
73
74                     device.print("';}\" onmouseup=\"if(document.images){this.src='");
75                     Utils.write(device, rolloverIcon != null ? rolloverIcon.getURL() : origIcon.getURL());
76
77                     device.print("';}\"");
78                 }
79             }
80         }
81
82     }
83
84     public void writeContent(final Device device, final SComponent component)
85             throws IOException JavaDoc {
86         final SAbstractButton button = (SAbstractButton) component;
87
88         if (button.getShowAsFormComponent()) {
89             writeButtonStart(device, button);
90             device.print(" type=\"submit\" name=\"");
91             Utils.write(device, Utils.event(button));
92             device.print("\"");
93             Utils.optAttribute(device, "tabindex", button.getFocusTraversalIndex());
94             Utils.optAttribute(device, "accesskey", button.getMnemonic());
95         } else {
96             RequestURL addr = button.getRequestURL();
97             addr.addParameter(button, button.getToggleSelectionParameter());
98             writeLinkStart(device, addr);
99
100             Utils.optAttribute(device, "accesskey", button.getMnemonic());
101         }
102         Utils.printCSSInlineFullSize(device, component.getPreferredSize());
103
104         // use class attribute instead of single attributes for IE compatibility
105
StringBuffer JavaDoc className = new StringBuffer JavaDoc();
106         if (!button.isEnabled()) {
107             className.append(component.getStyle());
108             className.append("_disabled ");
109         }
110         if (button.isSelected()) {
111             className.append(component.getStyle());
112             className.append("_selected ");
113         }
114         if (className.length() > 0) {
115             device.print(" class=\"");
116             device.print(className.toString());
117             device.print("\"");
118         }
119         if (component.isFocusOwner())
120             Utils.optAttribute(device, "focus", component.getName());
121
122         Utils.writeEvents(device, button);
123         device.print(">");
124
125         final String JavaDoc text = button.getText();
126         final SIcon icon = getIcon(button);
127
128         if (icon == null && text != null)
129             writeText(device, text);
130         else if (icon != null && text == null)
131             writeIcon(device, icon);
132         else if (icon != null && text != null) {
133             new IconTextCompound() {
134                 protected void text(Device d) throws IOException JavaDoc {
135                     writeText(d, text);
136                 }
137
138                 protected void icon(Device d) throws IOException JavaDoc {
139                     writeIcon(d, icon);
140                 }
141             }.writeCompound(device, component, button.getHorizontalTextPosition(), button.getVerticalTextPosition());
142         }
143
144         if (button.getShowAsFormComponent())
145             device.print("</button>");
146         else
147             device.print("</a>");
148     }
149
150     /**
151      * @param device
152      * @throws IOException
153      */

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

165     protected void writeLinkStart(final Device device, RequestURL addr) throws IOException JavaDoc {
166         device.print("<a HREF=\"");
167         addr.write(device);
168         device.print("\"");
169     }
170
171     protected SIcon getIcon(SAbstractButton abstractButton) {
172         if (abstractButton.isSelected()) {
173             return abstractButton.isEnabled()
174                     ? abstractButton.getSelectedIcon()
175                     : abstractButton.getDisabledSelectedIcon();
176         } else {
177             return abstractButton.isEnabled()
178                     ? abstractButton.getIcon()
179                     : abstractButton.getDisabledIcon();
180         }
181     }
182 }
183
Popular Tags