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.acewiki.gui.page;
016    
017    import java.util.ArrayList;
018    import java.util.Collections;
019    
020    import nextapp.echo2.app.Column;
021    import nextapp.echo2.app.Extent;
022    import nextapp.echo2.app.Font;
023    import nextapp.echo2.app.Insets;
024    import nextapp.echo2.app.event.ActionEvent;
025    import nextapp.echo2.app.event.ActionListener;
026    import ch.uzh.ifi.attempto.acewiki.Wiki;
027    import ch.uzh.ifi.attempto.acewiki.core.ontology.OntologyElement;
028    import ch.uzh.ifi.attempto.acewiki.gui.IndexBar;
029    import ch.uzh.ifi.attempto.acewiki.gui.ListItem;
030    import ch.uzh.ifi.attempto.acewiki.gui.Title;
031    import ch.uzh.ifi.attempto.acewiki.gui.WikiLink;
032    import ch.uzh.ifi.attempto.echocomp.SolidLabel;
033    import ch.uzh.ifi.attempto.echocomp.VSpace;
034    
035    /**
036     * This class represents an page that shows an index of all articles that exist in the wiki.
037     * 
038     * @author Tobias Kuhn
039     */
040    public class IndexPage extends WikiPage implements ActionListener {
041    
042            private static final long serialVersionUID = 6061966610996079528L;
043            
044            private static final int pageSize = 25;
045            
046            private String chosenChar = "A";
047            private int chosenPage = 0;
048            private ArrayList<OntologyElement> matchingElements;
049            
050            private Column indexColumn = new Column();
051            private IndexBar letterIndexBar;
052            private IndexBar numberIndexBar;
053            
054            /**
055             * Creates a new index page.
056             * 
057             * @param wiki The wiki instance.
058             */
059            public IndexPage(Wiki wiki) {
060                    super(wiki, new Title("Index", true));
061                    
062                    addTab("Main Page", this);
063                    addSelectedTab("Index");
064                    addTab("Search", this);
065                    
066                    add(new VSpace(20));
067                    
068                    letterIndexBar = new IndexBar("First letter:", this);
069                    add(letterIndexBar);
070                    
071                    numberIndexBar = new IndexBar("Page:", 0, this);
072                    add(numberIndexBar);
073                    
074                    indexColumn.setInsets(new Insets(10, 5, 5, 20));
075                    indexColumn.setCellSpacing(new Extent(2));
076                    add(indexColumn);
077                    
078                    update();
079            }
080            
081            protected void doUpdate() {
082                    ArrayList<OntologyElement> elements = new ArrayList<OntologyElement>(getWiki().getOntologyElements());
083                    Collections.sort(elements);
084                    
085                    indexColumn.removeAll();
086                    
087                    matchingElements = new ArrayList<OntologyElement>();
088                    for (OntologyElement e : elements) {
089                            if (e.getHeadword().toUpperCase().startsWith(chosenChar)) {
090                                    matchingElements.add(e);
091                            }
092                    }
093                    if (matchingElements.size() == 0) {
094                            numberIndexBar.setVisible(false);
095                            indexColumn.add(new SolidLabel("(no entry starting with '" + chosenChar + "')", Font.ITALIC, 10));
096                    } else {
097                            int i = ((matchingElements.size()-1) / pageSize) + 1;
098                            if (chosenPage > i) chosenPage = 0;
099                            numberIndexBar.setNumbers(i);
100                            numberIndexBar.setActiveButton(chosenPage);
101                            updatePage();
102                    }
103            }
104            
105            private void updatePage() {
106                    indexColumn.removeAll();
107                    
108                    numberIndexBar.setVisible(matchingElements.size() > pageSize);
109                    
110                    int max = matchingElements.size();
111                    if (max > (chosenPage + 1) * pageSize) max = (chosenPage + 1) * pageSize;
112                    
113                    for (int i = chosenPage * pageSize; i < max; i++) {
114                            indexColumn.add(new ListItem(new WikiLink(matchingElements.get(i), getWiki())));
115                    }
116            }
117            
118            public void actionPerformed(ActionEvent e) {
119                    if (e.getSource() == letterIndexBar) {
120                            chosenChar = e.getActionCommand();
121                            log("page", "pressed: first letter " + chosenChar);
122                            chosenPage = 0;
123                            update();
124                    } else if (e.getSource() == numberIndexBar) {
125                            chosenPage = Integer.parseInt(e.getActionCommand()) - 1;
126                            log("page", "pressed: page " + (chosenPage+1));
127                            updatePage();
128                    } else if ("Main Page".equals(e.getActionCommand())) {
129                            getWiki().showStartPage();
130                    } else if ("Search".equals(e.getActionCommand())) {
131                            getWiki().showSearchPage();
132                    }
133            }
134            
135            public boolean equals(Object obj) {
136                    return obj instanceof IndexPage;
137            }
138            
139            public String toString() {
140                    return "-INDEX-";
141            }
142    
143    }