Showing posts with label file searcher for extension in SWT. Show all posts
Showing posts with label file searcher for extension in SWT. Show all posts

Wednesday, July 10, 2013

SWT Code -FileDialog,DirectoryDialog and multi-lined text field

Search for files with specified extension

Learn  how to use FileDialog,DirectoryDialog and multi-lined text field in SWT using Grid layout



/*
 *  program to get the file with specified extension.
 *
 * Description: If we gave directory path and extension of file , we will be able to select files with that specified extension
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class FileSearchForExtension {
       Display display = new Display();
       Shell shell = new Shell(display);
       Text text;
       String selectedDir;
       Label label;
       Text text1, fileNameText;
       String fileFilterPath = "";
       StringBuilder sb = new StringBuilder("");

       public FileSearchForExtension() {
              init();
              shell.pack();
              shell.setSize(450, 300); // setting size of the shell
              shell.open();

              while (!shell.isDisposed()) {
                     if (!display.readAndDispatch()) {
                           display.sleep();
                     }
              }
              display.dispose();
       }

       private void init() {
              shell.setText("File Searcher for Extension");
              GridLayout gridLayout = new GridLayout();
              gridLayout.numColumns = 2;
              gridLayout.makeColumnsEqualWidth = true;
              shell.setLayout(gridLayout);
              GridData data = new GridData(GridData.FILL_BOTH);

              // UI design 
              // Directory selection button and text box

              Button button = new Button(shell, SWT.PUSH);
              button.setText("Select a directory");
              text = new Text(shell, SWT.NONE);
              text.setLayoutData(data);

              // Enter extension manually
              label = new Label(shell, SWT.BORDER | SWT.WRAP);
              label.setText("Enter the extension to search");
              text1 = new Text(shell, SWT.NONE);
              text1.setLayoutData(data);

              // Multiple/single file name selection button and text box

              Button buttonSelectFile = new Button(shell, SWT.PUSH);
              buttonSelectFile.setText("Select a file/multiple files names");
              fileNameText = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.WRAP
                           | SWT.V_SCROLL);
              fileNameText.setLayoutData(data);

              Button buttonClear = new Button(shell, SWT.PUSH);
              buttonClear.setText("clear fileds");
              GridData gridData = new GridData();
              gridData.horizontalAlignment = GridData.CENTER;
              gridData.horizontalSpan = 2;
              buttonClear.setLayoutData(gridData);

              // Adding listner for directory selection using directory dialog
              button.addListener(SWT.Selection, new Listener() {
                     public void handleEvent(Event event) {
                           DirectoryDialog directoryDialog = new DirectoryDialog(shell);

                           directoryDialog.setFilterPath(selectedDir);
                           directoryDialog
                                         .setMessage("Please select a directory and click OK");

                           String dir = directoryDialog.open();
                           if (dir != null) {
                                  text.setText(dir);
                                  text.setEditable(true);
                                  selectedDir = dir;
                           }
                     }
              });

              // adding listener to file names selection using file dialog
              buttonSelectFile.addListener(SWT.Selection, new Listener() {
                     public void handleEvent(Event event) {
                           FileDialog fileDialog = new FileDialog(shell, SWT.MULTI);

                           fileDialog.setFilterPath(text.getText());
                           String extension = text1.getText();
                           fileDialog
                                         .setFilterExtensions(new String[] { "*." + extension });

                           String firstFile = fileDialog.open();

                           if (firstFile != null) {
                                  fileFilterPath = fileDialog.getFilterPath();
                                  String[] selectedFiles = fileDialog.getFileNames();

                                  for (int i = 0; i < selectedFiles.length; i++) {
                                         sb.append(selectedFiles[i] + "\n");
                                  }
                                  fileNameText.setText(sb.toString());
                           }
                     }
              });

              // Adding listener to the clear button
              buttonClear.addListener(SWT.Selection, new Listener() {
                     public void handleEvent(Event event) {
                           text.setText("");
                           text1.setText("");
                           fileNameText.setText("");
                           sb.setLength(0);
                     }
              });

       }

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