Implementing the MIDlet FileView class

To implement the FileView class:

  1. Create the FileView.java class file.

  2. Import required packages and classes.

    import java.io.IOException;
    
    import com.nokia.mid.ui.FileSelect;
    import com.nokia.mid.ui.FileSelectDetail;
    
    import javax.microedition.lcdui.Form;
    import javax.microedition.lcdui.StringItem;
  3. Set FileView class to extend Form and implement FileItem.Listener. The FileView class uses FileItem.Listener interface to receive notifications when file item is selected.

    public class FileView
                extends Form
                implements FileItem.Listener
    {
  4. Create the required variables and the constructor.

    private Listener listener;
    private FileItem selectedFileItem;
    
    public FileView(Listener listener) {
        super("Selected files");
        this.listener = listener;
    
    }
  5. Implement the FileItem.Listener interface methods.

    public void onPressed(FileItem fileItem) {
        if (selectedFileItem != null) {
            selectedFileItem.setIsSelected(false);
        }
    
        if (fileItem != null) {
            selectedFileItem = fileItem;
            selectedFileItem.setIsSelected(true);
        }
    }
    
    
    public void onReleased(FileItem fileItem) {
    }
    
    public FileSelectDetail getSelectedFileDetails() {
        if (selectedFileItem != null) {
            return selectedFileItem.getFileDetails();
        }
    
        return null;
    }
  6. Create the getSelectedFile method, which runs a new thread for launching the file selector. The selected files are added to this Form.

    public void getSelectedFiles(final String startUrl,
                                 final int mediaType,
                                 final boolean multipleSelection)
    {
        final FileView fileView = this;
    
        deleteAll();
        setTitle("No files selected");
    
        new Thread(new Runnable() {
            public void run() {
                FileSelectDetail selectedFiles[] = null;
    
                try {
                    selectedFiles =
                        FileSelect.launch(startUrl, mediaType, multipleSelection);
                }
                catch (IllegalArgumentException e) {
                    e.printStackTrace();
                }
                catch (IOException e) {
                    setTitle("Error");
                    String errorMessage =
                        new String("Failed to launch the file selector: " + e.toString());
                    StringItem errorMessageItem = new StringItem(null, errorMessage);
                    append(errorMessageItem);
    
                    if (listener != null) {
                        listener.onError(errorMessage);
                    }
                }
    
                int numberOfSelectedFiles = 0;
    
                if (selectedFiles != null) {
                    numberOfSelectedFiles = selectedFiles.length;
                }
    
                if (numberOfSelectedFiles != 0) {
                    if (numberOfSelectedFiles > 1) {
                        setTitle(numberOfSelectedFiles + " files selected");
    
                        if (listener != null) {
                            listener.onMultipleFilesSelected(numberOfSelectedFiles);
                        }
                    }
                    else {
                        setTitle("One file selected");
    
                        if (listener != null) {
                            listener.onFileSelected(selectedFiles[0]);
                        }
                    }
    
                    for (int i = 0; i < numberOfSelectedFiles; ++i) {
                        append(new FileItem(selectedFiles[i], fileView));
                    }
                }
                else {
                    System.out.println("FileView::getSelectedFiles(): No files selected.");
                }
            }
        }).start();
    }
  7. Create the interface to listen to file selection events.

    public interface Listener {
        void onMultipleFilesSelected(int count);
        void onFileSelected(FileSelectDetail fileDetails);
        void onError(String errorMessage);
    }