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