Add address line 2 support to generator
[zxing.git] / zxing.appspot.com / generator / src / com / google / zxing / web / generator / client / TextGenerator.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.TextArea;
22 import com.google.gwt.user.client.ui.Widget;
23
24 /**
25  * A generator for any text content.
26  * 
27  * @author Yohann Coppel
28  */
29 public class TextGenerator implements GeneratorSource {
30   TextArea text = new TextArea();
31   Grid table = null;
32   String error = "";
33   
34   public TextGenerator(ChangeListener listener) {
35     text.addStyleName(StylesDefs.INPUT_FIELD_REQUIRED);
36     text.addChangeListener(listener);
37   }
38   
39   public String getName() {
40     return "Text";
41   }
42
43   public String getText() throws GeneratorException {
44     return getTextField();
45   }
46   
47   public String getTextField() throws GeneratorException {
48     String input = text.getText();
49     if (input.length() == 0) {
50       throw new GeneratorException("Text should be at least 1 character.");
51     }
52     return input;
53   }
54
55   public Grid getWidget() {
56     if (table != null) {
57       // early termination if the table has already been constructed
58       return table;
59     }
60     
61     table = new Grid(1, 2);
62     table.getColumnFormatter().addStyleName(0, "firstColumn");
63     
64     table.setText(0, 0, "Text content");
65     table.setWidget(0, 1, text);
66     
67     return table;
68   }
69
70   public String getErrorMessage() {
71     return error;
72   }
73
74   public void validate(Widget widget) throws GeneratorException {
75     if (widget == text) {
76       getTextField();
77     }
78   }
79   
80   public void setFocus() {
81     text.setFocus(true);
82   }
83 }