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