📄 contactitem.java
字号:
public Address getAddress() {
if (state==ADDRESS_STATE_NONE)
return null;
else {
try {
return contact.getAddress(state, selectedEmailIndex);
} catch (MailException ex) {
//TODO: better exception handling and localization
Log.error(this, "Error getting Address from contact item");
ex.printStackTrace();
return null;
}
}
}
/**
* @return the selected email as a string
*/
private String getSelectedEmail() {
switch (selectedEmailIndex) {
case Contact.DEFAULT_EMAIL:
return contact.getDefaultEmail();
case Contact.SECONDARY_EMAIL:
return contact.getEmail_2();
case Contact.TERTIARY_EMAIL:
return contact.getEmail_3();
default:
return contact.getDefaultEmail();
}
}
// WARNING: THIS NEED TO BE REFACTORED REMOVING isActive PARAM
/**
* paint the ContactItem on the current graphics starting at 0,0
* so a translate operation is often needed to correctly align the
* graphics object
*<br>
* this method draws the contactItem as an item with width equals to
* graphics.getClipWidth().
*
* @return the height of the painted area, this should be the same as
* calculateheight()
*
* @param graphics the Graphics object to paint on.
* @param isActive the current active / non active state of the element
*
*/
public int paint(Graphics graphics, boolean isActive) {
int w = graphics.getClipWidth();//- H_MARGIN;
int original_h = graphics.getClipHeight();
int h = calculateHeight(isActive);
UIController.getDrawer().drawBackground(graphics,w,h, isActive);
if (isActive) {
graphics.setColor(graphicalTheme.getHighlightedForegroundColor());
} else {
graphics.setColor(graphicalTheme.getForegroundColor());
}
// translate the graphics to position the icon
//clip the rect where we'll draw the icon
//graphics.setClip(0,0,getMaxLabelWidth()/2,font.getHeight()/3 - 2 * V_MARGIN);
graphics.translate( w -15, 1);
//graphics.setClip(w-15,1,12,8);
graphics.setClip(0,0,12,8);
drawSwitchEmailIcon(graphics);
graphics.translate( -w +15, -1);
graphics.setClip( 0, 0, w, original_h);
graphics.drawLine(getMaxLabelWidth()+2*H_MARGIN,0,getMaxLabelWidth()+2*H_MARGIN,h);
graphics.setClip(H_MARGIN,V_MARGIN, getMaxLabelWidth(),h-V_MARGIN);
graphics.translate(H_MARGIN,V_MARGIN);
drawLabel(graphics,w,h, isActive);
graphics.translate(-H_MARGIN, -V_MARGIN);
graphics.setClip(0,0,w,h);
graphics.setClip(getMaxLabelWidth()+3*H_MARGIN,V_MARGIN,w-(getMaxLabelWidth()+3*H_MARGIN), h);
graphics.translate(getMaxLabelWidth()+3*H_MARGIN,V_MARGIN);
drawName(graphics,w,h, isActive);
//back to the original graphic
graphics.translate(-getMaxLabelWidth()-3*H_MARGIN,-V_MARGIN);
graphics.setClip(0,0,w,original_h);
return h;
}
// compute the height of the item
private int calculateHeight(boolean isActive) {
if (height==0 || lastActive != isActive) {
lastActive = isActive;
height= V_MARGIN;
height+=Math.max(font.getHeight(), labelFont.getHeight());
height+=V_MARGIN;
// if ( hasMultipleEmails() ) {
if ( isActive ) {
height += font.getHeight();
}
}
return height;
}
/**
* draws the visiblename
*/
private void drawName(Graphics graphics, int w, int h, boolean isActive) {
if (isActive) {
graphics.setColor(graphicalTheme.getHighlightedForegroundColor());
} else {
graphics.setColor(graphicalTheme.getForegroundColor());
}
graphics.setFont(font);
//TODO: use something to avoid cutstring calls every time this function
//is called
// if (!hasMultipleEmails() ) {
if (!isActive) {
drawer.drawString(UiUtils.cutString(contact.getVisibleName(),graphics.getClipWidth(),font),graphics,0,0,isActive);
}else{
drawer.drawString(UiUtils.cutString(contact.getVisibleName(),graphics.getClipWidth()-15,font),graphics,0,0,isActive);
drawer.drawString(UiUtils.cutString(getSelectedEmail(),graphics.getClipWidth()-H_MARGIN,font),graphics,H_MARGIN, font.getHeight(),isActive);
}
}
// compute max label width for elements
private int getMaxLabelWidth() {
if (maxLabelWidth==0) {
int w1=labelFont.stringWidth(Localization.getMessages().TO_LABEL);
int w2=labelFont.stringWidth(Localization.getMessages().CC_LABEL);
int w3=labelFont.stringWidth(Localization.getMessages().BCC_LABEL);
maxLabelWidth= Math.max( w1, Math.max(w2,w3));
}
return maxLabelWidth;
}
// draw the switch email icons (arrows)
private void drawSwitchEmailIcon(final Graphics graphics) {
if ( hasMultipleEmails() ) {
//graphics.setColor(0xff3333);
graphics.fillTriangle(
graphics.getClipWidth() / 2 + 2 , 0 ,
graphics.getClipWidth(), graphics.getClipHeight() / 2,
graphics.getClipWidth() / 2 + 2 , graphics.getClipHeight()
);
graphics.fillTriangle(
graphics.getClipWidth() / 2 - 2 , 0 ,
0, graphics.getClipHeight() / 2,
graphics.getClipWidth() / 2 - 2 , graphics.getClipHeight()
);
}
}
/**
* draws the label ("to:", "cc:","bcc:")
*/
private void drawLabel(Graphics graphics, int w, int h, boolean isActive) {
if (isActive)
graphics.setColor(graphicalTheme.getHighlightedForegroundColor());
else
graphics.setColor(graphicalTheme.getForegroundColor());
graphics.setFont(labelFont);
graphics.drawString(getAddressTypeLabel(),0,0,Graphics.TOP|Graphics.LEFT);
}
/**
* @return the appropriate label given the address state
*/
private String getAddressTypeLabel() {
switch (state) {
case Address.TO:
return Localization.getMessages().TO_LABEL;
case Address.CC:
return Localization.getMessages().CC_LABEL;
case Address.BCC:
return Localization.getMessages().BCC_LABEL;
default: //and ADDRESS_STATE_NONE
return "";
}
}
/**
* return the current state as one of
* Address.TO, Address.CC, Address.BCC or ContactItem.ADDRESS_STATE_NONE
*/
public int getState() {
return state;
}
/**
* set the state.
*
* @param state the state to be set. this has to be one of
* Address.TO, Address.CC, Address.BCC or ContactItem.ADDRESS_STATE_NONE
* or the function will do nothing
*/
public void setState(int state) {
if(
(state == Address.TO) ||
(state == Address.CC) ||
(state == Address.BCC) ||
(state == ADDRESS_STATE_NONE)
) {
this.state = state;
}
}
/**
* set the current contact
* @param c the new contac
*/
public void setContact(Contact c) {
this.contact = c;
}
/**
* check if the contact item has more than one email
* @return true if this contact has a not null or empty
* secondary or tertiary email
*/
public boolean hasMultipleEmails() {
if ( StringUtil.isNullOrEmpty( contact.getEmail_2() ) &&
StringUtil.isNullOrEmpty( contact.getEmail_3() ) ) {
return false;
} else {
return true;
}
}
/**
* reset the contact item height. since the height is cached, this method
* should be called when the height should be recalculated
* (e.g. if a secondary email has been added)
*/
public void resetHeight() {
this.height = 0;
}
/**
* set the selected email.
* value can be one of Contact.DEFAULT_EMAIL, Contac.SECONDARY_EMAIL,
* Contac.TERTIARY_EMAIL
*/
public void setSelectedEmailIndex(int emailIndex) {
this.selectedEmailIndex = emailIndex;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -