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.echocomp;
016    
017    import nextapp.echo.app.Alignment;
018    import nextapp.echo.app.Border;
019    import nextapp.echo.app.Button;
020    import nextapp.echo.app.Color;
021    import nextapp.echo.app.Extent;
022    import nextapp.echo.app.Font;
023    import nextapp.echo.app.Insets;
024    import nextapp.echo.app.event.ActionListener;
025    
026    /**
027     * This is a convenience class for easy creation of buttons.
028     * 
029     * @author Tobias Kuhn
030     */
031    public class GeneralButton extends Button {
032    
033            private static final long serialVersionUID = -1385798331737572623L;
034    
035            /**
036             * Creates a new button.
037             * 
038             * @param text The button text.
039             * @param width The button width.
040             * @param actionListener The action-listener of the button.
041             */
042            public GeneralButton(String text, int width, ActionListener actionListener) {
043                    super(text);
044                    
045                    if (width > 0) {setBorder(new Border(1, Color.BLACK, Border.STYLE_OUTSET));
046                            setWidth(new Extent(width));
047                    }
048                    setHeight(new Extent(17));
049                    setBackground(Style.mediumBackground);
050                    setForeground(Style.darkForeground);
051                    setBorder(new Border(1, Color.BLACK, Border.STYLE_OUTSET));
052                    setDisabledBackground(Style.lightDisabled);
053                    setDisabledForeground(Style.darkDisabled);
054                    setDisabledBorder(new Border(1, Style.darkDisabled, Border.STYLE_OUTSET));
055                    setRolloverEnabled(true);
056                    setRolloverForeground(Style.lightForeground);
057                    setRolloverBackground(Style.darkBackground);
058                    setRolloverBorder(new Border(1, Color.BLACK, Border.STYLE_SOLID));
059                    setInsets(new Insets(2, 0));
060                    setAlignment(new Alignment(Alignment.CENTER, Alignment.CENTER));
061                    setTextAlignment(new Alignment(Alignment.CENTER, Alignment.CENTER));
062                    setFont(new Font(Style.fontTypeface, Font.ITALIC, new Extent(13)));
063                    
064                    setActionCommand(text);
065                    addActionListener(actionListener);
066            }
067            
068            /**
069             * Creates a new button.
070             * 
071             * @param text The button text.
072             * @param actionListener The action-listener of the button.
073             */
074            public GeneralButton(String text, ActionListener actionListener) {
075                    this(text, 0, actionListener);
076            }
077            
078            public void setEnabled(boolean enabled) {
079                    super.setEnabled(enabled);
080                    
081                    // There seems to be a bug in Echo: the disabled background color is not displayed
082                    // properly. This is a workaround for this Problem:
083                    if (enabled) {
084                            setBackground(Style.mediumBackground);
085                    } else {
086                            setBackground(null);
087                    }
088            }
089    
090    }