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

Last change on this file since 2227 was 2227, checked in by daven, 23 years ago

Custom cell renderer for the QueryHistoryPanel - with long collection
names as tooltips.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 13.7 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;
40import org.nzdl.gsdl.util.NzdlPreferences;
41
42
43/**
44 * A Class representing the Panel in which the Querying action happens.
45 *
46 * Does most of the actual `work' in the package.
47 *
48 * @author Dave Nichols ([email protected])
49 * @author stuart yeates ([email protected])
50 * @version $Revision: 2227 $
51 * @see org.nzdl.gsdl.service.SimpleGraphicalClient.SimpleGraphicalClient
52 * @see org.nzdl.gsdl.service.SimpleGraphicalClient.CSModel
53 * @see javax.swing.JPanel
54 */
55
56public class SearchPanel extends JPanel implements ActionListener, Constants
57{
58
59 CSModel csModel;
60 NzdlService nzdl;
61
62 JLabel collectionLabel;
63 JButton collectionInfoButton;
64 JTextField searchTextField;
65 JButton searchButton;
66 JComboBox collectionList;
67 JPanel queryFormulationPanel, resultsPanel, dataPanel;
68 JPanel collectionListPanel, searchTextFieldPanel, searchButtonPanel;
69 JList resultsList;
70 JScrollPane scrollResultsPane;
71 JScrollPane scrollDataPane;
72 JFrame windowParent;
73 //JEditorPane documentPane;
74 JTextPane documentPane; // make this a JTextComponent ??
75 JTextArea documentArea;
76 JTextComponent documentComponent;
77 HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
78 StyledEditorKit styledEditorKit = new StyledEditorKit();
79 GMLDocument htmlDoc = new GMLDocument();
80 DefaultStyledDocument defaultStyledDoc = new DefaultStyledDocument();
81 final static GMLDocument blankDoc = new GMLDocument();
82
83 /**
84 * Construct and initialise a new SearchPanel
85 */
86
87 public SearchPanel( CSModel newCsModel, JFrame parent)
88 {
89 super();
90 windowParent = parent;
91 csModel = newCsModel;
92 nzdl = csModel.getNzdlService();
93 setLayout( new BoxLayout(this, BoxLayout.Y_AXIS));
94 setBorder(BorderFactory.createTitledBorder("Search"));
95
96
97 searchTextField = new JTextField("Enter search terms here", 35) {
98 public boolean isRequestFocusEnabled(){
99 return true;
100 }
101 };
102 searchTextField.setText("Enter search terms here");
103 searchTextField.setFont(searchTextFieldFont);
104 searchTextField.setColumns(35);
105 searchTextField.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
106 searchButton = new JButton("Search");
107
108 searchButton.addActionListener(this);
109 //searchButton.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
110 searchButton.setPreferredSize(new Dimension(80, 20));
111
112 collectionListPanel = new JPanel();
113 collectionListPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
114 collectionListPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
115 collectionLabel = new JLabel("Collection: ");
116
117 collectionList = new JComboBox(csModel.getCollectionList()){
118 public boolean isFocusTraversable() {
119 return false;
120 }
121 };
122 collectionList.setEditable(false);
123
124 collectionInfoButton = new JButton("Info...") {
125 public String getToolTipText(MouseEvent e) {
126 return "Collection information for '" + csModel.getLongCollectionName((String)collectionList.getSelectedItem()) +"'";
127 }
128 };
129 ToolTipManager.sharedInstance().registerComponent(collectionInfoButton);
130 collectionInfoButton.addActionListener(this);
131
132 collectionListPanel.add(collectionLabel);
133 collectionListPanel.add(collectionList);
134 collectionListPanel.add(collectionInfoButton);
135
136 searchTextFieldPanel = new JPanel();
137 searchTextFieldPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
138 searchTextFieldPanel.add(searchTextField);
139
140 searchButtonPanel = new JPanel();
141 searchButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
142 searchButtonPanel.add(searchButton);
143
144 queryFormulationPanel = new JPanel();
145 queryFormulationPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
146 queryFormulationPanel.setLayout(new BoxLayout(queryFormulationPanel, BoxLayout.Y_AXIS));
147 queryFormulationPanel.add(Box.createHorizontalStrut(100));
148 queryFormulationPanel.add(collectionListPanel);
149 queryFormulationPanel.add(searchTextFieldPanel);
150 queryFormulationPanel.add(searchButtonPanel);
151
152 resultsPanel = new JPanel();
153 resultsPanel.setLayout(new BorderLayout());
154 resultsPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
155 ResultModel resultListModel = csModel.getResultsModel();
156 resultsList = new JList(resultListModel);
157 resultsList.setFont(resultsFont);
158 resultsList.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
159 resultsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
160 scrollResultsPane = new JScrollPane(resultsList);
161 resultsPanel.add(scrollResultsPane, BorderLayout.CENTER);
162 csModel.addResult(Result.INITIAL_FAKE_RESULT);
163
164 dataPanel = new JPanel();
165 dataPanel.setLayout(new BorderLayout());
166 dataPanel.setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
167 documentPane = new JTextPane() { // to control scrolling for raw text docs
168 public boolean getScrollableTracksViewportWidth() {
169 if (getSize().width < getParent().getSize().width)
170 return true;
171 else return false;
172 }
173 public void setSize(Dimension d) {
174 if (d.width < getParent().getSize().width)
175 d.width = getParent().getSize().width;
176 super.setSize(d);
177 }
178 }; // end custom JTextPane
179
180 documentPane.setEditable(false);
181 documentPane.setFont(docFont);
182 documentPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
183 //documentPane.setPreferredSize(new Dimension(100, 100));
184
185 scrollDataPane = new JScrollPane(documentPane);
186 scrollDataPane.setPreferredSize(new Dimension(300, 300));
187 dataPanel.add(scrollDataPane, BorderLayout.CENTER);
188
189 parent.getRootPane().setDefaultButton(searchButton);
190
191 resultsList.addListSelectionListener(new ResultListSelectionHandler() );
192
193 add(Box.createHorizontalStrut(400));
194 add(queryFormulationPanel);
195 add(resultsPanel);
196 add(dataPanel);
197 searchTextField.requestFocus();
198 }
199
200 /** respond to the user pressing the Search button */
201
202 public void actionPerformed(ActionEvent e) {
203 String collectionName = collectionList.getSelectedItem().toString();
204 if (e.getSource() == searchButton ) {
205 // cursor not set when RETURN pressed, only when button clicked ?
206 windowParent.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
207 String queryString = searchTextField.getText();
208 System.err.println("Search started (" + collectionName + "): \"" + queryString + "\" ..." );
209 //send query to collection
210 NzdlQuery nzdlQuery = new NzdlQuery(queryString);
211 // return the first numResults that match
212 //query.setEndResults( 15 );
213 // "-1" means consider all the documents that match
214 nzdlQuery.setMaxDocs( -1 );
215 NzdlRequest request = new NzdlRequest( nzdlQuery );
216 NzdlResponse response = new NzdlResponse();
217
218 nzdl.service( collectionName, request, response );
219 NzdlResultSet results = response.getResultSet();
220 ArrayList docIDs = new ArrayList(results.getDocumentIDs());
221 // System.err.println("Size of docIDs = " + docIDs.size());
222 // paired collection of docIDs and Titles - but as Sets!
223 Map titleMetaData = nzdl.getMetaData( collectionName, docIDs, "Title" );
224 // created a paired collection of docIDs and Titles - as simple Strings!
225 HashMap titleMap = new HashMap();
226 for (ListIterator i = docIDs.listIterator(); i.hasNext(); ) {
227 String docID = (String) i.next();
228 //Set titleSet = (Set) titleMetaData.get( docID );
229 ArrayList titleList = new ArrayList((Set)titleMetaData.get(docID));
230 // use the first title as *the* title
231 titleMap.put(docID, titleList.get(0));
232 } // end for
233 // update the results list
234 csModel.clearResults();
235
236 if (docIDs.size() == 0 ) { // give some feedback for 0 results
237 csModel.addResult(Result.FAKE_RESULT);
238 documentPane.setStyledDocument(blankDoc);
239 }
240 else { // there are some results
241 Iterator docIDsIterator = docIDs.iterator();
242 while (docIDsIterator.hasNext()) {
243 String resultDocID = (String) docIDsIterator.next();
244 csModel.addResult(new Result((String)titleMap.get(resultDocID), resultDocID, collectionName ));
245 } //end while
246 if (NzdlPreferences.getInstance().getBoolean(Constants.DISPLAY_FIRST_DOC))
247 resultsList.setSelectedIndex(0); // should be under Preference control!
248 } // end else
249
250
251 csModel.getQueryHistoryModel().add( new QueryHistoryItem(new Query(queryString), new Date(), collectionName, "user"));
252
253 titleMap.clear();
254 titleMetaData.clear();
255 docIDs.clear();
256 windowParent.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
257 } // end if
258 else {
259 if (e.getSource() == collectionInfoButton) {
260 CollectionInfoDialog cid = new CollectionInfoDialog(windowParent, collectionName, csModel);
261 }
262 else {
263 System.err.println("unknown Action event in SearchPanel");
264 }
265 }
266 } //end actionPerformed
267
268 /** handles selection events in the results list so that when the
269 user selects a new title (say) the document contents is
270 automatically displayed in the main panel */
271
272 class ResultListSelectionHandler implements ListSelectionListener {
273
274 // private Result previousSelection = null;
275
276 public void valueChanged(ListSelectionEvent e) {
277 // get the result object from the SelectedIndex
278 // assume we only get here with events from resultsList
279 int docLength = 0;
280 if (e.getValueIsAdjusting()) { return; }
281 Result result = (Result) resultsList.getSelectedValue();
282 if ((result == Result.FAKE_RESULT) || (result == Result.INITIAL_FAKE_RESULT)) {
283 documentPane.setStyledDocument( blankDoc );
284 System.err.println("clicked on fake result");
285 }
286 else { // a real result representing a real document to deal with
287
288 windowParent.setCursor(WAIT_CURSOR);
289
290 deleteDocContents(); // for memory/garbage collection
291
292 StringReader sr = new StringReader(nzdl.getDocument(result.getCollectionName(), result.getDocID()));
293
294 if (NzdlPreferences.getInstance().getBoolean(Constants.RAW_TEXT))
295 // display the document as raw text
296 displayAsRawText(sr);
297 else {
298 // treat as HTML
299 displayAsHTML(sr);
300 //htmlDoc.getImageData(nzdl, result.getCollectionName());
301 }
302
303 windowParent.setCursor(NORMAL_CURSOR);
304 }
305 } //end valueChanged
306 }//end ResultListSelectionHandler
307
308
309
310 private void deleteDocContents() {
311 //trash the old htmlDoc for memory/garbage collection
312 try {
313 if (htmlDoc != null)
314 htmlDoc.remove(0, htmlDoc.getLength());
315 if (defaultStyledDoc != null)
316 defaultStyledDoc.remove(0, defaultStyledDoc.getLength());
317 }
318 catch (Exception exception) {
319 throw new Error (exception.toString());
320 }
321 } // end deleteDocContents
322
323
324 public void switchToRawText() {
325 windowParent.setCursor(WAIT_CURSOR);
326 System.err.println("switching to raw text");
327 displayAsRawText(new StringReader(documentPane.getText()));
328 windowParent.setCursor(NORMAL_CURSOR);
329 }
330
331 public void displayAsRawText(StringReader sr) {
332 defaultStyledDoc = new DefaultStyledDocument();
333 documentPane.setEditorKit(styledEditorKit);
334 try {
335 styledEditorKit.read( sr, defaultStyledDoc, 0);
336 }
337 catch (Exception e) {
338 System.err.println(e);
339 }
340 documentPane.setDocument(defaultStyledDoc);
341 setCaretToStart(defaultStyledDoc.getLength());
342 } // end displayAsRawText
343
344
345 public void switchToHTML() {
346 windowParent.setCursor(WAIT_CURSOR);
347 System.err.println("switching to HTML");
348 displayAsHTML(new StringReader(documentPane.getText()));
349 windowParent.setCursor(NORMAL_CURSOR);
350 }
351
352 public void displayAsHTML(StringReader sr) {
353 htmlDoc = new GMLDocument();
354 documentPane.setEditorKit(htmlEditorKit);
355 try {
356 htmlEditorKit.read( sr, htmlDoc, 0);
357 }
358 catch (Exception e) {
359 System.err.println(e);
360 }
361 documentPane.setStyledDocument( htmlDoc );
362 setCaretToStart(htmlDoc.getLength());
363 } // end displayAsHTML
364
365
366 private void setCaretToStart(int docLength) {
367 // it seems as if they haven't considered
368 // documents with > MAX_INT num of chars ...
369 if (docLength > 0)
370 documentPane.setCaretPosition(1);
371 }
372
373
374 } //end SearchPanel
Note: See TracBrowser for help on using the repository browser.