Skip to content

"Plug Now" feature to select the serial port automatically #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 91 additions & 10 deletions app/src/processing/app/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,8 @@ protected void selectSerialPort(String name) {
}
JCheckBoxMenuItem selection = null;
for (int i = 0; i < serialMenu.getItemCount(); i++) {
if (!(serialMenu.getItem(i) instanceof JCheckBoxMenuItem))
continue;
JCheckBoxMenuItem item = ((JCheckBoxMenuItem)serialMenu.getItem(i));
if (item == null) {
System.out.println(_("name is null"));
Expand All @@ -955,7 +957,15 @@ protected void populateSerialMenu() {
//System.out.println("Clearing serial port menu.");

serialMenu.removeAll();
boolean empty = true;

rbMenuItem = new JMenuItem(_("Plug Now..."));
rbMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handlePlugNow();
}
});
serialMenu.add(rbMenuItem);
serialMenu.addSeparator();

try
{
Expand All @@ -971,13 +981,8 @@ protected void populateSerialMenu() {
rbMenuItem.addActionListener(serialMenuListener);
//serialGroup.add(rbMenuItem);
serialMenu.add(rbMenuItem);
empty = false;
}
}
if (!empty) {
//System.out.println("enabling the serialMenu");
serialMenu.setEnabled(true);
}

}

Expand All @@ -986,15 +991,91 @@ protected void populateSerialMenu() {
System.out.println(_("error retrieving port list"));
exception.printStackTrace();
}

if (serialMenu.getItemCount() == 0) {
serialMenu.setEnabled(false);
}

//serialMenu.addSeparator();
//serialMenu.add(item);
}

private Vector getSerialPortNames() {
Vector ports = new Vector();
try {
Enumeration elem = CommPortIdentifier.getPortIdentifiers();
while (elem.hasMoreElements()) {
CommPortIdentifier id = (CommPortIdentifier)elem.nextElement();
if (id.getPortType() == CommPortIdentifier.PORT_SERIAL)
ports.addElement(id.getName());
}
}
catch (Exception exception) {
System.out.println(_("error retrieving port list"));
exception.printStackTrace();
}
return ports;
}

protected void handlePlugNow() {
final JLabel portlabel = new JLabel(_("(not yet detected)"));
JLabel[] labels = {
new JLabel(_("Plug Arduino to PC now and click OK to select the serial port.")),
new JLabel(_("Detected port:")),
portlabel
};

JOptionPane pane = new JOptionPane(
labels,
JOptionPane.QUESTION_MESSAGE,
JOptionPane.OK_CANCEL_OPTION
);
pane.setOptions(new String[] { _("OK"), _("Cancel") });
pane.setInitialValue(_("Cancel"));
final JDialog dialog = pane.createDialog(this, _("Plug Now"));
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

final Vector ports = getSerialPortNames();

//XXX You may want to disable "OK" button while no port is detected.
final javax.swing.Timer timer = new javax.swing.Timer(
300, // 300 msec
new ActionListener() {
public void actionPerformed(ActionEvent e) {
Vector newports = getSerialPortNames();
if (newports.indexOf(portlabel.getText()) < 0)
portlabel.setText(_("(not yet detected)"));

Iterator it = newports.iterator();
while (it.hasNext()) {
String portname = (String)it.next();
//XXX not efficient
if (ports.indexOf(portname) < 0) {
portlabel.setText(portname);
break;
}
}

ports.clear();
ports.addAll(newports);
}
}
);

timer.start();
dialog.setVisible(true);
timer.stop();

if (pane.getValue() == _("OK")) {
String portname = portlabel.getText();
if (portname != _("(not yet detected)")) {
System.out.println(
I18n.format(_("Serial port \"{0}\" selected."), portname)
);
Preferences.set("serial.port", portname);
serialMonitor.closeSerialPort();
serialMonitor.setVisible(false);
serialMonitor = new SerialMonitor(Preferences.get("serial.port"));
base.onBoardOrPortChange();
}
}
}

protected JMenu buildHelpMenu() {
// To deal with a Mac OS X 10.5 bug, add an extra space after the name
Expand Down
Loading