Skip to content

Commit 05f5edd

Browse files
committed
Added setOnLabel and setOffLabel for adding labels on switch component.
Fixed #359
1 parent 1a01833 commit 05f5edd

File tree

1 file changed

+56
-21
lines changed

1 file changed

+56
-21
lines changed

gwt-material/src/main/java/gwt/material/design/client/ui/MaterialSwitch.java

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
import com.google.gwt.event.logical.shared.ValueChangeHandler;
3030
import com.google.gwt.event.shared.HandlerRegistration;
3131
import com.google.gwt.user.client.ui.HasValue;
32-
import gwt.material.design.client.base.MaterialWidget;
3332
import gwt.material.design.client.base.HasError;
33+
import gwt.material.design.client.base.MaterialWidget;
3434
import gwt.material.design.client.base.mixin.ErrorMixin;
3535
import gwt.material.design.client.constants.InputType;
3636
import gwt.material.design.client.ui.html.Label;
@@ -39,25 +39,29 @@
3939
//@formatter:off
4040

4141
/**
42-
* Material Switch or other call it toggle - used for an alternative for checkbox
43-
*
44-
* <h3>UiBinder Usage:</h3>
45-
* <pre>
46-
*{@code<m:MaterialSwitch value="true"/>
47-
*<m:MaterialSwitch value="true" disabled="true"/>
48-
* }
49-
* </pre>
42+
* Material Switch or other call it toggle - used for an alternative for checkbox
43+
*
44+
* <h3>UiBinder Usage:</h3>
45+
* <pre>
46+
*{@code<m:MaterialSwitch value="true"/>
47+
*<m:MaterialSwitch value="true" disabled="true"/>
48+
* }
49+
* </pre>
5050
*
51-
* @author kevzlou7979
52-
* @see <a href="http://gwt-material-demo.herokuapp.com/#forms">Material Switch</a>
53-
*/
51+
* @author kevzlou7979
52+
* @see <a href="http://gwtmaterialdesign.github.io/gwt-material-demo/#!forms">Material Switch</a>
53+
*/
5454
//@formatter:on
5555
public class MaterialSwitch extends MaterialWidget implements HasValue<Boolean>, HasClickHandlers, HasError {
5656

5757
private MaterialInput input = new MaterialInput();
5858
private Span span = new Span();
5959
private Label label = new Label();
6060
private MaterialLabel lblError = new MaterialLabel();
61+
private String onLabel = "";
62+
private Span onLabelSpan = new Span();
63+
private String offLabel = "";
64+
private Span offLabelSpan = new Span();
6165

6266
private final ErrorMixin<MaterialSwitch, MaterialLabel> errorMixin = new ErrorMixin<>(this, lblError, null);
6367

@@ -68,6 +72,22 @@ public MaterialSwitch() {
6872
super(Document.get().createDivElement(), "switch");
6973
span.setStyleName("lever");
7074
input.setType(InputType.CHECKBOX);
75+
}
76+
77+
/**
78+
* Creates a material switch with default value.
79+
*/
80+
public MaterialSwitch(boolean value) {
81+
this();
82+
setValue(value);
83+
}
84+
85+
@Override
86+
protected void onLoad() {
87+
super.onLoad();
88+
if(!offLabel.isEmpty()) {
89+
label.add(offLabelSpan);
90+
}
7191
label.add(input);
7292
label.add(span);
7393
add(label);
@@ -84,14 +104,9 @@ public void onClick(ClickEvent event) {
84104
event.stopPropagation();
85105
}
86106
});
87-
}
88-
89-
/**
90-
* Creates a material switch with default value.
91-
*/
92-
public MaterialSwitch(boolean value) {
93-
this();
94-
setValue(value);
107+
if(!onLabel.isEmpty()) {
108+
label.add(onLabelSpan);
109+
}
95110
}
96111

97112
@Override
@@ -176,13 +191,15 @@ public void setSpan(Span span) {
176191
/**
177192
* @return the label
178193
*/
194+
@Deprecated
179195
public Label getLabel() {
180196
return label;
181197
}
182198

183199
/**
184200
* @param label the label to set
185201
*/
202+
@Deprecated
186203
public void setLabel(Label label) {
187204
this.label = label;
188205
}
@@ -213,4 +230,22 @@ public void setSuccess(String success) {
213230
public void clearErrorOrSuccess() {
214231
errorMixin.clearErrorOrSuccess();
215232
}
216-
}
233+
234+
public String getOnLabel() {
235+
return onLabel;
236+
}
237+
238+
public void setOnLabel(String onLabel) {
239+
this.onLabel = onLabel;
240+
onLabelSpan.setText(onLabel);
241+
}
242+
243+
public String getOffLabel() {
244+
return offLabel;
245+
}
246+
247+
public void setOffLabel(String offLabel) {
248+
this.offLabel = offLabel;
249+
offLabelSpan.setText(offLabel);
250+
}
251+
}

0 commit comments

Comments
 (0)