BowlerKernel
UDPConnectionPanel.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 java.awt.event.ActionEvent;
18 import java.awt.event.ActionListener;
19 import java.net.InetAddress;
20 import java.util.ArrayList;
21 
22 import javax.swing.JButton;
23 import javax.swing.JComboBox;
24 import javax.swing.JLabel;
25 import javax.swing.JOptionPane;
26 import javax.swing.JTextField;
27 
28 import net.miginfocom.swing.MigLayout;
29 
30 import com.neuronrobotics.sdk.common.BowlerAbstractConnection;
31 import com.neuronrobotics.sdk.network.UDPBowlerConnection;
32 import com.neuronrobotics.sdk.util.IMonitorable;
33 import com.neuronrobotics.sdk.util.IProgressMonitorListener;
34 import com.neuronrobotics.sdk.util.ProcessMonitor;
35 
36 // TODO: Auto-generated Javadoc
41 
43  private static final long serialVersionUID = 1L;
44 
46  private static final int defaultPortNum = 1865;
47 
49  private JComboBox connectionCbo = null;
50 
52  private JButton refresh;
53 
55  private JTextField port = new JTextField(8);
56 
58  UDPBowlerConnection clnt=null;
59 
60 
67  super("UDP",ConnectionImageIconFactory.getIcon("images/ethernet-icon.png"),connectionDialog);
68 
69 
70  port.setText(new Integer(defaultPortNum).toString());
71 
72  setLayout(new MigLayout("", // Layout Constraints
73  "[right][left]", // Column constraints with default align
74  "[center][center]" // Row constraints with default align
75  ));
76 
77  add(new JLabel("Server:"), "cell 0 0");
78  connectionCbo = new JComboBox();
79  connectionCbo.setEditable(true);
80  connectionCbo.addItem("none");
81  add(connectionCbo);
82  refresh = new JButton("Refresh");
83  refresh.addActionListener(new ActionListener() {
84 
85  public void actionPerformed(ActionEvent arg0) {
86  refresh();
87  }
88  });
89  add(refresh);
90 
91  add(new JLabel("Port:"), "cell 0 1");
92  add(port, "cell 1 1");
93 
94  //refresh();
95  }
96 
97 
98  /* (non-Javadoc)
99  * @see com.neuronrobotics.sdk.ui.AbstractConnectionPanel#getConnection()
100  */
102  try {
103  int baud = Integer.parseInt(port.getText());
104  if(baud < 0) {
105  throw new NumberFormatException();
106  }
107  String address =connectionCbo.getSelectedItem().toString().trim();
108  clnt.setAddress(address);
109  setVisible(false);
110  return clnt;
111  } catch(NumberFormatException e) {
112  JOptionPane.showMessageDialog(null, "Invalid port given.", "Invalid port", JOptionPane.ERROR_MESSAGE);
113  } catch(RuntimeException e) {
114  JOptionPane.showMessageDialog(null, "Invalid address given.", "Invalid address", JOptionPane.ERROR_MESSAGE);
115  } finally {
116  setVisible(false);
117  }
118  return null;
119  }
120 
121 
122  /* (non-Javadoc)
123  * @see com.neuronrobotics.sdk.ui.AbstractConnectionPanel#refresh()
124  */
125  public void refresh() {
126  connectionCbo.removeAllItems();
127  connectionCbo.addItem("Searching...");
128  connectionCbo.setEnabled(false);
129 
130  port.setEnabled(false);
131 
132  refresh.setEnabled(false);
133 
135 
136  ProcessMonitor pm = new ProcessMonitor(nsp);
138 
139 
140  public void onUpdate(double value) {
141  // TODO Auto-generated method stub
142 
143  }
144 
145 
146  public void onComplete() {
147  connectionCbo.setEnabled(true);
148  port.setEnabled(true);
149  refresh.setEnabled(true);
150  }
151  });
152 
153  pm.start();
154  nsp.start();
155  getConnectionDialog().pack();
156  }
157 
161  private class NetworkSearchProcess extends Thread implements IMonitorable {
162 
164  private boolean isRunning = false;
165 
166  /* (non-Javadoc)
167  * @see java.lang.Thread#run()
168  */
169  public void run() {
170  setName("Bowler Platform UDP searcher");
171  isRunning = true;
172  //System.out.println("Searching for UDP devices, please wait...");
173  int prt;
174  try {
175  prt=new Integer(port.getText());
176  }catch (NumberFormatException e) {
177  prt=defaultPortNum;
178  port.setText(new Integer(defaultPortNum).toString());
179  }
180  clnt=new UDPBowlerConnection(prt);
181  ArrayList<InetAddress> addrs = clnt.getAllAddresses();
182 // if (addrs.size()>0)
183 // System.out.println("Bowler servers: "+addrs);
184  connectionCbo.removeAllItems();
185  for (InetAddress i:addrs) {
186  connectionCbo.addItem(i.getHostAddress());
187  }
188  if(addrs.size() ==0 )
189  connectionCbo.addItem("No Servers Found");
190 
191  isRunning = false;
192  }
193 
194  /* (non-Javadoc)
195  * @see com.neuronrobotics.sdk.util.IMonitorable#getPercentage()
196  */
197  public double getPercentage() {
198  return 0;
199  }
200 
201  /* (non-Javadoc)
202  * @see com.neuronrobotics.sdk.util.IMonitorable#isComplete()
203  */
204  public boolean isComplete() {
205  return !isRunning;
206  }
207  }
208 }
UDPConnectionPanel(ConnectionDialog connectionDialog)
void addProcessMonitorListener(IProgressMonitorListener listener)