import ch.psi.pshell.core.Context; import ch.psi.pshell.swing.DevicePanel; import ch.psi.utils.swing.SwingUtils; import java.util.concurrent.CompletableFuture; /** * */ public class BarcodeReaderPanel extends DevicePanel { volatile boolean enabled; public BarcodeReaderPanel() { initComponents(); this.startTimer(100); } CompletableFuture future; @Override public void onTimer(){ if ((getDevice()!=null) && enabled){ if ((future==null) || (future.isDone())){ future = Context.getInstance().evalLineBackgroundAsync("barcode_reader.get()"); } } } void execute(String statement, boolean showReturn){ try { Context.getInstance().evalLineBackgroundAsync(statement).handle((ret, ex) -> { if (BarcodeReaderPanel.this.isShowing()){ if (ex != null){ showException((Exception)ex); } else if (showReturn){ if (ret == null){ SwingUtils.showMessage(this, "Return", "No code detected", 20000); } else { SwingUtils.showMessage(this, "Return", "Detected code: " +ret, 20000); } } } return ret; }); } catch (Exception ex) { showException(ex); } } @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents private void initComponents() { jPanel3 = new javax.swing.JPanel(); buttonEnable = new javax.swing.JButton(); buttonDisable = new javax.swing.JButton(); buttonRead = new javax.swing.JButton(); deviceStatePanel1 = new ch.psi.pshell.swing.DeviceStatePanel(); deviceValuePanel1 = new ch.psi.pshell.swing.DeviceValuePanel(); jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Commands")); buttonEnable.setText("Enable"); buttonEnable.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonEnableActionPerformed(evt); } }); buttonDisable.setText("Disable"); buttonDisable.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonDisableActionPerformed(evt); } }); buttonRead.setText("Read"); buttonRead.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonReadActionPerformed(evt); } }); javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout.setHorizontalGroup( jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup() .addContainerGap() .addComponent(buttonEnable) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(buttonDisable) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(buttonRead) .addContainerGap()) ); jPanel3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {buttonDisable, buttonEnable, buttonRead}); jPanel3Layout.setVerticalGroup( jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(buttonEnable) .addComponent(buttonDisable) .addComponent(buttonRead)) .addContainerGap()) ); deviceStatePanel1.setDeviceName("barcode_reader"); deviceValuePanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Value")); deviceValuePanel1.setDeviceName("barcode_reader"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(deviceValuePanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(deviceStatePanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, 0) .addComponent(deviceValuePanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, 0) .addComponent(deviceStatePanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, 0)) ); }// //GEN-END:initComponents private void buttonEnableActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonEnableActionPerformed execute("barcode_reader.enable()", false); enabled = true; }//GEN-LAST:event_buttonEnableActionPerformed private void buttonDisableActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonDisableActionPerformed enabled = false; execute("barcode_reader.disable()", false); }//GEN-LAST:event_buttonDisableActionPerformed private void buttonReadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonReadActionPerformed enabled = false; execute("barcode_reader.read(5.0) ", true); }//GEN-LAST:event_buttonReadActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton buttonDisable; private javax.swing.JButton buttonEnable; private javax.swing.JButton buttonRead; private ch.psi.pshell.swing.DeviceStatePanel deviceStatePanel1; private ch.psi.pshell.swing.DeviceValuePanel deviceValuePanel1; private javax.swing.JPanel jPanel3; // End of variables declaration//GEN-END:variables }