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 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 (this instanceof SpecialMenuItem && m instanceof SpecialMenuItem) {
051 return s1.compareToIgnoreCase(s2);
052 } else if (this instanceof SpecialMenuItem) {
053 return -1;
054 } else if (m instanceof SpecialMenuItem) {
055 return 1;
056 }
057
058 Integer i1 = null;
059 Integer i2 = null;
060
061 try {
062 i1 = Integer.parseInt(s1);
063 } catch (NumberFormatException ex) {}
064 try {
065 i2 = Integer.parseInt(s2);
066 } catch (NumberFormatException ex) {}
067
068 if (i1 == null && i2 == null) {
069 return s1.compareToIgnoreCase(s2);
070 } else if (i1 == null) {
071 return 1;
072 } else if (i2 == null) {
073 return -1;
074 } else {
075 return i1 - i2;
076 }
077 }
078
079 }