/
FileFinder.java
186 lines (165 loc) · 5.27 KB
/
FileFinder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//#condition midp >= 2.0
/*
* Copyright 2009
* DedaSys LLC - http://www.dedasys.com
*
* Author: David N. Welton <davidw@dedasys.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hecl.files;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;
/**
* The <code>FileFinder</code> class displays a very simple file
* selection dialog that lets you descend through a file hierarchy and
* select a file that matches a condition specified by the
* FileFinderCallback.match method, and then perform an action
*
* @author <a href="mailto:davidw@dedasys.com">David N. Welton</a>
* @version 1.0
*/
public class FileFinder extends List implements CommandListener, Runnable {
Command selectFileCmd = null;
Command cancelBrowseCmd = null;
Command upCmd = null;
String currentFile = "";
String lastFile = "";
FileFinderCallback ffcallback = null;
/**
* Creates a new <code>FileFinder</code> instance.
*
* @param title a <code>String</code> value
* @param startDir a <code>String</code> value that specifies
* where to start looking. If this is null, start from the
* 'roots'.
* @param callback a <code>FileFinderCallback</code> value
*/
public FileFinder(String title, String startDir, FileFinderCallback callback) {
super(title, List.IMPLICIT);
ffcallback = callback;
currentFile = startDir;
this.setCommandListener(this);
selectFileCmd = new Command("Select", Command.ITEM, 1);
cancelBrowseCmd = new Command("Cancel", Command.CANCEL, 3);
upCmd = new Command("Up", Command.ITEM, 2);
this.setSelectCommand(selectFileCmd);
this.addCommand(upCmd);
this.addCommand(cancelBrowseCmd);
run();
}
/**
* The <code>commandAction</code> method is called when the end
* user presses either the 'select' or 'cancel' commands.
*
* @param c a <code>Command</code> value
* @param d a <code>Displayable</code> value
*/
public void commandAction(Command c, Displayable d) {
if (c == cancelBrowseCmd) {
ffcallback.cancel(this);
return;
} else if (c == upCmd) {
up();
this.deleteAll();
} else if (c == selectFileCmd) {
int idx = this.getSelectedIndex();
if (idx < 0) {
/* Nothing has been selected. */
return;
}
String newFile = this.getString(idx);
if (newFile == null) {
ffcallback.error(this, "No file selected");
return;
}
if(newFile.startsWith("file:///")) {
currentFile = newFile;
} else {
if (currentFile == null || currentFile == "") {
currentFile = "file:///";
}
currentFile = currentFile + newFile;
}
this.deleteAll();
}
/* Kick off the thread to finish processing and displaying
* information. */
new Thread(this).start();
}
/**
* The <code>up</code> method is called when the user presses the
* 'up' command, in order to attempt to go up a directory.
*
*/
private void up() {
if (currentFile == null) {
return;
}
int last = currentFile.lastIndexOf('/');
int secondlast = currentFile.lastIndexOf('/', last-1);
/* It's only a root. */
if (currentFile.startsWith("file:///") && secondlast == 7) {
currentFile = null;
return;
}
currentFile = currentFile.substring(0, secondlast) + "/";
}
/**
* The <code>run</code> method (in other words, a new thread) is
* where we offload the processing so that it is not run in the
* commandAction.
*
*/
public synchronized void run() {
FileConnection fconn = null;
/* If there is no currentFile, show the roots and return. */
if (currentFile == null) {
for (Enumeration e = FileSystemRegistry.listRoots(); e.hasMoreElements();) {
String root = (String)e.nextElement();
this.append(root, null);
}
return;
}
try {
fconn = (FileConnection)Connector.open(currentFile);
} catch (Exception e) {
ffcallback.error(this, "Cannot open FileConnection \"" + currentFile + "\" :" + e.toString());
return;
}
/* If the selected file is a match, we indicate that we have
* selected it, and return. */
if (ffcallback.match(this, fconn)) {
ffcallback.selected(this, currentFile);
return;
}
/* Otherwise, try and do a listing. */
try {
for (Enumeration e = fconn.list(); e.hasMoreElements();) {
String fname = (String)e.nextElement();
this.append(fname, null);
}
} catch (Exception e) {
ffcallback.error(this, "Cannot list files: " + e.toString());
}
}
}