📄 filebrowsingcanvas.java
字号:
/*****************************************************************************
Description: FileBrowsingCanvas
* The FileBrowser is used to traverse directories and showing the content.
* It has also the ability to show images.
Created By: Johan Kateby
@file FileBrowsingCanvas.java
COPYRIGHT All rights reserved Sony Ericsson Mobile Communications AB 2004.
The software is the copyrighted work of Sony Ericsson Mobile Communications AB.
The use of the software is subject to the terms of the end-user license
agreement which accompanies or is included with the software. The software is
provided "as is" and Sony Ericsson specifically disclaim any warranty or
condition whatsoever regarding merchantability or fitness for a specific
purpose, title or non-infringement. No warranty of any kind is made in
relation to the condition, suitability, availability, accuracy, reliability,
merchantability and/or non-infringement of the software provided herein.
*****************************************************************************/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
import javax.microedition.io.file.*;
import javax.microedition.io.*;
public class FileBrowsingCanvas extends Canvas implements FileActionInvoker, Runnable {
private static int stringIndex = 0;
public static final int TXT_CLOSE = stringIndex++;
public static final int TXT_VIEW = stringIndex++;
public static final int TXT_SELECT = stringIndex++;
public static final int TXT_BACK = stringIndex++;
public static final int TXT_VIEW_IMAGE = stringIndex++;
private final static char[][]systemText = new char[][] {"PRESS ANY KEY TO GO BACK".toCharArray(),
"View".toCharArray(),
"Select".toCharArray(),
"Back".toCharArray(),
"View Image".toCharArray()};
// canvas width and height
private final int WIDTH, HEIGHT;
private static String ROOT= "/";
private static String UP = "..";
// the selected index in the dirList
private int dirIndex =0;
private String currentDir="/";
// This vector contains the directories and files in thecurrent directory
private Vector dirList = new Vector();
private int dirSize=0;
// is used to check which index of the lst object in the dirListVector is a directory
private int lastDirIndex=0;
private Image folderImage;
private Image fileImage;
private Image downArrowImage;
private Image upArrowImage;
// This indicates for what purpose the FileBrowser is going to used for
public static final int TYPE_IMAGE_BROWSER=0;
public static final int TYPE_SET_HOMEDIR=3;
private boolean stopped=false;
private int browserType;
private Font small_Font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
private Font medium_Font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
private int font_medium_Height;
private int font_small_Height;
private int font_medium_Width;
private int font_small_Width;
private int text_button_Width;
// Every element in the dirList Vector that is greater than the visibleTopIndex
// and below the visibleBottom index should be painted
private int visibleTopIndex;
private int visibleBottomIndex;
private int visibleBottomIndex_START;
// Thread that is doing repaint on this canvas
private Thread thread= null;
// Are we showing an image
private boolean showingImage = false;
// The image we are showing if showingImage is true
private Image image = null;
// Is the current marked item in the Canvas a image
private boolean dirIndexIsImage=false;
// The fileAction object which we use to retrieve the directory contents
protected FileAction fileAction= null;
// The Displayable that is going to be shown when we are done with filebrowsing
private Displayable next;
public FileBrowsingCanvas(Displayable next, int browserType) {
setFullScreenMode(true);
this.next=next;
this.browserType=browserType;
fileAction = new FileAction(this);
WIDTH = getWidth();
HEIGHT = getHeight();
// setting all the graphical properties that we are going to use
setProperties();
// Load the images we are using
loadProgramImages();
// Start the paint thread
thread = new Thread(this);
thread.start();
// Get the content of the root ("/") directory
new Thread(new Runnable() {
public void run() {
fileAction.getDirContent(currentDir);
}
}).start();
}
public void paint(Graphics g){
g.setColor(0xFFFFFF);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(0x00);
g.setFont(medium_Font);
if(browserType==TYPE_IMAGE_BROWSER)
g.drawString("Image browser",0,0,0);
else
g.drawString(currentDir, 0,0 , 0);
g.setFont(small_Font);
// =============
int lastIndex = visibleBottomIndex < dirSize? visibleBottomIndex:dirSize;
int j = 0;
if(showingImage)
{
g.setColor(83,148,166);
g.fillRect(0,0,WIDTH,HEIGHT);
g.setColor(0x00);
g.setClip(10,10, WIDTH-20, HEIGHT-20);
g.drawImage(image, WIDTH/2,HEIGHT/2, Graphics.HCENTER | Graphics.VCENTER);
g.setClip(0,0, WIDTH, HEIGHT);
g.drawChars(systemText[TXT_CLOSE], 0, systemText[TXT_CLOSE].length, WIDTH, HEIGHT, Graphics.BOTTOM|Graphics.HCENTER);
}
else
{
for(int i = visibleTopIndex; i<lastIndex;++i)
{
//draw the current item in the directory list as marked
if(dirIndex==i){
g.setColor(83,148,166);
g.drawRoundRect(0, font_medium_Height+(j+1)*font_small_Height,WIDTH, font_small_Height, 4, 2);
}
if(i<lastDirIndex)
{
//if the item is a directory draw the directory image before the name
g.drawImage(folderImage,0,font_medium_Height+(j+1)*font_small_Height, 0);
}
else // if the item is a file draw the file image before the name
g.drawImage(fileImage,0,font_medium_Height+(j+1)*font_small_Height, 0);
// Draw the file or the Directory name
g.drawString((String)dirList.elementAt(i), 16,font_medium_Height+(j+1)*font_small_Height, 0);
g.setColor(0x00);
j++;
}
g.setColor(83,148,166);
if(this.visibleBottomIndex<dirSize) // If There are more items below, draw an arrow downward to indicate this
g.drawImage(downArrowImage,WIDTH-30,HEIGHT-font_medium_Height*2-10, 0);
if(this.visibleTopIndex>0) // If There are more items above, draw an arrow above to indicate this
g.drawImage(upArrowImage,WIDTH-30,0+font_medium_Height, 0);
g.drawLine(0, HEIGHT-font_medium_Height*2, WIDTH, HEIGHT-font_medium_Height*2);
// draw back button
g.drawChars(systemText[TXT_BACK], 0, systemText[TXT_BACK].length, 0, HEIGHT, Graphics.LEFT| Graphics.BOTTOM);
// If the browser type is for setting home directory, then draw a select button and some other stuff
if(browserType==TYPE_SET_HOMEDIR)
{
g.drawChars(systemText[TXT_SELECT], 0, systemText[TXT_SELECT].length, WIDTH, HEIGHT-font_medium_Height, Graphics.RIGHT | Graphics.BOTTOM);
g.drawString( "Current Save Dir", text_button_Width,HEIGHT-font_medium_Height*2, 0);
g.drawString( PropertyClass.HOME_DIR, text_button_Width,HEIGHT-font_medium_Height, 0);
}
// If the marked item is a directory draw the view button
if(dirIndex<lastDirIndex)
{
g.drawChars(systemText[TXT_VIEW], 0, systemText[TXT_VIEW].length, 0, HEIGHT-font_medium_Height, Graphics.LEFT | Graphics.BOTTOM);
}
else{ // else if the item is pointing to a image file draw "view image" button
if(dirIndexIsImage)
{
g.drawChars(systemText[TXT_VIEW_IMAGE], 0, systemText[TXT_VIEW_IMAGE].length, 0, HEIGHT-font_medium_Height, Graphics.LEFT | Graphics.BOTTOM);
}
}
}
g.setColor(0,0,0);
}
public void keyPressed(int key){
// If we are showing an image we remove it and don't register the key input
if(showingImage)
{
showingImage=false;
return;
}
switch(key){
case -1: // UP
if(dirIndex==0)
return;
if(visibleTopIndex==0){
dirIndex--;
dirIndexIsImage=isImage( ((String)dirList.elementAt(dirIndex)) );
}
else {
dirIndex--;
visibleTopIndex--;
visibleBottomIndex--;
dirIndexIsImage=isImage( ((String)dirList.elementAt(dirIndex)) );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -