Search in sources :

Example 1 with TaskListener

use of org.apache.pivot.util.concurrent.TaskListener in project pivot by apache.

the class TaskTest method testTaskGroup.

@Test
public void testTaskGroup() {
    TaskListener<Void> taskListener = new TaskListener<Void>() {

        @Override
        public synchronized void taskExecuted(Task<Void> task) {
            System.out.println("EXECUTED");
            notify();
        }

        @Override
        public synchronized void executeFailed(Task<Void> task) {
            System.out.println("FAILED: " + task.getFault());
            notify();
        }
    };
    TaskGroup taskGroup = new TaskGroup();
    SleepTask task1 = new SleepTask(500);
    taskGroup.add(task1);
    SleepTask task2 = new SleepTask(1000);
    taskGroup.add(task2);
    SleepTask task3 = new SleepTask(2000);
    taskGroup.add(task3);
    synchronized (taskListener) {
        taskGroup.execute(taskListener);
        try {
            taskListener.wait();
        } catch (InterruptedException exception) {
        // empty block
        }
    }
}
Also used : Task(org.apache.pivot.util.concurrent.Task) TaskListener(org.apache.pivot.util.concurrent.TaskListener) TaskGroup(org.apache.pivot.util.concurrent.TaskGroup) Test(org.junit.Test)

Example 2 with TaskListener

use of org.apache.pivot.util.concurrent.TaskListener in project pivot by apache.

the class TaskTest method testTaskSequence.

@Test
public void testTaskSequence() {
    TaskListener<Void> taskListener = new TaskListener<Void>() {

        @Override
        public synchronized void taskExecuted(Task<Void> task) {
            System.out.println("EXECUTED");
            notify();
        }

        @Override
        public synchronized void executeFailed(Task<Void> task) {
            System.out.println("FAILED: " + task.getFault());
            notify();
        }
    };
    TaskSequence taskSequence = new TaskSequence();
    SleepTask task1 = new SleepTask(500);
    taskSequence.add(task1);
    SleepTask task2 = new SleepTask(1000);
    taskSequence.add(task2);
    SleepTask task3 = new SleepTask(2000);
    taskSequence.add(task3);
    synchronized (taskListener) {
        taskSequence.execute(taskListener);
        try {
            taskListener.wait();
        } catch (InterruptedException exception) {
        // empty block
        }
    }
}
Also used : Task(org.apache.pivot.util.concurrent.Task) TaskSequence(org.apache.pivot.util.concurrent.TaskSequence) TaskListener(org.apache.pivot.util.concurrent.TaskListener) Test(org.junit.Test)

Example 3 with TaskListener

use of org.apache.pivot.util.concurrent.TaskListener in project pivot by apache.

the class SearchDemo method executeQuery.

/**
 * Executes a search.
 *
 * @param term The search term.
 * @throws IllegalArgumentException If <tt>term</tt> is <tt>null</tt> or
 * empty.
 * @throws IllegalStateException If a query is already executing.
 */
public void executeQuery(String term) {
    Utils.checkNullOrEmpty(term, "search term");
    if (getQuery != null) {
        throw new IllegalStateException("Query is already running!");
    }
    String country = Locale.getDefault().getCountry().toLowerCase();
    getQuery = new GetQuery(QUERY_HOSTNAME, BASE_QUERY_PATH);
    getQuery.getParameters().put("term", term);
    getQuery.getParameters().put("country", country);
    getQuery.getParameters().put("media", MEDIA);
    getQuery.getParameters().put("limit", Integer.toString(LIMIT));
    getQuery.getParameters().put("output", "json");
    System.out.println(getQuery.getLocation());
    statusLabel.setText("Searching...");
    updateActivityState();
    getQuery.execute(new TaskAdapter<>(new TaskListener<Object>() {

        @Override
        public void taskExecuted(Task<Object> task) {
            if (task == getQuery) {
                @SuppressWarnings("unchecked") Map<String, Object> result = (Map<String, Object>) task.getResult();
                @SuppressWarnings("unchecked") List<Object> results = (List<Object>) result.get("results");
                // Preserve any existing sort
                @SuppressWarnings("unchecked") List<Object> tableData = (List<Object>) resultsTableView.getTableData();
                Comparator<Object> comparator = tableData.getComparator();
                results.setComparator(comparator);
                // Update the table data
                resultsTableView.setTableData(results);
                statusLabel.setText("Found " + results.getLength() + " matching items.");
                getQuery = null;
                updateActivityState();
                if (results.getLength() > 0) {
                    resultsTableView.setSelectedIndex(0);
                    resultsTableView.requestFocus();
                } else {
                    termTextInput.requestFocus();
                }
            }
        }

        @Override
        public void executeFailed(Task<Object> task) {
            if (task == getQuery) {
                statusLabel.setText(task.getFault().getMessage());
                getQuery = null;
                updateActivityState();
                termTextInput.requestFocus();
            }
        }
    }));
}
Also used : Task(org.apache.pivot.util.concurrent.Task) GetQuery(org.apache.pivot.web.GetQuery) TaskListener(org.apache.pivot.util.concurrent.TaskListener) List(org.apache.pivot.collections.List) Map(org.apache.pivot.collections.Map)

