KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > amazon > search > GUIHandler


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package sample.amazon.search;
18
19 import org.apache.axis2.engine.AxisFault;
20
21 import javax.swing.*;
22 import java.awt.*;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.awt.event.KeyEvent JavaDoc;
26 import java.awt.event.KeyListener JavaDoc;
27 import java.io.*;
28
29 /**
30  * Build and desplay the GUI
31  * implements both KeyListner and ActionListner
32  * KeyListner is used to detect space or Enter key at textField
33  * ActionListner is used for menu cammands
34  * Thread is run to isolate the GUI from internal actions
35  *
36  * @auther Gayan Asanka (gayan@opensource.lk)
37  */

38 public class GUIHandler implements KeyListener JavaDoc, ActionListener JavaDoc, Runnable JavaDoc {
39
40     /**
41      * Results are desplayed here
42      */

43     private static JEditorPane text;
44
45     /**
46      * Query parameters typed here
47      */

48     private static JTextField textBox;
49
50     /**
51      * Menu command to set the key
52      */

53     private static JMenuItem mnuKey;
54
55     /**
56      * menu commant to set the maximum results per page
57      */

58     private static JMenuItem mnuMaxResults;
59
60     /**
61      * Build the GUI using awt and swing components
62      */

63     public void buildFrame() {
64         JFrame frame;
65         SpringLayout layout;
66         JMenuBar menuBar;
67         JMenu setMenu;
68         Spring hSpring,wSpring,xSpring,ySpring;
69
70         frame = new JFrame("Amazon Web Search");
71         layout = new SpringLayout();
72         Container pane = frame.getContentPane();
73         pane.setLayout(layout);
74
75         menuBar = new JMenuBar();
76         frame.setJMenuBar(menuBar);
77         setMenu = new JMenu("Set"); // Create Set menu
78
menuBar.add(setMenu);
79         setMenu.addActionListener(this);
80
81         mnuKey = new JMenuItem("Key");
82         mnuMaxResults = new JMenuItem("Results per Page");
83
84         setMenu.add(mnuKey);
85         setMenu.add(mnuMaxResults);
86
87         mnuKey.addActionListener(this);
88         mnuMaxResults.addActionListener(this);
89
90         Toolkit theKit = frame.getToolkit(); // Get the window toolkit
91
Dimension wndSize = theKit.getScreenSize(); // Get screen size
92
// Set the position to screen center and appropriate size
93
frame.setBounds(wndSize.width / 6, wndSize.height / 10, // Position
94
wndSize.width * 3 / 5, wndSize.height * 3 / 4); // Size
95
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
96
97         text = new JEditorPane();
98         text.setEditable(false);
99         text.setContentType("text/html");
100         text.addHyperlinkListener(new LinkFollower());
101
102         JScrollPane scroll = new JScrollPane(text, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
103                 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
104         pane.add(scroll);
105
106         textBox = new JTextField();
107         textBox.addKeyListener(this);
108         pane.add(textBox);
109
110         SpringLayout.Constraints textBoxConstraints = layout.getConstraints(textBox);
111         xSpring = Spring.constant(0); // Spring we値l use for X
112
ySpring = Spring.constant(0); // Spring we値l use for Y
113
wSpring = Spring.constant(frame.getBounds().width); // Spring we値l use for width
114
hSpring = Spring.constant(30); // Strut we値l use for height
115
textBoxConstraints.setWidth(wSpring); // Set component width constraint
116
textBoxConstraints.setHeight(hSpring);
117         textBoxConstraints.setX(xSpring); // Set the WEST edge constraint
118
textBoxConstraints.setY(ySpring);
119
120         SpringLayout.Constraints scrollConstraints = layout.getConstraints(scroll);
121         xSpring = Spring.constant(0); // Spring we値l use for X
122
ySpring = Spring.constant(30); // Spring we値l use for Y
123
wSpring = Spring.constant(frame.getBounds().width); // Spring we値l use for width
124
hSpring = Spring.constant(500); // Strut we値l use for height
125
scrollConstraints.setWidth(wSpring); // Set component width constraint
126
scrollConstraints.setHeight(hSpring);
127         scrollConstraints.setX(xSpring); // Set the WEST edge constraint
128
scrollConstraints.setY(ySpring); // Set the NORTH edge constraint
129

130         frame.setVisible(true);
131     }
132
133     /**
134      * method showResults
135      * desplay results by ClientCallbackHandler
136      *
137      * @param results
138      */

139     protected static void showResults(String JavaDoc results) {
140         text.setText(results);
141     }
142
143     /**
144      * method setKey
145      * Get the key from user via an inputDialog and
146      * store it in the properties file
147      */

148     protected void setKey() {
149         AsynchronousClient.amazonkey = JOptionPane.showInputDialog(null, "Enter the license Key");
150         if (AsynchronousClient.amazonkey == null) {
151             setKey();
152         }
153         OutputStream propOut;
154         try {
155             String JavaDoc workingDir = System.getProperty("user.dir");
156             File propertyFile = new File(workingDir + File.separator + "samples" + File.separator +
157                     "/key.properties");
158             propOut = new FileOutputStream(propertyFile);
159
160             AsynchronousClient.prop.setProperty("amazonKey", AsynchronousClient.amazonkey);
161             AsynchronousClient.prop.store(propOut, "License Key");
162         } catch (FileNotFoundException e) {
163             e.printStackTrace();
164         } catch (IOException e) {
165             e.printStackTrace();
166         }
167     }
168
169     /**
170      * method keyTyped
171      * fires when user typing in TextField textBox
172      * act when detects space and Enter key only
173      *
174      * @param e
175      */

176     public void keyTyped(KeyEvent JavaDoc e) {
177         System.out.println("inside");
178         int event = e.getKeyChar();
179
180         if (event == KeyEvent.VK_SPACE || event == KeyEvent.VK_ENTER) {
181             AsynchronousClient.search = textBox.getText().trim();
182             AsynchronousClient.search.trim();
183             System.out.println(textBox.getText());
184             if (!AsynchronousClient.prevSearch.equals(AsynchronousClient.search)) {
185                 AsynchronousClient.doSearch = true;
186             }
187         }
188     }
189
190     public void keyPressed(KeyEvent JavaDoc e) {
191     }
192
193     public void keyReleased(KeyEvent JavaDoc s) {
194     }
195
196     /**
197      * method actionPerformed
198      * Detects menu click events
199      * @param e
200      */

201     public void actionPerformed(ActionEvent JavaDoc e) {
202         if (e.getSource() == mnuMaxResults) {
203             do {
204                 System.out.println("come to the place");
205                 AsynchronousClient.maxResults =
206                         JOptionPane.showInputDialog(null,
207                           "Enter the number of maximum results per page (Maximum allowed is 1000)");
208                 //JOptionPane.get
209

210             } while (Integer.parseInt(AsynchronousClient.maxResults) > 1000 ||
211                     Integer.parseInt(AsynchronousClient.maxResults) < 0);
212         }
213         if (e.getSource() == mnuKey) {
214             setKey();
215         }
216     }
217
218     /**
219      * method run
220      * check the flag doSearch
221      * if it's set, call sendMsg method
222      */

223     public void run() {
224         while (true) {
225             AsynchronousClient.search.toString().trim();
226             if (AsynchronousClient.doSearch == true) {
227
228                 try {
229                     AsynchronousClient.doSearch = false;
230                     AsynchronousClient.sendMsg();
231
232                 } catch (AxisFault axisFault) {
233                     axisFault.printStackTrace();
234                 }
235             }
236             try {
237                 Thread.sleep(50);
238             } catch (InterruptedException JavaDoc e) {
239                 e.printStackTrace();
240             }
241         }
242     }
243 }
Popular Tags