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    
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, String text) {
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                    textField.setText(text);
081                    textFieldRow.add(new GeneralButton("Search", this));
082                    add(textFieldRow);
083                    
084                    add(new VSpace(15));
085                    
086                    addHeadline("Results");
087                    add(new VSpace(10));
088                    
089                    indexBar = new IndexBar("Page:", 0, this);
090                    add(indexBar);
091                    
092                    resultColumn.setInsets(new Insets(10, 2, 5, 20));
093                    resultColumn.setCellSpacing(new Extent(2));
094                    add(resultColumn);
095                    
096                    update();
097            }
098            
099            protected void doUpdate() {
100                    ApplicationInstance.getActive().setFocusedComponent(textField);
101                    
102                    resultColumn.removeAll();
103                    if (textField.getText().length() == 0) {
104                            indexBar.setVisible(false);
105                            resultColumn.add(new SolidLabel("(enter a search text)", Font.ITALIC, 10));
106                            return;
107                    }
108                    
109                    ArrayList<OntologyElement> elements = new ArrayList<OntologyElement>(getWiki().getOntologyElements());
110                    Collections.sort(elements);
111                    
112                    searchResult = new ArrayList<OntologyElement>();
113                    for (OntologyElement e : elements) {
114                            for (String w : e.getWords()) {
115                                    if (w == null) continue;
116                                    if (w.toLowerCase().replace("_", " ").contains(textField.getText().toLowerCase().replace("_", " "))) {
117                                            searchResult.add(e);
118                                            break;
119                                    }
120                            }
121                    }
122                    if (searchResult.size() == 0) {
123                            indexBar.setVisible(false);
124                            resultColumn.add(new SolidLabel("(nothing found)", Font.ITALIC, 10));
125                    } else {
126                            int i = ((searchResult.size()-1) / pageSize) + 1;
127                            if (chosenPage > i) chosenPage = 0;
128                            indexBar.setNumbers(i);
129                            indexBar.setActiveButton(chosenPage);
130                            updatePage();
131                    }
132            }
133            
134            private void updatePage() {
135                    resultColumn.removeAll();
136                    
137                    indexBar.setVisible(searchResult.size() > pageSize);
138                    
139                    int max = searchResult.size();
140                    if (max > (chosenPage + 1) * pageSize) max = (chosenPage + 1) * pageSize;
141                    
142                    for (int i = chosenPage * pageSize; i < max; i++) {
143                            resultColumn.add(new ListItem(new WikiLink(searchResult.get(i), getWiki())));
144                    }
145            }
146            
147            public void actionPerformed(ActionEvent e) {
148                    if (e.getSource() == indexBar) {
149                            chosenPage = Integer.parseInt(e.getActionCommand()) - 1;
150                            log("page", "pressed: page " + (chosenPage+1));
151                            updatePage();
152                    } else if ("Main Page".equals(e.getActionCommand())) {
153                            getWiki().showStartPage();
154                    } else if ("Index".equals(e.getActionCommand())) {
155                            getWiki().showIndexPage();
156                    } else {
157                            log("page", "search for " + textField.getText());
158                            update();
159                    }
160            }
161            
162            public boolean equals(Object obj) {
163                    return obj instanceof SearchPage;
164            }
165            
166            public String toString() {
167                    return "-SEARCH-";
168            }
169    
170    }