📄 selectservocontrollerpage.java
字号:
package net.sf.dz.setup.core;import java.awt.GridBagConstraints;import java.awt.GridBagLayout; import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.io.IOException;import java.util.Enumeration;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;import java.util.StringTokenizer;import java.util.TreeSet;import javax.comm.CommPortIdentifier;import javax.comm.PortInUseException;import javax.comm.SerialPort;import javax.comm.UnsupportedCommOperationException;import javax.swing.BorderFactory;import javax.swing.JButton;import javax.swing.JCheckBox;import javax.swing.JComboBox;import javax.swing.JScrollPane;import javax.swing.JPanel;import org.freehold.jukebox.logger.Logger;import org.freehold.servomaster.device.model.Servo;import org.freehold.servomaster.device.model.ServoController;import org.freehold.servomaster.device.impl.phidget.PhidgetServoController;import org.freehold.servomaster.device.impl.phidget.QuadServoController;import net.sf.dz.setup.UsbProbe;import net.sf.dz.util.wizard.Wizard;import net.sf.dz.util.wizard.WizardPage;class SelectServoControllerPage extends WizardPage { DevicePanel usbPanel = new DevicePanel("USB", "Devices found"); DevicePanel serialPanel = new DevicePanel("Serial", "Ports found"); private Map usbServoControllerByTriple = new HashMap(); private Map usbTripleByButton = new HashMap(); private Set serialDevices = new TreeSet(); public SelectServoControllerPage(Logger logger, Wizard owner) { super(logger, owner, "Select Servo Controller"); GridBagLayout layout = new GridBagLayout(); GridBagConstraints cs = new GridBagConstraints(); getContentPane().setLayout(layout); cs.gridx = 0; cs.gridy = 0; cs.gridwidth = 1; cs.gridheight = 1; cs.weightx = 1; cs.weighty = 1; cs.fill = GridBagConstraints.BOTH; layout.setConstraints(usbPanel, cs); getContentPane().add(usbPanel); cs.gridy++; layout.setConstraints(serialPanel, cs); getContentPane().add(serialPanel); usbPanel.probeButton.addActionListener(this); serialPanel.probeButton.addActionListener(this); } public String validate() { boolean needTest = false; Map context = getOwner().getContext(); for ( Iterator i = usbTripleByButton.keySet().iterator(); i.hasNext(); ) { UsbDevicePanel p = (UsbDevicePanel)i.next(); String triple = (String)usbTripleByButton.get(p); ServoController sc = (ServoController)usbServoControllerByTriple.get(triple); // If the controller doesn't exist, it means it hasn't been // tested. Let's forbid them to move ahead until that is // done. if ( p.check.isSelected() && sc == null ) { needTest = true; } // Let's hope that vendor:product:serial is a unique // identifier, even if the serial devices are included // VT: FIXME: Have to replace this mechanism with a single entry // for a servo controller in the context. It has to be shared // with the configuration reader to avoid hardware collisions. //context.put("servo." + triple + ".enabled", new Boolean(p.check.isSelected())); //context.put("servo." + triple + ".instance", sc); // This is a trick to make the name parsing easier //context.put("servo." + triple + ".id", triple); } if ( needTest ) { return "You need to test all selected controllers to proceed"; } return ""; } public boolean isEnabled() { return true; } public String getHelpURL() { return "FIXME"; } public void actionPerformed2(ActionEvent e) { Object source = e.getSource(); if ( source == usbPanel.probeButton ) { Set deviceDefs = new TreeSet(); // VT: NOTE: These are hex integers! deviceDefs.add("6c2:38"); // Phidget QuadServo deviceDefs.add("6c2:39"); // Phidget UniServo deviceDefs.add("6c2:3b"); // Phidget AdvancedServo UsbProbe probe = new UsbProbe(); probe.setLogger(getLogger()); Set usbDevicesFound = probe.probe(deviceDefs); complain(LOG_NOTICE, CH_WP, usbDevicesFound); usbPanel.devicePanel.removeAll(); GridBagLayout layout = new GridBagLayout(); GridBagConstraints cs = new GridBagConstraints(); usbPanel.devicePanel.setLayout(layout); cs.gridx = 0; cs.gridy = 0; cs.gridwidth = 1; cs.gridheight = 1; cs.weightx = 1; cs.weighty = 0; cs.fill = GridBagConstraints.HORIZONTAL; for ( Iterator i = usbDevicesFound.iterator(); i.hasNext(); ) { String triple = i.next().toString(); UsbDevicePanel p = new UsbDevicePanel(this, triple); layout.setConstraints(p, cs); usbPanel.devicePanel.add(p); usbTripleByButton.put(p, triple); p.check.addItemListener(this); cs.gridy++; } cs.weighty = 1; cs.fill = GridBagConstraints.BOTH; JPanel filler = new JPanel(); layout.setConstraints(filler, cs); usbPanel.devicePanel.add(filler); usbPanel.invalidate(); usbPanel.validate(); usbPanel.repaint(); } else if ( source == serialPanel.probeButton ) { Set ports = probeSerialPorts(); serialPanel.devicePanel.removeAll(); GridBagLayout layout = new GridBagLayout(); GridBagConstraints cs = new GridBagConstraints(); serialPanel.devicePanel.setLayout(layout); cs.gridx = 0; cs.gridy = 0; cs.gridwidth = 1; cs.gridheight = 1; cs.weightx = 1; cs.weighty = 0; cs.fill = GridBagConstraints.HORIZONTAL; for ( Iterator i = ports.iterator(); i.hasNext(); ) { String name = i.next().toString(); SerialDevicePanel p = new SerialDevicePanel(name); layout.setConstraints(p, cs); serialPanel.devicePanel.add(p); cs.gridy++; } cs.weighty = 1; cs.fill = GridBagConstraints.BOTH; JPanel filler = new JPanel(); layout.setConstraints(filler, cs); serialPanel.devicePanel.add(filler); serialPanel.invalidate(); serialPanel.validate(); serialPanel.repaint(); } } private Set probeSerialPorts() { serialPanel.probeButton.setEnabled(false); Set portsTried = new TreeSet(); for ( Enumeration ports = CommPortIdentifier.getPortIdentifiers(); ports.hasMoreElements(); ) { CommPortIdentifier id = (CommPortIdentifier)ports.nextElement(); if ( id.getPortType() == CommPortIdentifier.PORT_SERIAL ) { try { SerialPort port = (SerialPort)id.open(getClass().getName(), 5000); port.close(); portsTried.add(id.getName()); } catch ( PortInUseException piuex ) { portsTried.add(id.getName()); } } } serialPanel.probeButton.setEnabled(true); return portsTried; } protected class DevicePanel extends JPanel { public final JButton probeButton = new JButton("Probe"); private JPanel deviceWrapperPanel = new JPanel(); public final JPanel devicePanel = new JPanel(); private JScrollPane deviceScroller = new JScrollPane(devicePanel); public DevicePanel(String title, String foundTitle) { setBorder(BorderFactory.createTitledBorder(title)); GridBagLayout layout = new GridBagLayout(); GridBagConstraints cs = new GridBagConstraints(); setLayout(layout); cs.gridx = 0; cs.gridy = 0; cs.gridwidth = 1; cs.gridheight = 1; cs.weightx = 0; cs.weighty = 0; cs.fill = GridBagConstraints.NONE; cs.anchor = GridBagConstraints.WEST; layout.setConstraints(probeButton, cs);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -