Monday, March 26, 2007

My Legs Ache/burn At Night

Search

JProgressBar Progress bars and graphics applications

This example looks for matches in files from a directory specified by the user
.

Problem:
If we have an application that performs a process that consumes a relatively long time, it's good to know the user of the application that the task is being processed and more or less the time left, so typical of the message: 'The process is by 20%' for example.

The Java Swing libraries, has mechanisms to show this progress, we will make an example of an application that searches a text string in all files in a directory espedificado.

Original source: telepolis.com
 
java.awt.GridLayout import, import
java.awt.Cursor;
import java.awt.event .*;
import java.util .*;
import java. io .*;
import javax.swing .*;
import java.lang.reflect.InvocationTargetException; ProgressDemo public class String {

startdir / / search directory
String patt, / / \u200b\u200bso let's look
outarea JTextArea / / output area for file pathnames
JFrame frame / / frame
JProgressBar Progbear / / progress bar
fileslab JLabel / / number of files
found search_flag boolean / / true if the search is in progress


class Search extends Thread {

/ / update the GUI
void doupdate (Runnable r) {try {

SwingUtilities.invokeAndWait (r);}

catch (InvocationTargetException e1) {
System.err.println (e1);

} catch (InterruptedException e2) {
System.err.println (e2);

}}
/ / gives us the list of files void
getFileList directory (File f, List list) {

/ / recursion if a directory within the same
if (f.isDirectory ()) {
String entries [] = f.list ();
for ( int i = 0; i < entries.length; i++) {
getFileList (new File (f, entries [i])
list);}



} / / for fciheros be added to the list and updates the progress bar
else if (f.isFile ()) {list.add
(f.getPath ());
final int size = list.size ();
if (size% 100! = 0) {
return;}

doupdate (new Runnable () {
public void run () {
progbar.setValue (size% 1000 )

}});}



} / / check that the file contains the string
fileMatch boolean (String fn, String patt) {boolean found = false
;


try {FileReader fr = new FileReader (fn) ;
BufferedReader br = new BufferedReader (fr);
String str;
while ((str = br.readLine ())! = null) {
if (str.indexOf (patt)! = -1) {found =
true;
break;

}}
br.close ();

} catch (IOException e) {
System.err.println (e);}

return found;}




/ / perform the search
public void run () {
List filelist = new ArrayList ();

final String sep = System.getProperty ("line.separator");

doupdate (new Runnable () {public void
run () {
outarea.setText
fileslab.setText ("");}

("");});

/ / gives us the list of all files and displays the counter
getFileList (new File ( startdir), filelist);
filelist.size final int size = ();
doupdate (new Runnable () {
public void run () {
progbar.setValue (0);
fileslab.setText ("Found" + size +
"files, searching ...");

}});

/ / set the progress monitor final
ProgressMonitor pm = new ProgressMonitor (
frame, "Searching for files", "", 0, size - 1);
pm.setMillisToDecideToPopup (0);
pm . setMillisToPopup (0);

/ / iterate between the files, updating the progress monitor
for (int i = 0; i < size; i++) {
final String fn = (String) filelist.get (i);
final int curr = i ;
if (pm.isCanceled ()) {
break;
}
final boolean b = fileMatch(fn, patt);
doUpdate(new Runnable() {
public void run() {
pm.setProgress(curr);
pm.setNote(fn);
if (b) {
outarea.append(fn + sep);
}
}
});
}

doUpdate(new Runnable() {
public void run() {
pm.close();
outarea.setCaretPosition(0);
fileslab.setText("");
}
});
search_flag = false;
}
}

public ProgressDemo() {
frame = new JFrame("ProgressDemo");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});



JPanel paneltop = new JPanel ();
panelbot JPanel = new JPanel ();
paneltop.setLayout (new GridLayout (5, 1));
JPanel panel1 = new JPanel ();
panel1.add (new JLabel ("Home Directory")) ; final
dirfield = new JTextField JTextField (20);
panel1.add (dirfield)
panel2 JPanel = new JPanel ();
panel2.add (new JLabel ("Search Pattern"));
pattfield = final JTextField new JTextField (20);
panel2.add (pattfield)
panel3 JPanel = new JPanel ();
JButton button = new JButton ("Search");
panel3.add(button);
JPanel panel4 = new JPanel();
progbar = new JProgressBar(0, 999);
panel4.add(progbar);
JPanel panel5 = new JPanel();
fileslab = new JLabel();
panel5.add(fileslab);
JPanel panel6 = new JPanel();
outarea = new JTextArea(8, 40);
outarea.setEditable(false);
JScrollPane jsp = new JScrollPane(outarea,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
panel6.add(jsp);

button.addActionListener (new ActionListener () {public void
actionPerformed (ActionEvent e) {startdir = dirfield.getText
();
pattfield.getText patt = ();
if (startdir == null else if (search_flag) {JOptionPane.showMessageDialog
(
frame, "Search in progress,"
"Error", JOptionPane.ERROR_MESSAGE)

} else {
search_flag = true;
new Search (). start ();

}}}
)

paneltop.add (panel1);
paneltop.add (panel2)
paneltop.add (panel3)
paneltop.add (panel4)
paneltop.add (panel5)
panelbot.add (panel6)
JPanel panel = new JPanel ();
panel.setLayout (new GridLayout (2, 1));
panel.add (paneltop)
panel.add (panelbot)
frame. getContentPane (). add (panel);
frame.pack ();
frame.setLocation (200, 200);
frame.setVisible (true);


} public static void main (String args []) {
ProgressDemo new ();
}}





0 comments:

Post a Comment