2 messages in org.netbeans.mobility.usersRe: [mobility] There is some problem ...
FromSent OnAttachments
jonson_leeJun 16, 2007 10:21 pm 
Karol HarezlakJun 17, 2007 12:25 am 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: [mobility] There is some problem in Netbeans6.0M9's FileBrowserActions
From:Karol Harezlak (Karo@Sun.COM)
Date:Jun 17, 2007 12:25:57 am
List:org.netbeans.mobility.users

Hi Jonson,

Thanks for your contribution. I'll report this problem to the bug reporting tool - Netbeans Issuezilla with suggested bug fix. Please take a look at Step-by-Step guide how to use Issuezilla - http://www.netbeans.org/kb/articles/issuezilla.html so next time you'll be able to report issues directly to the bug tracking tool. Thanks again,

jonson_lee wrote:

To show the problem about FileBrowser in Netbeans6.0, I program a routine which can get the words in the txt file and display the results in the List.It can be run very well in the emulator,but when I deploy the routine in the Nokia N73,there is some problem, the N73 will throws"unhandled exception".After I choose the file in the filebrowser,the routines suspend and nothing will be displayed in the List.At last, I find the problem is in filebrowser and I program another FileBrowser, and it runs well both in emulator and the N73.

Following is my routine. /*****FileBrowser in Netbeans6.0******/ /* * The contents of this file are subject to the terms of the Common Development * and Distribution License (the License). You may not use this file except in * compliance with the License. * * You can obtain a copy of the License at http://www.netbeans.org/cddl.html * or http://www.netbeans.org/cddl.txt. * * When distributing Covered Code, include this CDDL Header Notice in each file * and include the License file at http://www.netbeans.org/cddl.txt. * If applicable, add the following below the CDDL Header, with the fields * enclosed by brackets [] replaced by your own identifying information: * "Portions Copyrighted [year] [name of copyright owner]" * * The Original Software is NetBeans. The Initial Developer of the Original * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun * Microsystems, Inc. All Rights Reserved. */

package filereader;

import java.util.*; import java.io.*; import javax.microedition.io.*; import javax.microedition.io.file.*; import javax.microedition.midlet.*; import javax.microedition.lcdui.*;

/** * * @author breh * edited: Karol Harezlak */

