Fix for #148 and possibly #149 plus a few code polishes here and there

git-svn-id: https://zxing.googlecode.com/svn/trunk@868 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2009-03-01 12:40:43 +00:00
parent b24fbdd7b0
commit 0c4c9fdb81
8 changed files with 76 additions and 62 deletions

View file

@ -40,9 +40,9 @@ import java.util.Date;
* @author Yohann Coppel * @author Yohann Coppel
*/ */
public class CalendarEventGenerator implements GeneratorSource { public class CalendarEventGenerator implements GeneratorSource {
public final static String[] FULL_DAY_ONLY_IDS = { "fullDayOnlyInfo1", public static final String[] FULL_DAY_ONLY_IDS = { "fullDayOnlyInfo1",
"fullDayOnlyInfo2", "fullDayOnlyInfo3", "fullDayOnlyInfo4" }; "fullDayOnlyInfo2", "fullDayOnlyInfo3", "fullDayOnlyInfo4" };
private final static long ONE_HOUR = 60 * 60 * 1000; private static final long ONE_HOUR = 60L * 60 * 1000;
Grid table = null; Grid table = null;
TextBox eventName = new TextBox(); TextBox eventName = new TextBox();
@ -77,7 +77,7 @@ public class CalendarEventGenerator implements GeneratorSource {
Date time2 = timePicker2.getDateTime(); Date time2 = timePicker2.getDateTime();
if (time2.after(time)) { if (time2.after(time)) {
// keep the same time difference if the interval is valid. // keep the same time difference if the interval is valid.
long diff = timeDifference(time, time2); long diff = time2.getTime() - time.getTime();
timePicker2.setDateTime(addMilliseconds(time1, diff)); timePicker2.setDateTime(addMilliseconds(time1, diff));
} else { } else {
// otherwise erase the end date and set it to startdate + one hour. // otherwise erase the end date and set it to startdate + one hour.
@ -98,8 +98,7 @@ public class CalendarEventGenerator implements GeneratorSource {
private void buildTimeZoneList() { private void buildTimeZoneList() {
for (TimeZoneInfo info : TimeZoneList.TIMEZONES) { for (TimeZoneInfo info : TimeZoneList.TIMEZONES) {
timeZones.addItem(info.GMTRelative + " " + info.abreviation, "" timeZones.addItem(info.GMTRelative + ' ' + info.abreviation, String.valueOf(info.gmtDiff));
+ info.gmtDiff);
} }
} }
@ -157,10 +156,7 @@ public class CalendarEventGenerator implements GeneratorSource {
private void setFullDay(boolean fullDay) { private void setFullDay(boolean fullDay) {
for (String s : FULL_DAY_ONLY_IDS) { for (String s : FULL_DAY_ONLY_IDS) {
Element element = DOM.getElementById(s); Element element = DOM.getElementById(s);
String style = ""; String style = fullDay ? "none" : "";
if (fullDay) {
style = "none";
}
DOM.setStyleAttribute(element, "display", style); DOM.setStyleAttribute(element, "display", style);
} }
} }
@ -168,13 +164,12 @@ public class CalendarEventGenerator implements GeneratorSource {
public String getText() throws GeneratorException { public String getText() throws GeneratorException {
String eventName = getEventNameField(); String eventName = getEventNameField();
String dates = getDateTimeFields(); String dates = getDateTimeFields();
StringBuilder output = new StringBuilder();
String output = ""; output.append("BEGIN:VEVENT\r\n");
output += "BEGIN:VEVENT\n"; output.append(eventName);
output += eventName; output.append(dates);
output += dates; output.append("END:VEVENT\r\n");
output += "END:VEVENT\n"; return output.toString();
return output;
} }
private String getEventNameField() throws GeneratorException { private String getEventNameField() throws GeneratorException {
@ -186,7 +181,7 @@ public class CalendarEventGenerator implements GeneratorSource {
throw new GeneratorException( throw new GeneratorException(
"Event name should not contain \\n characters."); "Event name should not contain \\n characters.");
} }
return "SUMMARY:" + inputName + "\n"; return "SUMMARY:" + inputName + "\r\n";
} }
private String getDateTimeFields() throws GeneratorException { private String getDateTimeFields() throws GeneratorException {
@ -206,10 +201,14 @@ public class CalendarEventGenerator implements GeneratorSource {
throw new GeneratorException("Ending date is after starting date."); throw new GeneratorException("Ending date is after starting date.");
} }
DateTimeFormat isoFormatter = DateTimeFormat.getFormat("yyyyMMdd"); DateTimeFormat isoFormatter = DateTimeFormat.getFormat("yyyyMMdd");
String output = ""; StringBuilder output = new StringBuilder();
output += "DTSTART:" + isoFormatter.format(date1) + "\n"; output.append("DTSTART:");
output += "DTEND:" + isoFormatter.format(date2) + "\n"; output.append(isoFormatter.format(date1));
return output; output.append("\r\n");
output.append("DTEND:");
output.append(isoFormatter.format(date2));
output.append("\r\n");
return output.toString();
} }
private String getDateTimeValues() throws GeneratorException { private String getDateTimeValues() throws GeneratorException {
@ -233,40 +232,41 @@ public class CalendarEventGenerator implements GeneratorSource {
throw new GeneratorException("Ending date is after starting date."); throw new GeneratorException("Ending date is after starting date.");
} }
DateTimeFormat isoFormatter = DateTimeFormat DateTimeFormat isoFormatter = DateTimeFormat
.getFormat("yyyyMMdd'T'kkmmss'Z'"); .getFormat("yyyyMMdd'T'HHmmss'Z'");
String output = ""; StringBuilder output = new StringBuilder();
output += "DTSTART:" + isoFormatter.format(dateTime1) + "\n"; output.append("DTSTART:");
output += "DTEND:" + isoFormatter.format(dateTime2) + "\n"; output.append(isoFormatter.format(dateTime1));
return output; output.append("\r\n");
output.append("DTEND:");
output.append(isoFormatter.format(dateTime2));
output.append("\r\n");
return output.toString();
} }
private Date mergeDateAndTime(Date date, Date time) { private static Date mergeDateAndTime(Date date, Date time) {
// Is that the only ugly way to do with GWT ? given that we don't // Is that the only ugly way to do with GWT ? given that we don't
// have java.util.Calendar for instance // have java.util.Calendar for instance
DateTimeFormat extractDate = DateTimeFormat.getFormat("yyyyMMdd"); DateTimeFormat extractDate = DateTimeFormat.getFormat("yyyyMMdd");
DateTimeFormat extractTime = DateTimeFormat.getFormat("kkmm"); DateTimeFormat extractTime = DateTimeFormat.getFormat("HHmm");
DateTimeFormat merger = DateTimeFormat.getFormat("yyyyMMddkkmmss"); DateTimeFormat merger = DateTimeFormat.getFormat("yyyyMMddHHmmss");
String d = extractDate.format(date); String d = extractDate.format(date);
String t = extractTime.format(time) + "00"; String t = extractTime.format(time) + "00";
return merger.parse(d + t); return merger.parse(d + t);
} }
public void validate(Widget widget) throws GeneratorException { public void validate(Widget widget) throws GeneratorException {
if (widget == eventName) if (widget == eventName) {
getEventNameField(); getEventNameField();
if (widget == datePicker1 || widget == timePicker1 || widget == datePicker2 } else if (widget == datePicker1 || widget == timePicker1 || widget == datePicker2
|| widget == timePicker2) || widget == timePicker2) {
getDateTimeFields(); getDateTimeFields();
}
} }
private static Date addMilliseconds(Date time1, long milliseconds) { private static Date addMilliseconds(Date time1, long milliseconds) {
return new Date(time1.getTime() + milliseconds); return new Date(time1.getTime() + milliseconds);
} }
private static long timeDifference(Date time1, Date time2) {
return time2.getTime() - time1.getTime();
}
public void setFocus() { public void setFocus() {
eventName.setFocus(true); eventName.setFocus(true);
} }

View file

@ -66,7 +66,9 @@ public class EmailGenerator implements GeneratorSource {
} }
public void validate(Widget widget) throws GeneratorException { public void validate(Widget widget) throws GeneratorException {
if (widget == email) getEmailField(); if (widget == email) {
getEmailField();
}
} }
public void setFocus() { public void setFocus() {

View file

@ -80,11 +80,11 @@ public class GeoLocationGenerator implements GeneratorSource, ChangeListener {
String lat = getLatitudeField(); String lat = getLatitudeField();
String lon = getLongitudeField(); String lon = getLongitudeField();
if (que.length() > 0) { if (null != que && que.length() > 0) {
return "geo:"+lat+","+lon+"?q="+que; return "geo:"+lat+ ',' +lon+"?q="+que;
} }
return "geo:"+lat+","+lon; return "geo:"+lat+ ',' +lon;
} }
private String getQueryField() { private String getQueryField() {
@ -159,16 +159,16 @@ public class GeoLocationGenerator implements GeneratorSource, ChangeListener {
} }
protected void mapClick(MapClickEvent event) { protected void mapClick(MapClickEvent event) {
latitude.setText("" + event.getLatLng().getLatitude()); latitude.setText(String.valueOf(event.getLatLng().getLatitude()));
longitude.setText("" + event.getLatLng().getLongitude()); longitude.setText(String.valueOf(event.getLatLng().getLongitude()));
setMapMarker(event.getLatLng().getLatitude(), event.getLatLng().getLongitude(), false); setMapMarker(event.getLatLng().getLatitude(), event.getLatLng().getLongitude(), false);
changeListener.onChange(latitude); changeListener.onChange(latitude);
changeListener.onChange(longitude); changeListener.onChange(longitude);
} }
protected void mapMarkerMoved() { protected void mapMarkerMoved() {
latitude.setText("" + mapMarker.getLatLng().getLatitude()); latitude.setText(String.valueOf(mapMarker.getLatLng().getLatitude()));
longitude.setText("" + mapMarker.getLatLng().getLongitude()); longitude.setText(String.valueOf(mapMarker.getLatLng().getLongitude()));
changeListener.onChange(latitude); changeListener.onChange(latitude);
changeListener.onChange(longitude); changeListener.onChange(longitude);
} }
@ -216,17 +216,17 @@ public class GeoLocationGenerator implements GeneratorSource, ChangeListener {
String lat = ""; String lat = "";
String lon = ""; String lon = "";
if (link.matches(".*&s?ll=[^&]*&.*")) { if (link.matches(".*&s?ll=[^&]*&.*")) {
boolean beforeComa = true; int start;
int start = 0;
if (link.indexOf("&sll=") == -1) { if (link.indexOf("&sll=") == -1) {
start = link.indexOf("&ll=") + 4; start = link.indexOf("&ll=") + 4;
} else { } else {
start = link.indexOf("&sll=") + 5; start = link.indexOf("&sll=") + 5;
} }
boolean beforeComma = true;
for (int i = start; i < link.length() && link.charAt(i) != '&'; ++i) { for (int i = start; i < link.length() && link.charAt(i) != '&'; ++i) {
if (beforeComa) { if (beforeComma) {
if (link.charAt(i) == ',') { if (link.charAt(i) == ',') {
beforeComa = false; beforeComma = false;
} else { } else {
lat += link.charAt(i); lat += link.charAt(i);
} }
@ -245,8 +245,12 @@ public class GeoLocationGenerator implements GeneratorSource, ChangeListener {
} }
public void validate(Widget widget) throws GeneratorException { public void validate(Widget widget) throws GeneratorException {
if (widget == latitude) getLatitudeField(); if (widget == latitude) {
if (widget == longitude) getLongitudeField(); getLatitudeField();
}
if (widget == longitude) {
getLongitudeField();
}
} }
public void setFocus() { public void setFocus() {

View file

@ -67,7 +67,9 @@ public class PhoneNumberGenerator implements GeneratorSource {
} }
public void validate(Widget widget) throws GeneratorException { public void validate(Widget widget) throws GeneratorException {
if (widget == number) getTelField(); if (widget == number) {
getTelField();
}
} }
public void setFocus() { public void setFocus() {

View file

@ -50,7 +50,7 @@ public class SmsAddressGenerator implements GeneratorSource {
String output = inputNumber; String output = inputNumber;
// we add the text only if there actually is something in the field. // we add the text only if there actually is something in the field.
if (inputMessage.length() > 0) { if (inputMessage.length() > 0) {
output += ":" + inputMessage; output += ':' + inputMessage;
} }
return "smsto:" + output; return "smsto:" + output;
@ -90,8 +90,12 @@ public class SmsAddressGenerator implements GeneratorSource {
} }
public void validate(Widget widget) throws GeneratorException { public void validate(Widget widget) throws GeneratorException {
if (widget == number) getTelField(); if (widget == number) {
if (widget == message) getMessageField(); getTelField();
}
if (widget == message) {
getMessageField();
}
} }
public void setFocus() { public void setFocus() {

View file

@ -41,8 +41,7 @@ public class TextGenerator implements GeneratorSource {
} }
public String getText() throws GeneratorException { public String getText() throws GeneratorException {
String message = getTextField(); return getTextField();
return message;
} }
public String getTextField() throws GeneratorException { public String getTextField() throws GeneratorException {
@ -73,7 +72,9 @@ public class TextGenerator implements GeneratorSource {
} }
public void validate(Widget widget) throws GeneratorException { public void validate(Widget widget) throws GeneratorException {
if (widget == text) getTextField(); if (widget == text) {
getTextField();
}
} }
public void setFocus() { public void setFocus() {

View file

@ -37,8 +37,8 @@ public class TimeZoneList {
} }
} }
private final static long ONE_HOUR = 60*60*1000; private static final long ONE_HOUR = 60L*60*1000;
private final static long THIRTY_MIN = 30*60*1000; private static final long THIRTY_MIN = 30L*60*1000;
public static final TimeZoneInfo[] TIMEZONES = { public static final TimeZoneInfo[] TIMEZONES = {
new TimeZoneInfo("GMT", "Greenwich Mean Time", "GMT", 0 * ONE_HOUR + 0 * THIRTY_MIN), // 0 new TimeZoneInfo("GMT", "Greenwich Mean Time", "GMT", 0 * ONE_HOUR + 0 * THIRTY_MIN), // 0

View file

@ -57,8 +57,7 @@ public class UrlGenerator implements GeneratorSource {
} }
public String getText() throws GeneratorException { public String getText() throws GeneratorException {
String input = getUrlField(); return getUrlField();
return input;
} }
private String getUrlField() throws GeneratorException { private String getUrlField() throws GeneratorException {
@ -68,7 +67,9 @@ public class UrlGenerator implements GeneratorSource {
} }
public void validate(Widget widget) throws GeneratorException { public void validate(Widget widget) throws GeneratorException {
if (widget == url) getUrlField(); if (widget == url) {
getUrlField();
}
} }
public void setFocus() { public void setFocus() {