All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.datastax.data.dataset.provider.LoadTask Maven / Gradle / Ivy

The newest version!


package com.datastax.data.dataset.provider;

import com.datastax.data.dataset.DataTable;

import javax.swing.*;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;


public abstract class LoadTask extends AbstractTask {
    
    private static final Logger LOG = Logger.getLogger(LoadTask.class.getName());
    
    
    private LinkedList loadQueue = new LinkedList();

    
    private LoadNotifier loadNotifier = new LoadNotifier();

    
    private DataTable[] tables;

    private boolean loadOnEDT = true;

    public void setLoadOnEDT(boolean val) {
        loadOnEDT = val;
    }
    
    
    public LoadTask(DataTable[] tables) {
        this.tables = tables == null ? new DataTable[0] : tables;
    }

    
    public void run() {
        setIndeterminate(true);
        try {
            
            readData(tables);
            scheduleLoad();
            setProgress(getMaximum());
        } catch (Exception e) {
            final Throwable error = e;
            LOG.log(Level.WARNING, "Failed to load data into tables {0}. {1}", new Object[]{tables, e.getStackTrace()});
            setProgress(getMaximum());
        }
    }

    
    protected abstract void readData(final DataTable[] tables) throws Exception;

    
    protected abstract void loadData(LoadItem[] items);

    
    protected void scheduleLoad(LoadItem item) {
        synchronized (loadQueue) {
            if (item != null) {
                loadQueue.addLast(item);
            }
            if (!loadNotifier.isPending()) {
                loadNotifier.setPending(true);
                if (loadOnEDT) {
                    SwingUtilities.invokeLater(loadNotifier);
                } else {
                    loadNotifier.run();
                }
            }
        }
    }

    
    protected void scheduleLoad() {
        scheduleLoad(null);
    }

    
    public String getDescription() {
        return "

Loading data

"; } public Icon getIcon() { return null; } public String getMessage() { return "Loading item " + (getProgress() + 1) + " of " + getMaximum(); } public boolean cancel() throws Exception { return false; } public static final class LoadItem { public DataTable table; public E data; public LoadItem(DataTable table, E data) { this.table = table; this.data = data; } } private class LoadNotifier implements Runnable { private boolean pending = false; LoadNotifier() { } public synchronized void setPending(boolean pending) { this.pending = pending; } public synchronized boolean isPending() { return pending; } public void run() { synchronized (loadQueue) { if (loadQueue.size() > 0) { LoadItem[] items = (LoadItem[]) loadQueue .toArray(new LoadItem[loadQueue.size()]); loadQueue.clear(); loadData(items); } setPending(false); } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy