Small style stuff
[zxing.git] / zxing.appspot.com / generator / src / com / google / zxing / web / generator / client / WifiGenerator.java
1 /*
2  * Copyright (C) 2010 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.ListBox;
24 import com.google.gwt.user.client.ui.Widget;
25
26 /**
27  * A Generator for Wifi networks.
28  * 
29  * @author Vikram Aggarwal
30  */
31 public class WifiGenerator implements GeneratorSource {
32   Grid table = null;
33   TextBox ssid = new TextBox();
34   TextBox password = new TextBox();
35   final boolean multipleSelections = false;
36   ListBox networkType = new ListBox(multipleSelections);
37   TextBox[] widgets = {ssid, password };
38   
39   public WifiGenerator(ChangeListener changeListener, KeyPressHandler keyListener) {
40         networkType.addItem("WEP", "WEP");
41         networkType.addItem("WPA/WPA2", "WPA");
42         networkType.addItem("No encryption", "nopass");
43         
44     for (TextBox w: widgets) {
45       w.addChangeListener(changeListener);
46       w.addKeyPressHandler(keyListener);
47     }
48   }
49   
50   public String getName() {
51     return "Wifi network";
52   }
53
54   public String getText() throws GeneratorException {
55     String ssid = getSsidField();
56     String password = getPasswordField();
57     String networkType = getNetworkTypeField();
58     
59     // Build the output with obtained data.
60     return getWifiString(ssid, password, networkType);
61   }
62
63   private String getWifiString(String ssid, String password, String type) {
64     StringBuilder output = new StringBuilder();
65     output.append("WIFI:");
66     output.append("S:").append(ssid).append(';');
67     maybeAppend(output, "T:", type);
68     maybeAppend(output, "P:", password);
69     output.append(';');
70     return output.toString();
71   }
72
73   private static void maybeAppend(StringBuilder output, String prefix, String value) {
74     if (value.length() > 0) {
75       output.append(prefix).append(value).append(';');
76     }
77   }
78
79   private static String parseTextField(String name, TextBox textBox) throws GeneratorException {
80     String input = textBox.getText();
81     if (input.length() < 1) {
82       return "";
83     }
84     if (input.contains("\n")) {
85       throw new GeneratorException(name + " field must not contain \\n characters.");
86     }
87     input = input.replace(";", "\\;");
88     return input;
89   }
90   
91   private String getSsidField() throws GeneratorException {
92     String input = ssid.getText();
93     if (input.length() < 1) {
94       throw new GeneratorException("SSID must be at least 1 character.");
95     }
96     return parseTextField("SSID", ssid);
97   }
98   
99   private String getPasswordField() throws GeneratorException {
100         return parseTextField("Password", password);
101   }
102   
103   private String getNetworkTypeField() throws GeneratorException {
104         String input = networkType.getValue(networkType.getSelectedIndex());
105         return input;
106   }
107   
108   public Grid getWidget() {
109     if (table != null) {
110       // early termination if the table has already been constructed
111       return table;
112     }
113     table = new Grid(8, 2);
114     
115     table.setText(0, 0, "SSID");
116     table.setWidget(0, 1, ssid);
117     table.setText(1, 0, "Password");
118     table.setWidget(1, 1, password);
119     table.setText(2, 0, "Network Type");
120     table.setWidget(2, 1, networkType);
121
122     ssid.addStyleName(StylesDefs.INPUT_FIELD_REQUIRED);
123     return table;
124   }
125
126   public void validate(Widget widget) throws GeneratorException {
127     if (widget == ssid) getSsidField();
128     if (widget == password) getPasswordField();
129     if (widget == networkType) getNetworkTypeField();
130   }
131
132   public void setFocus() {
133     ssid.setFocus(true);
134   }
135 }