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 java.awt.Font;
018    
019    import ch.uzh.ifi.attempto.echocomp.SmallButton;
020    import ch.uzh.ifi.attempto.echocomp.SolidLabel;
021    
022    import nextapp.echo2.app.Border;
023    import nextapp.echo2.app.Color;
024    import nextapp.echo2.app.Column;
025    import nextapp.echo2.app.Extent;
026    import nextapp.echo2.app.Insets;
027    import nextapp.echo2.app.Row;
028    import nextapp.echo2.app.event.ActionEvent;
029    import nextapp.echo2.app.event.ActionListener;
030    
031    /**
032     * This class represents an index bar that shows either letters from A to Z or numbers. This
033     * index bar is used to organize large amounts of entries.
034     * 
035     * @author Tobias Kuhn
036     */
037    public class IndexBar extends Column implements ActionListener {
038    
039            private static final long serialVersionUID = -1496012516474073170L;
040    
041            private ActionListener actionListener;
042            
043            private SmallButton activeButton;
044            private Row row;
045            
046            private IndexBar(String text) {
047                    Row mainRow = new Row();
048                    mainRow.setInsets(new Insets(10, 2, 5, 2));
049                    mainRow.setCellSpacing(new Extent(5));
050                    mainRow.add(new SolidLabel(text, Font.ITALIC, 10));
051                    row = new Row();
052                    mainRow.add(row);
053                    add(mainRow);
054            }
055            
056            /**
057             * Creates a new index bar showing letters from A to Z.
058             * 
059             * @param text The text to be shown on the left hand side of the index bar.
060             * @param actionListener The actionlistener.
061             */
062            public IndexBar(String text, ActionListener actionListener) {
063                    this(text);
064                    this.actionListener = actionListener;
065                    setLetters();
066            }
067            
068            /**
069             * Creates a new index bar showing numbers from 1 to the specified number.
070             * 
071             * @param text The text to be shown on the left hand side of the index bar.
072             * @param n The last number to be shown.
073             * @param actionListener The actionlistener.
074             */
075            public IndexBar(String text, int n, ActionListener actionListener) {
076                    this(text);
077                    this.actionListener = actionListener;
078                    setNumbers(n);
079            }
080            
081            /**
082             * Shows letters from A to Z.
083             */
084            public void setLetters() {
085                    row.removeAll();
086                    char c = 'A';
087                    while (c <= 'Z') {
088                            SmallButton b = new SmallButton(new String(new char[] {c}), this, 12);
089                            b.setBorder(new Border(1, Color.WHITE, Border.STYLE_SOLID));
090                            row.add(b);
091                            c++;
092                    }
093                    activeButton = null;
094                    setActiveButton((SmallButton) row.getComponent(0));
095            }
096            
097            /**
098             * Shows numbers from 1 to the specified number.
099             * 
100             * @param n The last number to be shown.
101             */
102            public void setNumbers(int n) {
103                    row.removeAll();
104                    for (int i = 0; i < n; i++) {
105                            SmallButton b = new SmallButton((i+1) + "", this, 12);
106                            b.setBorder(new Border(1, Color.WHITE, Border.STYLE_SOLID));
107                            row.add(b);
108                    }
109                    activeButton = null;
110                    if (n > 0) setActiveButton((SmallButton) row.getComponent(0));
111            }
112            
113            /**
114             * Sets the button (letter or number) at the given position as the currently active button.
115             * 
116             * @param i The position of the button.
117             */
118            public void setActiveButton(int i) {
119                    if (i >= 0 && i < row.getComponentCount()) {
120                            setActiveButton((SmallButton) row.getComponent(i));
121                    }
122            }
123            
124            private void setActiveButton(SmallButton button) {
125                    if (activeButton == button) return;
126                    if (activeButton != null) activeButton.setBorder(new Border(1, Color.WHITE, Border.STYLE_SOLID));
127                    activeButton = button;
128                    activeButton.setBorder(new Border(1, Color.DARKGRAY, Border.STYLE_SOLID));
129            }
130    
131            public void actionPerformed(ActionEvent e) {
132                    setActiveButton((SmallButton) e.getSource());
133                    actionListener.actionPerformed(new ActionEvent(this, e.getActionCommand()));
134            }
135    
136    }