source: trunk/java-client/org/nzdl/gsdl/SimpleGraphicalClient/SearchPanel.java@ 2149

Last change on this file since 2149 was 2149, checked in by say1, 23 years ago

resolved issues

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 10.3 KB
Line 
1/*
2 * SimpleGraphicalClient.java
3 * Copyright (C) 2001 New Zealand Digital Library Project
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.nzdl.gsdl.SimpleGraphicalClient;
20
21import java.awt.*;
22import java.awt.event.*;
23import java.io.*;
24import java.util.*;
25import javax.swing.*;
26import javax.swing.border.*;
27import javax.swing.event.*;
28
29// local libraries
30import org.nzdl.gsdl.service.NzdlCollectionInfo;
31import org.nzdl.gsdl.service.NzdlQuery;
32import org.nzdl.gsdl.service.NzdlRequest;
33import org.nzdl.gsdl.service.NzdlResponse;
34import org.nzdl.gsdl.service.NzdlResultSet;
35import org.nzdl.gsdl.service.NzdlService;
36import org.nzdl.gsdl.service.NzdlServiceClient;
37
38
39/**
40 * A Class representing the Panel in which the Queryign action happens.
41 *
42 * Does most of the actual `work' in the package.
43 *
44 * @author Dave Nichols ([email protected])
45 * @author stuart yeates ([email protected])
46 * @version $Revision: 2149 $
47 * @see org.nzdl.gsdl.service.SimpleGraphicalClient.SimpleGraphicalClient
48 * @see org.nzdl.gsdl.service.SimpleGraphicalClient.CSModel
49 * @see javax.swing.JPanel
50 */
51
52public class SearchPanel extends JPanel implements ActionListener, Constants
53{
54
55 CSModel csModel;
56 JLabel collectionLabel;
57 JTextField searchTextField;
58 JButton searchButton;
59 JComboBox collectionList; // can we search multiple collections at once?
60 JPanel queryFormulationPanel, resultsPanel, dataPanel;
61 JPanel collectionListPanel, searchTextFieldPanel, searchButtonPanel;
62 /** where the ranked results are displayed */
63 JList resultsList;
64 JScrollPane scrollResultsPane;
65 /** where the doc contents are displayed */
66 JEditorPane dataTextArea;
67 JScrollPane scrollDataPane;
68
69 final static String DOC_HEADER = "<html><body>";
70 final static String DOC_FOOTER = "</body></html>";
71
72 /**
73 * Construct and initialise a new SearchPanel
74 */
75
76 public SearchPanel( CSModel newCsModel, JFrame parent)
77 {
78 super();
79 csModel = newCsModel;
80 setLayout( new BoxLayout(this, BoxLayout.Y_AXIS));
81 setBorder(BorderFactory.createTitledBorder("Search"));
82
83
84 searchTextField = new JTextField("Enter search terms here", 35) {
85 public boolean isRequestFocusEnabled(){
86 return true;
87 }
88 };
89
90
91 searchTextField.setText("Enter search terms here");
92 searchTextField.setFont(searchTextFieldFont);
93 searchTextField.setColumns(35);
94 searchTextField.setBorder(BorderFactory.createEmptyBorder(0,2,0,2));
95 searchButton = new JButton("Search");
96
97 searchButton.addActionListener(this);
98 //searchButton.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
99 searchButton.setPreferredSize(new Dimension(80, 20));
100
101 collectionListPanel = new JPanel();
102 collectionListPanel.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
103 collectionListPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
104 collectionLabel = new JLabel("Collection: ");
105 /*collectionList = new JComboBox(csModel.getCollectionList()){
106 public boolean isFocusTraversable() {
107 return false;
108 }
109 }; */
110 //System.err.println("size is " + csModel.getCollectionListSize());
111 //Collection coll = csModel.getCollectionList();
112 //System.err.println("coll size is " + coll.size());
113 //Vector vec = new Vector(coll);
114 //System.err.println("vector size is " + vec.size());
115 collectionList = new JComboBox(csModel.getCollectionList()){
116 public boolean isFocusTraversable() {
117 return false;
118 }
119 };
120
121
122
123
124
125 //collectionList.setSelectedIndex(0); // only if there is content
126 collectionListPanel.add(collectionLabel);
127 collectionListPanel.add(collectionList);
128
129
130 searchTextFieldPanel = new JPanel();
131 searchTextFieldPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
132 searchTextFieldPanel.add(searchTextField);
133
134 searchButtonPanel = new JPanel();
135 searchButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
136 searchButtonPanel.add(searchButton);
137
138 queryFormulationPanel = new JPanel();
139 queryFormulationPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
140 queryFormulationPanel.setLayout(new BoxLayout(queryFormulationPanel, BoxLayout.Y_AXIS));
141 queryFormulationPanel.add(Box.createHorizontalStrut(100));
142 queryFormulationPanel.add(collectionListPanel);
143 queryFormulationPanel.add(searchTextFieldPanel);
144 queryFormulationPanel.add(searchButtonPanel);
145
146
147 resultsPanel = new JPanel();
148 resultsPanel.setLayout(new BorderLayout());
149 resultsPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
150 ResultModel resultListModel = csModel.getResultsModel();
151 resultsList = new JList(resultListModel);
152 resultsList.setFont(resultsFont);
153 resultsList.setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
154 resultsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
155 scrollResultsPane = new JScrollPane(resultsList);
156 resultsPanel.add(scrollResultsPane, BorderLayout.CENTER);
157 csModel.addResult(new Result("Search results will appear here", "", ""));
158
159 dataPanel = new JPanel();
160 dataPanel.setLayout(new BorderLayout());
161 dataPanel.setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
162 dataTextArea = new JEditorPane("text/html",
163 "<html><body>Document details will appear here.\n\n\n</body></html>");
164 dataTextArea.setEditable(false);
165 dataTextArea.setEditable(false);
166 dataTextArea.setFont(docFont);
167 dataTextArea.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
168 //dataTextArea.setPreferredSize(new Dimension(100, 100));
169 scrollDataPane = new JScrollPane(dataTextArea);
170 scrollDataPane.setPreferredSize(new Dimension(100, 100));
171 dataPanel.add(scrollDataPane, BorderLayout.CENTER);
172
173 parent.getRootPane().setDefaultButton(searchButton);
174
175 //resultsList.getSelectionModel().addListSelectionListener(new ResultListSelectionHandler() );
176 resultsList.addListSelectionListener(new ResultListSelectionHandler() );
177
178 add(Box.createHorizontalStrut(400));
179
180 add(queryFormulationPanel);
181 add(resultsPanel);
182 add(dataPanel);
183 searchTextField.requestFocus();
184 }
185
186 /** respond to the user pressing the Search button */
187
188 public void actionPerformed(ActionEvent e) {
189 if (e.getSource() == searchButton ) { //assume only searchButton generates events
190 System.out.println("Search button pressed in SearchPanel");
191 //send query to collection
192 NzdlQuery nzdlQuery = new NzdlQuery( searchTextField.getText() );
193 // return the first numResults that match
194 //query.setEndResults( 15 );
195 // "-1" means consider all the documents that match
196 nzdlQuery.setMaxDocs( -1 );
197 String collectionName = collectionList.getSelectedItem().toString();
198 NzdlRequest request = new NzdlRequest( nzdlQuery );
199 NzdlResponse response = new NzdlResponse( );
200 csModel.getNzdlService().service( collectionName, request, response ); //nzdl object ????
201 NzdlResultSet results = response.getResultSet();
202 java.util.List docIDs = results.getDocumentIDs();
203 // paired collection of docIDs and Titles - but as Sets!
204 Map titleMetaData = csModel.getNzdlService().getMetaData( collectionName, docIDs, "Title" );
205 // created a paired collection of docIDs and Titles - as simple Strings!
206 HashMap titleMap = new HashMap();
207 for (ListIterator i = docIDs.listIterator(); i.hasNext(); ) {
208 String docID = (String) i.next();
209 Set titleSet = (Set) titleMetaData.get( docID );
210 ArrayList titleList = new ArrayList(titleSet);
211 // use the first title as *the* title
212 titleMap.put(docID, titleList.get(0));
213 } // end for
214 // update the results list
215 // empty previous results List
216 csModel.clearResults();
217 // System.err.println("Size of docIDs = " + docIDs.size());
218 if (docIDs.size() == 0 ) { // no real results
219 csModel.addResult(new Result(Result.FAKE_RESULT, "", ""));
220 }
221 else { // there are some results
222 Iterator docIDsIterator = docIDs.iterator();
223 while (docIDsIterator.hasNext()) {
224 String resultDocID = (String) docIDsIterator.next();
225 //System.err.println("Adding result");
226 csModel.addResult(new Result((String)titleMap.get(resultDocID), resultDocID, collectionName ));
227 } //end while
228 } // end else
229 // default to showing the top ranked document
230 if (docIDs.size() > 0)
231 resultsList.setSelectedIndex(0);
232 } // end if
233 else {
234 System.out.println("unknown Action event in SearchPanel");
235 }
236 } //end actionPerformed
237
238 /** handles selection events in the results list so that when the
239 user selects a new title (say) the document contents is
240 automatically displayed in the main panel */
241
242 class ResultListSelectionHandler implements ListSelectionListener {
243 public void valueChanged(ListSelectionEvent e) {
244 // get the result object from the SelectedIndex
245 // assume we only get here with events from resultsList
246 Result result = (Result) resultsList.getSelectedValue();
247 if (result.toString() == Result.FAKE_RESULT) {
248 dataTextArea.setText(null);
249 //System.err.println("no results");
250 }
251 else {
252 //get the document content from the result object
253 // use a String for the moment to hold the contents
254 // should cache document contents here to speed access
255 String documentContents = csModel.getNzdlService().getDocument(result.getCollectionName(), result.getDocID());
256 documentContents = DOC_HEADER + documentContents + DOC_FOOTER;
257 dataTextArea.setText(documentContents);
258 dataTextArea.setCaretPosition(0);
259 }
260 } //end valueChanged
261 }//end ResultListSelectionHandler
262
263
264
265} //end SearchPanel
Note: See TracBrowser for help on using the repository browser.