Add address line 2 support to generator
[zxing.git] / zxing.appspot.com / generator / src / com / google / zxing / web / generator / client / SmsAddressGenerator.java
1 /*
2  * Copyright (C) 2008 ZXing authors
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.google.zxing.web.generator.client;
18
19 import com.google.gwt.user.client.ui.ChangeListener;
20 import com.google.gwt.user.client.ui.Grid;
21 import com.google.gwt.user.client.ui.TextArea;
22 import com.google.gwt.user.client.ui.TextBox;
23 import com.google.gwt.user.client.ui.Widget;
24
25 /**
26  * A generator for a sms address. Gives the option of filling up the content
27  * of the message as well.
28  * 
29  * @author Yohann Coppel
30  */
31 public class SmsAddressGenerator implements GeneratorSource {
32   Grid table = null;
33   TextBox number = new TextBox();
34   TextArea message = new TextArea();
35
36   public SmsAddressGenerator(ChangeListener listener) {
37     number.addStyleName(StylesDefs.INPUT_FIELD_REQUIRED);
38     number.addChangeListener(listener);
39     message.addChangeListener(listener);
40   }
41   
42   public String getName() {
43     return "SMS";
44   }
45
46   public String getText() throws GeneratorException {
47     String inputNumber = getTelField();    
48     String inputMessage = getMessageField();
49     
50     String output = inputNumber;
51     // we add the text only if there actually is something in the field.
52     if (inputMessage.length() > 0) {
53       output += ':' + inputMessage;
54     }
55     
56     return "smsto:" + output;
57   }
58
59   private String getTelField() throws GeneratorException {
60     String input = number.getText();
61     if (input.length() < 1) {
62       throw new GeneratorException("Phone number must be present.");
63     }
64     input = Validators.filterNumber(input);
65     Validators.validateNumber(input);
66     return input;
67   }
68  
69   private String getMessageField() throws GeneratorException {
70     String inputMessage = message.getText();
71     if (inputMessage.length() > 150) {
72       throw new GeneratorException("Sms message can not be longer than 150 characters.");
73     }
74     return inputMessage;
75   }
76   
77   public Grid getWidget() {
78     if (table != null) {
79       return table;
80     }
81     table = new Grid(2, 2);
82
83     table.setText(0, 0, "Phone number");
84     table.setWidget(0, 1, number);
85
86     table.setText(1, 0, "Message");
87     table.setWidget(1, 1, message);
88
89     return table;
90   }
91
92   public void validate(Widget widget) throws GeneratorException {
93     if (widget == number) {
94       getTelField();
95     }
96     if (widget == message) {
97       getMessageField();
98     }
99   }
100   
101   public void setFocus() {
102     number.setFocus(true);
103   }
104 }