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

📄 messagelist.java

📁 moblie syncml mail javame
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

    }

    /**
     * handle the keypress events, should be overridden
     */
    protected void handleKey(int keyCode) {
    }

    /**
     *this method is called by implementation when the commands need to be
     * initialized (in the constructor), should be overridden. After this element
     * the commands referenced in addCommands, addItemCommands, removeItemcommands
     * must be not null
     */
    protected void initCommands() {
    }

    /**
     *this method is called by implementation when the generic commands need to be
     * added (in the constructor), should be overridden.
     */
    protected void addCommands() {
        super.addCommands();
    }

    /**
     *this method is called by implementation when the item commands need to be
     * added (i.e. while setting a non empty elements array.  should be ovverridden
     */
    void removeItemCommands() {
    }

    /**
     *this method is called by implementation when the item commands need to be
     * removed (i.e. while setting an empty elements array). should be ovverridden
     */
    void addItemCommands() {
    }

    /**
     * paint the background
     */
    void paintBackground(Graphics graphics) {
        drawer.drawCanvasBackground(graphics, getWidth(), getHeight());
    }

    /**
     * delete the message eventually after a popup
     *
     */
    void deleteMessage() {
        Message msg = getSelectedMessage();
        if (msg != null) {

            if (UIController.getConfig().getDeleteConfirmation()) {
                ModalPopup popup =
                        new ModalPopup(Localization.getMessages().POPUP_DELETE_MESSAGE_TITLE,
                        Localization.getMessages().POPUP_DELETE_MESSAGE_TEXT,
                        new DeletePopupAction(msg));
                UIController.showModalPopup(popup, this);
            } else {
                UIController.deleteMessage(msg);
            }
        }
        if (elements.size() < 1) {
            removeItemCommands();
        }
    }

    /**
     * remove the message in the given position from the visualized list.
     * This function will NOT delete the message from the folder
     *
     * @param position the position of the messageinfo to be removed
     */
    public void deleteMessageAt(int position) {

        //#ifdef low_mem
//#         UIController.freeMemory();      //XXX
        //#endif

        elements.removeElementAt(position);

        // Update messageList.
        messageList.removeElementAt(position);
        for (int i = position; i < messageList.size(); i++) {
            ((CanvasMessageItem) messageList.elementAt(i)).setId(i);
        }

        // If the deleted message was the last, set the new last as
        // active.
        if (getActiveElementId() >= elements.size()) {
            setActiveElement(elements.size() - 1);
        }
        
        if (elements.size() < 1) {
            removeItemCommands();
        }

        repaint();
    }

    /**
     * Delete the message with the given msgID from the elements array, if present
     * this method does not delete the message from the rms!
     *
     * @param msgId the messageid of the message to be deleted
     */
    public void deleteMessage(String msgId) {
        int p = search(msgId);
        if (p >= 0) {
            deleteMessageAt(p);
        }
    }

    /**
     * Return the messageList
     */
    public Vector getMessageList() {
        return this.elements;
    }

    /**
     * Return the message at the given position
     * @param position the position of the element to be returned
     */
    public Message getMessageAt(int position) {
        return ((Message) elements.elementAt(position));
    }

