import ch.psi.pshell.core.Context; import ch.psi.pshell.device.GenericDevice; import ch.psi.pshell.imaging.MjpegSource; import ch.psi.pshell.imaging.RendererMode; import ch.psi.pshell.ui.Panel; import ch.psi.utils.State; import ch.psi.utils.Str; import ch.psi.utils.swing.SwingUtils; import java.awt.Component; import java.io.File; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.DefaultComboBoxModel; import javax.swing.ImageIcon; import javax.swing.JButton; /** * */ public class MonitoringCameras extends Panel { public MonitoringCameras() { initComponents(); renderer.setMode(RendererMode.Fit); updateControls(); } volatile boolean loadedCommands; volatile boolean moving; volatile String currentCamera; void updateControls(){ boolean isCameraSelected = (currentCamera!=null); boolean enabled = isCameraSelected && loadedCommands && !moving; for (Component button : SwingUtils.getComponentsByType(panelControls, JButton.class)){ button.setEnabled(enabled); } } //Overridable callbacks @Override public void onInitialize(int runCount) { updateControls(); DefaultComboBoxModel model = new DefaultComboBoxModel(); renderer.setDeviceName(null); for (GenericDevice dev : Context.getInstance().getDevicePool().getAllDevices(MjpegSource.class)){ MjpegSource camera =(MjpegSource) dev; model.addElement(camera.getName()); } model.addElement(""); model.setSelectedItem(null); comboCameras.setModel(model); currentCamera = null; comboCameras.requestFocus(); } @Override public void onStateChange(State state, State former) { loadedCommands = false; if ((state==State.Ready) & (former==State.Initializing)){ try { this.runAsync("devices/axis.py", true).handle((ret,ex)->{ loadedCommands = true; updateControls(); return ret; }); } catch (Context.ContextStateException ex) { getLogger().log(Level.SEVERE, null, ex); } } } @Override public void onExecutedFile(String fileName, Object result) { } //Callback to perform update - in event thread @Override protected void doUpdate() { } void selectCamera(String name){ renderer.clear(); currentCamera = ((name == null)||(name.isBlank()))? null : name; renderer.setDeviceName(currentCamera); updateControls(); } void move(String direction){ try { moving = true; updateControls(); String camera = comboCameras.getSelectedItem().toString(); this.evalAsync("axis_move(" + camera + ", '" + direction + "')", true).handle((ret,ex)->{ moving = false; updateControls(); return ret; }); } catch (Context.ContextStateException ex) { getLogger().log(Level.SEVERE, null, ex); moving = false; updateControls(); } } void zoom(int steps){ try { moving = true; updateControls(); String camera = comboCameras.getSelectedItem().toString(); this.evalAsync("axis_rzoom(" + camera + ", " + steps + ")", true).handle((ret,ex)->{ moving = false; updateControls(); return ret; }); } catch (Context.ContextStateException ex) { getLogger().log(Level.SEVERE, null, ex); moving = false; updateControls(); } } ImageIcon getIcon(String name) { ImageIcon ret = null; try { ret = new ImageIcon(ch.psi.pshell.ui.App.class.getResource("/ch/psi/pshell/ui/" + name + ".png")); if (ch.psi.utils.swing.MainFrame.isDark()) { try { ret = new ImageIcon(ch.psi.pshell.ui.App.class.getResource("/ch/psi/pshell/ui/dark/" + name + ".png")); } catch (Exception e) { } } } catch (Exception ex) { ex.printStackTrace(); } return ret; } @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents private void initComponents() { renderer = new ch.psi.pshell.imaging.Renderer(); panelControls = new javax.swing.JPanel(); jPanel2 = new javax.swing.JPanel(); buttonMoveLeft = new javax.swing.JButton(); buttonMoveDLeft = new javax.swing.JButton(); buttonMoveDRight = new javax.swing.JButton(); buttonMoveRight = new javax.swing.JButton(); buttonMoveUReight = new javax.swing.JButton(); buttonMoveHome = new javax.swing.JButton(); buttonMoveDown = new javax.swing.JButton(); buttonMoveULeft = new javax.swing.JButton(); buttonMoveUp = new javax.swing.JButton(); jPanel1 = new javax.swing.JPanel(); buttonZoomOut = new javax.swing.JButton(); buttonZooHome = new javax.swing.JButton(); buttonZoomIn = new javax.swing.JButton(); jPanel3 = new javax.swing.JPanel(); comboCameras = new javax.swing.JComboBox<>(); jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Move")); buttonMoveLeft.setIcon(getIcon("arrows/Left")); buttonMoveLeft.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonMoveLeftActionPerformed(evt); } }); buttonMoveDLeft.setIcon(getIcon("arrows/DLeft")); buttonMoveDLeft.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonMoveDLeftActionPerformed(evt); } }); buttonMoveDRight.setIcon(getIcon("arrows/DRight")); buttonMoveDRight.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonMoveDRightActionPerformed(evt); } }); buttonMoveRight.setIcon(getIcon("arrows/Right")); buttonMoveRight.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonMoveRightActionPerformed(evt); } }); buttonMoveUReight.setIcon(getIcon("arrows/URight")); buttonMoveUReight.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonMoveUReightActionPerformed(evt); } }); buttonMoveHome.setIcon(getIcon("arrows/Center")); buttonMoveHome.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonMoveHomeActionPerformed(evt); } }); buttonMoveDown.setIcon(getIcon("arrows/Down")); buttonMoveDown.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonMoveDownActionPerformed(evt); } }); buttonMoveULeft.setIcon(getIcon("arrows/ULeft")); buttonMoveULeft.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonMoveULeftActionPerformed(evt); } }); buttonMoveUp.setIcon(getIcon("arrows/Up")); buttonMoveUp.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonMoveUpActionPerformed(evt); } }); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(buttonMoveULeft, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(buttonMoveDLeft, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(buttonMoveLeft, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(buttonMoveDown, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE) .addComponent(buttonMoveHome, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE) .addComponent(buttonMoveUp, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(buttonMoveRight, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE) .addComponent(buttonMoveUReight, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE) .addComponent(buttonMoveDRight, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {buttonMoveDLeft, buttonMoveDRight, buttonMoveDown, buttonMoveHome, buttonMoveLeft, buttonMoveRight, buttonMoveULeft, buttonMoveUReight, buttonMoveUp}); jPanel2Layout.setVerticalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(buttonMoveUp, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(buttonMoveUReight, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(buttonMoveULeft, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(buttonMoveLeft, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(buttonMoveRight, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(buttonMoveHome, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(buttonMoveDown, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(buttonMoveDLeft, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(buttonMoveDRight, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap()) ); jPanel2Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {buttonMoveDLeft, buttonMoveDRight, buttonMoveDown, buttonMoveHome, buttonMoveLeft, buttonMoveRight, buttonMoveULeft, buttonMoveUReight, buttonMoveUp}); jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Zoom")); buttonZoomOut.setIcon(getIcon("arrows/Out")); buttonZoomOut.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonZoomOutActionPerformed(evt); } }); buttonZooHome.setIcon(getIcon("arrows/Center")); buttonZooHome.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonZooHomeActionPerformed(evt); } }); buttonZoomIn.setIcon(getIcon("arrows/In")); buttonZoomIn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonZoomInActionPerformed(evt); } }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(buttonZoomOut, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(buttonZooHome, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(buttonZoomIn, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {buttonZooHome, buttonZoomIn, buttonZoomOut}); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(buttonZoomOut, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(buttonZoomIn, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(buttonZooHome, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap()) ); jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Camera")); comboCameras.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { comboCamerasActionPerformed(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(comboCameras, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap()) ); jPanel3Layout.setVerticalGroup( jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup() .addContainerGap() .addComponent(comboCameras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); javax.swing.GroupLayout panelControlsLayout = new javax.swing.GroupLayout(panelControls); panelControls.setLayout(panelControlsLayout); panelControlsLayout.setHorizontalGroup( panelControlsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(panelControlsLayout.createSequentialGroup() .addContainerGap() .addGroup(panelControlsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap()) ); panelControlsLayout.setVerticalGroup( panelControlsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(panelControlsLayout.createSequentialGroup() .addContainerGap() .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(322, Short.MAX_VALUE)) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGap(0, 0, 0) .addComponent(panelControls, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(renderer, javax.swing.GroupLayout.DEFAULT_SIZE, 522, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(panelControls, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(renderer, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); }// //GEN-END:initComponents private void comboCamerasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboCamerasActionPerformed try{ if (getState().isInitialized()){ Object selection = comboCameras.getSelectedItem(); selectCamera((selection==null) ? null :selection.toString()); } } catch(Exception ex){ showException(ex); } }//GEN-LAST:event_comboCamerasActionPerformed private void buttonMoveULeftActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonMoveULeftActionPerformed move("upleft"); }//GEN-LAST:event_buttonMoveULeftActionPerformed private void buttonMoveUpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonMoveUpActionPerformed move("up"); }//GEN-LAST:event_buttonMoveUpActionPerformed private void buttonMoveUReightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonMoveUReightActionPerformed move("upright"); }//GEN-LAST:event_buttonMoveUReightActionPerformed private void buttonMoveLeftActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonMoveLeftActionPerformed move("left"); }//GEN-LAST:event_buttonMoveLeftActionPerformed private void buttonMoveHomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonMoveHomeActionPerformed move("home"); }//GEN-LAST:event_buttonMoveHomeActionPerformed private void buttonMoveRightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonMoveRightActionPerformed move("right"); }//GEN-LAST:event_buttonMoveRightActionPerformed private void buttonMoveDLeftActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonMoveDLeftActionPerformed move("downleft"); }//GEN-LAST:event_buttonMoveDLeftActionPerformed private void buttonMoveDownActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonMoveDownActionPerformed move("down"); }//GEN-LAST:event_buttonMoveDownActionPerformed private void buttonMoveDRightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonMoveDRightActionPerformed move("downright"); }//GEN-LAST:event_buttonMoveDRightActionPerformed private void buttonZoomOutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonZoomOutActionPerformed zoom(-100); }//GEN-LAST:event_buttonZoomOutActionPerformed private void buttonZooHomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonZooHomeActionPerformed move("home"); }//GEN-LAST:event_buttonZooHomeActionPerformed private void buttonZoomInActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonZoomInActionPerformed zoom(100); }//GEN-LAST:event_buttonZoomInActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton buttonMoveDLeft; private javax.swing.JButton buttonMoveDRight; private javax.swing.JButton buttonMoveDown; private javax.swing.JButton buttonMoveHome; private javax.swing.JButton buttonMoveLeft; private javax.swing.JButton buttonMoveRight; private javax.swing.JButton buttonMoveULeft; private javax.swing.JButton buttonMoveUReight; private javax.swing.JButton buttonMoveUp; private javax.swing.JButton buttonZooHome; private javax.swing.JButton buttonZoomIn; private javax.swing.JButton buttonZoomOut; private javax.swing.JComboBox comboCameras; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel3; private javax.swing.JPanel panelControls; private ch.psi.pshell.imaging.Renderer renderer; // End of variables declaration//GEN-END:variables }