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.ApplicationInstance; 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.Row; 026 import nextapp.echo2.app.event.ActionEvent; 027 import nextapp.echo2.app.event.ActionListener; 028 import ch.uzh.ifi.attempto.acewiki.Wiki; 029 import ch.uzh.ifi.attempto.acewiki.core.ontology.OntologyElement; 030 import ch.uzh.ifi.attempto.acewiki.gui.IndexBar; 031 import ch.uzh.ifi.attempto.acewiki.gui.ListItem; 032 import ch.uzh.ifi.attempto.acewiki.gui.Title; 033 import ch.uzh.ifi.attempto.acewiki.gui.WikiLink; 034 import ch.uzh.ifi.attempto.echocomp.GeneralButton; 035 import ch.uzh.ifi.attempto.echocomp.SolidLabel; 036 import ch.uzh.ifi.attempto.echocomp.TextField; 037 import ch.uzh.ifi.attempto.echocomp.VSpace; 038 039 /** 040 * This class represents a page on which the user can search for articles. 041 * 042 * @author Tobias Kuhn 043 */ 044 public class SearchPage extends WikiPage implements ActionListener { 045 046 private static final long serialVersionUID = 7192145568847087174L; 047 048 private static final int pageSize = 20; 049 050 private int chosenPage = 0; 051 private ArrayList<OntologyElement> searchResult; 052 053 private Column resultColumn = new Column(); 054 private IndexBar indexBar; 055 private TextField textField; 056 057 /** 058 * Creates a new search page. 059 * 060 * @param wiki 061 */ 062 public SearchPage(Wiki wiki) { 063 super(wiki, new Title("Search", true)); 064 065 addTab("Main Page", this); 066 addTab("Index", this); 067 addSelectedTab("Search"); 068 069 add(new VSpace(15)); 070 071 addHeadline("Search word"); 072 add(new VSpace(10)); 073 074 Row textFieldRow = new Row(); 075 textFieldRow.setInsets(new Insets(10, 0)); 076 textFieldRow.setCellSpacing(new Extent(5)); 077 textFieldRow.add(textField = new TextField(this)); 078 textField.setWidth(new Extent(300)); 079 textField.addActionListener(this); 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 ApplicationInstance.getActive().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 ArrayList<OntologyElement> elements = new ArrayList<OntologyElement>(getWiki().getOntologyElements()); 109 Collections.sort(elements); 110 111 searchResult = new ArrayList<OntologyElement>(); 112 for (OntologyElement e : elements) { 113 if (e.getWord().toLowerCase().replace("_", " ").contains(textField.getText().toLowerCase().replace("_", " "))) { 114 searchResult.add(e); 115 } 116 } 117 if (searchResult.size() == 0) { 118 indexBar.setVisible(false); 119 resultColumn.add(new SolidLabel("(nothing found)", Font.ITALIC, 10)); 120 } else { 121 int i = ((searchResult.size()-1) / pageSize) + 1; 122 if (chosenPage > i) chosenPage = 0; 123 indexBar.setNumbers(i); 124 indexBar.setActiveButton(chosenPage); 125 updatePage(); 126 } 127 } 128 129 private void updatePage() { 130 resultColumn.removeAll(); 131 132 indexBar.setVisible(searchResult.size() > pageSize); 133 134 int max = searchResult.size(); 135 if (max > (chosenPage + 1) * pageSize) max = (chosenPage + 1) * pageSize; 136 137 for (int i = chosenPage * pageSize; i < max; i++) { 138 resultColumn.add(new ListItem(new WikiLink(searchResult.get(i), getWiki()))); 139 } 140 } 141 142 public void actionPerformed(ActionEvent e) { 143 if (e.getSource() == indexBar) { 144 chosenPage = Integer.parseInt(e.getActionCommand()) - 1; 145 log("page", "pressed: page " + (chosenPage+1)); 146 updatePage(); 147 } else if ("Main Page".equals(e.getActionCommand())) { 148 getWiki().showStartPage(); 149 } else if ("Index".equals(e.getActionCommand())) { 150 getWiki().showIndexPage(); 151 } else { 152 log("page", "search for " + textField.getText()); 153 update(); 154 } 155 } 156 157 public boolean equals(Object obj) { 158 return obj instanceof SearchPage; 159 } 160 161 public String toString() { 162 return "-SEARCH-"; 163 } 164 165 }