2 * Copyright (C) 2008 ZXing authors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.zxing.web.generator.client;
19 import com.google.gwt.event.dom.client.KeyPressHandler;
20 import com.google.gwt.user.client.ui.ChangeListener;
21 import com.google.gwt.user.client.ui.Grid;
22 import com.google.gwt.user.client.ui.TextArea;
23 import com.google.gwt.user.client.ui.TextBox;
24 import com.google.gwt.user.client.ui.Widget;
27 * A generator for a sms address. Gives the option of filling up the content
28 * of the message as well.
30 * @author Yohann Coppel
32 public class SmsAddressGenerator implements GeneratorSource {
34 TextBox number = new TextBox();
35 TextArea message = new TextArea();
37 public SmsAddressGenerator(ChangeListener listener,
38 KeyPressHandler keyListener) {
39 number.addStyleName(StylesDefs.INPUT_FIELD_REQUIRED);
40 number.addChangeListener(listener);
41 number.addKeyPressHandler(keyListener);
42 message.addChangeListener(listener);
45 public String getName() {
49 public String getText() throws GeneratorException {
50 String inputNumber = getTelField();
51 String inputMessage = getMessageField();
53 String output = inputNumber;
54 // we add the text only if there actually is something in the field.
55 if (inputMessage.length() > 0) {
56 output += ':' + inputMessage;
59 return "smsto:" + output;
62 private String getTelField() throws GeneratorException {
63 String input = number.getText();
64 if (input.length() < 1) {
65 throw new GeneratorException("Phone number must be present.");
67 input = Validators.filterNumber(input);
68 Validators.validateNumber(input);
72 private String getMessageField() throws GeneratorException {
73 String inputMessage = message.getText();
74 if (inputMessage.length() > 150) {
75 throw new GeneratorException("Sms message can not be longer than 150 characters.");
80 public Grid getWidget() {
84 table = new Grid(2, 2);
86 table.setText(0, 0, "Phone number");
87 table.setWidget(0, 1, number);
89 table.setText(1, 0, "Message");
90 table.setWidget(1, 1, message);
95 public void validate(Widget widget) throws GeneratorException {
96 if (widget == number) {
99 if (widget == message) {
104 public void setFocus() {
105 number.setFocus(true);