Go to the documentation of this file.00001 package instruction.gui.tab;
00002
00003 import java.awt.GridBagLayout;
00004 import javax.swing.JPanel;
00005 import javax.swing.JTextField;
00006 import java.awt.GridBagConstraints;
00007 import javax.swing.JList;
00008 import javax.swing.JButton;
00009 import java.awt.Insets;
00010 import java.io.BufferedReader;
00011 import java.io.IOException;
00012 import java.io.InputStream;
00013 import java.io.InputStreamReader;
00014 import java.io.Reader;
00015 import java.net.MalformedURLException;
00016 import java.net.URL;
00017 import java.util.ArrayList;
00018 import java.util.List;
00019
00020 import javax.swing.JLabel;
00021 import javax.swing.JRadioButton;
00022
00023 import edu.stanford.nlp.web.HTMLParser;
00024
00025 public class OnlineSearchPanel extends JPanel {
00026
00027 public static String TITLE = "Online Search";
00028 private static final long serialVersionUID = 1L;
00029 private JTextField commandLine = null;
00030 private JList howtos = null;
00031 private JButton jButton = null;
00032 private JPanel jPanel = null;
00033 private JLabel jLabel = null;
00034 private JLabel jLabel1 = null;
00035 private JRadioButton jRadioButton = null;
00036 private JRadioButton jRadioButton1 = null;
00037
00041 public OnlineSearchPanel () {
00042
00043 super();
00044 initialize();
00045 }
00046
00052 private void initialize() {
00053
00054 GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
00055 gridBagConstraints4.gridx = 1;
00056 gridBagConstraints4.fill = GridBagConstraints.HORIZONTAL;
00057 gridBagConstraints4.gridy = 1;
00058 GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
00059 gridBagConstraints31.gridx = 1;
00060 gridBagConstraints31.fill = GridBagConstraints.HORIZONTAL;
00061 gridBagConstraints31.gridy = 0;
00062 GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
00063 gridBagConstraints21.gridx = 0;
00064 gridBagConstraints21.anchor = GridBagConstraints.WEST;
00065 gridBagConstraints21.insets = new Insets( 0, 10, 0, 0 );
00066 gridBagConstraints21.gridwidth = 1;
00067 gridBagConstraints21.gridy = 1;
00068 jLabel1 = new JLabel();
00069 jLabel1.setText( "for executing the task:" );
00070 GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
00071 gridBagConstraints11.gridx = 0;
00072 gridBagConstraints11.insets = new Insets( 10, 10, 0, 10 );
00073 gridBagConstraints11.gridwidth = 1;
00074 gridBagConstraints11.anchor = GridBagConstraints.WEST;
00075 gridBagConstraints11.gridy = 0;
00076 jLabel = new JLabel();
00077 jLabel.setText( "Type in a task for the robot. The system will automatically look up an Instruction" );
00078 GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
00079 gridBagConstraints3.gridx = 2;
00080 gridBagConstraints3.gridy = 3;
00081 GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
00082 gridBagConstraints2.gridx = 1;
00083 gridBagConstraints2.insets = new Insets( 10, 10, 10, 10 );
00084 gridBagConstraints2.gridy = 2;
00085 GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
00086 gridBagConstraints1.fill = GridBagConstraints.BOTH;
00087 gridBagConstraints1.gridy = 3;
00088 gridBagConstraints1.weightx = 1.0;
00089 gridBagConstraints1.weighty = 1.0;
00090 gridBagConstraints1.gridheight = 1;
00091 gridBagConstraints1.gridwidth = 2;
00092 gridBagConstraints1.ipadx = 50;
00093 gridBagConstraints1.ipady = 50;
00094 gridBagConstraints1.anchor = GridBagConstraints.CENTER;
00095 gridBagConstraints1.insets = new Insets( 10, 10, 10, 10 );
00096 gridBagConstraints1.gridx = 0;
00097 GridBagConstraints gridBagConstraints = new GridBagConstraints();
00098 gridBagConstraints.fill = GridBagConstraints.BOTH;
00099 gridBagConstraints.gridy = 2;
00100 gridBagConstraints.weightx = 1.0;
00101 gridBagConstraints.insets = new Insets( 10, 10, 10, 10 );
00102 gridBagConstraints.gridx = 0;
00103 this.setSize( 569, 319 );
00104 this.setLayout( new GridBagLayout() );
00105 this.add( getJTextField(), gridBagConstraints );
00106 this.add( getJList(), gridBagConstraints1 );
00107 this.add( getJButton(), gridBagConstraints2 );
00108 this.add( getJPanel(), gridBagConstraints3 );
00109 this.add( jLabel, gridBagConstraints11 );
00110 this.add( jLabel1, gridBagConstraints21 );
00111 this.add( getJRadioButton(), gridBagConstraints31 );
00112 this.add( getJRadioButton1(), gridBagConstraints4 );
00113 }
00114
00120 private JTextField getJTextField() {
00121
00122 if ( commandLine == null ) {
00123 commandLine = new JTextField();
00124 commandLine.setText( "set a table" );
00125 }
00126 return commandLine;
00127 }
00128
00134 private JList getJList() {
00135
00136 if ( howtos == null ) {
00137 howtos = new JList();
00138 }
00139 return howtos;
00140 }
00141
00147 private JButton getJButton() {
00148
00149 if ( jButton == null ) {
00150 jButton = new JButton();
00151 jButton.setText( "Search!" );
00152 jButton.addActionListener( new java.awt.event.ActionListener() {
00153 public void actionPerformed( java.awt.event.ActionEvent e ) {
00154
00155 URL url;
00156 try {
00157 url = new URL( "http://www.ehow.com/search.aspx?s=" + commandLine.getText().replaceAll( " ", "+" )
00158 + "&Options=0" );
00159 InputStream is = url.openStream();
00160 Reader reader = new BufferedReader( new InputStreamReader( is ) );
00161 StringBuilder answer = new StringBuilder();
00162 char[] buf = new char[1024];
00163 int read;
00164 while ( ( read = reader.read( buf ) ) > 0 ) {
00165 answer.append( buf, 0, read );
00166 }
00167
00168 HTMLParser parser = new HTMLParser();
00169
00170
00171 List<String> inst = new ArrayList<String>();
00172 String[] tokens = parser.parse( answer.toString() ).split( "\\|" );
00173 for (int i = 0; i < tokens.length; i++) {
00174 System.out.println(tokens[i]);
00175 int start = -1;
00176 int end = -1;
00177 start = tokens[i].indexOf( "How to" );
00178 if (start == -1)
00179 continue;
00180 else {
00181 end = tokens[i].indexOf( ".", start + 1 );
00182 if (end == -1)
00183 continue;
00184 inst.add( tokens[i].substring( start, end ) );
00185 }
00186
00187 }
00188 howtos.setListData( inst.toArray(new String[0]) );
00189
00190 }
00191 catch ( MalformedURLException e1 ) {
00192
00193 e1.printStackTrace();
00194 }
00195 catch ( IOException e1 ) {
00196
00197 e1.printStackTrace();
00198 }
00199
00200
00201 }
00202 } );
00203 }
00204 return jButton;
00205 }
00206
00212 private JPanel getJPanel() {
00213
00214 if ( jPanel == null ) {
00215 jPanel = new JPanel();
00216 jPanel.setLayout( new GridBagLayout() );
00217 jPanel.setSize( 50, 0 );
00218 }
00219 return jPanel;
00220 }
00221
00227 private JRadioButton getJRadioButton() {
00228
00229 if ( jRadioButton == null ) {
00230 jRadioButton = new JRadioButton();
00231 jRadioButton.setText( "ehow.com" );
00232 }
00233 return jRadioButton;
00234 }
00235
00241 private JRadioButton getJRadioButton1() {
00242
00243 if ( jRadioButton1 == null ) {
00244 jRadioButton1 = new JRadioButton();
00245 jRadioButton1.setText( "wikihow.com" );
00246 }
00247 return jRadioButton1;
00248 }
00249
00250 }