FileView.java
/**
* Copyright (c) 2013 Nokia Corporation. All rights reserved.
* Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
* Oracle and Java are trademarks or registered trademarks of Oracle and/or its
* affiliates. Other product and company names mentioned herein may be trademarks
* or trade names of their respective owners.
* See LICENSE.TXT for license information.
*/
package com.nokia.example.fileselectexample;
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;
/**
* A form for launching the file selector and dislaying the details of the
* selected files.
*/
public class FileView
extends Form
implements FileItem.Listener
{
// Members
private Listener listener;
private FileItem selectedFileItem;
/**
* Constructor.
* @param listener The listener for file selection events.
*/
public FileView(Listener listener){
super("Selected files");
this.listener = listener;
}
/**
* @see FileItem.Listener#onPressed(FileItem)
*/
public void onPressed(FileItem fileItem) {
if (selectedFileItem != null) {
selectedFileItem.setIsSelected(false);
}
if (fileItem != null) {
selectedFileItem = fileItem;
selectedFileItem.setIsSelected(true);
}
}
/**
* @see FileItem.Listener#onReleased(FileItem)
*/
public void onReleased(FileItem fileItem) {
}
/**
* @return The details of the selected file or null if no file is selected.
*/
public FileSelectDetail getSelectedFileDetails() {
if (selectedFileItem != null) {
return selectedFileItem.getFileDetails();
}
return null;
}
/**
* Runs a new thread for launching the file selector. The selected files
* are added to this form.
*
* @param startUrl Indicates the initial location to show in the file
* selector. Possible arguments are: FILE_SYSTEM_ALL, FILE_SYSTEM_INTERNAL
* and FILE_SYSTEM_EXTERNAL.
*
* @param mediaType Indicates what kind of files to show. Possible
* possible arguments are: MEDIA_TYPE_ALL, MEDIA_TYPE_APPLICATION,
* MEDIA_TYPE_AUDIO, MEDIA_TYPE_PICTURE and MEDIA_TYPE_VIDEO.
*
* @param multipleSelection If true, the user can select multiple files. If
* false, only one file can be selected.
*/
public void getSelectedFiles(final String startUrl,
final int mediaType,
final boolean multipleSelection)
{
final FileView fileView = this;
// Clear the form
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();
}
/**
* An interface for listener listening to file selection events.
*/
public interface Listener {
void onMultipleFilesSelected(int count);
void onFileSelected(FileSelectDetail fileDetails);
void onError(String errorMessage);
}
}