Search in sources :

Example 1 with TaskAdapter

use of org.apache.pivot.wtk.TaskAdapter 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 2 with TaskAdapter

use of org.apache.pivot.wtk.TaskAdapter in project pivot by apache.

the class ExpensesWindow method addExpense.

private void addExpense() {
    expenseSheet.clear();
    expenseSheet.open(this, new SheetCloseListener() {

        @Override
        public void sheetClosed(Sheet sheet) {
            if (sheet.getResult()) {
                // Get the expense data from the sheet
                final HashMap<String, Object> expense = new HashMap<>();
                expenseSheet.store(expense);
                // POST expense to server and then add to table
                Expenses expensesApplicationLocal = getExpensesApplication();
                PostQuery addExpenseQuery = new PostQuery(expensesApplicationLocal.getHostname(), expensesApplicationLocal.getPort(), "/pivot-tutorials/expenses", expensesApplicationLocal.isSecure());
                addExpenseQuery.setValue(expense);
                activityIndicatorBoxPane.setVisible(true);
                activityIndicator.setActive(true);
                addExpenseQuery.execute(new TaskAdapter<>(new TaskListener<URL>() {

                    @Override
                    public void taskExecuted(Task<URL> task) {
                        activityIndicatorBoxPane.setVisible(false);
                        activityIndicator.setActive(false);
                        URL location = task.getResult();
                        String file = location.getFile();
                        int id = Integer.parseInt(file.substring(file.lastIndexOf('/') + 1));
                        expense.put("id", id);
                        @SuppressWarnings("unchecked") List<Object> expenses = (List<Object>) expenseTableView.getTableData();
                        expenses.add(expense);
                    }

                    @Override
                    public void executeFailed(Task<URL> task) {
                        activityIndicatorBoxPane.setVisible(false);
                        activityIndicator.setActive(false);
                        Prompt.prompt(MessageType.ERROR, task.getFault().getMessage(), ExpensesWindow.this);
                    }
                }));
            }
        }
    });
}
Also used : HashMap(org.apache.pivot.collections.HashMap) SheetCloseListener(org.apache.pivot.wtk.SheetCloseListener) URL(java.net.URL) TaskAdapter(org.apache.pivot.wtk.TaskAdapter) PostQuery(org.apache.pivot.web.PostQuery) ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List) Sheet(org.apache.pivot.wtk.Sheet)

Example 3 with TaskAdapter

use of org.apache.pivot.wtk.TaskAdapter in project pivot by apache.

the class ExpensesWindow method updateSelectedExpense.

private void updateSelectedExpense() {
    Object expense = expenseTableView.getSelectedRow();
    final int id = JSON.getInt(expense, "id");
    expenseSheet.load(expense);
    expenseSheet.open(this, new SheetCloseListener() {

        @Override
        public void sheetClosed(Sheet sheet) {
            if (sheet.getResult()) {
                // Get the expense data from the sheet
                final HashMap<String, Object> expenseLocal = new HashMap<>();
                expenseSheet.store(expenseLocal);
                // PUT expense to server and then update table
                Expenses expensesApplicationLocal = getExpensesApplication();
                PutQuery updateExpenseQuery = new PutQuery(expensesApplicationLocal.getHostname(), expensesApplicationLocal.getPort(), "/pivot-tutorials/expenses/" + JSON.get(expenseLocal, "id"), expensesApplicationLocal.isSecure());
                updateExpenseQuery.setValue(expenseLocal);
                activityIndicatorBoxPane.setVisible(true);
                activityIndicator.setActive(true);
                updateExpenseQuery.execute(new TaskAdapter<>(new TaskListener<Boolean>() {

                    @Override
                    public void taskExecuted(Task<Boolean> task) {
                        activityIndicatorBoxPane.setVisible(false);
                        activityIndicator.setActive(false);
                        // Find matching row and update
                        @SuppressWarnings("unchecked") List<Object> expenses = (List<Object>) expenseTableView.getTableData();
                        for (int i = 0, n = expenses.getLength(); i < n; i++) {
                            if (JSON.get(expenses.get(i), "id").equals(id)) {
                                expenses.update(i, expenseLocal);
                                break;
                            }
                        }
                    }

                    @Override
                    public void executeFailed(Task<Boolean> task) {
                        activityIndicatorBoxPane.setVisible(false);
                        activityIndicator.setActive(false);
                        Prompt.prompt(MessageType.ERROR, task.getFault().getMessage(), ExpensesWindow.this);
                    }
                }));
            }
        }
    });
}
Also used : HashMap(org.apache.pivot.collections.HashMap) SheetCloseListener(org.apache.pivot.wtk.SheetCloseListener) PutQuery(org.apache.pivot.web.PutQuery) TaskAdapter(org.apache.pivot.wtk.TaskAdapter) ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List) Sheet(org.apache.pivot.wtk.Sheet)