Example 4 with TaskListener

use of org.apache.pivot.util.concurrent.TaskListener in project pivot by apache.

the class ExpensesWindow method refreshExpenseList.

private void refreshExpenseList() {
    Expenses expensesApplicationLocal = getExpensesApplication();
    GetQuery expenseListQuery = new GetQuery(expensesApplicationLocal.getHostname(), expensesApplicationLocal.getPort(), "/pivot-tutorials/expenses", expensesApplicationLocal.isSecure());
    activityIndicatorBoxPane.setVisible(true);
    activityIndicator.setActive(true);
    expenseListQuery.execute(new TaskAdapter<>(new TaskListener<Object>() {

        @Override
        public void taskExecuted(Task<Object> task) {
            activityIndicatorBoxPane.setVisible(false);
            activityIndicator.setActive(false);
            List<?> expenseData = (List<?>) task.getResult();
            expenseTableView.setTableData(expenseData);
        }

        @Override
        public void executeFailed(Task<Object> task) {
            activityIndicatorBoxPane.setVisible(false);
            activityIndicator.setActive(false);
            Prompt.prompt(MessageType.ERROR, task.getFault().getMessage(), ExpensesWindow.this);
        }
    }));
}
Also used : Task(org.apache.pivot.util.concurrent.Task) GetQuery(org.apache.pivot.web.GetQuery) TaskListener(org.apache.pivot.util.concurrent.TaskListener) ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List)

Example 5 with TaskListener

use of org.apache.pivot.util.concurrent.TaskListener in project pivot by apache.

the class WebQueries method startup.

@Override
public void startup(Display display, Map<String, String> properties) throws Exception {
    BXMLSerializer bxmlSerializer = new BXMLSerializer();
    window = (Window) bxmlSerializer.readObject(WebQueries.class, "web_queries.bxml");
    listView = (ListView) bxmlSerializer.getNamespace().get("listView");
    loadingLabel = (Label) bxmlSerializer.getNamespace().get("loadingLabel");
    // Execute the query:
    // http://pipes.yahoo.com/pipes/pipe.run?_id=43115761f2da5af5341ae2e56a93d646&_render=json
    GetQuery getQuery = new GetQuery("pipes.yahoo.com", "/pipes/pipe.run");
    getQuery.getParameters().put("_id", "43115761f2da5af5341ae2e56a93d646");
    getQuery.getParameters().put("_render", "json");
    getQuery.execute(new TaskAdapter<>(new TaskListener<Object>() {

        @Override
        public void taskExecuted(Task<Object> task) {
            List<?> items = (List<?>) JSON.get(task.getResult(), "value.items");
            if (items.getLength() > 0) {
                listView.setListData(items);
                loadingLabel.setVisible(false);
            } else {
                loadingLabel.setText("No results.");
            }
        }

        @Override
        public void executeFailed(Task<Object> task) {
            loadingLabel.setText(task.getFault().getMessage());
        }
    }));
    window.open(display);
}
Also used : Task(org.apache.pivot.util.concurrent.Task) GetQuery(org.apache.pivot.web.GetQuery) TaskListener(org.apache.pivot.util.concurrent.TaskListener) List(org.apache.pivot.collections.List) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer)

Aggregations

TaskListener (org.apache.pivot.util.concurrent.TaskListener)14 Task (org.apache.pivot.util.concurrent.Task)13 GetQuery (org.apache.pivot.web.GetQuery)6 List (org.apache.pivot.collections.List)5 MalformedURLException (java.net.MalformedURLException)3 URISyntaxException (java.net.URISyntaxException)3 URL (java.net.URL)3 IOException (java.io.IOException)2 Date (java.util.Date)2 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)2 ArrayList (org.apache.pivot.collections.ArrayList)2 Map (org.apache.pivot.collections.Map)2 TaskExecutionException (org.apache.pivot.util.concurrent.TaskExecutionException)2 Test (org.junit.Test)2 Desktop (java.awt.Desktop)1 File (java.io.File)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URI (java.net.URI)1 DateFormat (java.text.DateFormat)1 Random (java.util.Random)1