From: Jouni Koivuviita Date: Tue, 19 Jun 2007 11:39:21 +0000 (+0000) Subject: Added drafting classes for locale handling. X-Git-Tag: 6.7.0.beta1~6224 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ea15bacee1ff61429b0185f64eb08e6243d47f7f;p=vaadin-framework.git Added drafting classes for locale handling. Changed most component classnames to public. Added a new method hasVariable to UIDL class. Client now uses POST for requests and has some drafting methods for locale loading. svn changeset:1767/svn branch:trunk --- diff --git a/src/com/itmill/toolkit/terminal/gwt/client/Client.java b/src/com/itmill/toolkit/terminal/gwt/client/Client.java index 1cc34dad2e..f623c12727 100755 --- a/src/com/itmill/toolkit/terminal/gwt/client/Client.java +++ b/src/com/itmill/toolkit/terminal/gwt/client/Client.java @@ -82,7 +82,7 @@ public class Client implements EntryPoint { private void makeUidlRequest(String requestData) { console.log("Making UIDL Request with params: " + requestData); - rb = new RequestBuilder(RequestBuilder.GET, appUri + rb = new RequestBuilder(RequestBuilder.POST, appUri + "/UIDL/?requestId=" + (Math.random()) + "&" + requestData); try { rb.sendRequest(requestData, new RequestCallback() { @@ -353,4 +353,30 @@ public class Client implements EntryPoint { return (String) resourcesMap.get(name); } + public JSONObject getLocale(String locale) { + // TODO should perform synchronous call to server to fetch + // locale specific strings + // (GWT only supports synchrounous requests from v. 1.4) + console.log("Loading a new locale: " + locale); + rb = new RequestBuilder(RequestBuilder.POST, appUri + + "/locale/?requestId=" + (Math.random()) + "&" + locale); + /*try { + rb.sendRequest(locale, new RequestCallback() { + public void onError(Request request, Throwable exception) { + console.error("Got error"); + } + + public void onResponseReceived(Request request, + Response response) { + handleReceivedJSONMessage(response); + } + + }); + + } catch (RequestException e) { + console.error(e.getMessage()); + }*/ + // TODO + return null; + } } diff --git a/src/com/itmill/toolkit/terminal/gwt/client/DateTimeService.java b/src/com/itmill/toolkit/terminal/gwt/client/DateTimeService.java new file mode 100644 index 0000000000..05d2d15ff8 --- /dev/null +++ b/src/com/itmill/toolkit/terminal/gwt/client/DateTimeService.java @@ -0,0 +1,44 @@ +package com.itmill.toolkit.terminal.gwt.client; + +/** + * This class provides date/time parsing services to all components. + * + * @author Jouni Koivuviita + * + */ +public class DateTimeService { + + private LocaleService localeService; + + private String currentLocale; + + public DateTimeService(Client client) { + localeService = new LocaleService(client); + } + + public DateTimeService(Client client, String locale) { + this(client); + setLocale(locale); + } + + public void setLocale(String locale) { + currentLocale = locale; + } + + public String getLocale() { + return currentLocale; + } + + public String getMonth(int month) throws Exception { + if(currentLocale != null) + return localeService.getMonthNames(currentLocale)[month]; + else throw new Exception("No locale specified."); + } + + public String getShortMonth(int month) throws Exception { + if(currentLocale != null) + return localeService.getShortMonthNames(currentLocale)[month]; + else throw new Exception("No locale specified."); + } + +} diff --git a/src/com/itmill/toolkit/terminal/gwt/client/DefaultWidgetFactory.java b/src/com/itmill/toolkit/terminal/gwt/client/DefaultWidgetFactory.java index 8c7d224be0..b967b76654 100644 --- a/src/com/itmill/toolkit/terminal/gwt/client/DefaultWidgetFactory.java +++ b/src/com/itmill/toolkit/terminal/gwt/client/DefaultWidgetFactory.java @@ -6,6 +6,7 @@ import com.itmill.toolkit.terminal.gwt.client.ui.IButton; import com.itmill.toolkit.terminal.gwt.client.ui.ICheckBox; import com.itmill.toolkit.terminal.gwt.client.ui.IComponent; import com.itmill.toolkit.terminal.gwt.client.ui.ICustomLayout; +import com.itmill.toolkit.terminal.gwt.client.ui.IDateField; import com.itmill.toolkit.terminal.gwt.client.ui.IEmbedded; import com.itmill.toolkit.terminal.gwt.client.ui.IGridLayout; import com.itmill.toolkit.terminal.gwt.client.ui.IHorizontalLayout; @@ -75,6 +76,8 @@ public class DefaultWidgetFactory implements WidgetFactory { } if ("table".equals(tag)) return new ITable(); + if("datefield".equals(tag)) + return new IDateField(); return new IUnknownComponent(); } diff --git a/src/com/itmill/toolkit/terminal/gwt/client/LocaleService.java b/src/com/itmill/toolkit/terminal/gwt/client/LocaleService.java new file mode 100644 index 0000000000..3d369dd37b --- /dev/null +++ b/src/com/itmill/toolkit/terminal/gwt/client/LocaleService.java @@ -0,0 +1,81 @@ +package com.itmill.toolkit.terminal.gwt.client; + +import java.util.HashMap; +import java.util.Map; + +import com.google.gwt.json.client.JSONObject; + +/** + * Date / time etc. localisation service for all widgets. + * Should cache all loaded locales as JSON strings. + * + * @author Jouni Koivuviita + * + */ +public class LocaleService { + + private Client client; + + private Map cache = new HashMap(); + + public LocaleService(Client client){ + this.client = client; + } + + private void loadLocale(String locale) { + JSONObject resp = client.getLocale(locale); + cache.put(locale, resp); + } + + public String[] getMonthNames(String locale) { + // TODO + //if(cache.containsKey(locale)) + //else loadLocale(locale); + String[] temp = new String[12]; + temp[0] = "tammi"; temp[1] = "helmi"; temp[2] = "maalis"; temp[3] = "huhti"; + temp[4] = "touko"; temp[5] = "kesä"; temp[6] = "heinä"; temp[7] = "elo"; + temp[8] = "syys"; temp[9] = "loka"; temp[10] = "marras"; temp[11] = "joulu"; + return temp; + } + + public String[] getShortMonthNames(String locale) { + // TODO + //if(cache.containsKey(locale)) + //else loadLocale(locale); + String[] temp = new String[12]; + temp[0] = "tam"; temp[1] = "hel"; temp[2] = "maa"; temp[3] = "huh"; + temp[4] = "tou"; temp[5] = "kes"; temp[6] = "hei"; temp[7] = "elo"; + temp[8] = "syy"; temp[9] = "lok"; temp[10] = "mar"; temp[11] = "jou"; + return temp; + } + + public String[] getDayNames(String locale) { + // TODO + //if(cache.containsKey(locale)) + //else loadLocale(locale); + String[] temp = new String[7]; + temp[1] = "maanatai"; temp[2] = "tiistai"; temp[3] = "keskiviikko"; + temp[4] = "torstai"; temp[5] = "perjantai"; temp[6] = "lauantai"; + temp[0] = "sunnuntai"; + return temp; + } + + public String[] getShortDayNames(String locale) { + // TODO + //if(cache.containsKey(locale)) + //else loadLocale(locale); + String[] temp = new String[7]; + temp[1] = "ma"; temp[2] = "ti"; temp[3] = "ke"; + temp[4] = "to"; temp[5] = "pe"; temp[6] = "la"; + temp[0] = "su"; + return temp; + } + + public int getFirstDayOfWeek(String locale) { + // TODO + //if(cache.containsKey(locale)) + //else loadLocale(locale); + return 1; + } + +} diff --git a/src/com/itmill/toolkit/terminal/gwt/client/UIDL.java b/src/com/itmill/toolkit/terminal/gwt/client/UIDL.java index 43abb38ff7..d4789e7e1b 100644 --- a/src/com/itmill/toolkit/terminal/gwt/client/UIDL.java +++ b/src/com/itmill/toolkit/terminal/gwt/client/UIDL.java @@ -265,6 +265,14 @@ public class UIDL { throw new IllegalArgumentException("No variables defined in tag."); return v; } + + public boolean hasVariable(String name) { + Object v = null; + try { + v = getVariableHash().get(name); + } catch(IllegalArgumentException e) {} + return v != null; + } public String getStringVariable(String name) { JSONString t = (JSONString) getVariableHash().get(name); diff --git a/src/com/itmill/toolkit/terminal/gwt/client/ui/IButton.java b/src/com/itmill/toolkit/terminal/gwt/client/ui/IButton.java index 7868f9e63c..a4626a3bf8 100644 --- a/src/com/itmill/toolkit/terminal/gwt/client/ui/IButton.java +++ b/src/com/itmill/toolkit/terminal/gwt/client/ui/IButton.java @@ -9,7 +9,7 @@ import com.itmill.toolkit.terminal.gwt.client.UIDL; public class IButton extends Button implements Paintable { - private static final String CLASSNAME = "i-button"; + public static final String CLASSNAME = "i-button"; String id; diff --git a/src/com/itmill/toolkit/terminal/gwt/client/ui/IDateField.java b/src/com/itmill/toolkit/terminal/gwt/client/ui/IDateField.java new file mode 100644 index 0000000000..802853e6fe --- /dev/null +++ b/src/com/itmill/toolkit/terminal/gwt/client/ui/IDateField.java @@ -0,0 +1,126 @@ +package com.itmill.toolkit.terminal.gwt.client.ui; + +import java.util.Date; + +import com.google.gwt.user.client.ui.ChangeListener; +import com.google.gwt.user.client.ui.Composite; +import com.google.gwt.user.client.ui.FlowPanel; +import com.google.gwt.user.client.ui.ListBox; +import com.google.gwt.user.client.ui.Widget; +import com.itmill.toolkit.terminal.gwt.client.Client; +import com.itmill.toolkit.terminal.gwt.client.Paintable; +import com.itmill.toolkit.terminal.gwt.client.UIDL; + +public class IDateField extends Composite implements Paintable, ChangeListener { + + + /* + * This implementation is old already. + * We should use the DateTimeService class (which is a draft) to get + * locale specific strings and use them to build the datefield + * and its different styles (with inheritance of course). + * + * Drafting usage patterns: + * + * DateTimeService dts = new DateTimeService(client, "fi_FI"); + * String month = dts.getMonth(0); // Returns "January" + * String day = dts.getDay(19); // Returns e.g. "sunday" + * String dateformat = dts.getDateFormat(); // Returns something like MM_/_DD_/_YYYY + * String timeformat = dts.getTimeFormat(); // Returns something like HH_:_MM + * String date = dts.parseDate(new Date()); // Returns e.g. 6/19/2007 14:32 + * String date = dts.parseFullDate(new Date()); // Returns e.g. Tuesday 6th June 2007 14:32 + */ + + + public static final String CLASSNAME = "i-datefield"; + + String id; + + Client client; + + private boolean immediate; + + private FlowPanel container; + + private ListBox year; + + private static int RESOLUTION_YEAR = 0; + private static int RESOLUTION_MONTH = 1; + private static int RESOLUTION_DAY = 2; + private static int RESOLUTION_HOUR = 3; + private static int RESOLUTION_MIN = 4; + private static int RESOLUTION_SEC = 5; + private static int RESOLUTION_MSEC = 6; + + private int currentResolution = RESOLUTION_YEAR; + + public IDateField() { + container = new FlowPanel(); + initWidget(container); + } + + public void updateFromUIDL(UIDL uidl, Client client) { + // Ensure correct implementation and let layout manage caption + if (client.updateComponent(this, uidl, true)) + return; + + // Save details + this.client = client; + id = uidl.getId(); + immediate = uidl.getBooleanAttribute("immediate"); + + int newResolution = RESOLUTION_YEAR; + if(uidl.hasAttribute("month")) + newResolution = RESOLUTION_MONTH; + if(uidl.hasAttribute("day")) + newResolution = RESOLUTION_DAY; + if(uidl.hasAttribute("hour")) + newResolution = RESOLUTION_HOUR; + if(uidl.hasAttribute("min")) + newResolution = RESOLUTION_MIN; + if(uidl.hasAttribute("sec")) + newResolution = RESOLUTION_SEC; + if(uidl.hasAttribute("msec")) + newResolution = RESOLUTION_MSEC; + + if(currentResolution > newResolution) + container.clear(); + + if(uidl.hasVariable("year")) { + int selectedYear = uidl.getIntVariable("year"); + int y = container.getWidgetIndex(year); + if(y > -1) { + year = (ListBox) container.getWidget(y); + // Deselect old value + year.setItemSelected(year.getSelectedIndex(), false); + // and select new + for(int i=0; i < year.getItemCount(); i++) + if(year.getValue(i).equals(""+selectedYear)) { + year.setSelectedIndex(i); + break; + } + } else { + year = new ListBox(); + year.setStyleName(ISelect.CLASSNAME); + int today = 1900 + (new Date()).getYear(); + for(int i=1970; i