📄 uiutils.java
字号:
/*
* Funambol is a mobile platform developed by Funambol, Inc.
* Copyright (C) 2003 - 2007 Funambol, Inc.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License version 3 as published by
* the Free Software Foundation with the addition of the following permission
* added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
* WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE
* WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program; if not, see http://www.gnu.org/licenses or write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA.
*
* You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite
* 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License
* version 3, these Appropriate Legal Notices must retain the display of the
* "Powered by Funambol" logo. If the display of the logo is not reasonably
* feasible for technical reasons, the Appropriate Legal Notices must display
* the words "Powered by Funambol".
*
*/
/*
* Utils.java
*/
package com.funambol.mailclient.ui.utils;
import com.funambol.mail.Address;
import com.funambol.mail.MailException;
import com.funambol.mail.Message;
import com.funambol.mail.Folder;
import com.funambol.mailclient.mm.MessageManager;
import com.funambol.mailclient.ui.controller.UIController;
import com.funambol.util.ChunkedString;
import com.funambol.util.Log;
import com.funambol.util.MailDateFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class UiUtils {
private static String CONTINUE_STRING = "..";
//private constructor to avoid instantiation
private UiUtils() {
}
/**
* @return the best string to be drawn in the given region, i.e. the complete
* string if it fits, or partial string with ".." if it does not fit
*
* @param text the text to be drawn
* @g the graphics object
* @w the width of the region
*
*/
//Should we put this somewhere else??
public static String cutString(String text, int w, Font font) {
if(font.stringWidth(text)<=w) {
return text;
} else {
int width=0;
for (int i=1;i<text.length();i++) {
//width = font.substringWidth( text, 0, text.length()-i)+font.stringWidth(CONTINUE_STRING);
width=font.stringWidth(text.substring(0,text.length()-i).trim())+font.stringWidth(CONTINUE_STRING);
if (width<w) {
return text.substring(0,text.length()-i).trim()+ CONTINUE_STRING;
}
}
}
//we should never arrive here!
return "";
}
/**
* @return true if message is in inbox, false if in another folder or if no
* folder is set as parent
* @param message the message
*/
public static boolean isIncoming(Message message) {
Folder folder = MessageManager.getInstance().getFolder(MessageManager.INBOX);
Folder parent = message.getParent();
if (parent == null ) {
return false;
}
return (folder.getName().equals(parent.getName()));
}
/**
* @return true if message is in outbox or draft
* @param message the message
*/
public static boolean isOutgoing(Message message) {
Folder outboxFolder = MessageManager.getInstance().getFolder(MessageManager.OUTBOX);
Folder draftsFolder = MessageManager.getInstance().getFolder(MessageManager.DRAFTS);
return (outboxFolder.getName().equals(message.getParent().getName()) ||
draftsFolder.getName().equals(message.getParent().getName() ));
}
/**
* @return true if message is in sent
* @param message the message
*/
public static boolean isSent(Message message) {
Folder folder = MessageManager.getInstance().getFolder(MessageManager.SENT);
return (folder.getName().equals(message.getParent().getName() )) ;
}
/**
*
* @param inDate the input date
* @return the given date as a formatted string, if the date is in the
* same day we are now it return the time, otherwise the day and month
*/
public static String formatDate(Date inDate) {
String date;
Calendar now=Calendar.getInstance();
Calendar sent=Calendar.getInstance();
sent.setTime(inDate);
if (now.get(Calendar.DAY_OF_MONTH)==sent.get(Calendar.DAY_OF_MONTH)
&& now.get(Calendar.MONTH)==sent.get(Calendar.MONTH) &&
now.get(Calendar.YEAR)==sent.get(Calendar.YEAR) )
date= MailDateFormatter.getFormattedStringFromDate(inDate,
MailDateFormatter.FORMAT_HOURS_MINUTES, ":");
else
date= MailDateFormatter.getFormattedStringFromDate(inDate,
MailDateFormatter.FORMAT_MONTH_DAY, "/");
return date;
}
/**
* swaps two elements in the given array
*
* @param vector the vector containing the elements
* @param element1 index of the first element
* @param element2 index of the second element
*
*/
public static void swapElements(Vector vector, int element1, int element2) {
Object tmp = vector.elementAt(element1);
vector.setElementAt(vector.elementAt(element2), element1);
vector.setElementAt(tmp, element2);
}
/**
* get the addresses from the given message
*
* @return a vector of addresses containint the to, cc and bcc recipients
* retrieved from the given message
*/
public static Vector getMessageAddresses(Message message) {
if (message == null) {
Log.info("[getMessageAddresses] message is null, returning empty vector");
return new Vector(0);
}
Address[] toAddressList ;
Address[] ccAddressList ;
Address[] bccAddressList ;
int toSize, ccSize, bccSize;
try {
toAddressList=message.getTo();
toSize = toAddressList == null ? 0 : toAddressList.length;
ccAddressList=message.getCc();
ccSize = ccAddressList == null ? 0 : ccAddressList.length;
bccAddressList=message.getBcc();
bccSize = bccAddressList == null ? 0 : bccAddressList.length;
} catch (MailException ex) {
ex.printStackTrace();
Log.error("getMessageAddresses: "+ex.toString());
return new Vector(0);
}
int size = toSize + ccSize + bccSize;
Vector messageAddresses=new Vector(size);
for (int i=0; i < toSize; i++) {
messageAddresses.addElement(toAddressList[i]);
}
for (int i=0; i<ccSize; i++) {
messageAddresses.addElement(ccAddressList[i]);
}
for (int i=0; i<bccSize; i++) {
messageAddresses.addElement(bccAddressList[i]);
}
return messageAddresses;
}
/**
* this method creates a vector of strings that can fit
* on a single line of screenWidth pixel with given font.
*/
public static String[] getStringArray(String text, int screenWidth, Font font) {
// Log.debug(this, "GetStingArray() " + text.substring(0,10));
Vector lines = new Vector();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -