Implementing the MIDlet MainView class

The MainView class is used for implementing functionality of the MIDlet.

To implement the MainView class:

  1. Create the MainView.java class file.

  2. Import the required packages and classes.

    import com.nokia.mid.ui.FileSelect;
    import com.nokia.mid.ui.FileSelectDetail;
    
    import javax.microedition.io.ConnectionNotFoundException;
    
    import javax.microedition.lcdui.Alert;
    import javax.microedition.lcdui.AlertType;
    import javax.microedition.lcdui.Choice;
    import javax.microedition.lcdui.ChoiceGroup;
    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.lcdui.Form;
    import javax.microedition.lcdui.Item;
    import javax.microedition.lcdui.ItemStateListener;
  3. Set MainView class to extend Form and implement CommandListener, ItemStateListener and FileView.Listener. The MainView class uses CommandListener interface to receive notifications of Commands; ItemStateListener interface to receive notifications from Form items; and FileView.Listener interface to receive notifications of what type of files were selected.

    public class MainView
                extends Form
                implements CommandListener,
                ItemStateListener,
                FileView.Listener
    {
  4. Create the required variables.

    private static final int MODE_API_DEFINED = 0;
    private static final int MODE_BASED_ON_URL = 1;
    private static final String[] MODE_CHOICES =
        { "API defined", "System property URLs" };
    private static final String[] FILE_SYSTEMS =
        { FileSelect.FILE_SYSTEM_ALL,
          FileSelect.FILE_SYSTEM_INTERNAL,
          FileSelect.FILE_SYSTEM_EXTERNAL    };
    
    private static final int API_FILE_TYPE_ALL = 0;
    private static final int API_FILE_TYPE_PICTURE = 1;
    private static final int API_FILE_TYPE_AUDIO = 2;
    private static final int API_FILE_TYPE_VIDEO = 3;
    private static final int API_FILE_TYPE_APPLICATION = 4;
    private static final String[] API_FILE_TYPES =
        { "All", "Pictures", "Audio", "Videos", "Applications" };
    
    private static final int URL_FILE_TYPE_MUSIC = 0;
    private static final int URL_FILE_TYPE_PHOTOS = 1;
    private static final int URL_FILE_TYPE_VIDEO = 2;
    private static final String[] URL_FILE_TYPES =
        { "Music", "Photos", "Videos" };
    
    private Main midlet;
    private FileView fileView;
    private ChoiceGroup modeChoiceGroup;
    private ChoiceGroup fileSystemChoiceGroup;
    private ChoiceGroup fileTypeChoiceGroup;
    
    private final Command selectMultipleFilesCommand = new Command("Select multiple files", Command.SCREEN, 1);
    private final Command selectSingleFileCommand = new Command("Select single file", Command.SCREEN, 2);
    private final Command exitCommand = new Command("Exit", Command.EXIT, 1);
    private final Command backCommand = new Command("Back", Command.BACK, 1);
    private final Command openFileCommand = new Command("Open File", Command.SCREEN, 1);
    
    private int currentMode = -1;
  5. Create the constructor and initialize Form components.

    public MainView(String title, Main midlet) {
        super(title);
        this.midlet = midlet;
    
        modeChoiceGroup =
            new ChoiceGroup("Tap to select the mode", Choice.POPUP, MODE_CHOICES, null);
        fileSystemChoiceGroup =
            new ChoiceGroup("Tap to select the file system", Choice.POPUP, FILE_SYSTEMS, null);
    
        setMode(MODE_API_DEFINED);
    
        addCommand(selectMultipleFilesCommand);
        addCommand(selectSingleFileCommand);
        addCommand(exitCommand);
    
        setCommandListener(this);
        setItemStateListener(this);
    
        fileView = new FileView(this);
        fileView.addCommand(backCommand);
        fileView.addCommand(openFileCommand);
        fileView.setCommandListener(this);
    }
  6. Implement the commandAction method for detecting command invocations.

    public void commandAction(Command command, Displayable displayable) {
        if (command == selectMultipleFilesCommand
                || command == selectSingleFileCommand)
        {
            boolean multipleSelection = (command == selectMultipleFilesCommand);
    
            if (currentMode == MODE_API_DEFINED) {
                fileView.getSelectedFiles(getStartUrl(),
                                          getMediaType(),
                                          multipleSelection);
            }
            else {
                fileView.getSelectedFiles(getStartUrl(),
                                          FileSelect.MEDIA_TYPE_ALL,
                                          multipleSelection);
            }
        }
        else if (command == exitCommand) {
            midlet.quit();
        }
        else if (command == backCommand) {
            Display.getDisplay(midlet).setCurrent(this);
        }
        else if (command == openFileCommand) {
    
            onFileSelected(fileView.getSelectedFileDetails());
        }
    }
  7. Implement the itemStateChanged method for detecting item state invocations.

    public void itemStateChanged(Item item) {
        if (item == modeChoiceGroup) {
            setMode(modeChoiceGroup.getSelectedIndex());
        }
    }
  8. Implement the FileView.Listener interface methods.

    public void onMultipleFilesSelected(int count) {
        Display.getDisplay(midlet).setCurrent(fileView);
    }
    
    public void onFileSelected(FileSelectDetail fileDetails) {
        System.out.println("MainView::onFileSelected(): " + fileDetails.url);
    
        if (fileDetails != null) {
            try {
                midlet.platformRequest(fileDetails.url);
            }
            catch (ConnectionNotFoundException e) {
                System.out.println(e.toString());
                e.printStackTrace();
            }
        }
    }
    
    public void onError(String errorMessage) {
        Alert errorAlert = new Alert("Error", errorMessage, null, AlertType.ERROR);
        Display.getDisplay(midlet).setCurrent(errorAlert);
    }
  9. Create the setMode method to identify file selected based on URL or API mode.

    private void setMode(final int mode) {
        if (currentMode == mode) {
            return;
        }
    
        currentMode = mode;
    
        switch (currentMode) {
        case MODE_API_DEFINED:
            deleteAll();
            append(modeChoiceGroup);
            append(fileSystemChoiceGroup);
            fileTypeChoiceGroup =
                new ChoiceGroup("Tap to select file type", Choice.POPUP,
                                API_FILE_TYPES, null);
            append(fileTypeChoiceGroup);
            break;
        case MODE_BASED_ON_URL:
            deleteAll();
            append(modeChoiceGroup);
            fileTypeChoiceGroup =
                new ChoiceGroup("Tap to select file type", Choice.POPUP,
                                URL_FILE_TYPES, null);
            append(fileTypeChoiceGroup);
            break;
        default:
            System.out.println("MainView::setMode(): Invalid mode!");
            break;
        }
    }
  10. Create the getStartUrl method to retrieve the URL using SystemProperties method.

    private String getStartUrl() {
        if (currentMode == MODE_API_DEFINED) {
            // API defined
            return FILE_SYSTEMS[fileSystemChoiceGroup.getSelectedIndex()];
        }
    
        // URL based
        final int selectedFileType = fileTypeChoiceGroup.getSelectedIndex();
    
        switch (selectedFileType) {
        case URL_FILE_TYPE_MUSIC:
            return System.getProperty("fileconn.dir.music");
        case URL_FILE_TYPE_PHOTOS:
            return System.getProperty("fileconn.dir.photos");
        case URL_FILE_TYPE_VIDEO:
            return System.getProperty("fileconn.dir.videos");
        default:
            break;
        }
    
        return null;
    }
  11. Create the getMediaType method to retrieve the type of media to be selected.

    private int getMediaType() {
        final int selectedFileType = fileTypeChoiceGroup.getSelectedIndex();
    
        switch (selectedFileType) {
        case API_FILE_TYPE_ALL:
            return FileSelect.MEDIA_TYPE_ALL;
        case API_FILE_TYPE_PICTURE:
            return FileSelect.MEDIA_TYPE_PICTURE;
        case API_FILE_TYPE_AUDIO:
            return FileSelect.MEDIA_TYPE_AUDIO;
        case API_FILE_TYPE_VIDEO:
            return FileSelect.MEDIA_TYPE_VIDEO;
        case API_FILE_TYPE_APPLICATION:
            return FileSelect.MEDIA_TYPE_APPLICATION;
        default:
            break;
        }
    
        return FileSelect.MEDIA_TYPE_ALL;
    }