?? messagelist.java
字號:
package fengyun.Fastmail.beans;
import java.util.*;
import java.text.*;
import javax.mail.*;
import fengyun.Fastmail.Maildir.MaildirFolder;
import fengyun.Fastmail.Maildir.MaildirMessage;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
/**
* 獲取消息列表,分頁處理
* @author sanware & fengyun
* @version 1.01
*/
public class MessageList {
private static BeansConstants CONST = BeansConstants.getInstance();
private int MessageCount = 0;
private int PageCount = 0;
private int PageSize = CONST.DefaultPageSize;
private int Page = 1;
private int Start = 0;
private int End = 0;
private int range = -1;
private MaildirMessage[] messages = null;
private String FolderName = null;
private int FolderIndex = 0;
private SimpleDateFormat formatter = new SimpleDateFormat (CONST.dateformat);
/**
* 構造消息列表
*/
public MessageList(HttpServletRequest request) throws MessagingException {
HttpSession httpsession = request.getSession(false);
String folderid = request.getParameter(CONST.folderid);
String strPage = request.getParameter(CONST.page);
if (folderid == null || httpsession == null) {
folderid = String.valueOf(CONST.revbox);
}
try {
FolderIndex = Integer.parseInt(folderid);
httpsession.setAttribute(CONST.folderid,folderid);
}
catch(Exception e) {
throw new MessagingException("Error Folder Index" + e.getMessage());
}
try {
Page = Integer.parseInt(strPage);
}
catch(Exception e) {
Page = 1;
}
Store store = (Store)httpsession.getAttribute(CONST.FastmailStore);
FolderView folderview = (FolderView)httpsession.getAttribute(CONST.FastmailFolderView);
MaildirFolder folder = (MaildirFolder)store.getFolder(folderview.folderlist.getFolderid(FolderIndex));
FolderName = folderview.folderlist.getViewName(FolderIndex);
if (store == null || folderview == null)
throw new MessagingException("FolderView ERROR:It should be inited");
MessageCount = folder.getMessageCount();
PageCount = (MessageCount - 1) / PageSize + 1;
if (Page < 1) Page = 1;
if (Page > PageCount) Page = PageCount;
Start = (Page-1) * PageSize;
End = Math.min(MessageCount,Page * PageSize) - 1;
messages = null;
if (MessageCount == 0) {
Start = 0;
End = 0;
messages = new MaildirMessage[0];
} else {
messages = (MaildirMessage[])folder.getMessages(Start,End);
}
}
/**
* 返回當前頁數
*/
public int getCurrentPage() { return Page; }
/**
* 返回起點
*/
public int getStart() { return Start + 1; }
/**
* 返回終點
*/
public int getEnd() { return End + 1; }
/**
* 返回總頁數
*/
public int getPageCount() { return PageCount; }
/**
* 返回消息總數
*/
public int getMessageCount() { return MessageCount;}
/**
* 是否第一頁
*/
public boolean isFirstPage() {
return (Page == 1);
}
/**
* 是否最后一頁
*/
public boolean isLastPage() {
return (Page == PageCount);
}
/**
* 返回郵件夾顯示名稱
*/
public String getFolderName() {
return FolderName;
}
/**
* 返回郵件夾索引
*/
public int getFolderIndex() {
return FolderIndex;
}
/**
* 獲取的消息數
*/
public int getMessageNumber() throws MessagingException {
if (messages == null) throw new MessagingException("Please Fetch Message First");
return messages.length;
}
/**
* 檢查是否已經獲取消息,消息索引是否正確
*/
public boolean check(int index) {
return (messages != null) && index >= 0 && index < messages.length;
}
/**
* 返回標志位
* @param index 在消息集中的索引
*/
public char getFlag(int index) throws MessagingException {
if (!check(index)) throw new MessagingException("Have not fetched the messages or Error index");
return messages[index].getFlag();
}
public long getSize(int index) throws MessagingException {
if (!check(index)) throw new MessagingException("Have not fetched the messages or Error index");
return messages[index].getSize();
}
/**
* 返回發送日期
* @param index 索引
* @return String 發送日期
*/
public String getSentDate(int index) throws MessagingException {
if (!check(index)) throw new MessagingException("Have not fetched the messages or Error index");
String sentDate = null;
sentDate = formatter.format(messages[index].getSentDate());
if (sentDate == null) throw new MessagingException("ERROR SENDDATE");
return sentDate;
}
/**
* 返回消息來自
* @param index 索引
*/
public String getFrom(int index) throws MessagingException {
if (!check(index)) throw new MessagingException("Have not fetched the messages or Error index");
String from = null;
Address[] address = messages[index].getFrom();
if (address == null) throw new MessagingException("ERROR FROM");
if (address.length > 0) from = address[0].toString();
else from = "(no sender)";
return from;
}
/**
* 返回消息主題
* @param index 索引
*/
public String getSubject(int index) throws MessagingException {
if (!check(index)) throw new MessagingException("Have not fetched the messages or Error index");
String subject = messages[index].getSubject();
if (subject == null || subject.equals("")) subject = "(no subject)";
if (subject.length() > 30) {
subject = subject.substring(0,28) + "..";
}
return subject;
}
/**
* 返回消息ID
* @param index 索引
*/
public String getMessageID(int index) throws MessagingException {
if (!check(index)) throw new MessagingException("Have not fetched the messages or Error index");
return messages[index].getMessageID();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -