You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2009/10/01 15:17:57 UTC

svn commit: r820646 - in /incubator/pivot/trunk: core/src/org/apache/pivot/util/ tutorials/src/org/apache/pivot/tutorials/localization/ wtk/src/org/apache/pivot/wtk/

Author: gbrown
Date: Thu Oct  1 13:17:56 2009
New Revision: 820646

URL: http://svn.apache.org/viewvc?rev=820646&view=rev
Log:
Fix minor bug in Resources (language and country-specific resources were only being applied as overloads, not the root); add a localization tutorial; allow a caller to pass a resources argument to ScriptApplication.

Added:
    incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/localization/
    incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/localization/Localization.java
    incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/localization/Localization_en.json
    incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/localization/localization.wtkx
Modified:
    incubator/pivot/trunk/core/src/org/apache/pivot/util/Resources.java
    incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/ScriptApplication.java

Modified: incubator/pivot/trunk/core/src/org/apache/pivot/util/Resources.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/org/apache/pivot/util/Resources.java?rev=820646&r1=820645&r2=820646&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/org/apache/pivot/util/Resources.java (original)
+++ incubator/pivot/trunk/core/src/org/apache/pivot/util/Resources.java Thu Oct  1 13:17:56 2009
@@ -33,8 +33,8 @@
  * Reads a JSON resource at {@link #baseName} using
  * {@link ClassLoader#getResourceAsStream(String)}. It applies localization to
  * the resource using a method similar to that of
- * {@link java.util.ResourceBundle} in that it loads the base resource, then
- * applies a country specified resource over-writing the values in the base
+ * {@link java.util.ResourceBundle} in that it attempts to load the base resource,
+ * then applies a country specified resource over-writing the values in the base
  * using the country specified. It then does the same for country/language
  * specific.
  *
@@ -142,24 +142,32 @@
 
         String resourceName = baseName.replace('.', '/');
         resourceMap = readJSONResource(resourceName + ".json");
-        if (resourceMap == null) {
-            throw new MissingResourceException(
-                    "Can't find resource for base name " + baseName
-                            + ", locale " + locale, baseName, "");
-        }
 
-        // try to find resource for the language (e.g. resourceName_en)
+        // Try to find resource for the language (e.g. resourceName_en)
         Map<String, Object> overrideMap = readJSONResource(resourceName + "_"
-                + locale.getLanguage() + ".json");
+            + locale.getLanguage() + ".json");
+        if (overrideMap != null) {
+            if (resourceMap == null) {
+                resourceMap = overrideMap;
+            } else {
+                applyOverrides(resourceMap, overrideMap);
+            }
+        }
+
+        // Try to find resource for the entire locale (e.g. resourceName_en_GB)
+        overrideMap = readJSONResource(resourceName + "_" + locale.toString() + ".json");
         if (overrideMap != null) {
-            applyOverrides(resourceMap, overrideMap);
+            if (resourceMap == null) {
+                resourceMap = overrideMap;
+            } else {
+                applyOverrides(resourceMap, overrideMap);
+            }
         }
 
-        // try to find resource for the entire locale (e.g. resourceName_en_GB)
-        overrideMap = readJSONResource(resourceName + "_" + locale.toString()
-                + ".json");
-        if (null != overrideMap) {
-            applyOverrides(resourceMap, overrideMap);
+        if (resourceMap == null) {
+            throw new MissingResourceException(
+                "Can't find resource for base name " + baseName + ", locale "
+                    + locale, baseName, "");
         }
     }
 

Added: incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/localization/Localization.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/localization/Localization.java?rev=820646&view=auto
==============================================================================
--- incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/localization/Localization.java (added)
+++ incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/localization/Localization.java Thu Oct  1 13:17:56 2009
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pivot.tutorials.localization;
+
+import org.apache.pivot.collections.Map;
+import org.apache.pivot.util.Resources;
+import org.apache.pivot.wtk.Application;
+import org.apache.pivot.wtk.DesktopApplicationContext;
+import org.apache.pivot.wtk.Display;
+import org.apache.pivot.wtk.Window;
+import org.apache.pivot.wtkx.WTKXSerializer;
+
+public class Localization implements Application {
+    private Window window = null;
+
+    @Override
+    public void startup(Display display, Map<String, String> properties) throws Exception {
+        Resources resources = new Resources(getClass().getName());
+        WTKXSerializer wtkxSerializer = new WTKXSerializer(resources);
+
+        window = (Window)wtkxSerializer.readObject(this, "localization.wtkx");
+        window.open(display);
+    }
+
+    @Override
+    public boolean shutdown(boolean optional) {
+        if (window != null) {
+            window.close();
+        }
+
+        return false;
+    }
+
+    @Override
+    public void suspend() {
+    }
+
+    @Override
+    public void resume() {
+    }
+
+    public static void main(String[] args) {
+        DesktopApplicationContext.main(Localization.class, args);
+    }
+}

Added: incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/localization/Localization_en.json
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/localization/Localization_en.json?rev=820646&view=auto
==============================================================================
--- incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/localization/Localization_en.json (added)
+++ incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/localization/Localization_en.json Thu Oct  1 13:17:56 2009
@@ -0,0 +1,25 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+{   firstName: "First name",
+    lastName: "Last name",
+    street: "Street",
+    city: "City",
+    state: "State",
+    postalCode: "Zip",
+    country: "Country"
+}
+

Added: incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/localization/localization.wtkx
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/localization/localization.wtkx?rev=820646&view=auto
==============================================================================
--- incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/localization/localization.wtkx (added)
+++ incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/localization/localization.wtkx Thu Oct  1 13:17:56 2009
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to you under the Apache License,
+Version 2.0 (the "License"); you may not use this file except in
+compliance with the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+-->
+
+<Window title="Localization" maximized="true"
+    xmlns:wtkx="http://pivot.apache.org/wtkx"
+    xmlns="org.apache.pivot.wtk">
+    <windowStateListeners>
+        <wtkx:script>
+        <![CDATA[
+        function windowOpened(window) {
+            window.requestFocus();
+        }
+        ]]>
+        </wtkx:script>
+    </windowStateListeners>
+
+    <content>
+        <Form>
+            <sections>
+                <Form.Section>
+                    <TextInput Form.label="%firstName"/>
+                    <TextInput Form.label="%lastName"/>
+                    <TextInput Form.label="%street"/>
+                    <TextInput Form.label="%city"/>
+                    <TextInput Form.label="%state" textSize="4"/>
+                    <TextInput Form.label="%postalCode"/>
+                    <TextInput Form.label="%country"/>
+                </Form.Section>
+            </sections>
+        </Form>
+    </content>
+</Window>
+

Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/ScriptApplication.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/ScriptApplication.java?rev=820646&r1=820645&r2=820646&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/ScriptApplication.java (original)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/ScriptApplication.java Thu Oct  1 13:17:56 2009
@@ -17,6 +17,7 @@
 package org.apache.pivot.wtk;
 
 import org.apache.pivot.collections.Map;
+import org.apache.pivot.util.Resources;
 import org.apache.pivot.wtkx.WTKXSerializer;
 
 /**
@@ -25,26 +26,23 @@
 public class ScriptApplication implements Application {
     private Window window = null;
 
-    public static final String SRC_ARGUMENT = "src";
+    public static final String SRC_KEY = "src";
+    public static final String RESOURCES_KEY = "resources";
 
     @Override
     public void startup(Display display, Map<String, String> properties)
         throws Exception {
-        String src = null;
-
-        WTKXSerializer wtkxSerializer = new WTKXSerializer();
-        for (String property : properties) {
-            if (property.equals(SRC_ARGUMENT)) {
-                src = properties.get(property);
-            } else {
-                wtkxSerializer.put(property, properties.get(property));
-            }
+        String src = properties.get(SRC_KEY);
+        if (src == null) {
+            throw new IllegalArgumentException(SRC_KEY + " argument is required.");
         }
 
-        if (src == null) {
-            throw new IllegalArgumentException(SRC_ARGUMENT + " argument is required.");
+        Resources resources = null;
+        if (properties.containsKey(RESOURCES_KEY)) {
+            resources = new Resources(properties.get(RESOURCES_KEY));
         }
 
+        WTKXSerializer wtkxSerializer = new WTKXSerializer(resources);
         window = (Window)wtkxSerializer.readObject(src);
         window.open(display);
     }