Search in sources :

Example 1 with FileExaminator

use of com.igormaznitsa.sciareto.ui.misc.FileExaminator in project netbeans-mmd-plugin by raydac.

the class FindFilesForTextPanel method startSearchThread.

private void startSearchThread(@Nonnull @MustNotContainNull final List<NodeFileOrFolder> scope, @Nonnull final byte[] dataToFindVariant1, @Nonnull final byte[] dataToFindVariant2) {
    int size = 0;
    for (final NodeFileOrFolder p : scope) {
        size += p.size();
    }
    final byte[] fileOpBuffer = new byte[1024 * 1024];
    final Runnable runnable = new Runnable() {

        int value = 0;

        private void processFile(final NodeFileOrFolder file) {
            value++;
            final File f = file.makeFileForNode();
            try {
                if (new FileExaminator(f).doesContainData(fileOpBuffer, dataToFindVariant1, dataToFindVariant2)) {
                    addFileIntoList(file);
                }
            } catch (Exception ex) {
                LOGGER.error("Error during text search in '" + f + '\'', ex);
            }
            if (!Thread.currentThread().isInterrupted()) {
                safeSetProgressValue(value);
            }
        }

        private void processFolder(final NodeFileOrFolder folder) {
            value++;
            for (final NodeFileOrFolder f : folder) {
                if (f.isLeaf()) {
                    processFile(f);
                } else {
                    processFolder(f);
                }
            }
            if (!Thread.currentThread().isInterrupted()) {
                safeSetProgressValue(value);
            }
        }

        @Override
        public void run() {
            for (final NodeFileOrFolder p : scope) {
                for (final NodeFileOrFolder f : p) {
                    if (Thread.currentThread().isInterrupted()) {
                        return;
                    }
                    if (f.isLeaf()) {
                        processFile(f);
                    } else {
                        processFolder(f);
                    }
                }
            }
            safeSetProgressValue(Integer.MAX_VALUE);
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    buttonFind.setEnabled(true);
                    fieldText.setEnabled(true);
                    comboCharsets.setEnabled(true);
                    comboLocale.setEnabled(true);
                    if (foundFiles.isEmpty()) {
                        fieldText.requestFocus();
                    } else {
                        listOfFoundElements.requestFocus();
                    }
                }
            });
        }
    };
    // NOI18N
    final Thread thread = new Thread(runnable, "SciaRetoSearchUsage");
    thread.setDaemon(true);
    final Thread oldThread = this.searchingThread.getAndSet(thread);
    if (oldThread != null) {
        oldThread.interrupt();
        try {
            oldThread.join(1000L);
        } catch (InterruptedException ex) {
            // NOI18N
            LOGGER.error("Exception during waiting of search thread interruption", ex);
        }
    }
    this.progressBarSearch.setMinimum(0);
    this.progressBarSearch.setMaximum(size);
    this.progressBarSearch.setValue(0);
    thread.start();
}
Also used : NodeFileOrFolder(com.igormaznitsa.sciareto.ui.tree.NodeFileOrFolder) FileExaminator(com.igormaznitsa.sciareto.ui.misc.FileExaminator) File(java.io.File) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

FileExaminator (com.igormaznitsa.sciareto.ui.misc.FileExaminator)1 NodeFileOrFolder (com.igormaznitsa.sciareto.ui.tree.NodeFileOrFolder)1 File (java.io.File)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1