1 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 ; 24 import java.awt.event.ActionListener ; 25 import java.awt.event.KeyEvent ; 26 import java.awt.event.KeyListener ; 27 import java.io.*; 28 29 38 public class GUIHandler implements KeyListener , ActionListener , Runnable { 39 40 43 private static JEditorPane text; 44 45 48 private static JTextField textBox; 49 50 53 private static JMenuItem mnuKey; 54 55 58 private static JMenuItem mnuMaxResults; 59 60 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"); 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(); Dimension wndSize = theKit.getScreenSize(); frame.setBounds(wndSize.width / 6, wndSize.height / 10, wndSize.width * 3 / 5, wndSize.height * 3 / 4); 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); ySpring = Spring.constant(0); wSpring = Spring.constant(frame.getBounds().width); hSpring = Spring.constant(30); textBoxConstraints.setWidth(wSpring); textBoxConstraints.setHeight(hSpring); 117 textBoxConstraints.setX(xSpring); textBoxConstraints.setY(ySpring); 119 120 SpringLayout.Constraints scrollConstraints = layout.getConstraints(scroll); 121 xSpring = Spring.constant(0); ySpring = Spring.constant(30); wSpring = Spring.constant(frame.getBounds().width); hSpring = Spring.constant(500); scrollConstraints.setWidth(wSpring); scrollConstraints.setHeight(hSpring); 127 scrollConstraints.setX(xSpring); scrollConstraints.setY(ySpring); 130 frame.setVisible(true); 131 } 132 133 139 protected static void showResults(String results) { 140 text.setText(results); 141 } 142 143 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 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 176 public void keyTyped(KeyEvent 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 e) { 191 } 192 193 public void keyReleased(KeyEvent s) { 194 } 195 196 201 public void actionPerformed(ActionEvent 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 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 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 e) { 239 e.printStackTrace(); 240 } 241 } 242 } 243 } | Popular Tags |