movieratingsui.java

来自「SUN公司发布的SmartTicket 2.0蓝图」· Java 代码 · 共 524 行 · 第 1/2 页

JAVA
524
字号
            addCommand(okCommand);            cancelCommand =                 new Command(uiController.getString(UIMessageCodes.CANCEL),                             Command.CANCEL, 1);            addCommand(cancelCommand);            setCommandListener(this);        }        public void init(MovieRating movieRating, String movieTitle) {            this.movieRating = movieRating;            titleItem.setText(movieTitle);            movieRatingItem.init(movieRating.getStarNumber(), false);            commentItem.setText(movieRating.getComment());            messageField.setString(null);        }         public void commandAction(Command command, Displayable displayable) {            if (command == okCommand) {                uiController.handleEvent(EventIds.EVENT_ID_MOVIE_RATING_MESSAGE_SEND_REQUESTED,                                          new Object[] {                    messageField.getString()                });            } else {    // (command == cancelCommand)                uiController.handleEvent(EventIds.EVENT_ID_MOVIE_RATING_MESSAGE_SEND_CANCELLED,                                          null);            }         }     }    public static class MovieRatingUI extends Form         implements CommandListener {        private UIController uiController;        private MovieRating movieRating;        private StringItem titleItem;        private MovieRatingItem movieRatingItem;        private StringItem authorItem;        private int authorItemNum;        private TextField commentField;        private StringItem commentItem;        private int commentItemNum;        private Command okCommand;        private Command cancelCommand;        public MovieRatingUI(UIController uiController) {            super(uiController.getString(UIMessageCodes.RATE_MOVIE));            this.uiController = uiController;            titleItem =                 new StringItem(uiController.getString(UIMessageCodes.MOVIE_TITLE),                                null);            movieRatingItem = new MovieRatingItem(uiController);            movieRatingItem.setLayout(Item.LAYOUT_NEWLINE_BEFORE                                       | Item.LAYOUT_NEWLINE_AFTER);            authorItem =                 new StringItem(uiController.getString(UIMessageCodes.AUTHOR),                                null);            commentField =                 new TextField(uiController.getString(UIMessageCodes.COMMENT),                               null, 100, TextField.ANY);            commentItem =                 new StringItem(uiController.getString(UIMessageCodes.COMMENT),                                null);            okCommand = new Command(uiController.getString(UIMessageCodes.OK),                                     Command.OK, 1);            cancelCommand =                 new Command(uiController.getString(UIMessageCodes.CANCEL),                             Command.CANCEL, 1);            append(titleItem);            append(movieRatingItem);            authorItemNum = append(authorItem);            commentItemNum = append(commentField);            addCommand(okCommand);            addCommand(cancelCommand);            setCommandListener(this);        }        public void init(MovieRating movieRating, String movieTitle) {            this.movieRating = movieRating;            titleItem.setText(movieTitle);            if (movieRating.getAuthor().equals(MovieRating.AUTHOR_SELF)) {                movieRatingItem.init(movieRating.getStarNumber(), true);                delete(commentItemNum);                insert(commentItemNum, commentField);                if (get(authorItemNum).equals(authorItem)) {                    delete(authorItemNum);                    commentItemNum--;                }                 authorItem.setText(null);                commentField.setString(movieRating.getComment());            } else {                movieRatingItem.init(movieRating.getStarNumber(), false);                if (!get(authorItemNum).equals(authorItem)) {                    insert(authorItemNum, authorItem);                    commentItemNum++;                }                 delete(commentItemNum);                insert(commentItemNum, commentItem);                authorItem.setText(movieRating.getAuthor());                commentItem.setText(movieRating.getComment());            }         }         public void commandAction(Command command, Displayable displayable) {            if (command == okCommand) {                movieRating.setStarNumber(movieRatingItem.getValue());                movieRating.setComment(commentField.getString());                uiController.handleEvent(EventIds.EVENT_ID_MOVIE_RATING_SAVE_REQUESTED,                                          new Object[] {                    movieRating                });            } else {    // (command == cancelCommand)                uiController.handleEvent(EventIds.EVENT_ID_MOVIE_RATINGS_REQUESTED,                                          null);            }         }     }    private static class MovieRatingItem extends CustomItem {        private UIController uiController;        private Image fullStarImage;        private Image emptyStarImage;        private int numStars = 0;        private boolean modified;        private boolean editable;        private boolean hasFocus;        public MovieRatingItem(UIController uiController) {            super(uiController.getString(UIMessageCodes.RATING));            this.uiController = uiController;            fullStarImage =                 uiController.getImage(UIController.ICON_IDX_FULL_STAR);            emptyStarImage =                 uiController.getImage(UIController.ICON_IDX_EMPTY_STAR);        }        public void init(int numStars, boolean editable) {            this.numStars = numStars;            this.editable = editable;        }         public int getMinContentHeight() {            return Math.max(fullStarImage.getHeight(),                             emptyStarImage.getHeight());        }         public int getMinContentWidth() {            return 5                    * Math.max(fullStarImage.getWidth(),                               emptyStarImage.getWidth());        }         public int getPrefContentHeight(int h) {            return Math.max(fullStarImage.getHeight(),                             emptyStarImage.getHeight());        }         public int getPrefContentWidth(int w) {            return 5                    * Math.max(fullStarImage.getWidth(),                               emptyStarImage.getWidth());        }         public int getValue() {            return numStars;        }         public boolean traverse(int dir, int viewportWidth,                                 int viewportHeight, int[] visRect_inout) {            switch (dir) {            case Canvas.DOWN:            case Canvas.UP:                if (!editable || hasFocus) {                    return false;                } else {                    hasFocus = true;                    return true;                }             case Canvas.LEFT:                if (!editable) {                    return false;                } else if (!hasFocus) {                    hasFocus = true;                    return true;                } else {                    if (numStars > 0) {                        numStars--;                        modified = true;                    }                     repaint();                    return true;                }             case Canvas.RIGHT:                if (!editable) {                    return false;                } else if (!hasFocus) {                    hasFocus = true;                    return true;                } else {                    if (numStars < 5) {                        numStars++;                        modified = true;                    }                     repaint();                    return true;                }             default:                return modified;            }        }         public void traverseOut() {            hasFocus = false;        }         public void paint(Graphics g, int w, int h) {            for (int i = 0; i != numStars; i++) {                g.drawImage(fullStarImage, i * fullStarImage.getWidth(), 0,                             Graphics.TOP | Graphics.LEFT);            }             for (int i = numStars; i != 5; i++) {                g.drawImage(emptyStarImage, i * emptyStarImage.getWidth(), 0,                             Graphics.TOP | Graphics.LEFT);            }         }     }}

⌨️ 快捷键说明

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