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.List; 018 019 import nextapp.echo.app.Column; 020 import nextapp.echo.app.Extent; 021 import nextapp.echo.app.Font; 022 import nextapp.echo.app.Insets; 023 import nextapp.echo.app.Row; 024 import nextapp.echo.app.event.ActionEvent; 025 import nextapp.echo.app.event.ActionListener; 026 import ch.uzh.ifi.attempto.acewiki.Wiki; 027 import ch.uzh.ifi.attempto.acewiki.core.OntologyElement; 028 import ch.uzh.ifi.attempto.acewiki.core.WordIndex; 029 import ch.uzh.ifi.attempto.echocomp.GeneralButton; 030 import ch.uzh.ifi.attempto.echocomp.SolidLabel; 031 import ch.uzh.ifi.attempto.echocomp.TextField; 032 import ch.uzh.ifi.attempto.echocomp.VSpace; 033 034 /** 035 * This class represents a page on which the user can search for articles. 036 * 037 * @author Tobias Kuhn 038 */ 039 public class SearchPage extends WikiPage implements ActionListener { 040 041 private static final long serialVersionUID = 7192145568847087174L; 042 043 private static final int pageSize = 50; 044 045 private int chosenPage = 0; 046 private List<OntologyElement> searchResult; 047 048 private Column resultColumn = new Column(); 049 private IndexBar indexBar; 050 private TextField textField; 051 052 /** 053 * Creates a new search page. 054 * 055 * @param wiki The wiki instance. 056 * @param text The search text. 057 */ 058 public SearchPage(Wiki wiki, String text) { 059 super(wiki); 060 061 addTab("Main Page", this); 062 addTab("Index", this); 063 addSelectedTab("Search"); 064 addTab("About", this); 065 066 add(new Title("Search", true)); 067 addHorizontalLine(); 068 add(new VSpace(15)); 069 070 addHeadline("Search word"); 071 add(new VSpace(10)); 072 073 Row textFieldRow = new Row(); 074 textFieldRow.setInsets(new Insets(10, 0)); 075 textFieldRow.setCellSpacing(new Extent(5)); 076 textFieldRow.add(textField = new TextField(this)); 077 textField.setWidth(new Extent(300)); 078 textField.addActionListener(this); 079 textField.setText(text); 080 textFieldRow.add(new GeneralButton("Search", this)); 081 add(textFieldRow); 082 083 add(new VSpace(15)); 084 085 addHeadline("Results"); 086 add(new VSpace(10)); 087 088 indexBar = new IndexBar("Page:", 0, this); 089 add(indexBar); 090 091 resultColumn.setInsets(new Insets(10, 2, 5, 20)); 092 resultColumn.setCellSpacing(new Extent(2)); 093 add(resultColumn); 094 095 update(); 096 } 097 098 protected void doUpdate() { 099 getWiki().getApplication().setFocusedComponent(textField); 100 101 resultColumn.removeAll(); 102 if (textField.getText().length() == 0) { 103 indexBar.setVisible(false); 104 resultColumn.add(new SolidLabel("(enter a search text)", Font.ITALIC, 10)); 105 return; 106 } 107 108 WordIndex index = getWiki().getEngine().getWordIndex(); 109 searchResult = index.searchForElements(textField.getText()); 110 111 if (searchResult.size() == 0) { 112 indexBar.setVisible(false); 113 resultColumn.add(new SolidLabel("(nothing found)", Font.ITALIC, 10)); 114 } else { 115 int i = ((searchResult.size()-1) / pageSize) + 1; 116 if (chosenPage > i) chosenPage = 0; 117 indexBar.setNumbers(i); 118 indexBar.setActiveButton(chosenPage); 119 updatePage(); 120 } 121 } 122 123 private void updatePage() { 124 resultColumn.removeAll(); 125 126 indexBar.setVisible(searchResult.size() > pageSize); 127 128 int max = searchResult.size(); 129 if (max > (chosenPage + 1) * pageSize) max = (chosenPage + 1) * pageSize; 130 131 for (int i = chosenPage * pageSize; i < max; i++) { 132 resultColumn.add(new ListItem(new WikiLink(searchResult.get(i), getWiki()))); 133 } 134 } 135 136 public void actionPerformed(ActionEvent e) { 137 if (e.getSource() == indexBar) { 138 chosenPage = Integer.parseInt(e.getActionCommand()) - 1; 139 log("page", "pressed: page " + (chosenPage+1)); 140 updatePage(); 141 } else if ("Main Page".equals(e.getActionCommand())) { 142 getWiki().showStartPage(); 143 } else if ("Index".equals(e.getActionCommand())) { 144 getWiki().showIndexPage(); 145 } else if ("About".equals(e.getActionCommand())) { 146 getWiki().showAboutPage(); 147 } else { 148 log("page", "search for " + textField.getText()); 149 update(); 150 } 151 } 152 153 public boolean equals(Object obj) { 154 return obj instanceof SearchPage; 155 } 156 157 public String toString() { 158 return "-SEARCH-"; 159 } 160 161 }