Add address line 2 support to generator
[zxing.git] / zxing.appspot.com / generator / src / com / google / zxing / web / generator / client / PhoneNumberGenerator.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.TextBox;
22 import com.google.gwt.user.client.ui.Widget;
23
24 /**
25  * A generator for a phone number.
26  * 
27  * @author Yohann Coppel
28  */
29 public class PhoneNumberGenerator implements GeneratorSource {
30   Grid table = null;
31   TextBox number = new TextBox();
32   
33   public PhoneNumberGenerator(ChangeListener listener) {
34     number.addStyleName(StylesDefs.INPUT_FIELD_REQUIRED);
35     number.addChangeListener(listener);
36   }
37   
38   public String getName() {
39     return "Phone number";
40   }
41
42   public String getText() throws GeneratorException {
43     String tel = getTelField();
44     return "tel:" + tel;
45   }
46
47   private String getTelField() throws GeneratorException {
48     String input = number.getText();
49     if (input.length() < 1) {
50       throw new GeneratorException("Phone number must be present.");
51     }
52     input = Validators.filterNumber(input);
53     Validators.validateNumber(input);
54     return input;
55   }
56   
57   public Grid getWidget() {
58     if (table != null) {
59       return table;
60     }
61     table = new Grid(1, 2);
62
63     table.setText(0, 0, "Phone number");
64     table.setWidget(0, 1, number);
65     
66     return table;
67   }
68
69   public void validate(Widget widget) throws GeneratorException {
70     if (widget == number) {
71       getTelField();
72     }
73   }
74   
75   public void setFocus() {
76     number.setFocus(true);
77   }
78 }