BowlerKernel
UsbConnectionPanel.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.io.UnsupportedEncodingException;
20 import java.util.List;
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.SwingUtilities;
27 import javax.usb.UsbDevice;
28 import javax.usb.UsbDisconnectedException;
29 import javax.usb.UsbException;
30 
31 import org.usb4java.Context;
32 import org.usb4java.Device;
33 import org.usb4java.DeviceDescriptor;
34 import org.usb4java.HotplugCallback;
35 import org.usb4java.HotplugCallbackHandle;
36 import org.usb4java.LibUsb;
37 import org.usb4java.LibUsbException;
38 
39 import net.miginfocom.swing.MigLayout;
40 
41 import com.neuronrobotics.sdk.common.BowlerAbstractConnection;
42 import com.neuronrobotics.sdk.common.Log;
43 import com.neuronrobotics.sdk.javaxusb.IUsbDeviceEventListener;
44 import com.neuronrobotics.sdk.javaxusb.UsbCDCSerialConnection;
45 import com.neuronrobotics.sdk.util.ThreadUtil;
46 
47 // TODO: Auto-generated Javadoc
52 
54  private static final long serialVersionUID = 1L;
55 
56 
58  private JComboBox connectionCbo = null;
59 
61  private JButton refresh;
62 
65 
72  super("USB", ConnectionImageIconFactory.getIcon("images/usb-icon.png"),connectionDialog);
73 
74 
75  connectionCbo = new JComboBox();
76  connectionCbo.setEditable(true);
77 
78  refresh = new JButton("Refresh");
79  refresh.addActionListener(new ActionListener() {
80 
81  public void actionPerformed(ActionEvent arg0) {
82  refresh();
83  }
84  });
85 
86  setLayout(new MigLayout("", // Layout Constraints
87  "[right][left]", // Column constraints with default align
88  "[center][center]" // Row constraints with default align
89  ));
90 
91  add(new JLabel("Connection:"), "cell 0 0");
92  add(connectionCbo);
93  add(refresh);
94 
95  refresh();
96 
98 
99 
100  }
101 
102 
103  /* (non-Javadoc)
104  * @see com.neuronrobotics.sdk.ui.AbstractConnectionPanel#getConnection()
105  */
107 
108  String port =connectionCbo.getSelectedItem().toString();
110  Log.info("Using port:"+port+"\n");
111 
112 
113 
114  setVisible(false);
115 
116  return connection;
117  }
118 
119 
120  /* (non-Javadoc)
121  * @see com.neuronrobotics.sdk.ui.AbstractConnectionPanel#refresh()
122  */
123  public void refresh() {
124  //System.err.println("Refreshing USB");
125  connectionCbo.removeAllItems();
126 
127  List<UsbDevice> prts=null;
128 
129  try {
131  } catch ( UsbException e1) {
132  e1.printStackTrace();
133  throw new RuntimeException(e1);
134  } catch (UnsupportedEncodingException e) {
135  // TODO Auto-generated catch block
136  e.printStackTrace();
137  } catch (UsbDisconnectedException e) {
138  // TODO Auto-generated catch block
139  e.printStackTrace();
140  } catch (SecurityException e) {
141  // TODO Auto-generated catch block
142  e.printStackTrace();
143  }
144  for(int i=0;i<prts.size();i++) {
145  try {
146  String s = UsbCDCSerialConnection.getUniqueID(prts.get(i));
147  connectionCbo.addItem(s);
148  } catch (Exception e) {
149  e.printStackTrace();
150  throw new RuntimeException(e);
151  }
152  }
153 
154  getConnectionDialog().pack();
155  }
156 
157  /* (non-Javadoc)
158  * @see com.neuronrobotics.sdk.javaxusb.IUsbDeviceEventListener#onDeviceEvent(javax.usb.UsbDevice)
159  */
160  @Override
161  public void onDeviceEvent(UsbDevice device) {
162  SwingUtilities.invokeLater(new Runnable() {
163  public void run() {
164  ThreadUtil.wait(500);
165  refresh();
166  }
167  });
168  }
169 
170 
171 }
static void info(String message)
Definition: Log.java:110
static void addUsbDeviceEventListener(IUsbDeviceEventListener l)
UsbConnectionPanel(ConnectionDialog connectionDialog)