2 * Copyright (C) 2010 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.web.generator.client;
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;
27 * A Generator for Wifi networks.
29 * @author Vikram Aggarwal
31 public class WifiGenerator implements GeneratorSource {
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 };
39 public WifiGenerator(ChangeListener changeListener, KeyPressHandler keyListener) {
40 networkType.addItem("WEP", "WEP");
41 networkType.addItem("WPA/WPA2", "WPA");
42 networkType.addItem("No encryption", "nopass");
44 for (TextBox w: widgets) {
45 w.addChangeListener(changeListener);
46 w.addKeyPressHandler(keyListener);
50 public String getName() {
51 return "Wifi network";
54 public String getText() throws GeneratorException {
55 String ssid = getSsidField();
56 String password = getPasswordField();
57 String networkType = getNetworkTypeField();
59 // Build the output with obtained data.
60 return getWifiString(ssid, password, networkType);
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);
70 return output.toString();
73 private static void maybeAppend(StringBuilder output, String prefix, String value) {
74 if (value.length() > 0) {
75 output.append(prefix).append(value).append(';');
79 private static String parseTextField(String name, TextBox textBox) throws GeneratorException {
80 String input = textBox.getText();
81 if (input.length() < 1) {
84 if (input.contains("\n")) {
85 throw new GeneratorException(name + " field must not contain \\n characters.");
87 input = input.replace(";", "\\;");
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.");
96 return parseTextField("SSID", ssid);
99 private String getPasswordField() throws GeneratorException {
100 return parseTextField("Password", password);
103 private String getNetworkTypeField() throws GeneratorException {
104 String input = networkType.getValue(networkType.getSelectedIndex());
108 public Grid getWidget() {
110 // early termination if the table has already been constructed
113 table = new Grid(8, 2);
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);
122 ssid.addStyleName(StylesDefs.INPUT_FIELD_REQUIRED);
126 public void validate(Widget widget) throws GeneratorException {
127 if (widget == ssid) getSsidField();
128 if (widget == password) getPasswordField();
129 if (widget == networkType) getNetworkTypeField();
132 public void setFocus() {