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