⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 funcanvasviewmessage.java

📁 The Funambol J2ME Mail Client aims to be a light, easy to use, free email client for J2ME devices.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        /**
         * message fields
         */
        private String from="";
        private String subject="";
        private String to="";
        private String date="";
        private String cc="";
        private String bcc="";
        
        /**
         * String constants
         */
        private final String FROM=Localization.getMessages().MVHI_FROM_LABEL;
        private final String SUBJECT=Localization.getMessages().MVHI_SUBJ_LABEL;
        private final String TO=Localization.getMessages().TO_LABEL;
        private final String DATE=Localization.getMessages().MVHI_ON_LABEL;
        private final String CC=Localization.getMessages().CC_LABEL;
        private final String BCC=Localization.getMessages().BCC_LABEL;
        
        
        
        /**
         * vector to store lines
         */
        private String [] fromStrings;
        private String [] subjectStrings;
        private String  toStrings;
        private String [] dateStrings;
        private String  ccStrings;
        private String  bccStrings;
        
        private final int BORDER=2;
        
        private Font fromFont= theme.getSmallFont();
        private Font toFont= theme.getSmallFont();
        private Font subjectFont = theme.getSmallFont();
        private Font hiddenFieldsFont = theme.getSmallFont();
        private Font visibleFieldsFont = theme.getSmallFont();
        private int subjectColor= theme.getHighlightedForegroundColor();
        private int hiddenFieldsColor=theme.getHighlightedForegroundColor();
        private int visibleFieldsColor=theme.getHighlightedForegroundColor();
        
        public HeaderItem() {
            //filling fields..
            setMessage(message);
            
        }
        
        private void setFonts() {
          /*  if (UiUtils.isIncoming(message) ) {
                fromFont = theme.getDefaultFont();
                toFont = theme.getSmallFont();
            } else {
                fromFont = theme.getSmallFont();
                toFont = theme.getDefaultFont();
            }
           */
            subjectFont = theme.getSmallBoldFont();
            subjectColor = theme.getHighlightedForegroundColor();
        }
        
        private void setMessage(Message msg) {
            try {
                if (message.getFrom()!=null) {
                    from=message.getFrom().getVisibleName();
                } else {
                    from ="";
                }
            } catch (MailException ex) {
                Log.error(this,"error getting from field");
                ex.printStackTrace();
            }
            
            try {
                for (int i=0; i<message.getTo().length; i++) {
                    to+= (i==0) ? "" : ", ";
                    to+=message.getTo()[i].getVisibleName().trim();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                Log.error(this, "error fetching to addresses");
            }
            
            try {
                for (int i=0; i<message.getCc().length; i++) {
                    cc+= (i==0) ? "" : ", ";
                    cc+=message.getCc()[i].getVisibleName().trim();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                Log.error(this, "error fetching cc addresses");
            }
            
            try {
                for (int i=0; i<message.getBcc().length; i++) {
                    bcc+= (i==0) ? "" : ", ";
                    bcc+=message.getBcc()[i].getVisibleName().trim();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                Log.error(this, "error fetching Bcc addresses");
            }
            
            Date dateObj = message.getSentDate();
            // Calendar calendar = Calendar.getInstance();
            // calendar.setTime(dateObj);
            // calendar.setTimeZone(TimeZone.getDefault());
            
            // this is only for visualization purposes
            //TODO: check if the variables in formatLocalTime are really used
            
            //date = MailDateFormatter.formatLocalTime(calendar.getTime());
            date = MailDateFormatter.formatLocalTime(dateObj);
            subject=message.getSubject();
            
            setFonts();
            
            
            
        }
        
        
        /**
         *a traverse-like method that updates the state of this item
         */
        public void traverse(int dir) {
            
            // Log.debug("traverse header Item, insideheader = " + insideHeader);
            if (insideHeader) {
                if ( (dir==Canvas.UP) ) {
                    //  Log.debug("traverse up");
                    if(!expanded) {
                        expand();
                    }
                    //else expand();
                } else if ( (dir == Canvas.DOWN) ) {
                    //   Log.debug("traverse down");
                    if (expanded) {
                        shrink();
                    } else {
                        exitHeader(dir);
                    }
                }
            }
        }
        private void shrink() {
            //  Log.debug("shrink()");
            expanded=false;
        }
        
        private void expand() {
            //   Log.debug("expand()");
            expanded=true;
        }
        /**
         * @return the estimated height for this item, used to draw the
         * background etc.
         */
        protected int calculateHeight(int width) {
            
            if (!showHeader) return (0);
            
            //TODO: check why sometimes passed width is 0
            if (width==-1 || width==0) {
                initializeHeaderStringVectors(width-4*BORDER);
            } else
                initializeHeaderStringVectors(width-4*BORDER);
            
            int  h=subjectFont.getHeight()* subjectStrings.length;
            if (UiUtils.isIncoming(message)) {
                h+=visibleFieldsFont.getHeight()* fromStrings.length;
            } else {
                if (toStrings!= null)
                    h+=visibleFieldsFont.getHeight();
            }
            
            if (expanded) {
                if (UiUtils.isIncoming(message)) {
                    h+=hiddenFieldsFont.getHeight();
                }else {
                    h+=hiddenFieldsFont.getHeight()* fromStrings.length;
                }
                
                h+=hiddenFieldsFont.getHeight()*dateStrings.length;
                if (ccStrings!= null) {
                    h+=hiddenFieldsFont.getHeight();
                }
                if (bccStrings!=null) {
                    h+=hiddenFieldsFont.getHeight();
                }
            }
            h+=4*BORDER;
            return h;
        }
        
        private void initializeHeaderStringVectors(int w){
            if (headerLastWidth!=w) {
                int width=w-4*BORDER;
                if (from.equals("")) {
                    fromStrings=new String[0];
                } else {
                    fromStrings=getStringArray(FROM + " " + from, width, fromFont);
                }
                subjectStrings=getStringArray(SUBJECT+" " +subject,width, subjectFont);
                if (to.length()!=0) {
                    toStrings = UiUtils.cutString(TO+" "+to, width,toFont);//getStringArray(TO+" "+to, width,toFont);
                } /*else {
                    toStrings= new String();
                }*/
                
                dateStrings=getStringArray(DATE+" " +date,width,hiddenFieldsFont);
                if (cc.length()!=0) {
                    ccStrings = UiUtils.cutString(CC+" "+cc, width,hiddenFieldsFont);//getStringArray(CC+" " +cc, width,hiddenFieldsFont);
                } /*else {
                    ccStrings=new String();
                }*/
                if (bcc.length()!=0) {
                    bccStrings = UiUtils.cutString(BCC+" "+bcc, width,hiddenFieldsFont);//getStringArray(CC+" " +cc, width,hiddenFieldsFont);
                } /*else {
                    bccStrings=new String();
                }*/
                headerLastWidth=w;
            }
            
        }
        
        
        /**
         * paint function. it draws the strings ecc.
         *h it's the current graphics height to print at,
         *it's modified by this function to return the next y where content
         *can be drawn.
         *@return the h + the height drawn by the item
         */
        protected int draw(Graphics graphics, int w, int h) {
            //graphics.getFont().
            //   int oldCol=graphics.getColor();
            
            //   Log.debug("drawing headerItem");
            
            
            if (!showHeader) {
                return h;
            }
            int inith = h;
            initializeHeaderStringVectors(w-2*BORDER);
            int height = calculateHeight(w);
            
            
            //we need to translate the graphics, because the drawer.drawbackground
            //mathod starts at 0,0
           /* graphics.translate(MARGIN, h);
            Log.debug("drawing background");
            drawer.drawBackground(graphics, w, h, true);
            graphics.translate(-MARGIN, -h);
            */
            
            
            graphics.setColor(theme.getHighlightedBackgroundColor());
            //TODO: use drawer and optimize!
            graphics.fillRoundRect(BORDER,h,w-2*BORDER,height,10,10);
            
            graphics.setColor(theme.getHighlightedBorderColor());
            graphics.drawRoundRect(BORDER,h,w-2*BORDER,height,10,10);
            
            h+=2*BORDER;
            
            //we set the font to draw the "from: address" line and we draw it
            //graphics.getDisplayColor()
            
            //if expanded we draw to, cc, bbc & date fields
            if (expanded){
                if (UiUtils.isIncoming(message)) {
                    h = paintToField(graphics, h);
                } else {
                    h = paintFromField(graphics, h);
                }
                h = paintCcField(graphics, h);
                h = paintBccField(graphics, h);
                h = paintDateField(graphics, h);
            }
            
            if (UiUtils.isIncoming(message)) {
                h=paintFromField(graphics, h);
            } else {
                h= paintToField(graphics,h);
            }
            h=paintSubjectField(graphics, h);
            
            
            graphics.setColor(theme.getHighlightedForegroundColor());
            if(!expanded) {
                //a small arrow up to let the user know we have something hidden that
                // can be discovered pressing the up key
                graphics.fillTriangle(w-9-BORDER,inith+8+BORDER,w-6-BORDER,inith+1+BORDER,w-2-BORDER,inith+8+BORDER);
            } else {
                //small arrow down
                graphics.fillTriangle(w-9-BORDER,inith+1+BORDER,w-6-BORDER,inith+8+BORDER,w-2-BORDER,inith+1+BORDER);
            }
            return height+ inith;
        }
        
        private int paintSubjectField(final Graphics graphics, int h) {
            //we set the font to draw the subject line
            graphics.setFont(subjectFont);
            graphics.setColor(subjectColor);
            //we draw the lines
            for (int i =0; i<subjectStrings.length; i++) {
                graphics.drawString(subjectStrings[i],2*BORDER,h,graphics.TOP|graphics.LEFT);
                h+=subjectFont.getHeight();
            }
            return h;
        }
        
        private int paintDateField(final Graphics graphics, int h) {
            graphics.setFont(hiddenFieldsFont);
            graphics.setColor(hiddenFieldsColor);
            for (int i = 0; i< dateStrings.length; i++) {
                graphics.drawString(dateStrings[i],2*BORDER,h,graphics.TOP|graphics.LEFT);
                h+=hiddenFieldsFont.getHeight();
            }
            return h;
        }
        
        private int paintBccField(final Graphics graphics, int h) {
            if (bccStrings == null) {
                return h;
            }
            graphics.setFont(hiddenFieldsFont);
            graphics.setColor(hiddenFieldsColor);
            
            graphics.drawString(bccStrings,2*BORDER,h,graphics.TOP|graphics.LEFT);
            h+=hiddenFieldsFont.getHeight();
            
            return h;
            
        }
        
        private int paintCcField(final Graphics graphics, int h) {
            if (ccStrings == null) {
                return h;
            }
            
            graphics.setFont(hiddenFieldsFont);
            graphics.setColor(hiddenFieldsColor);
            
            graphics.drawString(ccStrings,2*BORDER,h,graphics.TOP|graphics.LEFT);
            h+=hiddenFieldsFont.getHeight();
            
            return h;
        }
        
        private int paintFromField(final Graphics graphics, int h) {
            
            graphics.setColor(theme.getHighlightedForegroundColor());
            graphics.setFont(fromFont);
            for (int i = 0; i<fromStrings.length; i++) {
                //drawer.drawString(fromStrings[i], graphics, 2*BORDER,h,true);
                graphics.drawString( fromStrings[i],2*MARGIN,h,graphics.TOP|graphics.LEFT);
                h+=fromFont.getHeight();
            }
            return h;
        }
        
        private int paintToField(final Graphics graphics, int h) {
            if (toStrings==null) {
                return h;
            }
            graphics.setColor(theme.getHighlightedForegroundColor());
            graphics.setFont(toFont);
            
            graphics.drawString(toStrings,2*MARGIN,h,graphics.TOP|graphics.LEFT);
            h+=toFont.getHeight();
            return h;
            
        }
        
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -