1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.nordija.tapestry.bayeux.form;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.apache.tapestry.IActionListener;
21 import org.apache.tapestry.IBinding;
22 import org.apache.tapestry.IForm;
23 import org.apache.tapestry.IMarkupWriter;
24 import org.apache.tapestry.IRequestCycle;
25 import org.apache.tapestry.form.AbstractFormComponent;
26
27
28
29
30
31
32
33
34
35 public abstract class Html4Button extends AbstractFormComponent {
36
37
38
39
40 public static final String BUTTON_SUBMIT_VALUE = "x";
41
42 private static final Log LOG = LogFactory.getLog(Html4Button.class);
43
44 public abstract String getLabel();
45
46 public abstract String getType();
47
48 public abstract IBinding getSelectedBinding();
49
50 public abstract boolean isDisabled();
51
52 public abstract String getOnclickEventHandler();
53
54 public abstract IActionListener getListener();
55
56 public abstract Object getTag();
57
58 protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle) {
59 IForm form = getForm();
60 String buttonName = form.getElementId(this);
61 String formName = form.getName();
62
63 if (LOG.isDebugEnabled()) {
64 LOG.debug("form=" + form + ", buttonName=" + buttonName);
65 }
66 processRendering(cycle, formName, buttonName, writer);
67 }
68
69 protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle) {
70 IForm form = getForm();
71 String buttonName = form.getElementId(this);
72 if (LOG.isDebugEnabled()) {
73 LOG.debug("rewinding: form=" + form + ", buttonName=" + buttonName);
74 }
75 processRewinding(cycle, buttonName);
76 }
77
78 private void processRendering(IRequestCycle cycle, String formName, String buttonName, IMarkupWriter writer) {
79
80
81
82
83
84
85
86
87
88 writer.begin("input");
89 writer.attribute("type", "hidden");
90 writer.attribute("name", getHiddenElementName(buttonName));
91 writer.end();
92
93 writer.begin("button");
94 writer.attribute("type", getType());
95 writer.attribute("name", buttonName);
96
97 if (isDisabled()) {
98 writer.attribute("disabled", "disabled");
99 }
100
101 String label = getLabel();
102 if (label != null) {
103 writer.attribute("value", label);
104 }
105
106
107 String onclickEvent = "document." + formName + "." + getHiddenElementName(buttonName) + ".value='" + BUTTON_SUBMIT_VALUE + "'";
108
109
110 if ((getOnclickEventHandler() != null) && (getOnclickEventHandler().trim().length() > 0)) {
111 onclickEvent += "; " + getOnclickEventHandler();
112 }
113
114 writer.attribute("onclick", onclickEvent);
115
116 renderInformalParameters(writer, cycle);
117 renderBody(writer, cycle);
118 writer.end();
119 }
120
121 private void processRewinding(IRequestCycle cycle, String buttonName) {
122 if (isDisabled()) {
123 return;
124 }
125
126
127
128
129
130
131 String value = cycle.getParameter(getHiddenElementName(buttonName));
132 if (value == null || (!value.equals(BUTTON_SUBMIT_VALUE))) {
133 return;
134 }
135
136 if (LOG.isDebugEnabled()) {
137 LOG.debug("determined that it was this button '" + buttonName + "' that was clicked");
138 }
139
140 IBinding selectedBinding = getSelectedBinding();
141 if (selectedBinding != null) {
142 selectedBinding.setObject(getTag());
143 }
144
145 IActionListener listener = getListener();
146 if (listener != null) {
147 listener.actionTriggered(this, cycle);
148 }
149 }
150
151 private String getHiddenElementName(String buttonName) {
152 return buttonName + "_ctrl";
153 }
154 }