f117e51e48062a67517fca48df11ff0449a36bbc
[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.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;
25
26 /**
27  * A generator for a sms address. Gives the option of filling up the content
28  * of the message as well.
29  * 
30  * @author Yohann Coppel
31  */
32 public class SmsAddressGenerator implements GeneratorSource {
33   Grid table = null;
34   TextBox number = new TextBox();
35   TextArea message = new TextArea();
36
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);
43   }
44   
45   public String getName() {
46     return "SMS";
47   }
48
49   public String getText() throws GeneratorException {
50     String inputNumber = getTelField();    
51     String inputMessage = getMessageField();
52     
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;
57     }
58     
59     return "smsto:" + output;
60   }
61
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.");
66     }
67     input = Validators.filterNumber(input);
68     Validators.validateNumber(input);
69     return input;
70   }
71  
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.");
76     }
77     return inputMessage;
78   }
79   
80   public Grid getWidget() {
81     if (table != null) {
82       return table;
83     }
84     table = new Grid(2, 2);
85
86     table.setText(0, 0, "Phone number");
87     table.setWidget(0, 1, number);
88
89     table.setText(1, 0, "Message");
90     table.setWidget(1, 1, message);
91
92     return table;
93   }
94
95   public void validate(Widget widget) throws GeneratorException {
96     if (widget == number) {
97       getTelField();
98     }
99     if (widget == message) {
100       getMessageField();
101     }
102   }
103   
104   public void setFocus() {
105     number.setFocus(true);
106   }
107 }