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