?? searchmessageaction.java
字號:
package net.sf.pim.mail.reader;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Store;
import jp.gr.java_conf.roadster.net.pop.MessageIndex;
import net.sf.pim.mail.MailPlugin;
import net.sf.pim.mail.MailUtil;
import net.sf.util.StringUtil;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
/**
* 查找郵件
* @author levin
*/
public class SearchMessageAction implements IWorkbenchWindowActionDelegate {
private IWorkbenchWindow window;
public void dispose() {
}
public void init(IWorkbenchWindow window) {
this.window=window;
}
public void run(IAction action) {
final InputDialog input=new InputDialog(window.getShell(),"搜索郵件","提示:還可以用保留字:關鍵詞方式,保留字有head|from|to|cc|subject|body\n請輸入關鍵詞","",null);
if(input.open() == Dialog.OK && input.getValue() != null){
final String key=input.getValue();
BusyIndicator.showWhile(window.getShell().getDisplay(), new Runnable() {
public void run(){
try {
List<Message> hit=new ArrayList<Message>();
SearchFolder searchFolder=new SearchFolder(null,hit);
MessageIndex.setCacheContent(false);
for(Store store:MailPlugin.getDefault().getStore().getStores()){
for(Folder folder:store.getDefaultFolder().list()){
if(!folder.isOpen())
folder.open(Folder.READ_ONLY);
Message[] messages=folder.getMessages();
for(Message msg:messages){
if(match(msg,key))
hit.add(msg);
}
}
}
MessageIndex.setCacheContent(true);
FolderViewer fv=(FolderViewer) window. getActivePage().findView(FolderViewer.ID);
fv.showFolder(searchFolder);
} catch (MessagingException e) {
e.printStackTrace();
}
}
});
}
}
public void selectionChanged(IAction action, ISelection selection) {
}
//郵件中是否包含關鍵詞
private static boolean match(Message message,String key) throws MessagingException{
//對關鍵詞進行解析
String prefix=null;
if(key.indexOf(':') != -1){
prefix=key.substring(0, key.indexOf(':'));
if(";head;from;to;cc;subject;body;".indexOf(";"+prefix+";")!= -1)
key=key.substring(prefix.length()+1);
else
prefix=null;
}
if(prefix != null && ! StringUtil.isNull(key)){
if(prefix.equals("head"))
return matchFrom(message, key) || matchTo(message, key) || matchCc(message, key) || matchSubject(message, key);
String methodName="match"+prefix.substring(0,1).toUpperCase()+prefix.substring(1);
Method method = SearchMessageAction.class.getDeclaredMethod(methodName, Message.class,String.class);
method.setAccessible(true);
return (Boolean) method.invoke(null, message,key);
} catch (Exception e) {
throw new MessagingException("search error",e);
}
}
return matchFrom(message, key) || matchTo(message, key) || matchCc(message, key) || matchSubject(message, key) || matchBody(message, key);
}
//一組搜索郵件的方法
private static boolean matchBody(Message message, String key) {
return MailUtil.dumpBodyText(message).indexOf(key) != -1;
}
private static boolean matchSubject(Message message, String key)
throws MessagingException {
return message.getSubject() != null && message.getSubject().indexOf(key) != -1;
}
private static boolean matchCc(Message message, String key)
throws MessagingException {
return MailUtil.getAddress(message.getRecipients(Message.RecipientType.CC)).indexOf(key) != -1;
}
private static boolean matchTo(Message message, String key)
throws MessagingException {
return MailUtil.getAddress(message.getRecipients(Message.RecipientType.TO)).indexOf(key) != -1;
}
private static boolean matchFrom(Message message, String key)
throws MessagingException {
return MailUtil.getAddress(message.getFrom()).indexOf(key) != -1;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -