📄 browser.java
字号:
// 尝试从缓冲池中加载图片
CacheObject obj = cache.getCookiesObject(s);
// 如果缓冲池中没有图片,则通过HTTP请求下载
if (obj == null) {
buffer = HttpConnector.sendHttpRequest(new HttpRequest(s,"GET",null,null),
null,this);
if (s.indexOf('?') == -1) {
// 将图片内容保存缓冲池
cache.setCache(s,HttpConnector.lastContentType, buffer);
}
type = HttpConnector.lastContentType;
} else {
// 否则直接使用缓冲池中的图片
buffer = obj.buffer;
type = obj.contentType;
}
if(type!=null && buffer!=null){
// png
if(type.toLowerCase().indexOf("image/png")!=-1){
image = Image.createImage(buffer,0,buffer.length);
}
// gif
else if(type.toLowerCase().indexOf("image/gif")!=-1){
GifFrame GF = GifFrame.CreateGifImage(buffer);
if(GF!=null || GF.size()>0){
image = GF.getImage();
}
}
// midp1.0不支持jpeg格式的图片
//#if (!MIDP1 || !SUPPORT_JPEG)
else if(type.toLowerCase().indexOf("image/jpeg")!=-1){
image = Image.createImage(buffer,0,buffer.length);
}
//#endif
}
// 如果图片加载不成功,则返回"无法显示"的图片
if(image==null){
return noneImage;
}else{
return image;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 加载CSS层叠样式列表
* @param s
* @return
*/
Hashtable loadCss(String s){
try{
byte[] buffer;
// 首先尝试从cache中获得,如果没有,则从服务器加载
if((buffer = cache.getCache(s)) == null){
buffer = HttpConnector.sendHttpRequest(new HttpRequest(s,"GET",null,null),
null, this);
// 加载完毕应该先保存在cache中,方便下一次刷新该页面时不用从服务器加载
cache.setCache(s,HttpConnector.lastContentType, buffer);
}
ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
InputStreamReader isr = new InputStreamReader(bais);
// css样式被保存在hashtable中
Hashtable ht = new Hashtable();
String tmpName = "";
Vector tmpVector = null;
String str = "";
// body {
// margin: 0px;
// font-size: 12px;
// background-color: #F7FFDE;
// }
// .idxgamebar {
// background-color: #4A9C00;
// height: 18px;
// margin-top: 5px;
// background-image: url(../img/idx_titlebk.gif);
// }
//
// .topbar {
// background-color: #006600;
// color: #FFFFFF;
// padding: 3px;
// overflow: hidden;
// }.menudiv {
// background-color: #E6FFA5;
// padding-top: 5px;
// padding-bottom: 5px;
// padding-right: 1px;
// padding-left: 1px;
// height: 25px;
// }
// .menubut {
// margin-right: 2px;
// margin-left: 2px;
// float: left;
// }
// .sizebox {
// align:center;
// width: 175px;
// margin: 0px;
// padding: 0px;
// overflow: hidden;
// text-align: center;
// }
// .gameimg {
// margin-top: 5px;
// padding-left: 5px;
// }
// .busbar {
// text-align: center;
// }
//
// .copy {
// width:175px;
// padding-top: 10px;
// padding-left: 10px;
// padding-right: 10px;
// text-align: center;
// }
//
// .gametitle {
// background-color: #FCD621;
// background-image: url(../img/bk_game.gif);
// margin-top: 5px;
// height: 10px;
// }
// a {
// color: blue;
// text-decoration: none;
// }
//
// .downlink {
// color: #FF0000;
// text-decoration: none;
// }
//
// .floatleft {
// margin: 0px;
// padding: 0px;
// overflow: hidden;
// text-align: left;
// }
//
// .main {
// }
//
// .nobreak{
// padding-top: 10px;
// }
//
//
// .timebar {
// text-align:center;
// width:175px;
// padding-top: 0px;
// padding-left: 10px;
// }
//
// input {
// font-size: 12px;
// }
// 以上为CSS文件内容可能的格式
do{
if(str.length()==0){
if((str = Tools.ReadLine(isr))==null) break;
str = Tools.RepString(str," ","").trim();
}
if(str.length()>0){
// 一段CSS定义结束
if(str.charAt(0)=='}'){
if(tmpVector!=null){
ht.put(tmpName,tmpVector);
tmpName="";
tmpVector = null;
}
str = str.substring(1);
continue;
}else
// 一段CSS定义开始
if(str.charAt(str.length()-1)=='{'){
str = str.substring(0,str.length()-1);
if(str.length()>0){
if(str.charAt(0)=='.'){
str = str.substring(1);
}
//#if DEBUG
//# System.out.println("["+str+"]");
//#endif
tmpName = str;
tmpVector = new Vector();
}
str = "";
}else{
tmpVector.addElement(str);
str = "";
}
}
}while(true);
return ht;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
/**
* 当browser作为一个插件时,可供外部调用的方法,向上滚动浏览器
*/
public void scrollUp(){
wapRender.scrollUp();
}
/**
* 当browser作为一个插件时,可供外部调用的方法,向下滚动浏览器
*/
public void scrollDown(){
wapRender.scrollDown();
}
Displayable browserDisplayable;
/**
* 当browser作为一个插件时,可供外部调用的方法,处理选中的元素
*/
public void dealHotSpot(){
// 页面解释中不允许请求其它新页面(只能上下查看页面)
if(loadStep == LOAD_STEP_PARSER) return;
HotSpot hotspot = wapRender.getHotSpot();
if(hotspot == null) return;
switch(hotspot.type){
case Element.NORMAL_TEXT:
case Element.NORMAL_IMAGE:
{
// 直接发送一个请求
String url = hotspot.url;
if(url!=null){
// 检查是否是一些do命令
if(url.equals("prev")){
back();
}else{
request(getUrl(hotspot.url));
}
}
}
break;
case Element.TEXT_INPUT:
case Element.PASSWORD_INPUT:
{
formElement = (FormElement)hotspot.obj;
String name = formElement.getName();
String value = formElement.getValue();
if(value==null) value = "";
int maxLength = formElement.getMaxLength();
if(hotspot.type == Element.PASSWORD_INPUT){
inputTextBox = new TextBox(name,value,maxLength, TextField.PASSWORD);
}else{
inputTextBox = new TextBox(name,value,maxLength,TextField.ANY);
}
inputTextBox.addCommand(inputOk);
inputTextBox.addCommand(inputBack);
inputTextBox.setCommandListener(this);
browserDisplayable = display.getCurrent();
display.setCurrent(inputTextBox);
}
break;
case Element.SELECT_EXCLUSIVE:
case Element.SELECT_MULTIPLE:
{
Select select = (Select)hotspot.obj;
Vector labels = select.labels;
selectList = new List(select.getName(),
(hotspot.type==Element.SELECT_EXCLUSIVE?List.EXCLUSIVE:List.MULTIPLE));
for(int i=0;i<labels.size();i++){
selectList.append((String)labels.elementAt(i), null);
if(hotspot.type == Element.SELECT_MULTIPLE){
selectList.setSelectedIndex(i,select.getSelectedState(i));
}
}
selectList.addCommand(inputOk);
selectList.addCommand(inputBack);
selectList.setCommandListener(this);
browserDisplayable = display.getCurrent();
display.setCurrent(selectList);
formElement = select;
}
break;
case Element.BUTTON:
{
Button button = (Button)hotspot.obj;
// 获得一个表单提交请求
HttpRequest httpRequest = button.getHttpRequest();
// 提交表单
request(httpRequest);
}
break;
case Element.ANCHOR:
{
Anchor anchor = (Anchor)hotspot.obj;
if(anchor!=null){
String parm = anchor.getAnchorString();
String method = anchor.getMethod();
String url = anchor.getHref();
if(url==null || url.equals("")) url = curRequest.getUrl();
url = getUrl(url);
if(method.equals(HttpConnection.GET)){
// get方法
request(url + "?" + parm);
}else{
try{
// post方法
HttpRequest req = new HttpRequest(url,HttpConnection.POST,parm.getBytes(encoding),null);
request(req);
}catch(Exception e){
e.printStackTrace();
}
}
}
}
break;
}
}
private FormElement formElement;
public void commandAction(Command c,Displayable d){
if(c == inputOk){
if(formElement instanceof Input){
formElement.setValue(inputTextBox.getString());
inputTextBox = null;
}else if(formElement instanceof Select){
Select select = (Select)formElement;
if(select.type == Element.SELECT_EXCLUSIVE){
formElement.setValue(selectList.getSelectedIndex()+"");
}else{
int size = selectList.size();
boolean b[] = new boolean[size];
selectList.getSelectedFlags(b);
for(int i=0;i<size;i++){
if(b[i]!=select.getSelectedState(i)){
select.changeSelected(i);
}
}
select.showSelected();
}
selectList = null;
}
display.setCurrent(browserDisplayable);
formElement = null;
browserDisplayable = null;
}else if(c == inputBack){
display.setCurrent(browserDisplayable);
formElement = null;
browserDisplayable = null;
}
}
public int strWidth(String s) {
return font.stringWidth(s);
}
public void drawBoldString(Graphics g, String s, int x, int y,int anchor) {
g.drawString(s, x, y, anchor);
g.drawString(s, x + 1, y, anchor);
}
public void paint(Graphics g){
g.setFont(font);
g.translate(x, y);
wapRender.render(g);
g.setClip(0,0,width,height);
if(loadStep == LOAD_STEP_CONTENT){
paintLoadContentScreen(g);
}else if(loadStep == LOAD_STEP_PARSER){
paintParserScreen(g);
}
g.translate(-x,-y);
}
private void paintLoadContentScreen(Graphics g){
int w = width;
int h = fontHeight+2;
int x = 0;
int y = height - h;
g.setColor(0x808080);
g.fillRect(x, y, w, h);
g.setColor(0x000000);
drawBoldString(g, " 连接中...", x+2, y+1, 20);
}
private int maxElementNum = 0;
/**
* 加载图片时屏幕下边显示的进度条
* @param g
*/
private void paintParserScreen(Graphics g){
int w = width;
int h = fontHeight+2;
int x = 0;
int y = height - h;
int curLoaded = 0;
int value = 0;
int iPercent = 0;
if(maxElementNum!=0){
curLoaded = wapRender.getCurLoadedTag();
value = curLoaded * 100 / maxElementNum;
iPercent = (curLoaded * (w - 2)) / maxElementNum;
}
g.setColor(0x808080);
g.fillRect(x, y, w, h);
g.setColor(0x0000ff);
g.fillRect(x + 1, y + 1, iPercent - 2, h - 1);
g.setColor(0xffffff);
g.drawString("加载中..." + value+"%",
width>>1, y + 1, Graphics.TOP|Graphics.HCENTER);
}
/**
* 清空browser可能占用的内存<br>
* 当从browser切换到游戏界面前,务必做此操作
*/
public void destroy(){
try{
historyUrl.removeAllElements();
wapRender.destory();
cookies.clear();
Tools.destroy();
HttpConnector.destroy();
historyUrl = null;
noneImage = null;
curRequest = null;
wapRender = null;
cookies = null;
browserDisplayable = null;
}catch(Exception e){
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -