BowlerKernel
TCPConnectionPanel.java
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright 2010 Neuron Robotics, LLC
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  ******************************************************************************/
15 package com.neuronrobotics.sdk.ui;
16 
17 import javax.swing.JComboBox;
18 import javax.swing.JLabel;
19 import javax.swing.JOptionPane;
20 import javax.swing.JTextField;
21 
22 import net.miginfocom.swing.MigLayout;
23 
24 import com.neuronrobotics.sdk.common.BowlerAbstractConnection;
25 import com.neuronrobotics.sdk.common.Log;
26 import com.neuronrobotics.sdk.network.BowlerTCPClient;
27 
28 
29 // TODO: Auto-generated Javadoc
34 
36  private static final long serialVersionUID = 1L;
37 
39  private static int defaultPortNum = 1866;
40 
42  private static String defaultServer = "localhost";
43 
45  private JComboBox connectionCbo = null;
46 
48  private JTextField port = new JTextField(8);
49 
51  BowlerTCPClient clnt=null;
52 
53 
60  super("TCP", ConnectionImageIconFactory.getIcon("images/ethernet-icon.png"),connectionDialog);
61 
62 
63  port.setText(new Integer(defaultPortNum).toString());
64 
65  setLayout(new MigLayout("", // Layout Constraints
66  "[right][left]", // Column constraints with default align
67  "[center][center]" // Row constraints with default align
68  ));
69 
70  add(new JLabel("Server:"), "cell 0 0");
71  connectionCbo = new JComboBox();
72  connectionCbo.setEditable(true);
73 
74 // Socket s;
75 // try {
76 // s = new Socket("google.com", 80);
77 // connectionCbo.addItem(s.getLocalAddress().getHostAddress());
78 // //System.out.println(s.getLocalAddress().getHostAddress());
79 // s.close();
80 // } catch (UnknownHostException e) {
81 // // TODO Auto-generated catch block
82 // //e.printStackTrace();
83 // } catch (IOException e) {
84 // // TODO Auto-generated catch block
85 // //e.printStackTrace();
86 // }
87 
88  add(connectionCbo, "cell 1 0");
89 
90  add(new JLabel("Port:"), "cell 0 1");
91  add(port, "cell 1 1");
92 
93  }
94 
100  public static void setDefaultServer(String server){
101  defaultServer = server;
102  }
103 
104  /* (non-Javadoc)
105  * @see com.neuronrobotics.sdk.ui.AbstractConnectionPanel#getConnection()
106  */
107  @Override
109  if(clnt == null){
110  try {
111  int thePort = Integer.parseInt(port.getText());
112  if(thePort < 0) {
113  throw new NumberFormatException();
114  }
115  String address =connectionCbo.getSelectedItem().toString();
116  Log.info("Connecting on: "+address+":"+thePort);
117  clnt = new BowlerTCPClient(address,thePort);
118  setVisible(false);
119  return clnt;
120  } catch(NumberFormatException e) {
121  JOptionPane.showMessageDialog(null, "Invalid port given.", "Invalid port", JOptionPane.ERROR_MESSAGE);
122  } catch(RuntimeException e) {
123  e.printStackTrace();
124  JOptionPane.showMessageDialog(null, "Invalid address given.", "Invalid address", JOptionPane.ERROR_MESSAGE);
125  } catch (Exception e) {
126  e.printStackTrace();
127  JOptionPane.showMessageDialog(null, "Invalid address given.", "Invalid address", JOptionPane.ERROR_MESSAGE);
128  } finally {
129  setVisible(false);
130  }
131  return null;
132  }
133  return clnt;
134  }
135 
136  /* (non-Javadoc)
137  * @see com.neuronrobotics.sdk.ui.AbstractConnectionPanel#refresh()
138  */
139  @Override
140  public void refresh() {
141  getConnectionDialog().pack();
142  }
143 }
static void info(String message)
Definition: Log.java:110
TCPConnectionPanel(ConnectionDialog connectionDialog)