In barcode generaotor (zxing.appspot.com), generate barcode upon pressing enter....
[zxing.git] / zxing.appspot.com / generator / src / com / google / zxing / web / generator / client / EmailGenerator.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  * Generator for email address.
27  * 
28  * @author Yohann Coppel
29  */
30 public class EmailGenerator implements GeneratorSource {
31   Grid table = null;
32   TextBox email = new TextBox();
33   
34   public EmailGenerator(ChangeListener listener, KeyPressHandler keyListener) {
35     email.addStyleName(StylesDefs.INPUT_FIELD_REQUIRED);
36     email.addChangeListener(listener);
37     email.addKeyPressHandler(keyListener);
38   }
39   
40   public String getName() {
41     return "Email address";
42   }
43
44   public String getText() throws GeneratorException {
45     String email = getEmailField();
46     return "mailto:"+email;
47   }
48
49   private String getEmailField() throws GeneratorException {
50     String input = email.getText();
51     if (input.length() < 1) {
52       throw new GeneratorException("Email must be present.");
53     }
54     Validators.validateEmail(input);
55     return input;
56   }
57   
58   public Grid getWidget() {
59     if (table != null) {
60       return table;
61     }
62     table = new Grid(1, 2);
63
64     table.setText(0, 0, "Address");
65     table.setWidget(0, 1, email);
66     
67     return table;
68   }
69
70   public void validate(Widget widget) throws GeneratorException {
71     if (widget == email) {
72       getEmailField();
73     }
74   }
75
76   public void setFocus() {
77     email.setFocus(true);
78   }
79 }