MyToolbarContribution.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package urgrip.toolbar;
  2. import com.ur.urcap.api.contribution.toolbar.ToolbarAPIProvider;
  3. import com.ur.urcap.api.contribution.toolbar.ToolbarContext;
  4. import com.ur.urcap.api.contribution.toolbar.swing.SwingToolbarContribution;
  5. import com.ur.urcap.api.domain.io.DigitalIO;
  6. import urgrip.installation.URGripInstallationNodeContribution;
  7. import urgrip.installation.URGripInstallationNodeService;
  8. import javax.swing.*;
  9. import java.awt.*;
  10. import java.util.*;
  11. class MyToolbarContribution implements SwingToolbarContribution {
  12. private static final int VERTICAL_SPACE = 10;
  13. private static final int HEADER_FONT_SIZE = 24;
  14. private final ToolbarContext context;
  15. private final ToolbarAPIProvider apiProvider;
  16. private final IOHandler ioHandler;
  17. private final URGripInstallationNodeContribution installationNode;
  18. private JLabel demoToolStatus;
  19. private HashMap<String, JCheckBox> boxes=new HashMap<>();
  20. private DigitalIO ioGripClose, ioGripOpen, ioCoupleClose, ioCoupleOpen, blowout, waitfor;
  21. private JButton bGrip;
  22. private JButton bRelease;
  23. private JButton bBlow;
  24. private JButton bCouple;
  25. private JButton bDecouple;
  26. MyToolbarContribution(ToolbarContext context) {
  27. this.context = context;
  28. this.apiProvider = context.getAPIProvider();
  29. this.ioHandler = new IOHandler(this.apiProvider.getApplicationAPI().getIOModel());
  30. this.installationNode = apiProvider.getApplicationAPI().getInstallationNode(URGripInstallationNodeContribution.class);
  31. }
  32. @Override
  33. public void openView() {
  34. initializeIO();
  35. }
  36. @Override
  37. public void closeView() {
  38. }
  39. private void initializeIO() {
  40. ioGripClose = ioHandler.getDigitalIO(installationNode.getGripCloseIO());
  41. ioGripOpen = ioHandler.getDigitalIO(installationNode.getGripOpenIO());
  42. ioCoupleClose = ioHandler.getDigitalIO(installationNode.getCoupleCloseIO());
  43. ioCoupleOpen = ioHandler.getDigitalIO(installationNode.getCoupleOpenIO());
  44. blowout = ioHandler.getDigitalIO(installationNode.getBlowoutIO());
  45. waitfor = ioHandler.getDigitalIO(installationNode.getWaitforIO());
  46. }
  47. public void buildUI(JPanel jPanel) {
  48. jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.Y_AXIS));
  49. jPanel.add(createHeader());
  50. jPanel.add(createVerticalSpace());
  51. jPanel.add(createButtons());
  52. jPanel.add(createInfo());
  53. }
  54. private Box createHeader() {
  55. Box headerBox = Box.createHorizontalBox();
  56. headerBox.setAlignmentX(Component.CENTER_ALIGNMENT);
  57. JLabel header = new JLabel("Gripper");
  58. header.setFont(header.getFont().deriveFont(Font.BOLD, HEADER_FONT_SIZE));
  59. headerBox.add(header);
  60. return headerBox;
  61. }
  62. private Box createButtons() {
  63. Box box = Box.createHorizontalBox();
  64. bGrip = new JButton("grip");
  65. box.add(bGrip);
  66. new MyButton(bGrip, new HandleButton() {
  67. @Override public void action() {
  68. ioGripOpen.setValue(true);
  69. ioGripClose.setValue(false);
  70. }
  71. });
  72. bRelease = new JButton("release");
  73. box.add(bRelease);
  74. new MyButton(bRelease, new HandleButton() {
  75. @Override public void action() {
  76. ioGripOpen.setValue(false);
  77. ioGripClose.setValue(true);
  78. }
  79. });
  80. bBlow = new JButton("blow out");
  81. box.add(bBlow);
  82. new MyButton(bBlow, new HandleButton() {
  83. @Override
  84. public void down() {
  85. blowout.setValue(true);
  86. }
  87. @Override
  88. public void up() {
  89. blowout.setValue(false);
  90. }
  91. });
  92. bCouple = new JButton("couple");
  93. box.add(bCouple);
  94. new MyButton(bCouple, new HandleButton() {
  95. @Override public void action() {
  96. ioCoupleClose.setValue(false);
  97. ioCoupleOpen.setValue(true);
  98. }
  99. });
  100. bDecouple = new JButton("decouple");
  101. box.add(bDecouple);
  102. new MyButton(bDecouple, new HandleButton() {
  103. @Override public void action() {
  104. ioCoupleClose.setValue(true);
  105. ioCoupleOpen.setValue(false);
  106. }
  107. });
  108. return box;
  109. }
  110. private Box createInfo() {
  111. Box infoBox = Box.createVerticalBox();
  112. infoBox.setAlignmentX(Component.CENTER_ALIGNMENT);
  113. demoToolStatus = new JLabel();
  114. demoToolStatus.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  115. infoBox.add(demoToolStatus);
  116. return infoBox;
  117. }
  118. private Component createVerticalSpace() {
  119. return Box.createRigidArea(new Dimension(0, VERTICAL_SPACE));
  120. }
  121. private void updateText(String text) {
  122. SwingUtilities.invokeLater(() -> demoToolStatus.setText("<HTML>" + text +"</HTML>"));
  123. }
  124. }