use of org.apache.pivot.util.concurrent.TaskListener in project pivot by apache.
the class LargeData method loadData.
private void loadData() {
int index = fileListButton.getSelectedIndex();
int capacity = (int) Math.pow(10, index + 1);
tableView.setTableData(new ArrayList<>(capacity));
pageSize = Math.max(capacity / 1000, 100);
String fileName = (String) fileListButton.getSelectedItem();
URL fileURL = null;
try {
fileURL = new URL(origin, basePath + "/" + fileName);
} catch (MalformedURLException exception) {
System.err.println(exception.getMessage());
}
if (fileURL != null) {
statusLabel.setText("Loading " + fileURL);
final long t0 = System.currentTimeMillis();
System.out.println("Loading \"" + fileURL + "\" ...");
loadDataTask = new LoadDataTask(fileURL);
loadDataTask.execute(new TaskAdapter<>(new TaskListener<Void>() {
@Override
public void taskExecuted(Task<Void> task) {
long t1 = System.currentTimeMillis();
String msg = "Read " + tableView.getTableData().getLength() + " rows in " + (t1 - t0) + "ms";
loadDataButton.setEnabled(true);
cancelButton.setEnabled(false);
loadDataTask = null;
ApplicationContext.queueCallback(() -> {
System.out.println(msg);
statusLabel.setText(msg);
statusLabel.repaint(true);
});
}
@Override
public void executeFailed(Task<Void> task) {
String taskFault = task.getFault().toString();
loadDataButton.setEnabled(true);
cancelButton.setEnabled(false);
loadDataTask = null;
ApplicationContext.queueCallback(() -> {
System.out.println(taskFault);
statusLabel.setText(taskFault);
statusLabel.repaint(true);
});
}
}));
}
}
use of org.apache.pivot.util.concurrent.TaskListener in project pivot by apache.
the class RSSFeedDemo method initialize.
@Override
public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
feedListView = (ListView) namespace.get("feedListView");
cardPane = (CardPane) namespace.get("cardPane");
statusLabel = (Label) namespace.get("statusLabel");
feedListView.getComponentMouseButtonListeners().add(new ComponentMouseButtonListener() {
private int index = -1;
@Override
public boolean mouseClick(Component component, Mouse.Button button, int x, int y, int count) {
if (count == 1) {
index = feedListView.getItemAt(y);
} else if (count == 2 && feedListView.getItemAt(y) == index) {
Element itemElement = (Element) feedListView.getListData().get(index);
String link = XML.getText(itemElement, "link");
Desktop desktop = Desktop.getDesktop();
try {
desktop.browse(new URL(link).toURI());
} catch (MalformedURLException exception) {
throw new RuntimeException(exception);
} catch (URISyntaxException exception) {
throw new RuntimeException(exception);
} catch (IOException exception) {
System.out.println("Unable to open " + link + " in default browser.");
}
}
return false;
}
});
GetQuery getQuery = new GetQuery("feeds.dzone.com", "/javalobby/frontpage");
getQuery.setSerializer(new XMLSerializer());
getQuery.getParameters().put("format", "xml");
getQuery.execute(new TaskAdapter<>(new TaskListener<Object>() {
@Override
public void taskExecuted(Task<Object> task) {
Element root = (Element) task.getResult();
feedListView.setListData(XML.getElements(root, "channel", "item"));
cardPane.setSelectedIndex(1);
}
@Override
public void executeFailed(Task<Object> task) {
statusLabel.setText(task.getFault().toString());
}
}));
}
use of org.apache.pivot.util.concurrent.TaskListener in project pivot by apache.
the class ImageView method setImage.
/**
* Sets the image view's current image by URL. <p> If the icon already
* exists in the application context resource cache, the cached value will
* be used. Otherwise, the icon will be loaded synchronously and added to
* the cache.
*
* @param imageURL The location of the image to set.
*/
public final void setImage(final URL imageURL) {
Utils.checkNull(imageURL, "imageURL");
Image imageLocal = (Image) ApplicationContext.getResourceCache().get(imageURL);
if (imageLocal == null) {
// Convert to URI because using a URL as a key causes performance problems
final java.net.URI imageURI;
try {
imageURI = imageURL.toURI();
} catch (URISyntaxException exception) {
throw new RuntimeException(exception);
}
if (asynchronous) {
if (loadMap.containsKey(imageURI)) {
// Add this to the list of image views that are interested in
// the image at this URL
loadMap.get(imageURI).add(this);
} else {
Image.load(imageURL, new TaskAdapter<>(new TaskListener<Image>() {
@Override
public void taskExecuted(Task<Image> task) {
Image imageLoadedLocal = task.getResult();
// Update the contents of all image views that requested this image
for (ImageView imageView : loadMap.get(imageURI)) {
imageView.setImage(imageLoadedLocal);
}
loadMap.remove(imageURI);
// Add the image to the cache
ApplicationContext.getResourceCache().put(imageURL, imageLoadedLocal);
}
@Override
public void executeFailed(Task<Image> task) {
// No-op
}
}));
loadMap.put(imageURI, new ArrayList<>(this));
}
} else {
imageLocal = Image.loadFromCache(imageURL);
}
}
setImage(imageLocal);
}
use of org.apache.pivot.util.concurrent.TaskListener in project pivot by apache.
the class TerraVFSBrowserSkin method refreshFileList.
private void refreshFileList() {
// Cancel any outstanding task
if (refreshFileListTask != null) {
refreshFileListTask.abort();
if (indicator != null) {
indicator.setActive(false);
fileStackPane.remove(fileStackPane.getLength() - 1, 1);
}
}
if (indicator == null) {
indicator = new ActivityIndicator();
activityGrid = new GridPane(5);
GridPane.Row row1 = new GridPane.Row(activityGrid);
GridPane.Row row2 = new GridPane.Row(activityGrid);
GridPane.Row row3 = new GridPane.Row(activityGrid);
for (int i = 0; i < 5; i++) {
row1.add(new GridPane.Filler());
if (i == 2) {
row2.add(indicator);
} else {
row2.add(new GridPane.Filler());
}
row3.add(new GridPane.Filler());
}
}
fileStackPane.add(activityGrid);
indicator.setActive(true);
fileTableView.setTableData(new ArrayList<FileObject>());
String text = searchTextInput.getText().trim();
Filter<FileObject> disabledFileFilter = hideDisabledFiles ? ((VFSBrowser) getComponent()).getDisabledFileFilter() : null;
Filter<FileObject> includeFileFilter = text.length() != 0 ? new IncludeFileFilter(text) : null;
TableView.SortDictionary sort = fileTableView.getSort();
final FileComparator fileComparator;
if (sort.isEmpty()) {
fileComparator = null;
} else {
Dictionary.Pair<String, SortDirection> pair = fileTableView.getSort().get(0);
fileComparator = getFileComparator(pair.key, pair.value);
}
refreshFileListTask = new RefreshFileListTask(includeFileFilter, disabledFileFilter, fileComparator);
refreshFileListTask.execute(new TaskAdapter<>(new TaskListener<ArrayList<FileObject>>() {
@Override
public void taskExecuted(Task<ArrayList<FileObject>> task) {
if (task == refreshFileListTask) {
indicator.setActive(false);
fileStackPane.remove(fileStackPane.getLength() - 1, 1);
ArrayList<FileObject> fileList = task.getResult();
fileTableView.setTableData(fileList);
updateSelectedFiles((VFSBrowser) getComponent());
refreshFileListTask = null;
}
}
@Override
public void executeFailed(Task<ArrayList<FileObject>> task) {
if (task == refreshFileListTask) {
indicator.setActive(false);
fileStackPane.remove(fileStackPane.getLength() - 1, 1);
refreshFileListTask = null;
}
}
}));
}
Aggregations