/************************************************************************** /* GUI application for document search. /* /* Copyright (c) 2003-2004 by Bernhard Bablok (mail@bablokb.de) /* /* This library is free software; you can redistribute it and/or modify /* it under the terms of the GNU Lesser General Public License as published /* by the Free Software Foundation; either version 2 of the License or /* (at your option) any later version. /* /* This library is distributed in the hope that it will be useful, but /* WITHOUT ANY WARRANTY; without even the implied warranty of /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /* GNU Lesser General Public License for more details. /* /* You should have received a copy of the GNU Lesser General Public License /* along with this library; see the file COPYING.LESSER. If not, write to /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, /* Boston, MA 02111-1307 USA /**************************************************************************/ package de.bablokb.luala.app; import java.util.*; import java.io.*; import java.net.*; import java.awt.*; import javax.swing.*; import gnu.getopt.*; import org.apache.lucene.document.*; import de.bablokb.luala.lib.*; import de.bablokb.luala.lib.event.*; import de.bablokb.luala.lib.gui.*; /** GUI application for document search.

TODO:Add options using getopt. @version $Revision: 1.15 $ @author $Author: bablokb $ */ public class GuiSearcher implements SearchEventListener, ShowDocumentEventListener { //////////////////////////////////////////////////////////////////////////// /** String-constant for the properties-file. */ private static final String GUI_SEARCHER_PROPERTIES = "/GuiSearcher.properties"; //////////////////////////////////////////////////////////////////////////// /** The index directory. */ private String iIndexDir; //////////////////////////////////////////////////////////////////////////// /** The browser to use for hyperlinks. */ private String iBrowser; //////////////////////////////////////////////////////////////////////////// /** The path-prefix. */ private String iPathPrefix = ""; //////////////////////////////////////////////////////////////////////////// /** The start-page. */ private String iStartPage = ""; //////////////////////////////////////////////////////////////////////////// /** The debug-mode. */ private boolean iDebug = false; //////////////////////////////////////////////////////////////////////////// /** The singleton-object of this class. */ private static GuiSearcher iSearcher = new GuiSearcher(); //////////////////////////////////////////////////////////////////////////// /** The {@link SearchPanel} for this application. */ private SearchPanel iSearchPanel; //////////////////////////////////////////////////////////////////////////// /** The {@link ResultView} for this application. */ private ResultView iResultView; //////////////////////////////////////////////////////////////////////////// /** The {@link MessageLine} for this application. */ private MessageLine iMessageLine; ///////////////////////////////////////////////////////////////////////////// /** A private constructor, since we use the singleton-design pattern. Use {@link #getInstance()} to access the singleton. */ private GuiSearcher() { } ///////////////////////////////////////////////////////////////////////////// /** Query the singleton-instance. @return The singleton instance. */ public static GuiSearcher getInstance() { return iSearcher; } //////////////////////////////////////////////////////////////////////////// /** Display Usage information and exit. */ private void usage() { System.out.println("\nusage: java " + GuiSearcher.class.getName() + " -i indexDir -b browser [-p path-prefix] [-dh] start-page\n\n" + "valid options:\n" + "\t-i indexDir directory with the index-files (required)\n" + "\t-b browser browser to use for results (required)\n" + "\t-p path-prefix a prefix to prepend to the results\n" + "\t-d display debug-output\n" + "\t-h display usage-information and exit\n" ); System.exit(1); } //////////////////////////////////////////////////////////////////////////// /** Parse commandline arguments. @param args The cmd-line arguments. */ private void parseArguments(String[] args) { Getopt g = new Getopt(GuiSearcher.class.getName(),args, ":i:b:p:dh"); int c; while ((c = g.getopt()) != -1) { switch(c) { case 'i': iIndexDir = g.getOptarg(); break; case 'b': iBrowser = g.getOptarg(); break; case 'p': iPathPrefix = g.getOptarg(); break; case 'd': iDebug = true; break; case 'h': case ':': case '?': usage(); break; default: break; } } if (g.getOptind() == args.length-1) iStartPage = args[g.getOptind()]; else usage(); } //////////////////////////////////////////////////////////////////////////// /** Check arguments. */ private void checkArguments() { if (iIndexDir == null || iBrowser == null || iStartPage == null) usage(); if (! new File(iIndexDir).isDirectory()) { System.err.println("error: " + iIndexDir + " does not exist!"); usage(); } } //////////////////////////////////////////////////////////////////////////// /** Main-program. Currently, the program just supports three command-line arguments:

  1. The index directory
  2. The browser to use for hyperlinks
  3. The optional path-prefix for results
@param args The cmd-line arguments. */ public static void main(String[] args) { try { GuiSearcher searcher = GuiSearcher.getInstance(); searcher.parseArguments(args); searcher.checkArguments(); // searcher.loadProperties(); searcher.initLogging(); searcher.createGUI(); } catch (Exception e) { e.printStackTrace(); } } ///////////////////////////////////////////////////////////////////////////// /** Load a properties-file into the System-properties. */ private void loadProperties() { Properties props = System.getProperties(); try { InputStream lStream = GuiSearcher.class.getResourceAsStream(GUI_SEARCHER_PROPERTIES); props.load(lStream); lStream.close(); } catch (IOException e) { e.printStackTrace(); } System.setProperties(props); } ///////////////////////////////////////////////////////////////////////////// /** Initiate log4j-logging. */ private void initLogging() { } ///////////////////////////////////////////////////////////////////////////// /** Create GUI of this application. */ private void createGUI() throws Exception { iSearchPanel = new SimpleSearchPanel(); iSearchPanel.addSearchEventListener(this); File startPage = new File(iStartPage); iResultView = new HtmlResultView(startPage.toURL()); iResultView.addShowDocumentEventListener(this); GuiSearcherPanel panel = new GuiSearcherPanel(iSearchPanel,iResultView); iMessageLine = new MessageLine(); GuiSearcherFrame frame = new GuiSearcherFrame("Index: " + iIndexDir); frame.getContentPane().add(panel,BorderLayout.CENTER); frame.getContentPane().add(iMessageLine,BorderLayout.SOUTH); frame.pack(); WindowHelper.center(frame); frame.show(); } ///////////////////////////////////////////////////////////////////////////// /** Process the exit-command. TODO: save location and other settings in preferences. */ public void processExit() { System.exit(0); } ///////////////////////////////////////////////////////////////////////////// /** Process the openIndex-command. TODO: implement it! */ public void processOpenIndex() { } //////////////////////////////////////////////////////////////////////////// /** {@inheritDoc} */ public void search(SearchEvent se) { iMessageLine.start(); iMessageLine.setText("searching ..."); final String searchString = se.getSearchString(); Runnable searchThread = new Runnable() { public void run() { try { SearchEngine engine = new SearchEngine(iIndexDir); Document[] results = engine.search(searchString); ResultRenderer renderer = new HtmlResultRenderer(iPathPrefix); iResultView.setData(renderer.render(results)); iMessageLine.stop(); iMessageLine.setText("Search finished"); } catch (Exception ex) { ex.printStackTrace(); } } }; SwingUtilities.invokeLater(searchThread); } //////////////////////////////////////////////////////////////////////////// /** {@inheritDoc} */ public void showDocument(ShowDocumentEvent sde) { iMessageLine.start(); final String doc = sde.getName(); Runnable viewerThread = new Runnable() { public void run() { try { Runtime.getRuntime().exec(iBrowser + " " + doc); iMessageLine.stop(); iMessageLine.setText("Browser " + iBrowser + " started!"); } catch (IOException ex) { } } }; new Thread(viewerThread).start(); } }