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