source: other-projects/gs3-webservices-java-client/trunk/src/GS3DemoClient/org/greenstone/gs3client/QueryForm.java@ 26174

Last change on this file since 26174 was 15732, checked in by ak19, 16 years ago

Made the searchButton of the QueryForm be the default button so that it would respond to Enter being pressed in text fields

File size: 9.5 KB
Line 
1/**
2 *#########################################################################
3 * QueryForm.java - part of the demo-client for Greenstone 3, of the
4 * Greenstone digital library suite from the New Zealand Digital Library
5 * Project at the * University of Waikato, New Zealand.
6 * <BR><BR>
7 * Copyright (C) 2008 New Zealand Digital Library Project
8 * <BR><BR>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 * <BR><BR>
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *########################################################################
19 */
20
21package org.greenstone.gs3client;
22
23import java.util.HashMap;
24import java.util.Vector;
25
26import org.greenstone.gs3client.data.QueryFormData;
27import org.greenstone.gsdl3.util.GSXML;
28
29import org.w3c.dom.Element;
30import org.w3c.dom.NodeList;
31import org.w3c.dom.Node;
32
33import java.awt.Cursor;
34import java.awt.BorderLayout;
35import javax.swing.BoxLayout;
36import javax.swing.BorderFactory;
37import javax.swing.JButton;
38import javax.swing.JPanel;
39import javax.swing.JScrollPane;
40import javax.swing.border.TitledBorder;
41
42import java.awt.event.KeyListener;
43import java.awt.event.KeyEvent;
44import java.awt.event.ActionListener;
45import java.awt.event.ActionEvent;
46
47/**
48 * Panel that represents a Query Form based on GS3 describe response XML
49 * messages sent by a Query Service. An inner panel will contain all the
50 * actual form controls. The form can be reset to empty which will clear
51 * its contents. Its contents can be reset to display controls appropriate
52 * to new Query Service describe response XML messages.
53 * @author ak19
54*/
55public class QueryForm extends JPanel
56 implements ActionListener, KeyListener, ColourCombo.ColourChangeable
57{
58 /** Handle to the running instance of the GS3JavaClient in order to have
59 * access to its methods */
60 protected GS3JavaClient client;
61 /** Panel to contain all the form controls specific to the chosen Query
62 * Service. */
63 protected JPanel paramsPanel = null;
64 /** Search button that will cause the search to be executed when pressed */
65 protected JButton searchButton = null;
66 /** Query parameters specified by the query services */
67 protected QueryFormData[] paramTags = null;
68
69 /** Constructor that instantiates this Form's internal GUI items.
70 * @param client - handle to the running instance of the GS3JavaClient */
71 public QueryForm(GS3JavaClient client) {
72 super(new BorderLayout());
73 this.client = client;
74
75 // Create the search button and register listeners for button pressed
76 // and Enter key pressed events on it.
77 searchButton = new JButton();
78 searchButton.addActionListener(this);
79 searchButton.addKeyListener(this);
80
81 paramsPanel = new JPanel();
82 paramsPanel.setLayout(new BoxLayout(paramsPanel, BoxLayout.Y_AXIS));
83 }
84
85 /** Call this method to empty all the form controls. This Query Form
86 * component can then be reused. */
87 public void clear() {
88 // empty all controls
89
90 paramsPanel.removeAll();
91 paramTags = null; //get rid of the Params data for the query
92
93 this.removeAll();
94 this.validate();
95 this.setBorder(null);
96 this.repaint();
97 }
98
99 /** Changes the colour of the query form and its controls to the
100 * current colours set in class ColourCombo. Specified by
101 * the ColourCombo.ColourChangeable interface. */
102 public void changeUIColour() {
103 ColourCombo.changeColor(this);
104 ColourCombo.changeColor(this.paramsPanel);
105 }
106
107 /** Called when this QueryForm's search button is pressed: the data
108 * entered/controls settings specified by the user are passed to the
109 * digital library's Query Service in order to perform the search. */
110 public void actionPerformed(ActionEvent e) {
111 this.searchButtonPressed();
112 }
113
114 /** Defined by KeyListener interface. Does nothing. */
115 public void keyPressed(KeyEvent e) {}
116 /** Defined by KeyListener interface. Does nothing. */
117 public void keyTyped(KeyEvent e) {}
118
119 /** Called when the searchButton was in focus and a key was pressed. If
120 * this key was the Enter button, then the search is performed: the data
121 * entered/controls settings specified by the user are passed to the
122 * digital library's Query Service in order to perform the search. */
123 public void keyReleased(KeyEvent e) {
124 if(e.getKeyCode() == KeyEvent.VK_ENTER) {
125 this.searchButtonPressed();
126 }
127 }
128
129 /** Called when the searchButton was clicked or when it was in focus
130 * and the enter key was pressed. The search is performed by passing the
131 * data entered/controls settings specified by the user to the digital
132 * library's Query Service in order to perform the search. */
133 protected void searchButtonPressed() {
134 // (1) Search button pressed: obtain all the user-input from the
135 // query form. Use these to create a HashMap of name-val pairs
136 // representing the parameters for the query.
137 HashMap nameValParamsMap = new HashMap();
138 for(int i = 0; i < paramTags.length; i++) {
139 QueryFormControl formControl = (QueryFormControl)paramTags[i];
140 formControl.setSelectedValue(nameValParamsMap);
141 }
142 // (2) Let the main java-client class handle the rest:
143 this.client.performSearch(nameValParamsMap);
144 }
145
146 /**
147 * Sets up the the query form for display (queryPanel) given the XML of
148 * the response-message returned by a "describe" request sent to a
149 * Query Service. That is, generates the query form as specified
150 * by the given response XML for a Query describe.
151 * @param el - a serviceResponseXML for a describe request that specifies
152 * how the paramsPanel should look.
153 */
154 public void formFromQueryServiceDescribe(Element el) {
155 this.clear();
156
157 // Display the form controls associated with the Query Servie
158
159 // Get the service element <service></service> embedded inside message:
160 // <message><response><service></service></message></response>
161 // service = (Element)el.getElementsByTagName("service").item(0);
162 Element service
163 = (Element)el.getElementsByTagName(GSXML.SERVICE_ELEM).item(0);
164 if(service == null) // shouldn't be the case
165 return; // do nothing
166 Element paramList = null;
167
168 // Some heading information for each Query service display form
169 String nameVal = "", description = "", submit ="";
170
171 // Get all the children. For *services*, these can only be <paramList>
172 // or <displayItemList> elements, as per the manual p.37:
173 // "Subset options for the request [sent to a service] include
174 // paramList and displayItemList."
175 NodeList serviceInfo = service.getChildNodes();
176 for(int i = 0; i < serviceInfo.getLength(); i++){
177 Node n = serviceInfo.item(i);
178 // Skip non-element nodes
179 if(n.getNodeType() != Node.ELEMENT_NODE)
180 continue;
181
182 Element e = (Element)n;
183
184 // Process <displayItem> elements that are *direct* children
185 // of <service>
186 if(e.getTagName().equals(GSXML.DISPLAY_TEXT_ELEM)) {
187 // Retrieve values for attributes where name="name",
188 // name="description", name="submit"
189 if(e.hasAttributes()) {
190 // get value of attribute where name=name:
191 String attribute = e.getAttribute(GSXML.DISPLAY_TEXT_NAME);
192 // now based on the attribute value set the appropriate var:
193 if(attribute.equals(GSXML.DISPLAY_TEXT_NAME))
194 // get text node of element and get its text
195 nameVal = e.getFirstChild().getNodeValue();
196 // name=description
197 else if(attribute.equals(GSXML.DISPLAY_TEXT_DESCRIPTION))
198 description = e.getFirstChild().getNodeValue();
199 else if(attribute.equals(GSXML.DISPLAY_TEXT_SUBMIT))
200 submit = e.getFirstChild().getNodeValue();// text
201 }
202 }
203 // <paramList> element which contains param elements as children
204 else if(e.getTagName().equals(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER))
205 {
206 paramList = e;
207 } // else skip the element
208 }
209
210 if(paramList == null) // shouldn't be the case
211 return;
212 // Now process the parameters contained in the <paramList> element
213
214 if(paramList != null && paramList.hasChildNodes()) {
215 NodeList nl = paramList.getChildNodes();
216 // NodeList nl = paramList.getElementsByTagName(GSXML.PARAM_ELEM);
217 if(nl != null && nl.getLength() > 0) {
218 Vector v = new Vector(nl.getLength());
219 for(int i = 0; i < nl.getLength(); i++) {
220 Node n = nl.item(i);
221 // For Element Nodes, getNodeName() returns the tag name,
222 // and it *never* returns null for *any* Node type
223 if(n.getNodeName().equals(GSXML.PARAM_ELEM)) {
224 v.add((Element)n);
225 }
226 }
227 paramTags = new QueryFormData[v.size()];
228 for(int i = 0; i < paramTags.length; i++) {
229 // we've obtained only elements of <param> type (no
230 // <option>s among them), so:
231 paramTags[i] =
232 new QueryFormControl((Element)v.get(i), paramsPanel);
233 }
234 v.clear();
235 v = null;
236 }
237 }
238
239 TitledBorder title = BorderFactory.createTitledBorder(
240 nameVal + " - " + description);
241 this.setBorder(title);
242 // add scrollpane around query form just in case the screen
243 // is too small to display all the form controls at once
244 this.add(new JScrollPane(paramsPanel), BorderLayout.CENTER);
245 this.add(searchButton, BorderLayout.SOUTH);
246 searchButton.setText(submit);
247
248 // Setting the search button as the default button so that it may respond
249 // to Enter presses in textfields.
250 // See http://java.sun.com/docs/books/tutorial/uiswing/components/button.html
251 this.getRootPane().setDefaultButton(searchButton);
252
253 this.validate();
254 }
255}
Note: See TracBrowser for help on using the repository browser.