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/09/30 20:36:17 UTC

svn commit: r820378 - in /incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/backgroundtasks: ./ BackgroundTasks.java SleepTask.java background_tasks.wtkx

Author: gbrown
Date: Wed Sep 30 18:36:17 2009
New Revision: 820378

URL: http://svn.apache.org/viewvc?rev=820378&view=rev
Log:
Add background task tutorial.

Added:
    incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/backgroundtasks/
    incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/backgroundtasks/BackgroundTasks.java
    incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/backgroundtasks/SleepTask.java
    incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/backgroundtasks/background_tasks.wtkx

Added: incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/backgroundtasks/BackgroundTasks.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/backgroundtasks/BackgroundTasks.java?rev=820378&view=auto
==============================================================================
--- incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/backgroundtasks/BackgroundTasks.java (added)
+++ incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/backgroundtasks/BackgroundTasks.java Wed Sep 30 18:36:17 2009
@@ -0,0 +1,127 @@
+/*
+ * 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.backgroundtasks;
+
+import org.apache.pivot.collections.Map;
+import org.apache.pivot.util.concurrent.Task;
+import org.apache.pivot.util.concurrent.TaskExecutionException;
+import org.apache.pivot.util.concurrent.TaskListener;
+import org.apache.pivot.wtk.ActivityIndicator;
+import org.apache.pivot.wtk.Application;
+import org.apache.pivot.wtk.Button;
+import org.apache.pivot.wtk.ButtonPressListener;
+import org.apache.pivot.wtk.DesktopApplicationContext;
+import org.apache.pivot.wtk.Display;
+import org.apache.pivot.wtk.PushButton;
+import org.apache.pivot.wtk.TaskAdapter;
+import org.apache.pivot.wtk.Window;
+import org.apache.pivot.wtkx.WTKXSerializer;
+
+public class BackgroundTasks implements Application {
+    private Window window = null;
+
+    private ActivityIndicator activityIndicator = null;
+    private PushButton executeSynchronousButton = null;
+    private PushButton executeAsynchronousButton = null;
+
+    @Override
+    public void startup(Display display, Map<String, String> properties) throws Exception {
+        WTKXSerializer wtkxSerializer = new WTKXSerializer();
+        window = (Window)wtkxSerializer.readObject(this, "background_tasks.wtkx");
+
+        activityIndicator = (ActivityIndicator)wtkxSerializer.get("activityIndicator");
+
+        executeSynchronousButton = (PushButton)wtkxSerializer.get("executeSynchronousButton");
+        executeSynchronousButton.getButtonPressListeners().add(new ButtonPressListener() {
+            @Override
+            public void buttonPressed(Button button) {
+                activityIndicator.setActive(true);
+
+                System.out.println("Starting synchronous task execution.");
+
+                SleepTask sleepTask = new SleepTask();
+
+                String result = null;
+                try {
+                    result = sleepTask.execute();
+                } catch (TaskExecutionException exception) {
+                    System.err.println(exception);
+                }
+
+                System.out.println("Synchronous task execution complete: \"" + result + "\"");
+
+                activityIndicator.setActive(false);
+            }
+        });
+
+        executeAsynchronousButton = (PushButton)wtkxSerializer.get("executeAsynchronousButton");
+        executeAsynchronousButton.getButtonPressListeners().add(new ButtonPressListener() {
+            @Override
+            public void buttonPressed(Button button) {
+                activityIndicator.setActive(true);
+                window.setEnabled(false);
+
+                System.out.println("Starting asynchronous task execution.");
+
+                SleepTask sleepTask = new SleepTask();
+                TaskListener<String> taskListener = new TaskListener<String>() {
+                    @Override
+                    public void taskExecuted(Task<String> task) {
+                        activityIndicator.setActive(false);
+                        window.setEnabled(true);
+
+                        System.out.println("Synchronous task execution complete: \""
+                            + task.getResult() + "\"");
+                    }
+
+                    @Override
+                    public void executeFailed(Task<String> task) {
+                        activityIndicator.setActive(false);
+                        window.setEnabled(true);
+
+                        System.err.println(task.getFault());
+                    }
+                };
+
+                sleepTask.execute(new TaskAdapter<String>(taskListener));
+            }
+        });
+
+        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(BackgroundTasks.class, args);
+    }
+}

Added: incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/backgroundtasks/SleepTask.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/backgroundtasks/SleepTask.java?rev=820378&view=auto
==============================================================================
--- incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/backgroundtasks/SleepTask.java (added)
+++ incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/backgroundtasks/SleepTask.java Wed Sep 30 18:36:17 2009
@@ -0,0 +1,35 @@
+/*
+ * 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.backgroundtasks;
+
+import org.apache.pivot.util.concurrent.Task;
+import org.apache.pivot.util.concurrent.TaskExecutionException;
+
+public class SleepTask extends Task<String> {
+    @Override
+    public String execute() throws TaskExecutionException {
+        // Simulate a long-running activity (5s)
+        try {
+            Thread.sleep(5000);
+        } catch (InterruptedException exception) {
+            throw new TaskExecutionException(exception);
+        }
+
+        // Return a simulated result value
+        return "Done sleeping!";
+    }
+}

Added: incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/backgroundtasks/background_tasks.wtkx
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/backgroundtasks/background_tasks.wtkx?rev=820378&view=auto
==============================================================================
--- incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/backgroundtasks/background_tasks.wtkx (added)
+++ incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/backgroundtasks/background_tasks.wtkx Wed Sep 30 18:36:17 2009
@@ -0,0 +1,35 @@
+<?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="Background Tasks" maximized="true"
+    xmlns:wtkx="http://pivot.apache.org/wtkx"
+    xmlns="org.apache.pivot.wtk">
+    <content>
+        <BoxPane orientation="vertical"
+            styles="{horizontalAlignment:'center', verticalAlignment:'center'}">
+            <Border>
+                <content>
+                    <ActivityIndicator wtkx:id="activityIndicator"/>
+                </content>
+            </Border>
+
+            <PushButton wtkx:id="executeSynchronousButton" buttonData="Execute Synchronously"/>
+            <PushButton wtkx:id="executeAsynchronousButton" buttonData="Execute Asynchronously"/>
+        </BoxPane>
+    </content>
+</Window>