    001    // This file is part of the Attempto Java Packages.
    002    // Copyright 2008, Attempto Group, University of Zurich (see http://attempto.ifi.uzh.ch).
    003    //
    004    // The Attempto Java Packages is free software: you can redistribute it and/or modify it under the
    005    // terms of the GNU Lesser General Public License as published by the Free Software Foundation,
    006    // either version 3 of the License, or (at your option) any later version.
    007    //
    008    // The Attempto Java Packages is distributed in the hope that it will be useful, but WITHOUT ANY
    009    // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
    010    // PURPOSE. See the GNU Lesser General Public License for more details.
    011    //
    012    // You should have received a copy of the GNU Lesser General Public License along with the Attempto
    013    // Java Packages. If not, see http://www.gnu.org/licenses/.
    014
    015    package ch.uzh.ifi.attempto.preditor;
    016
    017    import nextapp.echo2.app.Extent;
    018    import nextapp.echo2.app.Font;
    019    import ch.uzh.ifi.attempto.echocomp.Style;
    020    import ch.uzh.ifi.attempto.preditor.text.TextElement;
    021
    022    /**
    023     * This class represents a menu item that contains a text element. The text element is added to
    024     * the partial sentence when the user clicks on a menu entry in the predictive editor.
    025     *
    026     * @author Tobias Kuhn
    027     */
    028    public class MenuEntry extends MenuItem {
    029
    030            private static final long serialVersionUID = -4231372412315340523L;
    031
    032            private TextElement textElement;
    033            private boolean strong = true;
    034            private boolean highlighted = false;
    035
    036            /**
    037             * Creates a new menu entry for the given text element.
    038             *
    039             * @param textElement The text element.
    040             */
    041            public MenuEntry(TextElement textElement) {
    042            this.textElement = textElement;
    043                    setText(textElement.getText());
    044                    setFont(new Font(Style.fontTypeface, Font.PLAIN, new Extent(12)));
    045        }
    046
    047            /**
    048             * Returns the text element.
    049             *
    050             * @return The text element.
    051             */
    052        public TextElement getTextElement() {
    053                    return textElement;
    054        }
    055
    056        /**
    057         * This method determines whether the menu entry is highlighted or not. Hightlighted menu entries
    058         * are displayed in bold font and are in front of non-highlighted menu entries in sorted lists.
    059         *
    060         * @param highlighted
    061         */
    062        public void setHighlighted(boolean highlighted) {
    063            this.highlighted = highlighted;
    064                    if (highlighted) {
    065                            setFont(new Font(Style.fontTypeface, Font.BOLD, new Extent(12)));
    066                    } else {
    067                            setFont(new Font(Style.fontTypeface, Font.PLAIN, new Extent(12)));
    068                    }
    069        }
    070
    071        public boolean equals(Object obj) {
    072                    if (obj instanceof MenuEntry) {
    073                            return textElement.equals(((MenuEntry) obj).textElement);
    074                    }
    075                    return false;
    076        }
    077
    078            public int compareTo(MenuItem m) {
    079                    if (m instanceof MenuEntry) {
    080                            MenuEntry other = (MenuEntry) m;
    081                            if (this.strong && !other.strong) {
    082                                    return -1;
    083                            } else if (!this.strong && other.strong) {
    084                                    return 1;
    085                            } else {
    086                                    if (this.highlighted && !other.highlighted) {
    087                                            return -1;
    088                                    } else if (!this.highlighted && other.highlighted) {
    089                                            return 1;
    090                                    } else {
    091                                            return super.compareTo(m);
    092                                    }
    093                            }
    094                    } else {
    095                            return super.compareTo(m);
    096                    }
    097            }
    098
    099    }