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.acewiki.gui;
016    
017    import ch.uzh.ifi.attempto.echocomp.Style;
018    import nextapp.echo2.app.Alignment;
019    import nextapp.echo2.app.Border;
020    import nextapp.echo2.app.Button;
021    import nextapp.echo2.app.Color;
022    import nextapp.echo2.app.Column;
023    import nextapp.echo2.app.Extent;
024    import nextapp.echo2.app.Font;
025    import nextapp.echo2.app.Insets;
026    import nextapp.echo2.app.ResourceImageReference;
027    import nextapp.echo2.app.event.ActionEvent;
028    import nextapp.echo2.app.event.ActionListener;
029    import echopointng.DropDown;
030    
031    /**
032     * This class represents drop down menus that are shown in front of the ACE sentences as
033     * small triangles.
034     * 
035     * @author Tobias Kuhn
036     */
037    public class DropDownMenu extends DropDown implements ActionListener {
038    
039            private static final long serialVersionUID = 3646511994109953476L;
040            
041            private Column menuColumn = new Column();
042            private ActionListener actionListener;
043            private Column menuSeparator = null;
044            
045            /**
046             * Creates a empty drop down menu with an icon of the given color.
047             * 
048             * @param color The color of the icon, one of "blue", "light-blue", or "red".
049             * @param actionListener The actionlistener.
050             */
051            public DropDownMenu(String color, ActionListener actionListener) {
052                    this.actionListener = actionListener;
053                    setWidth(new Extent(13));
054                    setPopUp(menuColumn);
055                    setPopUpBorder(new Border(1, Color.BLACK, Border.STYLE_OUTSET));
056                    setPopUpAlignment(new Alignment(Alignment.RIGHT, Alignment.BOTTOM));
057                    setPopUpInsets(new Insets(0, 0));
058                    setPopUpOutsets(new Insets(15, -15, 0, 0));
059                    String img1 = null;
060                    String img2 = null;
061                    if (color.equals("blue")) {
062                            img1 = "ch/uzh/ifi/attempto/acewiki/gui/img/DropDown1.png";
063                            img2 = "ch/uzh/ifi/attempto/acewiki/gui/img/DropDown2.png";
064                    } else if (color.equals("red")) {
065                            img1 = "ch/uzh/ifi/attempto/acewiki/gui/img/DropDownRed1.png";
066                            img2 = "ch/uzh/ifi/attempto/acewiki/gui/img/DropDownRed2.png";
067                    } else if (color.equals("light-blue")) {
068                            img1 = "ch/uzh/ifi/attempto/acewiki/gui/img/DropDownLBlue1.png";
069                            img2 = "ch/uzh/ifi/attempto/acewiki/gui/img/DropDownLBlue2.png";
070                    } else if (color.equals("gray")) {
071                            img1 = "ch/uzh/ifi/attempto/acewiki/gui/img/DropDownGray1.png";
072                            img2 = "ch/uzh/ifi/attempto/acewiki/gui/img/DropDownGray2.png";
073                    }
074                    setToggleIcon(new ResourceImageReference(img1));
075                    setToggleRolloverIcon(new ResourceImageReference(img2));
076                    setTogglePressedIcon(new ResourceImageReference(img2));
077                    setBorder(new Border(0, Color.BLACK, Border.STYLE_SOLID));
078                    setRolloverBorder(new Border(0, Color.BLACK, Border.STYLE_SOLID));
079            }
080            
081            /**
082             * Adds a menu entry to the drop down menu.
083             * 
084             * @param text The text of the menu entry.
085             */
086            public void addMenuEntry(String text) {
087                    if (menuSeparator != null) {
088                            menuColumn.add(menuSeparator);
089                            menuSeparator = null;
090                    }
091                    Button menuEntry = new Button(text);
092                    menuEntry.setActionCommand(text);
093                    menuEntry.setHeight(new Extent(16));
094                    menuEntry.setWidth(new Extent(130));
095                    menuEntry.setBackground(Style.mediumBackground);
096                    menuEntry.setForeground(Style.darkForeground);
097                    menuEntry.setRolloverEnabled(true);
098                    menuEntry.setRolloverForeground(Style.lightForeground);
099                    menuEntry.setRolloverBackground(Style.darkBackground);
100                    menuEntry.setInsets(new Insets(2, 0));
101                    menuEntry.setAlignment(new Alignment(Alignment.LEFT, Alignment.CENTER));
102                    menuEntry.setTextAlignment(new Alignment(Alignment.LEFT, Alignment.CENTER));
103                    menuEntry.setFont(new Font(Style.fontTypeface, Font.ITALIC, new Extent(13)));
104                    menuEntry.addActionListener(this);
105                    menuColumn.add(menuEntry);
106            }
107            
108            /**
109             * Adds a separator to the menu.
110             */
111            public void addMenuSeparator() {
112                    if (menuColumn.getComponentCount() == 0) return;
113                    menuSeparator = new Column();
114                    menuSeparator.setInsets(new Insets(2, 0, 2, 0));
115                    menuSeparator.setBackground(Style.mediumBackground);
116                    Column line = new Column();
117                    line.setBackground(Style.darkBackground);
118                    line.setInsets(new Insets(0, 1, 0, 0));
119                    menuSeparator.add(line);
120            }
121            
122            public void actionPerformed(ActionEvent e) {
123                    setExpanded(false);
124                    actionListener.actionPerformed(new ActionEvent(this, e.getActionCommand()));
125            }
126    
127    }