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);
}
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);
}
}));
}
}
});
}
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);
}
}));
}
}
});
}
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));
}
});
}
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);
}
}));
}
}
});
}
Aggregations