BowlerKernel
ConnectionDialog.java
Go to the documentation of this file.
1 package com.neuronrobotics.sdk.ui;
2 
3 import gnu.io.NativeResource;
4 
5 import java.awt.GraphicsEnvironment;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.awt.event.WindowAdapter;
9 import java.awt.event.WindowEvent;
10 
11 import javax.swing.JButton;
12 import javax.swing.JDialog;
13 import javax.swing.JOptionPane;
14 import javax.swing.JPanel;
15 import javax.swing.JTabbedPane;
16 import javax.swing.LookAndFeel;
17 
18 import net.miginfocom.swing.MigLayout;
19 
20 import com.neuronrobotics.sdk.common.BowlerAbstractConnection;
21 import com.neuronrobotics.sdk.common.BowlerAbstractDevice;
22 import com.neuronrobotics.sdk.common.ConfigManager;
23 import com.neuronrobotics.sdk.common.Log;
24 import com.neuronrobotics.sdk.dyio.DyIOCommunicationException;
25 import com.neuronrobotics.sdk.serial.SerialConnection;
26 import com.neuronrobotics.sdk.util.OsInfoUtil;
27 
28 // TODO: Auto-generated Javadoc
32 public class ConnectionDialog extends JDialog {
33 
35  private static final long serialVersionUID = 1L;
36 
38  private SerialConnection connection = null;
39 
41  private boolean isCancled = true;
42 
44  private JPanel panel;
45 
47  private JButton connectBtn;
48 
50  private JButton refresh;
51 
53  private JButton cancelBtn;
54 
56  private JTabbedPane connectionPanels;
57 
59  private LookAndFeel laf;
60 
64  public ConnectionDialog() {
65  setIconImage( ConnectionImageIconFactory.getIcon("images/hat.png").getImage());
66 
67 
68  setModal(true);
69 
70  connectionPanels = new JTabbedPane();
71 
73 
74  connectBtn = new JButton("Connect");
75  connectBtn.addActionListener(new ActionListener() {
76 
77  public void actionPerformed(ActionEvent arg0) {
78  try {
79  Log.info("Using connection" + getConnection() + "\n");
80  isCancled = false;
81  } catch(Exception e) {
82  JOptionPane.showMessageDialog(null, "Error connecting with the given connection.", "Connection Error", JOptionPane.ERROR_MESSAGE);
83  } finally {
84  setVisible(false);
85  }
86  }
87  });
88 
89  cancelBtn = new JButton("Cancel");
90  cancelBtn.addActionListener(new ActionListener() {
91 
92  public void actionPerformed(ActionEvent arg0) {
93  isCancled = true;
94  setVisible(false);
95  }
96  });
97 
98  refresh = new JButton("Refresh");
99  refresh.addActionListener(new ActionListener() {
100 
101  public void actionPerformed(ActionEvent arg0) {
102  for(int i = 0; i < connectionPanels.getTabCount(); i++) {
103  ((AbstractConnectionPanel) connectionPanels.getComponentAt(i)).refresh();
104  }
105  }
106  });
107  panel = new JPanel(new MigLayout("", // Layout Constraints
108  "[right][left]", // Column constraints with default align
109  "[center][center]" // Row constraints with default align
110  ));
111 
112  panel.add(connectionPanels);
113  panel.add(connectBtn, "cell 0 2 2 1");
114  panel.add(cancelBtn, "cell 0 2 2 2");
115 
116  add(panel);
117  //setResizable(false);
118  setTitle("Connection Information");
119  //pack();
120 
121  if (connection != null) {
123  }
124  connection = null;
125 
126  addWindowFocusListener(new WindowAdapter() {
127  public void windowGainedFocus(WindowEvent e) {
128  connectBtn.requestFocusInWindow();
129  }
130  });
131  pack();
132  setAlwaysOnTop(true);
133 
134  }
135 
139  private void loadDefaultConnections() {
140  try{
141  try{
142 // if(OsInfoUtil.isLinux())
143 // addConnectionPanel(new UsbConnectionPanel(this));
146  //addConnectionPanel(new SerialConnectionPanel(this));
147  }catch(Exception ex){
150  }
151  }catch(Error e){
152  e.printStackTrace();
153  Log.error("This is not a java 8 compliant system, removing the serial, bluetooth and usb connections");
154  }
157 
158  }
159 
166  connectionPanels.addTab(panel.getTitle(), panel.getIcon(), panel, panel.getToolTipText());
167  connectionPanels.invalidate();
168  connectionPanels.repaint();
169  }
170 
178  public boolean showDialog() {
179  setLocationRelativeTo(null);
180  setVisible(true);
181  return !isCancled;
182  }
183 
190 
192  if(c == null) {
193  JOptionPane.showMessageDialog(null, "Unable to create connection.", "Invalid Connection", JOptionPane.ERROR_MESSAGE);
194  }
195 
196  return c;
197  }
198 
205  public static boolean getBowlerDevice(BowlerAbstractDevice dev) {
206  return getBowlerDevice(dev, null);
207  }
208 
217  if (dev == null) {
218  return false;
219  }
221  while(connection == null) {
222  Log.info("Select connection:");
224  if (connection == null) {
225  Log.info("No connection selected...");
226  return false;
227  }
228  Log.info("setting connection");
229  try {
231  dev.connect();
232  Log.info("Connected");
233  } catch(DyIOCommunicationException e1) {
234  String m = "The DyIO has not reported back to the library. \nCheck your connection and ensure you are attempting to talk to a DyIO, not another Bowler Device\nThis program will now exit.";
235  JOptionPane.showMessageDialog(null, m, "DyIO Not Responding"+e1.getMessage(), JOptionPane.ERROR_MESSAGE);
236  continue;
237  } catch(Exception e) {
238  e.printStackTrace();
239  JOptionPane.showMessageDialog(null, e.getMessage(), "DyIO Connection problem ", JOptionPane.ERROR_MESSAGE);
240  return false;
241  }
242  Log.info("Attempting to ping");
243  if(dev.ping() ){
244  Log.info("Ping OK!");
245  break;
246  }else{
247  connection = null;
248  JOptionPane.showMessageDialog(null, "No device on that port", "", JOptionPane.ERROR_MESSAGE);
249  }
250  }
251  return true;
252  }
253 
254 
261  if(!GraphicsEnvironment.isHeadless()) {
263  cd.showDialog();
264 
265  return cd.isCancled?null:cd.getConnection();
266  }
267  if(System.getProperty("nrdk.config.file") == null) {
268  return null;
269  }
270  return getHeadlessConnection(System.getProperty("nrdk.config.file"));
271  }
272 
279  public static BowlerAbstractConnection getHeadlessConnection(String config){
280  return ConfigManager.loadDefaultConnection(config);
281  }
282 
290  if(System.getProperty("nrdk.config.file") == null) {
292  if(panel != null) {
294  }
295  cd.showDialog();
296 
297  return cd.isCancled?null:cd.getConnection();
298  }
299  if(System.getProperty("nrdk.config.file") == null) {
300  return null;
301  }
302  return getHeadlessConnection(System.getProperty("nrdk.config.file"));
303  }
304 
305 }
void setConnection(BowlerAbstractConnection connection)
static BowlerAbstractConnection loadDefaultConnection(String filename)
static void info(String message)
Definition: Log.java:110
static void error(String message)
Definition: Log.java:92
static BowlerAbstractConnection getHeadlessConnection(String config)
void addConnectionPanel(AbstractConnectionPanel panel)
static BowlerAbstractConnection promptConnection()
static boolean getBowlerDevice(BowlerAbstractDevice dev)
static BowlerAbstractConnection promptConnection(AbstractConnectionPanel panel)
static boolean getBowlerDevice(BowlerAbstractDevice dev, AbstractConnectionPanel panel)