Search in sources :

Example 11 with Task

use of org.apache.pivot.util.concurrent.Task 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);
                });
            }
        }));
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) Task(org.apache.pivot.util.concurrent.Task) IOTask(org.apache.pivot.io.IOTask) TaskListener(org.apache.pivot.util.concurrent.TaskListener) URL(java.net.URL)

Example 12 with Task

use of org.apache.pivot.util.concurrent.Task 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());
        }
    }));
}
Also used : MalformedURLException(java.net.MalformedURLException) XMLSerializer(org.apache.pivot.xml.XMLSerializer) Task(org.apache.pivot.util.concurrent.Task) Element(org.apache.pivot.xml.Element) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URL(java.net.URL) Mouse(org.apache.pivot.wtk.Mouse) Desktop(java.awt.Desktop) GetQuery(org.apache.pivot.web.GetQuery) TaskListener(org.apache.pivot.util.concurrent.TaskListener) ComponentMouseButtonListener(org.apache.pivot.wtk.ComponentMouseButtonListener) Component(org.apache.pivot.wtk.Component)

Example 13 with Task

use of org.apache.pivot.util.concurrent.Task 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);
}
Also used : Task(org.apache.pivot.util.concurrent.Task) TaskListener(org.apache.pivot.util.concurrent.TaskListener) URISyntaxException(java.net.URISyntaxException) Image(org.apache.pivot.wtk.media.Image)

Example 14 with Task

use of org.apache.pivot.util.concurrent.Task 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;
            }
        }
    }));
}
Also used : Dictionary(org.apache.pivot.collections.Dictionary) Task(org.apache.pivot.util.concurrent.Task) GridPane(org.apache.pivot.wtk.GridPane) ActivityIndicator(org.apache.pivot.wtk.ActivityIndicator) Point(org.apache.pivot.wtk.Point) SortDirection(org.apache.pivot.wtk.SortDirection) TaskListener(org.apache.pivot.util.concurrent.TaskListener) FileObject(org.apache.commons.vfs2.FileObject) TableView(org.apache.pivot.wtk.TableView)

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