bb5d23c98c706d56f3b310594e90d1afb0ff2409
[zxing.git] / zxing.appspot.com / generator / src / com / google / zxing / web / generator / client / UrlGenerator.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 URL addresses.
26  * 
27  * @author Yohann Coppel
28  */
29 public class UrlGenerator implements GeneratorSource {
30   Grid table = null;
31   TextBox url = new TextBox();
32   
33   public UrlGenerator(ChangeListener listener) {
34     url.addStyleName(StylesDefs.INPUT_FIELD_REQUIRED);
35     url.addChangeListener(listener);
36   }
37   
38   public Grid getWidget() {
39     if (table != null) {
40       // early termination if the table has already been constructed
41       return table;
42     }
43     
44     table = new Grid(1, 2);
45     table.getColumnFormatter().addStyleName(0, "firstColumn");
46     
47     url.setText("http://");
48     
49     table.setText(0, 0, "URL");
50     table.setWidget(0, 1, url);
51     
52     return table;
53   }
54
55   public String getName() {
56     return "URL";
57   }
58
59   public String getText() throws GeneratorException {
60     String input = getUrlField();
61     return input;
62   }
63
64   private String getUrlField() throws GeneratorException {
65     String input = url.getText();
66     Validators.validateUrl(input);
67     return input;
68   }
69   
70   public void validate(Widget widget) throws GeneratorException {
71     if (widget == url) getUrlField();
72   }
73   
74   public void setFocus() {
75     url.setFocus(true);
76   }
77 }