Java Code Examples for org.jdesktop.swingworker.SwingWorker

The following examples show how to use org.jdesktop.swingworker.SwingWorker. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: 07kit   Source File: SocialStreamPlugin.java    License: GNU General Public License v3.0 4 votes vote down vote up
@Schedule(120000)
public void redrawSidebar() {
    if (loading) {
        return;
    }
    SocialStreamPlugin self = this;
    try {
        (new SwingWorker<List<StreamWidget>, Void>() {
            @Override
            protected void done() {
                try {
                    List<StreamWidget> widgets = get();
                    if (widgets == null) {
                        return;
                    }
                    widget.createContainer(widgets);
                } catch (InterruptedException | ExecutionException e1) {
                    logger.error("Error loading social stream", e1);
                    NotificationsUtil.showNotification("Social", "Error loading social stream");
                    loading = false;
                }
            }

            @Override
            protected List<StreamWidget> doInBackground() throws Exception {
                loading = true;
                logger.info("Updating social stream");
                Request request = Request.Get(API_URL + entriesToShow);
                request.addHeader(AUTH_HEADER_KEY, "Bearer " + Session.get().getApiToken());

                HttpResponse response = Executor.newInstance(HttpUtil.getClient()).execute(request).returnResponse();
                if (response != null) {
                    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                        byte[] bytes = EntityUtils.toByteArray(response.getEntity());
                        List<TwitchStream> streams = GSON.fromJson(new String(bytes), LIST_TYPE);
                        return streams.stream().map(t -> new StreamWidget(t, self))
                                .collect(Collectors.toList());
                    }
                }
                return null;
            }
        }).execute();
    } catch (Exception e) {
        logger.error("Error occurred getting social stream data.", e);
    } finally {
        loading = false;
    }
}