/** * * @title DictSearch * @author James McElvenny * * This class reads the parameters passed to the applet, sets up the user interface and calls the IndexSearcher * when the user clicks the search button. * */ import javax.swing.JButton; import javax.swing.JTextField; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JApplet; import javax.swing.JList; import javax.swing.JScrollPane; import javax.swing.ListSelectionModel; import javax.swing.DefaultListModel; import java.awt.Container; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.net.URL; import java.net.MalformedURLException; import java.io.IOException; import javax.xml.xpath.XPathExpressionException; import org.xml.sax.SAXException; import javax.xml.parsers.ParserConfigurationException; public class DictSearch extends JApplet { public static final String ERROR_MESSAGE = "No matches found."; private int appletWidth; private int appletHeight; private String[] indexes; private String entryPath; private String indexFile; private String server; private IndexSearcher searcher; public void init() { getSettings(); try { searcher = new IndexSearcher(server, indexFile, entryPath); } catch(ParserConfigurationException pce) { System.out.println("Exception occurred: " + pce); } setupGUI(); } /** * * This method reads the applet parameters from the HTML code and sets up the other applet settings. * */ private void getSettings() { String appletWidthIn = getParameter("width"); if(appletWidthIn == null) { appletWidth = 640; } else { appletWidth = Integer.parseInt(appletWidthIn); } String appletHeightIn = getParameter("height"); if(appletHeightIn == null) { appletHeight = 480; } else { appletHeight = Integer.parseInt(appletHeightIn); } String indexesInput = getParameter("indexes"); if(indexesInput == null) { System.out.println("OICH!"); //need some error code here!! } else { indexes = indexesInput.split("[\\s|,]"); } entryPath = getParameter("entryPath"); if(entryPath == null) { entryPath = "/index/entry"; } indexFile = getParameter("indexFile"); if(indexFile == null) { indexFile = "index.xml"; } //Get the address of the server String docBase = this.getDocumentBase().toString(); int endIndex = docBase.lastIndexOf("/"); server = new String(docBase.substring(0,endIndex) + "/"); } /** * * This method sets up the applet GUI. * */ private void setupGUI() { int searchAreaHeight = 30; int buttonWidth = 100; int textFieldWidth = appletWidth - (buttonWidth * 2); int searchPanelHeight = searchAreaHeight * 2; int resultsHeight = appletHeight - (searchAreaHeight + searchPanelHeight); Dimension buttonSize = new Dimension(buttonWidth, searchAreaHeight); Dimension textSize = new Dimension(textFieldWidth, searchAreaHeight); Dimension indexSize = new Dimension(appletWidth, searchAreaHeight); Dimension resultsSize = new Dimension(appletWidth, resultsHeight); Dimension searchPanelSize = new Dimension(appletWidth, searchPanelHeight); //Set up the search text field final JTextField searchText = new JTextField(); searchText.setMinimumSize(textSize); searchText.setPreferredSize(textSize); //Set up the search button JButton searchButton = new JButton("Search"); searchButton.setMinimumSize(buttonSize); searchButton.setPreferredSize(buttonSize); //Set up panel for the search area JPanel searchPanel = new JPanel(new FlowLayout()); searchPanel.setMinimumSize(searchPanelSize); searchPanel.setPreferredSize(searchPanelSize); searchPanel.add(searchText); searchPanel.add(searchButton); //Set up the index selection box DefaultListModel indexListModel = new DefaultListModel(); final JList indexList = new JList(indexListModel); indexList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); indexList.setLayoutOrientation(JList.HORIZONTAL_WRAP); indexList.setVisibleRowCount(-1); for (String e : indexes) indexListModel.addElement(e); indexList.setSelectedIndex(0); //Set up the results list final DefaultListModel resultsListModel = new DefaultListModel(); final JList resultsList = new JList(resultsListModel); resultsList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); resultsList.setLayoutOrientation(JList.VERTICAL); resultsList.setVisibleRowCount(-1); //Set up the scroll pane for the list JScrollPane listScroller = new JScrollPane(resultsList); listScroller.setPreferredSize(resultsSize); listScroller.setMaximumSize(resultsSize); //Add components to the GUI and display JPanel topPanel = new JPanel(new BorderLayout()); topPanel.add(listScroller, BorderLayout.SOUTH); topPanel.add(indexList, BorderLayout.CENTER); topPanel.add(searchPanel, BorderLayout.NORTH); Container content = getContentPane(); content.add(topPanel); /** * * This is an embedded class that provides the action for the button * */ class SearchClickListener implements ActionListener { public void actionPerformed(ActionEvent event) { resultsListModel.clear(); String index = (String) indexList.getSelectedValue(); String searchString = searchText.getText(); DefaultListModel outputTarget = resultsListModel; try { searcher.searchIndex(index, searchString, outputTarget); } catch(SAXException se) { System.out.println(se + " occurred."); } catch(IOException ioe) { System.out.println(ioe + "occurred."); } catch(XPathExpressionException xpe) { System.out.println(xpe + "occurred."); } } } /** * * This is an embedded class that detects double-clicks on the resultsList * */ class ResultsDoubleClick extends MouseAdapter { public void mouseClicked(MouseEvent event) { if(event.getClickCount() == 2) { String entryToOpen = (String) resultsList.getSelectedValue(); if(entryToOpen != null && entryToOpen != ERROR_MESSAGE) { try { URL destination = new URL(server + entryToOpen); getAppletContext().showDocument(destination); } catch(MalformedURLException mue) { System.out.println(mue + " occurred."); } } } } } //Link embedded action listener class to the button ActionListener searchButtonListener = new SearchClickListener(); searchButton.addActionListener(searchButtonListener); //Link embedded mouse listener to resultsList MouseAdapter resultsListListener = new ResultsDoubleClick(); resultsList.addMouseListener(resultsListListener); } }