📄 selectservocontrollerpage.java
字号:
add(probeButton); cs.gridy++; cs.weightx = 1; cs.weighty = 1; cs.fill = GridBagConstraints.BOTH; layout.setConstraints(deviceWrapperPanel, cs); add(deviceWrapperPanel); deviceWrapperPanel.setBorder(BorderFactory.createTitledBorder(foundTitle)); GridBagLayout wrapperLayout = new GridBagLayout(); GridBagConstraints wrapperCs = new GridBagConstraints(); deviceWrapperPanel.setLayout(wrapperLayout); wrapperCs.gridx = 0; wrapperCs.gridy = 0; wrapperCs.gridwidth = 1; wrapperCs.gridheight = 1; wrapperCs.weightx = 1; wrapperCs.weighty = 1; wrapperCs.fill = GridBagConstraints.BOTH; wrapperLayout.setConstraints(deviceScroller, wrapperCs); deviceWrapperPanel.add(deviceScroller); deviceScroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); deviceScroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); } } protected class SerialDevicePanel extends JPanel implements ActionListener, ItemListener { public final JCheckBox check = new JCheckBox(); public final JComboBox combo = new JComboBox(); public final JButton test = new JButton("Test"); public SerialDevicePanel(String title) { check.setText(title); combo.addItem("FerretTronics FT639"); combo.addItem("Parallax PSC"); combo.addItem("..."); check.addItemListener(this); combo.addItemListener(this); test.addActionListener(this); setLayout(new GridLayout(1, 3)); add(check); add(combo); add(test); combo.setEnabled(false); test.setEnabled(false); } public void actionPerformed(ActionEvent e) { complain(LOG_WARNING, CH_WP, "Fixme: " + e); } public void itemStateChanged(ItemEvent e) { combo.setEnabled(check.isSelected()); test.setEnabled(check.isSelected()); } } protected class UsbDevicePanel extends JPanel implements ActionListener, ItemListener { private SelectServoControllerPage owner; public final JCheckBox check = new JCheckBox(); public final JButton test = new JButton("Test"); public final String triple; public final String vendor; public final String product; public final String serial; public UsbDevicePanel(SelectServoControllerPage owner, String triple) { this.owner = owner; this.triple = triple; StringTokenizer st = new StringTokenizer(triple, ":"); vendor = st.nextToken(); product = st.nextToken(); serial = st.nextToken(); String vendorTitle = vendor; String productTitle = product; if ( "6c2".equals(vendor) ) { vendorTitle = "Phidget"; if ( "38".equals(product) ) { productTitle = "QuadServo"; } else if ( "39".equals(product) ) { productTitle = "UniServo"; } else if ( "3b".equals(product) ) { productTitle = "AdvancedServo"; } else { productTitle = "ProductID 0x" + product; } } else { vendorTitle = "VendorID 0x" + vendor; productTitle = "ProductID 0x" + product; } check.setText(vendorTitle + " " + productTitle + ", serial #" + serial); check.addItemListener(this); test.addActionListener(this); GridBagLayout layout = new GridBagLayout(); GridBagConstraints cs = new GridBagConstraints(); cs.gridx = 0; cs.gridy = 0; cs.weightx = 1; cs.weighty = 0; cs.fill = GridBagConstraints.HORIZONTAL; setLayout(layout); layout.setConstraints(check, cs); add(check); cs.gridx++; cs.weightx = 0; layout.setConstraints(test, cs); add(test); test.setEnabled(false); } public void actionPerformed(ActionEvent e) { // Let's see if we have the device for the port ServoController sc = (ServoController)owner.usbServoControllerByTriple.get(triple); if ( sc == null ) { // Let's create it, then complain(LOG_DEBUG, CH_WP, "Creating " + check.getText()); if ( "6c2".equals(vendor) ) { if ( "38".equals(product) ) { // Let's see if we have it in the context already. A // class is PhidgetServoController, and the serial // number must match. for ( Iterator i = getContextValues("servo_controller.").iterator(); i.hasNext(); ) { ServoController haveSc = (ServoController)i.next(); if ( (haveSc instanceof PhidgetServoController) && serial.equals(haveSc.getPort()) ) { // Yep, that's ours complain(LOG_DEBUG, CH_WP, "Already have a servo controller with id=" + serial); sc = haveSc; break; } } if ( sc == null ) { // VT: FIXME: Implement the constructor for QuadServo sc = new PhidgetServoController(); try { sc.init(serial); getOwner().getContext().put("servo_controller." + serial, sc); // Now that we have just created a new servo // controller, let's create servo // descriptors for it. We're guaranteed not // to have clashes since we have *just* // created it. for ( Iterator i = sc.getServos(); i.hasNext(); ) { Servo s = (Servo)i.next(); ServoDescriptor sd = new ServoDescriptor(s); getOwner().getContext().put("servo." + sd.getComparable(), sd); } } catch ( IOException ioex ) { complain(LOG_ERR, CH_WP, "Initialization failed:", ioex); } } } else { complain(LOG_ALERT, CH_WP, check.getText() + ": not implemented for product id 0x" + product); } } else { complain(LOG_ALERT, CH_WP, check.getText() + ": not implemented for vendor id 0x" + vendor); } if ( sc != null ) { owner.usbServoControllerByTriple.put(triple, sc); } else { complain(LOG_ERR, CH_WP, "Failed to create " + check.getText()); } } if ( sc != null ) { try { sc.reset(); for ( Iterator i = sc.getServos(); i.hasNext(); ) { Servo s = (Servo)i.next(); s.setPosition(0); complain(LOG_DEBUG, CH_WP, "Servo: " + s + " set to 0"); } Thread.sleep(800); for ( Iterator i = sc.getServos(); i.hasNext(); ) { Servo s = (Servo)i.next(); s.setPosition(1); complain(LOG_DEBUG, CH_WP, "Servo: " + s + " set to 0"); } Thread.sleep(800); for ( Iterator i = sc.getServos(); i.hasNext(); ) { Servo s = (Servo)i.next(); s.setPosition(0.5); complain(LOG_DEBUG, CH_WP, "Servo: " + s + " set to 0.5"); } } catch ( IOException ioex ) { complain(LOG_ERR, CH_WP, "Test failed:", ioex); } catch ( InterruptedException iex ) { complain(LOG_WARNING, CH_WP, "Test interrupted:", iex); } } // Finally, since a new servo controller may have been created, // let's make the wizard revalidate the page. owner.getOwner().validateButtons(); } public void itemStateChanged(ItemEvent e) { test.setEnabled(check.isSelected()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -