001 // This file is part of AceWiki.
002 // Copyright 2008-2012, AceWiki developers.
003 //
004 // AceWiki is free software: you can redistribute it and/or modify it under the terms of the GNU
005 // Lesser General Public License as published by the Free Software Foundation, either version 3 of
006 // the License, or (at your option) any later version.
007 //
008 // AceWiki is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009 // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
010 // Lesser General Public License for more details.
011 //
012 // You should have received a copy of the GNU Lesser General Public License along with AceWiki. If
013 // not, see http://www.gnu.org/licenses/.
014
015 package ch.uzh.ifi.attempto.preditor;
016
017 import nextapp.echo.app.Button;
018 import nextapp.echo.app.Extent;
019 import nextapp.echo.app.Font;
020 import nextapp.echo.app.Insets;
021 import ch.uzh.ifi.attempto.echocomp.Style;
022
023 /**
024 * This abstract class represents an item of a menu of the predictive editor. Each menu item
025 * contains the name of the menu group it belongs to.
026 *
027 * @author Tobias Kuhn
028 */
029 public abstract class MenuItem extends Button {
030
031 private static final long serialVersionUID = 6061341215846815821L;
032
033 private String menuGroup;
034 private String id;
035 private boolean highlighted = false;
036 private int colorShift;
037
038 /**
039 * Initializes a new menu item.
040 *
041 * @param menuGroup The menu group to which this item should be assigned.
042 */
043 public MenuItem(String menuGroup) {
044 if (menuGroup == null) menuGroup = "";
045 this.menuGroup = menuGroup;
046 setWidth(new Extent(146));
047 setHeight(new Extent(15));
048 setInsets(new Insets(2,0));
049 setBackground(Style.mediumBackground);
050 setForeground(Style.darkForeground);
051 setRolloverEnabled(true);
052 setRolloverForeground(Style.lightForeground);
053 setRolloverBackground(Style.darkBackground);
054 setLineWrap(false);
055 updateStyle();
056 }
057
058 /**
059 * Returns the name of the menu group of this menu item.
060 *
061 * @return The name of the menu group.
062 */
063 public String getMenuGroup() {
064 return menuGroup;
065 }
066
067 /**
068 * This method returns an identifier that is unique (within one instance of the predictive
069 * editor).
070 *
071 * @return the identifier.
072 */
073 public String getMenuItemID() {
074 if (id == null) recalculateID();
075 return id;
076 }
077
078 /**
079 * This method determines whether the menu entry is highlighted or not. Hightlighted menu
080 * entries are displayed in bold font and are shown in front of non-highlighted menu entries
081 * in sorted lists.
082 *
083 * @param highlighted true if this entry should be highlighted.
084 */
085 public void setHighlighted(boolean highlighted) {
086 this.highlighted = highlighted;
087 updateStyle();
088 }
089
090 /**
091 * Returns whether this menu item is highlighted or not.
092 *
093 * @return true if this menu item is highlighted.
094 */
095 public boolean isHighlighted() {
096 return highlighted;
097 }
098
099 /**
100 * This methods sets the color shift that defines the color in which this menu item is to be
101 * displayed.
102 *
103 * @see MenuCreator#getColorShift
104 * @param colorShift The color shift value.
105 */
106 public void setColorShift(int colorShift) {
107 int colorShiftDiff = colorShift - this.colorShift;
108 this.colorShift = colorShift;
109 setBackground(Style.shiftColor(super.getBackground(), colorShiftDiff));
110 setForeground(Style.shiftColor(super.getForeground(), colorShiftDiff));
111 setRolloverBackground(Style.shiftColor(super.getRolloverBackground(), colorShiftDiff));
112 setRolloverForeground(Style.shiftColor(super.getRolloverForeground(), colorShiftDiff));
113 }
114
115 /**
116 * This method should be called internally whenever something changed that has an influence on
117 * the identifier.
118 */
119 protected void recalculateID() {
120 id = "";
121 for (String s : getContent()) {
122 id += s.replaceAll(":", "~:").replaceAll("~", "~~") + ":";
123 }
124 }
125
126 /**
127 * This method sets the style according to whether or not this menu item is highlighted.
128 */
129 protected void updateStyle() {
130 if (highlighted) {
131 setFont(new Font(Style.fontTypeface, Font.BOLD, new Extent(12)));
132 } else {
133 setFont(new Font(Style.fontTypeface, Font.PLAIN, new Extent(12)));
134 }
135 }
136
137 /**
138 * This method is used to calculate the unique identifier.
139 *
140 * @return An array of strings that uniquely defines the menu item object.
141 */
142 protected abstract String[] getContent();
143
144 public boolean equals(Object obj) {
145 if (obj instanceof MenuItem) {
146 return getMenuItemID().equals(((MenuItem) obj).getMenuItemID());
147 } else {
148 return false;
149 }
150 }
151
152 }