Example usage for javax.swing SwingWorker SwingWorker

List of usage examples for javax.swing SwingWorker SwingWorker

Introduction

In this page you can find the example usage for javax.swing SwingWorker SwingWorker.

Prototype

public SwingWorker() 

Source Link

Document

Constructs this SwingWorker .

Usage

From source file:RosAdProgram.model.Model.java

public void copy() {
    final File open = getOpenPath();
    final File save = getSavePath();
    final double probability = getProbability();
    resetErrors();/*from w  w  w  . j  a v a2  s  .  c o m*/

    SwingWorker<Void, Integer[]> worker = new SwingWorker<Void, Integer[]>() { //  ? progressBar
        @Override
        protected Void doInBackground() throws Exception {
            FileInputStream in = null;
            FileOutputStream out = null;

            try {
                try {
                    in = new FileInputStream(open);
                } catch (FileNotFoundException ex) {
                    System.out.println("   ");
                    Logger.getLogger(ConfigGUI.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    out = new FileOutputStream(save);
                } catch (FileNotFoundException ex) {
                    System.out.println("?   ");
                    Logger.getLogger(ConfigGUI.class.getName()).log(Level.SEVERE, null, ex);
                }
                int c;

                try { // Noise adding 
                    Integer[] data = new Integer[6];
                    data[0] = 0; // ? ? 
                    data[1] = 0; // ?  
                    data[2] = 0; // ?  
                    data[3] = 0; // ?  
                    data[4] = 0; // ? ? 
                    data[5] = 0; // ? ? 

                    int currentBlockCount = 0; //  ? ? 
                    int blockSize = getBlockSize(); //    
                    int bytes = 0; // ? ? 

                    for (int bytesInBlockCounter = 0; (c = in.read()) != -1;) { // ? . (? ?  - ?  )
                        bytes++; // ? ? 
                        bytesInBlockCounter++;
                        int result = mask(probability, c); // result - 8   ? ? 
                        out.write(result); // ?  ?                         
                        if ((bytesInBlockCounter == blockSize) || (getFileSize() == bytes - 1)) { //      ?  
                            //      ?  (   ?  ,
                            //  ?    )
                            if (getByteErrorFlag()) {
                                setBlockError(1);
                                setBlockErrorFlag(false); //     
                            }
                            currentBlockCount++; //  ? 
                            bytesInBlockCounter = 0; // ?? ?   
                        }
                        data[0] = getPercent(bytes + 1);
                        data[1] = getBitError();
                        data[2] = getByteError();
                        data[3] = getBlockError();
                        data[4] = bytes;
                        data[5] = currentBlockCount;

                        publish(data);
                    }
                    System.out.println("? :");
                    System.out.println(" " + getBitError());
                    System.out.println(" " + getByteError());
                    System.out.println(" " + getBlockError());

                } catch (IOException ex) {
                    System.out.println("   ?");
                    Logger.getLogger(ConfigGUI.class.getName()).log(Level.SEVERE, null, ex);
                }
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException ex) {
                        Logger.getLogger(ConfigGUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException ex) {
                        Logger.getLogger(ConfigGUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
            return null;
        }

        @Override
        // This will be called if you call publish() from doInBackground()
        // Can safely update the GUI here.
        protected void process(List<Integer[]> chunks) { // ?    
                                                         // publish ?
            Integer[] value = chunks.get(chunks.size() - 1); // ?
            statisticsGUI.setProgress(value[0]); //  ??
            statisticsGUI.setValueButtomTable(value[1], value[2], value[3], value[4], value[5]); //  ?    ??

        }
    };
    worker.execute();
}

From source file:ru.goodfil.catalog.ui.forms.CarsPanel.java

private void filterSearchBtnActionPerformed(ActionEvent e) {
    final String filterCode = filterSearchField.getText();
    if (StringUtils.isBlank(filterCode)) {
        String msg = "? ?    \"\"";
        JOptionPane.showMessageDialog(null, msg, "!", JOptionPane.WARNING_MESSAGE);
        return;//  w w  w.  j  a  v  a  2  s. co  m
    }

    SwingWorker worker = new SwingWorker<Object, Void>() {
        @Override
        protected Object doInBackground() throws Exception {
            allFilters.clear();
            allFilters.addAll(filtersService.getFiltersByName(filterCode));
            return null;
        }
    };
    worker.execute();

    adjustButtonsEnabledProperty();
}

From source file:savant.sql.SQLWorker.java

public SQLWorker(String prog, String fail) {
    this.progressMessage = prog;
    this.failureMessage = fail;
    innerWorker = new SwingWorker() {
        private T result;

        @Override// w w  w. j a  v  a  2s.co m
        public Object doInBackground() {
            try {
                result = SQLWorker.this.doInBackground();
            } catch (final Exception x) {
                MiscUtils.invokeLaterIfNecessary(new Runnable() {
                    @Override
                    public void run() {
                        showProgress(1.0);
                        DialogUtils.displayException("SQL Error", failureMessage, x);
                    }
                });
            }
            return null;
        }

        @Override
        public void done() {
            try {
                synchronized (waiter) {
                    workerDone = true;
                    waiter.notifyAll();
                }
                showProgress(1.0);
                SQLWorker.this.done(result);
            } catch (Exception x) {
                DialogUtils.displayException("SQL Error", failureMessage, x);
            }
        }
    };
}

From source file:screenieup.ImgurUpload.java

/**
 * Upload image./*  w w  w.j av a 2  s  . co m*/
 * @param imgToUpload the image to upload
 */
public void upload(BufferedImage imgToUpload) {
    System.out.println("Preparing for upload...");
    final BufferedImage img = imgToUpload; // was turned into 'final' when using JDK 7 to compile
    SwingWorker uploader;
    uploader = new SwingWorker<Void, Integer>() {
        @Override
        protected Void doInBackground() {
            publish(0);
            ByteArrayOutputStream baos = writeImage(img);
            publish(1);
            String dataToSend = encodeImage(baos);
            publish(2);
            URLConnection connection = connect();
            publish(3);
            sendImage(connection, dataToSend);
            publish(4);
            String response = getResponse(connection);
            publish(5);
            getImageLinks(response);
            return null;
        }

        @Override
        protected void done() {
            publish(6);
            copyToClipBoard();
            publish(7);
            urlarea.setText(imgurl);
            urlarea.setEnabled(true);
            browserBtn.setEnabled(true);
            new ListWriter("imgur_links.txt").writeList(
                    "Image link: " + imgurl + " - Image delete link: " + IMGUR_DELETE_URI + imgurl_del, true); // true = append to file, false = overwrite
            JOptionPane.showMessageDialog(null, "Uploaded!\n"
                    + "The image link has been copied to your clipboard!\nImage link and deletion link has been logged to file.");
            progressDialog.dispose();
        }

        @Override
        protected void process(List<Integer> chunks) {
            progressLabel.setText(progressText[chunks.get(chunks.size() - 1)]); // The last value in this array is all we care about.
            progressBar.setValue(chunks.get(chunks.size() - 1) + 1);
        }
    };
    uploader.execute();
}

From source file:screenieup.MixtapeUpload.java

private void startUpload(byte[] b) {
    SwingWorker uploader;//from  w w  w  . j a  va2  s .co m
    uploader = new SwingWorker<Void, Integer>() {
        @Override
        protected Void doInBackground() throws IOException { // not handled anywhere, do fix
            publish(0);
            publish(2);
            HttpURLConnection connection = connect();
            publish(3);
            try {
                send(b, connection);
            } catch (SSLHandshakeException ex) {
                JOptionPane.showMessageDialog(null,
                        "You need to add Let's Encrypt's certificates to your Java CA Certificate store.");
                return null;
            }
            publish(4);
            String response = getResponse(connection);
            System.out.println("response received: " + response);
            parseResponse(response);
            return null;
        }

        @Override
        protected void done() {
            publish(6);
            copyToClipBoard(mixtapeurl);
            publish(7);
            urlarea.setText(mixtapeurl);
            urlarea.setEnabled(true);
            browserBtn.setEnabled(true);
            new ListWriter("mixtape_links.txt").writeList("Image link: " + mixtapeurl, true); // true = append to file, false = overwrite
            JOptionPane.showMessageDialog(null, "Uploaded!\n"
                    + "The image link has been copied to your clipboard!\nImage link has been logged to file.");
            progressDialog.dispose();
        }

        @Override
        protected void process(List<Integer> chunks) {
            progressLabel.setText(progressText[chunks.get(chunks.size() - 1)]); // The last value in this array is all we care about.
            progressBar.setValue(chunks.get(chunks.size() - 1) + 1);
        }
    };
    uploader.execute();
}

From source file:screenieup.PomfUpload.java

private void startUpload(byte[] b) {
    SwingWorker uploader;/*from w ww. j  a  v  a 2 s.co m*/
    uploader = new SwingWorker<Void, Integer>() {
        @Override
        protected Void doInBackground() throws IOException { // not handled anywhere, do fix
            publish(0);
            publish(2);
            HttpURLConnection connection = connect();
            publish(3);
            try {
                send(b, connection);
            } catch (SSLHandshakeException ex) {
                JOptionPane.showMessageDialog(null,
                        "You need to add Let's Encrypt's certificates to your Java CA Certificate store.");
                return null;
            }
            publish(4);
            String response = getResponse(connection);
            System.out.println("response received: " + response);
            parseResponse(response);
            return null;
        }

        @Override
        protected void done() {
            publish(6);
            copyToClipBoard(pomfurl);
            publish(7);
            urlarea.setText(pomfurl);
            urlarea.setEnabled(true);
            browserBtn.setEnabled(true);
            new ListWriter("pomf_links.txt").writeList("Image link: " + pomfurl, true); // true = append to file, false = overwrite
            JOptionPane.showMessageDialog(null, "Uploaded!\n"
                    + "The image link has been copied to your clipboard!\nImage link has been logged to file.");
            progressDialog.dispose();
        }

        @Override
        protected void process(List<Integer> chunks) {
            progressLabel.setText(progressText[chunks.get(chunks.size() - 1)]); // The last value in this array is all we care about.
            progressBar.setValue(chunks.get(chunks.size() - 1) + 1);
        }
    };
    uploader.execute();
}

From source file:screenieup.UguuUpload.java

private void startUpload(byte[] b) {
    SwingWorker uploader;//  w  ww.  j  av a2s  .co m
    uploader = new SwingWorker<Void, Integer>() {
        @Override
        protected Void doInBackground() throws IOException { // not handled anywhere, do fix
            publish(0);
            publish(2);
            HttpURLConnection connection = connect();
            publish(3);
            send(b, connection);
            publish(4);
            uguurl = getResponse(connection);
            System.out.println("response received: " + uguurl);
            return null;
        }

        @Override
        protected void done() {
            publish(6);
            copyToClipBoard(uguurl);
            publish(7);
            urlarea.setText(uguurl);
            urlarea.setEnabled(true);
            browserBtn.setEnabled(true);
            new ListWriter("uguu_links.txt").writeList("Image link: " + uguurl, true); // true = append to file, false = overwrite
            JOptionPane.showMessageDialog(null, "Uploaded!\n"
                    + "The image link has been copied to your clipboard!\nImage link has been logged to file.");
            progressDialog.dispose();
        }

        @Override
        protected void process(List<Integer> chunks) {
            progressLabel.setText(progressText[chunks.get(chunks.size() - 1)]); // The last value in this array is all we care about.
            progressBar.setValue(chunks.get(chunks.size() - 1) + 1);
        }
    };
    uploader.execute();
}

From source file:se.streamsource.streamflow.client.ui.workspace.cases.general.forms.geo.GeoLocationFieldPanel.java

private void initiateGetAddressInfo(GeoMarker marker) {
    final GeoPosition firstPoint = marker.getPoints().get(0);
    new SwingWorker<MapquestQueryResult, Object>() {

        @Override/*w w  w  .  ja  v  a 2 s.  c  o m*/
        protected MapquestQueryResult doInBackground() throws Exception {
            String urlPattern = formDraftSettings.mapquestReverseLookupUrlPattern().get();
            MapquestNominatimService geoLookupService = geoLookupServiceBuilder.use(urlPattern).newInstance();
            return geoLookupService.reverseLookup(firstPoint.getLatitude(), firstPoint.getLongitude());
        }

        @Override
        protected void done() {
            try {
                updateAddressInfo(get());
            } catch (Exception e) {
                logger.warn("Failed to get address info", e);
            }
        }
    }.execute();
}

From source file:sk.uniza.fri.pds.spotreba.energie.gui.DbTab.java

private void load() {
    new SwingWorker<List, RuntimeException>() {
        @Override//from w w w . j av a 2 s  .  co m
        protected List doInBackground() throws Exception {
            try {
                return service.findAll();
            } catch (RuntimeException e) {
                publish(e);
                return null;
            }
        }

        @Override
        protected void done() {
            try {
                if (get() != null) {
                    tableModel = new BeanTableModel(clazz, get());
                    tableModel.sortColumnNames();
                    jTable.setModel(tableModel);
                }
            } catch (InterruptedException | ExecutionException ex) {
                Logger.getLogger(DbTab.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        @Override
        protected void process(List<RuntimeException> chunks) {
            showException("Chyba", chunks.get(0));
        }

    }.execute();
}

From source file:sk.uniza.fri.pds.spotreba.energie.gui.DbTab.java

private void create() {
    new SwingWorker<Boolean, RuntimeException>() {
        @Override//w w w.  j av a2  s  .co  m
        protected Boolean doInBackground() throws Exception {
            try {
                service.create(metawidget.getToInspect());
                return true;
            } catch (RuntimeException e) {
                publish(e);
            }
            return false;
        }

        @Override
        protected void done() {
            try {
                if (get()) {
                    load();
                }
            } catch (InterruptedException | ExecutionException ex) {
                Logger.getLogger(DbTab.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        @Override
        protected void process(List<RuntimeException> chunks) {
            showException("Chyba", chunks.get(0));
        }
    }.execute();
}