//------------------------ CanvasMessageItem ---------------------------------//
    /**
     * this class draws the message item
     */
    class CanvasMessageItem {

        Message message;
        private int id;
        private int lastw;
        private String date;
        /**
         * the subject that fits on a single line
         */
        private String shortSubject;
        /**
         * the address that fits on a single line
         */
        private String shortAddress;
        /**
         * the height of this element when not expanded
         */
        private int unexpanded_h;
        /**
         * the height of this element when expanded
         */
        private int expanded_h;
        /**
         * track if the last time we painted the item the read flag was
         * set or not (to recalculate the short strings if necessary)
         */
        private boolean lastReadFlag;

        CanvasMessageItem(Message msg, int id) {
            this.message = msg;
            this.id = id;


        }

        private boolean isActive() {
            return id == getActiveElementId();
        }

        /**
         * paint the messageitem
         */
        public int paint(Graphics graphics) {

            //we need to calculate the short strings
            int w = getWidth();
            int h = calculateHeight();
            calculateShortStrings();

            //we draw the background, image and date
            drawer.drawBackground(graphics, w, h, isActive());
            graphics.drawImage(Theme.getImageFromFlags(getFlagValue()), MARGIN, MARGIN, Graphics.TOP | Graphics.LEFT);
            drawer.drawString(getDate(), graphics, w - MARGIN, MARGIN, Graphics.TOP | Graphics.RIGHT, isActive());

            //if this is a read message we use normal font, otherwhise we use the bold font
            if (isRead()) {

                drawer.drawString(shortAddress, graphics, Theme.getIMAGE_PART_WIDTH() +
                        2 * MARGIN, MARGIN, isActive());
            } else {
                drawer.drawBoldString(shortAddress, graphics, Theme.getIMAGE_PART_WIDTH() +
                        2 * MARGIN, MARGIN, isActive());
            }
            if (hasAttach()) {
                int attachPos = 0;
                if (isRead()) {
                    attachPos = Theme.getIMAGE_PART_WIDTH() +
                            3 * MARGIN +
                            font.stringWidth(shortAddress);
                } else {
                    attachPos = Theme.getIMAGE_PART_WIDTH() +
                            3 * MARGIN +
                            boldFont.stringWidth(shortAddress);
                }
                graphics.drawImage(Theme.getCLIP_IMG(), attachPos, MARGIN, Graphics.TOP | Graphics.LEFT);

            }
            //if expanded we draw the second line
            if (isExpanded()) {
                drawer.drawString(shortSubject, graphics, MARGIN, getUnexpandedh(), isActive());
            }
            return h;
        }

        /**
         * return the flag value used to identify the image in the image array
         */
        private int getFlagValue() {
            int flags = message.getFlags().getFlags();
            //we mask the flag to be sure we have a return value <32
            flags = (flags & 0x0000001f);
            return flags;
        }

        /**
         * calculate the short version of address and subject and
         * store them in shortAddress and shortSubject
         */
        private void calculateShortStrings() {

            if (shortAddress == null || shortSubject == null || lastReadFlag != isRead()) {

                int w = getWidth();
                int firstlineSpace = w - 4 * MARGIN - Theme.getIMAGE_PART_WIDTH() - font.stringWidth(getDate());
                if (hasAttach()) {
                    firstlineSpace = firstlineSpace - Theme.getCLIP_IMG().getWidth();
                }
                int secondlineSpace = w - MARGIN;
                shortSubject = UiUtils.cutString(message.getSubject(), secondlineSpace, font);

                String address = null;
                if (isIncomingMessage()) {
                    Address from = message.getFrom();
                    if (from != null) {
                        address = from.getVisibleName();
                    }
                } else {
                    Address to[] = message.getTo();
                    Address cc[] = message.getCc();
                    if (to != null && to.length > 0) {
                        Address singleTo = to[0];
                        address = singleTo.getVisibleName();
                    } else if (cc != null && cc.length > 0) {
                        Address singleCc = cc[0];
                        address = singleCc.getVisibleName();
                    }
                }
                if (address == null) {
                    address = Localization.getMessages().EMPTY_RECIPIENTS;
                }
                if (isRead()) {
                    shortAddress = UiUtils.cutString(address, firstlineSpace, font);
                    lastReadFlag = true;
                } else {
                    shortAddress = UiUtils.cutString(address, firstlineSpace, boldFont);
                    lastReadFlag = false;
                }
            //   Log.error("shortstring: " + shortSubject);

            } else {
                //   Log.error("no need to calculate shortstring");
            }


        }

        /**
         * @return true if message is in the inbox
         */
        private boolean isIncomingMessage() {
            return (Store.INBOX.equals(message.getParent().getName()));
        }

        /**
         * @return true if the message has been opened
         */
        private boolean isRead() {
            return message.getFlags().isSet(MessageFlags.OPENED);
        }

        /**
         * @return true if the message has one ore more attachments
         */
        private boolean hasAttach() {
            return message.isMultipart();

        }

        /**
         * return true if this item is expanded, false otherwise
         */
        public boolean isExpanded() {
            boolean ret;
            //#ifdef MessageItemAlwaysExpanded
//#             ret = true;
            //#else
            ret = isActive();
            //#endif
            return ret;
        }

        /**
         * compute height using information from the bean,
         * the drawer and the icon size
         */
        private int calculateHeight() {
            if (isExpanded()) {
                return getExpandedh();
            } else {
                return getUnexpandedh();
            }
        }

        /**
         * @retunr the height of this element not expanded
         */
        private int getUnexpandedh() {
            if (unexpanded_h == 0) {
                unexpanded_h = MARGIN;
                unexpanded_h += Math.max(
                        Math.max(font.getHeight(), boldFont.getHeight()), Theme.getIMAGE_PART_HEIGHT());
            //unselected_h+=MARGIN;
            }
            return unexpanded_h;
        }

        /**
         * @retunr the height of this element expanded
         */
        private int getExpandedh() {
            if (expanded_h == 0) {
                expanded_h = getUnexpandedh();
                expanded_h += font.getHeight();
                expanded_h += MARGIN;
            }
            return expanded_h;
        }

        //  new Vector()
        /**
         * return the date as a formatted string
         */
        private String getDate() {
            if (date == null) {
                date = UiUtils.formatDate(message.getReceivedDate());
            }
            return date;


        }

        /**
         *
         */
        private void setId(int newId) {
            this.id = newId;
        }
    }

    boolean isVisible(int elementId) {
        return elements.elementAt(elementId) != null;
    //return true;
    }

    boolean isActivable(int elementId) {
        return isVisible(elementId);
    }
}

⌨️ 快捷键说明

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