org.jdesktop.swingworker.SwingWorker Java Examples

The following examples show how to use org.jdesktop.swingworker.SwingWorker. 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 File: SocialStreamPlugin.java    From 07kit with 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;
    }
}