Search in sources :

Example 1 with Task

use of org.apache.pivot.util.concurrent.Task 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 Task

use of org.apache.pivot.util.concurrent.Task 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 Task

use of org.apache.pivot.util.concurrent.Task 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 Task

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

the class CalendarTest method startup.

@Override
public void startup(Display display, Map<String, String> properties) throws Exception {
    BXMLSerializer bxmlSerializer = new BXMLSerializer();
    window = (Window) bxmlSerializer.readObject(CalendarTest.class, "calendar_test.bxml");
    bxmlSerializer.bind(this, CalendarTest.class);
    Filter<CalendarDate> todayFilter = new Filter<CalendarDate>() {

        @Override
        public boolean include(CalendarDate date) {
            CalendarDate today = new CalendarDate();
            return (!date.equals(today));
        }
    };
    calendar.setDisabledDateFilter(todayFilter);
    calendarButton.getCalendarButtonListeners().add(new CalendarButtonListener() {

        @Override
        public void yearChanged(CalendarButton calendarButtonArgument, int previousYear) {
            disable();
        }

        @Override
        public void monthChanged(CalendarButton calendarButtonArgument, int previousMonth) {
            disable();
        }

        private void disable() {
            calendarButton.setDisabledDateFilter(new Filter<CalendarDate>() {

                @Override
                public boolean include(CalendarDate date) {
                    return true;
                }
            });
            Task<Void> task = new Task<Void>() {

                @Override
                public Void execute() {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException exception) {
                    // ignore exception
                    }
                    return null;
                }
            };
            System.out.println("STARTING TASK");
            task.execute(new TaskAdapter<>(new TaskListener<Void>() {

                @Override
                public void taskExecuted(Task<Void> taskArgument) {
                    System.out.println("EXECUTED");
                    calendarButton.setDisabledDateFilter(null);
                }

                @Override
                public void executeFailed(Task<Void> taskArgument) {
                    System.out.println("FAILED");
                    calendarButton.setDisabledDateFilter(null);
                }
            }));
        }
    });
    window.open(display);
}
Also used : Task(org.apache.pivot.util.concurrent.Task) CalendarDate(org.apache.pivot.util.CalendarDate) Filter(org.apache.pivot.util.Filter) TaskAdapter(org.apache.pivot.wtk.TaskAdapter) CalendarButton(org.apache.pivot.wtk.CalendarButton) CalendarButtonListener(org.apache.pivot.wtk.CalendarButtonListener) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer)

Example 5 with Task

use of org.apache.pivot.util.concurrent.Task 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)

Aggregations

Task (org.apache.pivot.util.concurrent.Task)14 TaskListener (org.apache.pivot.util.concurrent.TaskListener)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 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)3 IOException (java.io.IOException)2 Date (java.util.Date)2 ArrayList (org.apache.pivot.collections.ArrayList)2 Map (org.apache.pivot.collections.Map)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 FileObject (org.apache.commons.vfs2.FileObject)1