Example 4 with TaskAdapter

use of org.apache.pivot.wtk.TaskAdapter in project pivot by apache.

the class BackgroundTasks method initialize.

@Override
public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
    activityIndicator = (ActivityIndicator) namespace.get("activityIndicator");
    executeSynchronousButton = (PushButton) namespace.get("executeSynchronousButton");
    executeAsynchronousButton = (PushButton) namespace.get("executeAsynchronousButton");
    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.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            activityIndicator.setActive(true);
            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);
                    setEnabled(true);
                    System.out.println("Synchronous task execution complete: \"" + task.getResult() + "\"");
                }

                @Override
                public void executeFailed(Task<String> task) {
                    activityIndicator.setActive(false);
                    setEnabled(true);
                    System.err.println(task.getFault());
                }
            };
            sleepTask.execute(new TaskAdapter<>(taskListener));
        }
    });
}
Also used : ButtonPressListener(org.apache.pivot.wtk.ButtonPressListener) TaskExecutionException(org.apache.pivot.util.concurrent.TaskExecutionException) PushButton(org.apache.pivot.wtk.PushButton) Button(org.apache.pivot.wtk.Button) TaskAdapter(org.apache.pivot.wtk.TaskAdapter) TaskListener(org.apache.pivot.util.concurrent.TaskListener)

Example 5 with TaskAdapter

use of org.apache.pivot.wtk.TaskAdapter in project pivot by apache.

the class ExpensesWindow method deleteSelectedExpense.

private void deleteSelectedExpense() {
    Object expense = expenseTableView.getSelectedRow();
    final int id = JSON.getInt(expense, "id");
    deleteConfirmationPrompt.open(this, new SheetCloseListener() {

        @Override
        public void sheetClosed(Sheet sheet) {
            if (sheet.getResult() && ((Prompt) sheet).getSelectedOptionIndex() == 1) {
                // DELETE expense from server and then remove from table
                Expenses expensesApplicationLocal = getExpensesApplication();
                DeleteQuery deleteExpenseQuery = new DeleteQuery(expensesApplicationLocal.getHostname(), expensesApplicationLocal.getPort(), "/pivot-tutorials/expenses/" + id, expensesApplicationLocal.isSecure());
                activityIndicatorBoxPane.setVisible(true);
                activityIndicator.setActive(true);
                deleteExpenseQuery.execute(new TaskAdapter<>(new TaskListener<Void>() {

                    @Override
                    public void taskExecuted(Task<Void> task) {
                        activityIndicatorBoxPane.setVisible(false);
                        activityIndicator.setActive(false);
                        // Find matching row and remove
                        @SuppressWarnings("unchecked") List<Object> expenses = (List<Object>) expenseTableView.getTableData();
                        for (int i = 0, n = expenses.getLength(); i < n; i++) {
                            if (JSON.get(expenses.get(i), "id").equals(id)) {
                                expenses.remove(i, 1);
                                break;
                            }
                        }
                    }

                    @Override
                    public void executeFailed(Task<Void> task) {
                        activityIndicatorBoxPane.setVisible(false);
                        activityIndicator.setActive(false);
                        Prompt.prompt(MessageType.ERROR, task.getFault().getMessage(), ExpensesWindow.this);
                    }
                }));
            }
        }
    });
}
Also used : TaskAdapter(org.apache.pivot.wtk.TaskAdapter) DeleteQuery(org.apache.pivot.web.DeleteQuery) ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List) SheetCloseListener(org.apache.pivot.wtk.SheetCloseListener) Sheet(org.apache.pivot.wtk.Sheet)

Aggregations

TaskAdapter (org.apache.pivot.wtk.TaskAdapter)6 ArrayList (org.apache.pivot.collections.ArrayList)3 List (org.apache.pivot.collections.List)3 Sheet (org.apache.pivot.wtk.Sheet)3 SheetCloseListener (org.apache.pivot.wtk.SheetCloseListener)3 HashMap (org.apache.pivot.collections.HashMap)2 Button (org.apache.pivot.wtk.Button)2 ButtonPressListener (org.apache.pivot.wtk.ButtonPressListener)2 PushButton (org.apache.pivot.wtk.PushButton)2 URL (java.net.URL)1 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)1 CalendarDate (org.apache.pivot.util.CalendarDate)1 Filter (org.apache.pivot.util.Filter)1 Task (org.apache.pivot.util.concurrent.Task)1 TaskExecutionException (org.apache.pivot.util.concurrent.TaskExecutionException)1 TaskListener (org.apache.pivot.util.concurrent.TaskListener)1 DeleteQuery (org.apache.pivot.web.DeleteQuery)1 PostQuery (org.apache.pivot.web.PostQuery)1 PutQuery (org.apache.pivot.web.PutQuery)1 CalendarButton (org.apache.pivot.wtk.CalendarButton)1