001    // This file is part of the Attempto Java Packages.
002    // Copyright 2008-2009, 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 ch.uzh.ifi.attempto.echocomp.Style;
018    import nextapp.echo2.app.Button;
019    import nextapp.echo2.app.Extent;
020    import nextapp.echo2.app.Font;
021    import nextapp.echo2.app.Insets;
022    
023    /**
024     * This abstract class represents an item of a menu of the predictive editor. 
025     * 
026     * @author Tobias Kuhn
027     */
028    public abstract class MenuItem extends Button implements Comparable<MenuItem> {
029            
030            /**
031             * Initializes a new menu item.
032             */
033            public MenuItem() {
034            setWidth(new Extent(146));
035            setHeight(new Extent(15));
036            setInsets(new Insets(2,0));
037                    setBackground(Style.mediumBackground);
038                    setForeground(Style.darkForeground);
039                    setRolloverEnabled(true);
040                    setRolloverForeground(Style.lightForeground);
041                    setRolloverBackground(Style.darkBackground);
042            setLineWrap(false);
043            setFont(new Font(Style.fontTypeface, Font.PLAIN, new Extent(12)));
044            }
045    
046            public int compareTo(MenuItem m) {
047                    String s1 = getText();
048                    String s2 = m.getText();
049                    
050                    if (s1.startsWith("the ") || s1.startsWith("The ")) {
051                            s1 = s1.substring(4);
052                    }
053                    if (s2.startsWith("the ") || s2.startsWith("The ")) {
054                            s2 = s2.substring(4);
055                    }
056                    
057                    if (this instanceof SpecialMenuItem && m instanceof SpecialMenuItem) {
058                            return s1.compareToIgnoreCase(s2);
059                    } else if (this instanceof SpecialMenuItem) {
060                            return -1;
061                    } else if (m instanceof SpecialMenuItem) {
062                            return 1;
063                    }
064                    
065                    Integer i1 = null;
066                    Integer i2 = null;
067                    
068                    try {
069                            i1 = Integer.parseInt(s1);
070                    } catch (NumberFormatException ex) {}
071                    try {
072                            i2 = Integer.parseInt(s2);
073                    } catch (NumberFormatException ex) {}
074                    
075                    if (i1 == null && i2 == null) {
076                            return s1.compareToIgnoreCase(s2);
077                    } else if (i1 == null) {
078                            return 1;
079                    } else if (i2 == null) {
080                            return -1;
081                    } else {
082                            return i1 - i2;
083                    }
084            }
085            
086    }