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

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

dynamically update display mode based on preference changes. i.e. if the user
switches to raw text mode the document display should update on the
exit of the prefences dialog, not until the user selects a new result.
Altered the listhandler in SearchPanel.java to simplify this and reduce
occurrences of multiple code.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 13.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.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: 2222 $
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 collectionInfoButton = new JButton("Info...");
123 collectionInfoButton.addActionListener(this);
124
125 collectionListPanel.add(collectionLabel);
126 collectionListPanel.add(collectionList);
127 collectionListPanel.add(collectionInfoButton);
128
129 searchTextFieldPanel = new JPanel();
130 searchTextFieldPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
131 searchTextFieldPanel.add(searchTextField);
132
133 searchButtonPanel = new JPanel();
134 searchButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
135 searchButtonPanel.add(searchButton);
136
137 queryFormulationPanel = new JPanel();
138 queryFormulationPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
139 queryFormulationPanel.setLayout(new BoxLayout(queryFormulationPanel, BoxLayout.Y_AXIS));
140 queryFormulationPanel.add(Box.createHorizontalStrut(100));
141 queryFormulationPanel.add(collectionListPanel);
142 queryFormulationPanel.add(searchTextFieldPanel);
143 queryFormulationPanel.add(searchButtonPanel);
144
145 resultsPanel = new JPanel();
146 resultsPanel.setLayout(new BorderLayout());
147 resultsPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
148 ResultModel resultListModel = csModel.getResultsModel();
149 resultsList = new JList(resultListModel);
150 resultsList.setFont(resultsFont);
151 resultsList.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
152 resultsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
153 scrollResultsPane = new JScrollPane(resultsList);
154 resultsPanel.add(scrollResultsPane, BorderLayout.CENTER);
155 csModel.addResult(Result.INITIAL_FAKE_RESULT);
156
157 dataPanel = new JPanel();
158 dataPanel.setLayout(new BorderLayout());
159 dataPanel.setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
160 documentPane = new JTextPane() { // to control scrolling for raw text docs
161 public boolean getScrollableTracksViewportWidth() {
162 if (getSize().width < getParent().getSize().width)
163 return true;
164 else return false;
165 }
166 public void setSize(Dimension d) {
167 if (d.width < getParent().getSize().width)
168 d.width = getParent().getSize().width;
169 super.setSize(d);
170 }
171 }; // end custom JTextPane
172
173 documentPane.setEditable(false);
174 documentPane.setFont(docFont);
175 documentPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
176 //documentPane.setPreferredSize(new Dimension(100, 100));
177
178 scrollDataPane = new JScrollPane(documentPane);
179 scrollDataPane.setPreferredSize(new Dimension(300, 300));
180 dataPanel.add(scrollDataPane, BorderLayout.CENTER);
181
182 parent.getRootPane().setDefaultButton(searchButton);
183
184 resultsList.addListSelectionListener(new ResultListSelectionHandler() );
185
186 add(Box.createHorizontalStrut(400));
187 add(queryFormulationPanel);
188 add(resultsPanel);
189 add(dataPanel);
190 searchTextField.requestFocus();
191 }
192
193 /** respond to the user pressing the Search button */
194
195 public void actionPerformed(ActionEvent e) {
196 if (e.getSource() == searchButton ) {
197 // cursor not set when RETURN pressed, only when button clicked ?
198 windowParent.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
199 String queryString = searchTextField.getText();
200 String collectionName = collectionList.getSelectedItem().toString();
201 System.err.println("Search started (" + collectionName + "): \"" + queryString + "\" ..." );
202 //send query to collection
203 NzdlQuery nzdlQuery = new NzdlQuery(queryString);
204 // return the first numResults that match
205 //query.setEndResults( 15 );
206 // "-1" means consider all the documents that match
207 nzdlQuery.setMaxDocs( -1 );
208 NzdlRequest request = new NzdlRequest( nzdlQuery );
209 NzdlResponse response = new NzdlResponse();
210
211 nzdl.service( collectionName, request, response );
212 NzdlResultSet results = response.getResultSet();
213 ArrayList docIDs = new ArrayList(results.getDocumentIDs());
214 // System.err.println("Size of docIDs = " + docIDs.size());
215 // paired collection of docIDs and Titles - but as Sets!
216 Map titleMetaData = nzdl.getMetaData( collectionName, docIDs, "Title" );
217 // created a paired collection of docIDs and Titles - as simple Strings!
218 HashMap titleMap = new HashMap();
219 for (ListIterator i = docIDs.listIterator(); i.hasNext(); ) {
220 String docID = (String) i.next();
221 //Set titleSet = (Set) titleMetaData.get( docID );
222 ArrayList titleList = new ArrayList((Set)titleMetaData.get(docID));
223 // use the first title as *the* title
224 titleMap.put(docID, titleList.get(0));
225 } // end for
226 // update the results list
227 csModel.clearResults();
228
229 if (docIDs.size() == 0 ) { // give some feedback for 0 results
230 csModel.addResult(Result.FAKE_RESULT);
231 documentPane.setStyledDocument(blankDoc);
232 }
233 else { // there are some results
234 Iterator docIDsIterator = docIDs.iterator();
235 while (docIDsIterator.hasNext()) {
236 String resultDocID = (String) docIDsIterator.next();
237 csModel.addResult(new Result((String)titleMap.get(resultDocID), resultDocID, collectionName ));
238 } //end while
239 if (NzdlPreferences.getInstance().getBoolean(Constants.DISPLAY_FIRST_DOC))
240 resultsList.setSelectedIndex(0); // should be under Preference control!
241 } // end else
242 titleMap.clear();
243 titleMetaData.clear();
244 docIDs.clear();
245 windowParent.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
246 } // end if
247 else {
248 if (e.getSource() == collectionInfoButton) {
249 CollectionInfoDialog cid = new CollectionInfoDialog(windowParent, collectionList.getSelectedItem().toString(), nzdl);
250 }
251 else {
252 System.err.println("unknown Action event in SearchPanel");
253 }
254 }
255 } //end actionPerformed
256
257 /** handles selection events in the results list so that when the
258 user selects a new title (say) the document contents is
259 automatically displayed in the main panel */
260
261 class ResultListSelectionHandler implements ListSelectionListener {
262
263 // private Result previousSelection = null;
264
265 public void valueChanged(ListSelectionEvent e) {
266 // get the result object from the SelectedIndex
267 // assume we only get here with events from resultsList
268 int docLength = 0;
269 if (e.getValueIsAdjusting()) { return; }
270 Result result = (Result) resultsList.getSelectedValue();
271 if ((result == Result.FAKE_RESULT) || (result == Result.INITIAL_FAKE_RESULT)) {
272 documentPane.setStyledDocument( blankDoc );
273 System.err.println("clicked on fake result");
274 }
275 else { // a real result representing a real document to deal with
276
277 windowParent.setCursor(WAIT_CURSOR);
278
279 deleteDocContents(); // for memory/garbage collection
280
281 StringReader sr = new StringReader(nzdl.getDocument(result.getCollectionName(), result.getDocID()));
282
283 if (NzdlPreferences.getInstance().getBoolean(Constants.RAW_TEXT))
284 // display the document as raw text
285 displayAsRawText(sr);
286 else {
287 // treat as HTML
288 displayAsHTML(sr);
289 htmlDoc.getImageData(nzdl, result.getCollectionName());
290 }
291
292 windowParent.setCursor(NORMAL_CURSOR);
293 }
294 } //end valueChanged
295 }//end ResultListSelectionHandler
296
297
298
299 private void deleteDocContents() {
300 //trash the old htmlDoc for memory/garbage collection
301 try {
302 if (htmlDoc != null)
303 htmlDoc.remove(0, htmlDoc.getLength());
304 if (defaultStyledDoc != null)
305 defaultStyledDoc.remove(0, defaultStyledDoc.getLength());
306 }
307 catch (Exception exception) {
308 throw new Error (exception.toString());
309 }
310 } // end deleteDocContents
311
312
313 public void switchToRawText() {
314 windowParent.setCursor(WAIT_CURSOR);
315 System.err.println("switching to raw text");
316 displayAsRawText(new StringReader(documentPane.getText()));
317 windowParent.setCursor(NORMAL_CURSOR);
318 }
319
320 public void displayAsRawText(StringReader sr) {
321 defaultStyledDoc = new DefaultStyledDocument();
322 documentPane.setEditorKit(styledEditorKit);
323 try {
324 styledEditorKit.read( sr, defaultStyledDoc, 0);
325 }
326 catch (Exception e) {
327 System.err.println(e);
328 }
329 documentPane.setDocument(defaultStyledDoc);
330 setCaretToStart(defaultStyledDoc.getLength());
331 } // end displayAsRawText
332
333
334 public void switchToHTML() {
335 windowParent.setCursor(WAIT_CURSOR);
336 System.err.println("switching to HTML");
337 displayAsHTML(new StringReader(documentPane.getText()));
338 windowParent.setCursor(NORMAL_CURSOR);
339 }
340
341 public void displayAsHTML(StringReader sr) {
342 htmlDoc = new GMLDocument();
343 documentPane.setEditorKit(htmlEditorKit);
344 try {
345 htmlEditorKit.read( sr, htmlDoc, 0);
346 }
347 catch (Exception e) {
348 System.err.println(e);
349 }
350 documentPane.setStyledDocument( htmlDoc );
351 setCaretToStart(htmlDoc.getLength());
352 } // end displayAsHTML
353
354
355 private void setCaretToStart(int docLength) {
356 // it seems as if they haven't considered
357 // documents with > MAX_INT num of chars ...
358 if (docLength > 0)
359 documentPane.setCaretPosition(1);
360 }
361
362
363 } //end SearchPanel
Note: See TracBrowser for help on using the repository browser.