public class FileBrowser extends List implements CommandListener {

public static final Command SELECT_FILE_COMMAND = new Command("Select",Command.OK,1);

private String currDirName; private String currFile; private Image dirIcon, fileIcon; private Image[] iconList; private CommandListener commandListener;

/* special string denotes upper directory */ private final static String UP_DIRECTORY = "..";

/* special string that denotes upper directory accessible by this browser. * this virtual directory contains all roots. */ private final static String MEGA_ROOT = "/";

/* separator string as defined by FC specification */ private final static String SEP_STR = "/";

/* separator character as defined by FC specification */ private final static char SEP = '/';

private Display display;

private String selectedURL;

private String filter = null;

private String title;

public FileBrowser(Display display) { super("", IMPLICIT); currDirName = MEGA_ROOT; this.display = display; super.setCommandListener(this); setSelectCommand(SELECT_FILE_COMMAND); try { dirIcon = Image.createImage("/client/resources/dir.png"); } catch (IOException e) { dirIcon = null; } try { fileIcon = Image.createImage("/client/resources/file.png"); } catch (IOException e) { fileIcon = null; } iconList = new Image[] { fileIcon, dirIcon };

showDir();

}

private void showDir() { new Thread(new Runnable(){ public void run(){ try { showCurrDir(); } catch (SecurityException e) { Alert alert = new Alert("Error", "You are not authorized to access the restricted API", null, AlertType.ERROR); alert.setTimeout(2000); display.setCurrent(alert, FileBrowser.this); } catch (Exception e) { e.printStackTrace(); } } }).start(); }

public void commandAction(Command c, Displayable d) { if(c.equals(SELECT_FILE_COMMAND)){ List curr = (List)d; currFile = curr.getString(curr.getSelectedIndex()); new Thread(new Runnable() { public void run() { if (currFile.endsWith(SEP_STR) || currFile.equals(UP_DIRECTORY)) { openDir(currFile); } else {//是文件 //switch To Next doDismiss();//执行外部窗口的commandAction } } }).start(); }else{ commandListener.commandAction(c, d);//执行上一层窗口的commandAction方法 } }

public void setTitle(String title){ this.title = title; super.setTitle(title); } /** * Show file list in the current directory . */ private void showCurrDir() { if(title == null){ super.setTitle(currDirName); } Enumeration e = null; FileConnection currDir = null;

deleteAll(); if (MEGA_ROOT.equals(currDirName)) {//是最顶目录,最顶目录为'/' append(UP_DIRECTORY, dirIcon); e = FileSystemRegistry.listRoots(); } else { try { append(UP_DIRECTORY, dirIcon);//原来的有Bug,增加了这一行 currDir = (FileConnection) Connector.open("file:///" + currDirName); e = currDir.list(); } catch (IOException ioe) {

}

}

if (e == null) { try { currDir.close(); } catch (IOException ioe) { ioe.printStackTrace(); } return; }

while (e.hasMoreElements()) { String fileName = (String)e.nextElement(); if (fileName.charAt(fileName.length()-1) == SEP) { // This is directory append(fileName, dirIcon); } else { // this is regular file if(filter == null || fileName.indexOf(filter) > -1 ){ append(fileName, fileIcon); } } }

if (currDir != null) { try { currDir.close(); } catch (IOException ioe) { ioe.printStackTrace(); } }

}

private void openDir(String fileName) { /* In case of directory just change the current directory * and show it */ if (currDirName.equals(MEGA_ROOT)) { if (fileName.equals(UP_DIRECTORY)) { // can not go up from MEGA_ROOT return; } currDirName = fileName; } else if (fileName.equals(UP_DIRECTORY)) { // Go up one directory // TODO use setFileConnection when implemented int i = currDirName.lastIndexOf(SEP, currDirName.length()-2); if (i != -1) { currDirName = currDirName.substring(0, i+1); } else { currDirName = MEGA_ROOT; } } else { currDirName = currDirName + fileName; } showDir(); }

public FileConnection getSelectedFile() throws IOException{ return (FileConnection)Connector.open(selectedURL); }

public String getSelectedFileURL(){ return selectedURL; }

public void setFilter(String filter){ this.filter = filter; }

protected CommandListener getCommandListener() { return commandListener; }

/** * Sets command listener to this component * @param commandListener - command listener to be used */ public void setCommandListener(CommandListener commandListener) { this.commandListener = commandListener; }

private void doDismiss() { selectedURL = "file:///" + currDirName + SEP_STR + currFile; CommandListener commandListener = getCommandListener(); if (commandListener != null) { commandListener.commandAction(SELECT_FILE_COMMAND,this); } }

} /***My FileBrowser***/

import java.io.*; import java.util.*;

import javax.microedition.io.*; import javax.microedition.io.file.*; import javax.microedition.lcdui.*; import javax.microedition.lcdui.Image;

// Simple file selector class. // It navigates the file system and shows images currently available class FileBrowser extends List implements CommandListener, FileSystemListener {

private Image ROOT_IMAGE; private Image FOLDER_IMAGE;

private Image FILE_IMAGE; private final OperationsQueue queue = new OperationsQueue(); /*private final static String FILE_SEPARATOR = (System.getProperty("file.separator")!=null)? System.getProperty("file.separator"):"/";*/ private final static String FILE_SEPARATOR = "/"; private final static String UPPER_DIR = ".."; private Display display; public final static Command SELECT_FILE_COMMAND = new Command("Open", Command.ITEM, 1); private final Command createDirCommand = new Command("Create new directory", Command.ITEM, 2); private final Command deleteCommand = new Command("Delete", Command.ITEM, 3); private final Command renameCommand = new Command("Rename", Command.ITEM, 4); private final static int RENAME_OP = 0; private final static int MKDIR_OP = 1; private final static int INIT_OP = 2; private final static int OPEN_OP = 3; private final static int DELETE_OP = 4; private Vector rootsList = new Vector(); // Stores the current root, if null we are showing all the roots private FileConnection currentRoot = null; // Stores a suggested title in case it is available private String suggestedTitle = null; private String selectedURL; private CommandListener commandListener; FileBrowser(Display display) { super("Image Viewer", List.IMPLICIT); this.display = display; addCommand(SELECT_FILE_COMMAND); addCommand(createDirCommand); addCommand(deleteCommand); addCommand(renameCommand); setSelectCommand(SELECT_FILE_COMMAND); super.setCommandListener(this); //初始化图标 try{ ROOT_IMAGE = Image.createImage("/icons/dir.png"); }catch(IOException e){ e.printStackTrace(); } try{ FOLDER_IMAGE = Image.createImage("/icons/dir.png"); }catch(IOException e){ e.printStackTrace(); } try{ FILE_IMAGE = Image.createImage("/icons/file.png"); }catch(IOException e) {

}

queue.enqueueOperation(new FileOperations(INIT_OP)); FileSystemRegistry.addFileSystemListener(FileBrowser.this); } void stop() { if (currentRoot != null) { try { currentRoot.close(); } catch (IOException e) { e.printStackTrace(); } } queue.abort(); FileSystemRegistry.removeFileSystemListener(this); } void inputReceived(String input, int code) { switch (code) { case RENAME_OP: queue.enqueueOperation(new FileOperations( input, RENAME_OP)); break; case MKDIR_OP: queue.enqueueOperation(new FileOperations( input, MKDIR_OP)); break; } } public String getSelectedFileURL(){ return selectedURL; } public void setCommandListener(CommandListener commandListener){ this.commandListener = commandListener; } public void commandAction(Command c, Displayable d) { if (c == SELECT_FILE_COMMAND) { queue.enqueueOperation(new FileOperations(OPEN_OP)); } else if (c == renameCommand) { queue.enqueueOperation(new FileOperations(RENAME_OP)); } else if (c == deleteCommand) { queue.enqueueOperation(new FileOperations(DELETE_OP)); } else if (c == createDirCommand) { queue.enqueueOperation(new FileOperations(MKDIR_OP)); }else {//例如后退返回命令就执行这一步 commandListener.commandAction(c, d); }

} // Listen for changes in the roots public void rootChanged(int state, String rootName) { queue.enqueueOperation(new FileOperations(INIT_OP)); } private void displayAllRoots() { setTitle("Image Viewer - [Roots]"); deleteAll(); Enumeration roots = rootsList.elements(); while (roots.hasMoreElements()) { String root = (String) roots.nextElement(); append(root.substring(1), ROOT_IMAGE); } // try{ currentRoot.close(); } catch(IOException e){ e.printStackTrace(); }

currentRoot = null; } private void createNewDir() { if (currentRoot == null) { showMsg("Is not possible to create a new root"); } else { //midlet.requestInput("New dir name", "", MKDIR_OP); } } private void createNewDir(String newDirURL) { if (currentRoot != null) { try { FileConnection newDir = (FileConnection) Connector.open( currentRoot.getURL() + newDirURL, Connector.WRITE); newDir.mkdir(); newDir.close(); } catch (IOException e) { showError(e); } catch (SecurityException e) { showMsg(e.getMessage()); } displayCurrentRoot(); } } private void loadRoots() { if (!rootsList.isEmpty()) { rootsList.removeAllElements(); } try { Enumeration roots = FileSystemRegistry.listRoots(); while (roots.hasMoreElements()) { rootsList.addElement(FILE_SEPARATOR + (String) roots.nextElement()); } } catch (SecurityException e) { showMsg(e.getMessage()); } } private void deleteCurrent() { if (currentRoot == null) { showMsg("Is not possible to delete a root"); } else { int selectedIndex = getSelectedIndex(); if (selectedIndex >= 0) { String selectedFile = getString(selectedIndex); if (selectedFile.equals(UPPER_DIR)) { showMsg("Is not possible to delete an upper dir");

} else { try { FileConnection fileToDelete = (FileConnection) Connector.open( currentRoot.getURL() + selectedFile, Connector.WRITE); if (fileToDelete.exists()) { fileToDelete.delete(); } else { showMsg("File " + fileToDelete.getName() + " does not exists"); } fileToDelete.close(); } catch (IOException e) { showError(e); } catch (SecurityException e) { showError(e); } displayCurrentRoot(); } } } } private void renameCurrent() { if (currentRoot == null) { showMsg("Is not possible to rename a root"); } else { int selectedIndex = getSelectedIndex(); if (selectedIndex >= 0) { String selectedFile = getString(selectedIndex); if (selectedFile.equals(UPPER_DIR)) { showMsg("Is not possible to rename the upper dir"); } else { //midlet.requestInput("New name", selectedFile, RENAME_OP); } } } } private void renameCurrent(String newName) { if (currentRoot == null) { showMsg("Is not possible to rename a root"); } else { int selectedIndex = getSelectedIndex(); if (selectedIndex >= 0) { String selectedFile = getString(selectedIndex); if (selectedFile.equals(UPPER_DIR)) { showMsg("Is not possible to rename the upper dir"); } else { try { FileConnection fileToRename = (FileConnection) Connector.open( currentRoot.getURL() + selectedFile, Connector.WRITE); if (fileToRename.exists()) { fileToRename.rename(newName); } else { showMsg("File " + fileToRename.getName() + " does not exists"); } fileToRename.close(); } catch (IOException e) { showError(e); } catch (SecurityException e) { showError(e); } displayCurrentRoot(); } } } }

private void openSelected() { int selectedIndex = getSelectedIndex(); if (selectedIndex >= 0) { String selectedFile = getString(selectedIndex);

if (selectedFile.endsWith(FILE_SEPARATOR)) { try { if (currentRoot == null) { currentRoot = (FileConnection) Connector.open( "file:///" + selectedFile, Connector.READ); } else { currentRoot.setFileConnection(selectedFile);

} displayCurrentRoot(); } catch (IOException e) { showError(e); } catch (SecurityException e) { showError(e); }

} else if (selectedFile.equals(UPPER_DIR)) { //ddddddddddddddddd //System.out.println(currentRoot.getPath()); // //System.out.println(currentRoot.getName()); //dddddddddddddd if(rootsList.contains(currentRoot.getPath() +currentRoot.getName())) { displayAllRoots(); } else { try { currentRoot.setFileConnection(UPPER_DIR); displayCurrentRoot();

} catch (IOException e) { showError(e); } catch (SecurityException e) { showMsg(e.getMessage()); } } } else {

//在列表中显示的,除了目录就是Jpg,PNG文件 selectedURL = currentRoot.getURL() + selectedFile; commandListener.commandAction(SELECT_FILE_COMMAND, this); } } } private void displayCurrentRoot() { try { setTitle("Image Viewer - [" + ((suggestedTitle!=null)?suggestedTitle:currentRoot.getURL()) + "]"); // open the root deleteAll(); append(UPPER_DIR, FOLDER_IMAGE); // list all dirs Enumeration listOfDirs = currentRoot.list("*", false);

while (listOfDirs.hasMoreElements()) { String currentDir = (String) listOfDirs.nextElement(); if (currentDir.endsWith(FILE_SEPARATOR)) { append(currentDir, FOLDER_IMAGE); } }

// list all png files and don’t show hidden files Enumeration listOfFiles = currentRoot.list("*.png", false);

while (listOfFiles.hasMoreElements()) { String currentFile = (String) listOfFiles.nextElement(); if (currentFile.endsWith(FILE_SEPARATOR)) { append(currentFile, FOLDER_IMAGE); } else { append(currentFile, FILE_IMAGE); } }

// also list the jpg files listOfFiles = currentRoot.list("*.jpg", false); while (listOfFiles.hasMoreElements()) { String currentFile = (String) listOfFiles.nextElement(); if (currentFile.endsWith(FILE_SEPARATOR)) { append(currentFile, FOLDER_IMAGE); } else { append(currentFile, FILE_IMAGE); } } listOfFiles = currentRoot.list("*.txt", false); while (listOfFiles.hasMoreElements()) { String currentFile = (String) listOfFiles.nextElement(); if (currentFile.endsWith(FILE_SEPARATOR)) { append(currentFile, FOLDER_IMAGE); } else { append(currentFile, FILE_IMAGE); } }

} catch (IOException e) { showError(e); } catch (SecurityException e) { showError(e); } }

private class FileOperations implements Operation { private final String parameter; private final int operationCode;

FileOperations(int operationCode) { this.parameter = null; this.operationCode = operationCode; } FileOperations(String parameter, int operationCode) { this.parameter = parameter; this.operationCode = operationCode; } public void execute() { switch (operationCode) { case INIT_OP: String initDir = System.getProperty("fileconn.dir.photos"); suggestedTitle = System.getProperty("fileconn.dir.photos.name"); loadRoots(); if (initDir != null) { try { currentRoot = (FileConnection) Connector.open( initDir, Connector.READ); displayCurrentRoot(); } catch (IOException e) { showError(e); displayAllRoots(); } catch (SecurityException e) { showError(e); } } else { displayAllRoots(); } break; case OPEN_OP: openSelected(); break; case DELETE_OP: deleteCurrent(); break; case RENAME_OP: if (parameter != null) { renameCurrent(parameter); } else { renameCurrent(); } break; case MKDIR_OP: if (parameter != null) { createNewDir(parameter); } else { createNewDir(); } } } } void showError(Exception e) { Alert infoScreen = new Alert("Image Viewer", e.getMessage(), null, AlertType.INFO); infoScreen.setTimeout(3000); display.setCurrent(infoScreen, this);

} void showMsg(String text) {

Alert infoScreen = new Alert("Image Viewer", text, null, AlertType.INFO); infoScreen.setTimeout(3000); display.setCurrent(infoScreen, this); }

class OperationsQueue implements Runnable { private volatile boolean running = true; private final Vector operations = new Vector(); OperationsQueue() { //Notice that all operations will be done in another //thread to avoid deadlocks with GUI thread new Thread(this).start(); }

void enqueueOperation(Operation nextOperation) { operations.addElement(nextOperation);

synchronized (this) { notify(); } } //stop the thread public void abort() { running = false; synchronized (this) { notify(); } } public void run() { while (running) { while (operations.size() > 0) { try { //execute the first operation on the queue ((Operation) operations.firstElement()).execute(); } catch (Exception e) { //Nothing to do. It is expected that each operation handles //its own exception locally but this block is to ensure //that the queue continues to operate } operations.removeElementAt(0); } synchronized (this) { try { wait(); } catch (InterruptedException e) { //it doesn't matter } } } }

} public interface Operation { // Implement here the operation to be executed void execute(); } }

Following is the routine Pack

http://www.nabble.com/file/p11160683/filereader.rar filereader.rar