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

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

regularised borders

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 12.5 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.io.IOException;
25import java.util.*;
26import javax.swing.*;
27import javax.swing.event.*;
28import javax.swing.text.*;
29import javax.swing.text.html.*;
30
31
32// local libraries
33import org.nzdl.gsdl.service.NzdlCollectionInfo;
34import org.nzdl.gsdl.service.NzdlQuery;
35import org.nzdl.gsdl.service.NzdlRequest;
36import org.nzdl.gsdl.service.NzdlResponse;
37import org.nzdl.gsdl.service.NzdlResultSet;
38import org.nzdl.gsdl.service.NzdlService;
39import org.nzdl.gsdl.service.NzdlServiceClient;
40
41
42/**
43 * A Class representing the Panel in which the Querying action happens.
44 *
45 * Does most of the actual `work' in the package.
46 *
47 * @author Dave Nichols ([email protected])
48 * @author stuart yeates ([email protected])
49 * @version $Revision: 2177 $
50 * @see org.nzdl.gsdl.service.SimpleGraphicalClient.SimpleGraphicalClient
51 * @see org.nzdl.gsdl.service.SimpleGraphicalClient.CSModel
52 * @see javax.swing.JPanel
53 */
54
55public class SearchPanel extends JPanel implements ActionListener, Constants
56{
57
58 CSModel csModel;
59 NzdlService nzdl;
60
61 JLabel collectionLabel;
62 JTextField searchTextField;
63 JButton searchButton;
64 JComboBox collectionList;
65 JPanel queryFormulationPanel, resultsPanel, dataPanel;
66 JPanel collectionListPanel, searchTextFieldPanel, searchButtonPanel;
67 JList resultsList;
68 JScrollPane scrollResultsPane;
69 JScrollPane scrollDataPane;
70 JFrame windowParent;
71 //JEditorPane documentPane;
72 JTextPane documentPane;
73 HTMLEditorKit htmlEditorKit;
74 GMLDocument htmlDoc = new GMLDocument();
75 final static GMLDocument blankDoc = new GMLDocument();
76
77 /**
78 * Construct and initialise a new SearchPanel
79 */
80
81 public SearchPanel( CSModel newCsModel, JFrame parent)
82 {
83 super();
84 windowParent = parent;
85 csModel = newCsModel;
86 nzdl = csModel.getNzdlService();
87 setLayout( new BoxLayout(this, BoxLayout.Y_AXIS));
88 setBorder(BorderFactory.createTitledBorder("Search"));
89
90
91 searchTextField = new JTextField("Enter search terms here", 35) {
92 public boolean isRequestFocusEnabled(){
93 return true;
94 }
95 };
96 searchTextField.setText("Enter search terms here");
97 searchTextField.setFont(searchTextFieldFont);
98 searchTextField.setColumns(35);
99 searchTextField.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
100 searchButton = new JButton("Search");
101
102 searchButton.addActionListener(this);
103 //searchButton.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
104 searchButton.setPreferredSize(new Dimension(80, 20));
105
106 collectionListPanel = new JPanel();
107 collectionListPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
108 collectionListPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
109 collectionLabel = new JLabel("Collection: ");
110
111 collectionList = new JComboBox(csModel.getCollectionList()){
112 public boolean isFocusTraversable() {
113 return false;
114 }
115 };
116
117 collectionListPanel.add(collectionLabel);
118 collectionListPanel.add(collectionList);
119
120 searchTextFieldPanel = new JPanel();
121 searchTextFieldPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
122 searchTextFieldPanel.add(searchTextField);
123
124 searchButtonPanel = new JPanel();
125 searchButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
126 searchButtonPanel.add(searchButton);
127
128 queryFormulationPanel = new JPanel();
129 queryFormulationPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
130 queryFormulationPanel.setLayout(new BoxLayout(queryFormulationPanel, BoxLayout.Y_AXIS));
131 queryFormulationPanel.add(Box.createHorizontalStrut(100));
132 queryFormulationPanel.add(collectionListPanel);
133 queryFormulationPanel.add(searchTextFieldPanel);
134 queryFormulationPanel.add(searchButtonPanel);
135
136 resultsPanel = new JPanel();
137 resultsPanel.setLayout(new BorderLayout());
138 resultsPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
139 ResultModel resultListModel = csModel.getResultsModel();
140 resultsList = new JList(resultListModel);
141 resultsList.setFont(resultsFont);
142 resultsList.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
143 resultsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
144 scrollResultsPane = new JScrollPane(resultsList);
145 resultsPanel.add(scrollResultsPane, BorderLayout.CENTER);
146 csModel.addResult(Result.INITIAL_FAKE_RESULT);
147
148 dataPanel = new JPanel();
149 dataPanel.setLayout(new BorderLayout());
150 dataPanel.setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
151 documentPane = new JTextPane();
152 //documentPane = new JEditorPane();
153 htmlEditorKit = new HTMLEditorKit();
154 documentPane.setEditorKit(htmlEditorKit);
155 documentPane.setEditable(false);
156 documentPane.setFont(docFont);
157 documentPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
158 //documentPane.setPreferredSize(new Dimension(100, 100));
159
160 scrollDataPane = new JScrollPane(documentPane);
161 scrollDataPane.setPreferredSize(new Dimension(300, 300));
162 dataPanel.add(scrollDataPane, BorderLayout.CENTER);
163
164 parent.getRootPane().setDefaultButton(searchButton);
165
166 resultsList.addListSelectionListener(new ResultListSelectionHandler() );
167
168 add(Box.createHorizontalStrut(400));
169 add(queryFormulationPanel);
170 add(resultsPanel);
171 add(dataPanel);
172 searchTextField.requestFocus();
173 }
174
175 /** respond to the user pressing the Search button */
176
177 public void actionPerformed(ActionEvent e) {
178 if (e.getSource() == searchButton ) { //assume only searchButton generates events
179 // cursor not set when RETURN pressed, only when button clicked ?
180 windowParent.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
181 //System.err.println("set wait cursor..");
182 String queryString = searchTextField.getText();
183 String collectionName = collectionList.getSelectedItem().toString();
184 System.err.println("Search started (" + collectionName + "): \"" + queryString + "\" ..." );
185 //send query to collection
186 NzdlQuery nzdlQuery = new NzdlQuery(queryString);
187 // return the first numResults that match
188 //query.setEndResults( 15 );
189 // "-1" means consider all the documents that match
190 nzdlQuery.setMaxDocs( -1 );
191 NzdlRequest request = new NzdlRequest( nzdlQuery );
192 NzdlResponse response = new NzdlResponse();
193
194 nzdl.service( collectionName, request, response );
195 Date t8 = new Date();
196 NzdlResultSet results = response.getResultSet();
197 ArrayList docIDs = new ArrayList(results.getDocumentIDs());
198 // System.err.println("Size of docIDs = " + docIDs.size());
199 // paired collection of docIDs and Titles - but as Sets!
200 Map titleMetaData = csModel.getNzdlService().getMetaData( collectionName, docIDs, "Title" );
201 // created a paired collection of docIDs and Titles - as simple Strings!
202 HashMap titleMap = new HashMap();
203 for (ListIterator i = docIDs.listIterator(); i.hasNext(); ) {
204 String docID = (String) i.next();
205 //Set titleSet = (Set) titleMetaData.get( docID );
206 ArrayList titleList = new ArrayList((Set)titleMetaData.get(docID));
207 // use the first title as *the* title
208 titleMap.put(docID, titleList.get(0));
209 } // end for
210 Date t9 = new Date();
211 long sdiff = t9.getTime() - t8.getTime();
212 System.err.println("processing results = " + sdiff + " ms");
213 // update the results list
214 csModel.clearResults();
215
216 if (docIDs.size() == 0 ) { // give some feedback for 0 results
217 csModel.addResult(Result.FAKE_RESULT);
218 //documentPane.setText("");
219 documentPane.setStyledDocument(blankDoc);
220 }
221 else { // there are some results
222 Iterator docIDsIterator = docIDs.iterator();
223 while (docIDsIterator.hasNext()) {
224 String resultDocID = (String) docIDsIterator.next();
225 csModel.addResult(new Result((String)titleMap.get(resultDocID), resultDocID, collectionName ));
226 } //end while
227 resultsList.setSelectedIndex(0);
228 } // end else
229 titleMap.clear();
230 titleMetaData.clear();
231 docIDs.clear();
232 windowParent.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
233 } // end if
234 else {
235 System.err.println("unknown Action event in SearchPanel");
236 }
237 } //end actionPerformed
238
239 /** handles selection events in the results list so that when the
240 user selects a new title (say) the document contents is
241 automatically displayed in the main panel */
242
243 class ResultListSelectionHandler implements ListSelectionListener {
244
245 // private Result previousSelection = null;
246
247 public void valueChanged(ListSelectionEvent e) {
248 // get the result object from the SelectedIndex
249 // assume we only get here with events from resultsList
250 if (e.getValueIsAdjusting()) { return; }
251 Result result = (Result) resultsList.getSelectedValue();
252 // System.err.println("result = " + result);
253 // System.err.println("fake result = " + Result.FAKE_RESULT);
254 if ((result == Result.FAKE_RESULT) || (result == Result.INITIAL_FAKE_RESULT)) {
255 //documentPane.setText("");
256 documentPane.setStyledDocument( blankDoc );
257 System.err.println("clicked on fake result");
258 }
259 else {
260
261 Date startTime = new Date();
262 windowParent.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
263 //trash the old htmlDoc ...
264 try {
265 if (htmlDoc != null)
266 htmlDoc.remove(0, htmlDoc.getLength());
267 } catch (Exception exception) {
268 throw new Error (exception.toString());
269 }
270 htmlDoc = new GMLDocument();
271 Date t1 = new Date();
272 Date d1 = new Date();
273 Date d2 = new Date();
274 Date d3 = new Date();
275 try { // get the document and read it in as HTML
276 d1 = new Date();
277 String docContents = csModel.getNzdlService().getDocument(result.getCollectionName(), result.getDocID());
278 d2 = new Date();
279 //long ddiff1 = d2.getTime() - d1.getTime();
280 // System.err.println("getDocument = " + ddiff1 + "ms");
281 htmlEditorKit.read( new StringReader(docContents), htmlDoc, 0);
282 d3 = new Date();
283 //long ddiff2 = d3.getTime() - d2.getTime();
284 //System.err.println("StringReader() = " + ddiff2 + "ms");
285
286 // htmlEditorKit.read( new StringReader(csModel.getNzdlService().getDocument(result.getCollectionName(), result.getDocID())), htmlDoc, 0);
287 }
288 catch(BadLocationException badLocException)
289 {
290 System.err.println("Bad Location Exception in creating HTML doc" + badLocException);
291 }
292 catch(IOException ioException)
293 {
294 System.err.println("IO Exception whilst reading doc contents" + ioException);
295 }
296 Date t2 = new Date();
297 long diff = t2.getTime() - t1.getTime();
298 long ddiff1 = d2.getTime() - d1.getTime();
299 long ddiff2 = d3.getTime() - d2.getTime();
300 System.err.println("nzdl.getDocument = " + ddiff1 + "ms");
301 System.err.println("StringReader() = " + ddiff2 + "ms");
302 System.err.println("getDocument total = " + diff + " ms");
303
304 Date t3 = new Date();
305
306 documentPane.setStyledDocument( htmlDoc );
307
308 Date t4 = new Date();
309 long diff2 = t4.getTime() - t3.getTime();
310 System.err.println("documentPane set " + diff2 + " ms");
311
312 documentPane.setCaretPosition(1);
313
314 //Date t5 = new Date();
315 //long diff7 = t5.getTime() - t4.getTime();
316 //System.err.println("documentPane scroll " + diff7 + " ms");
317
318 //System.err.println("getting image data...");
319 // htmlDoc.getImageData();
320 windowParent.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
321 Date endTime = new Date();
322 long diff5 = endTime.getTime() - startTime.getTime();
323 System.err.println("total time " + diff5 + " ms");
324 }
325 } //end valueChanged
326 }//end ResultListSelectionHandler
327
328} //end SearchPanel
Note: See TracBrowser for help on using the repository browser.