MyToolbarContribution.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package uraxis.toolbar;
  2. import com.ur.urcap.api.contribution.toolbar.ToolbarContext;
  3. import com.ur.urcap.api.contribution.toolbar.swing.SwingToolbarContribution;
  4. import uraxis.Activator;
  5. import javax.swing.*;
  6. import java.awt.*;
  7. import java.awt.event.ActionEvent;
  8. import java.util.*;
  9. import java.util.Timer;
  10. import java.util.concurrent.CompletableFuture;
  11. import java.util.concurrent.ExecutionException;
  12. class MyToolbarContribution implements SwingToolbarContribution {
  13. private static final int VERTICAL_SPACE = 10;
  14. private static final int HEADER_FONT_SIZE = 24;
  15. private final ToolbarContext context;
  16. private JLabel demoToolStatus;
  17. private Timer uiTimer;
  18. MyToolbarContribution(ToolbarContext context) {
  19. this.context = context;
  20. }
  21. @Override
  22. public void openView() {
  23. //UI updates from non-GUI threads must use EventQueue.invokeLater (or SwingUtilities.invokeLater)
  24. uiTimer = new Timer(true);
  25. uiTimer.schedule(new TimerTask() {
  26. @Override
  27. public void run() {
  28. EventQueue.invokeLater(new Runnable() {
  29. @Override
  30. public void run() {
  31. CompletableFuture<Integer> fPos = Activator.daemonInterface.getpos();
  32. CompletableFuture<String> fStatus = Activator.daemonInterface.getstatus();
  33. CompletableFuture.allOf(fPos, fStatus).whenComplete((aVoid, throwable) -> {
  34. Date now = new Date();
  35. try {
  36. updateText(String.format("Position: %d, read at %tF %tT.<br/>%s", fPos.get(), now, now, fStatus.get()));
  37. } catch (InterruptedException|ExecutionException e) {
  38. e.printStackTrace();
  39. }
  40. });
  41. }
  42. });
  43. }
  44. }, 0, 1000);
  45. }
  46. @Override
  47. public void closeView() {
  48. uiTimer.cancel();
  49. }
  50. public void buildUI(JPanel jPanel) {
  51. jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.Y_AXIS));
  52. jPanel.add(createHeader());
  53. jPanel.add(createVerticalSpace());
  54. jPanel.add(createButtons());
  55. jPanel.add(createInfo());
  56. jPanel.add(createCheckboxes());
  57. }
  58. private Box createCheckboxes() {
  59. Box hbox=Box.createHorizontalBox();
  60. hbox.setAlignmentY(Component.TOP_ALIGNMENT);
  61. {
  62. Box vbox = Box.createVerticalBox();
  63. vbox.setAlignmentY(Component.TOP_ALIGNMENT);
  64. vbox.add(newCheckbox("opm"));
  65. vbox.add(newCheckbox("fct"));
  66. vbox.add(newCheckbox("rdyen"));
  67. vbox.add(newCheckbox("fault"));
  68. vbox.add(newCheckbox("warn"));
  69. vbox.add(newCheckbox("open"));
  70. vbox.add(newCheckbox("enabled"));
  71. hbox.add(vbox);
  72. }
  73. {
  74. Box vbox = Box.createVerticalBox();
  75. vbox.setAlignmentY(Component.TOP_ALIGNMENT);
  76. vbox.add(newCheckbox("ref"));
  77. vbox.add(newCheckbox("still"));
  78. vbox.add(newCheckbox("dev"));
  79. vbox.add(newCheckbox("mov"));
  80. vbox.add(newCheckbox("teach"));
  81. vbox.add(newCheckbox("mc"));
  82. vbox.add(newCheckbox("ack"));
  83. vbox.add(newCheckbox("halt"));
  84. hbox.add(vbox);
  85. }
  86. {
  87. Box vbox = Box.createVerticalBox();
  88. vbox.setAlignmentY(Component.TOP_ALIGNMENT);
  89. vbox.add(newCheckbox("func"));
  90. vbox.add(newCheckbox("fgrp"));
  91. vbox.add(newCheckbox("fnum"));
  92. vbox.add(newCheckbox("com"));
  93. vbox.add(newCheckbox("abs"));
  94. vbox.add(newCheckbox("istMoment"));
  95. vbox.add(newCheckbox("istPosition"));
  96. hbox.add(vbox);
  97. }
  98. return hbox;
  99. }
  100. private JCheckBox newCheckbox(String name) {
  101. return new JCheckBox(name);
  102. }
  103. private Box createHeader() {
  104. Box headerBox = Box.createHorizontalBox();
  105. headerBox.setAlignmentX(Component.CENTER_ALIGNMENT);
  106. JLabel header = new JLabel("Festo Axis Control");
  107. header.setFont(header.getFont().deriveFont(Font.BOLD, HEADER_FONT_SIZE));
  108. headerBox.add(header);
  109. return headerBox;
  110. }
  111. private Box createButtons() {
  112. Box box = Box.createHorizontalBox();
  113. JButton left100 = new JButton("<<<1000");
  114. left100.addActionListener(new AbstractAction() {
  115. @Override
  116. public void actionPerformed(ActionEvent e) {
  117. Activator.daemonInterface.rel(-1000, 20);
  118. }
  119. });
  120. box.add(left100);
  121. JButton left10 = new JButton("<<100");
  122. left10.addActionListener(new AbstractAction() {
  123. @Override
  124. public void actionPerformed(ActionEvent e) {
  125. Activator.daemonInterface.rel(-100, 20);
  126. }
  127. });
  128. box.add(left10);
  129. JButton left1 = new JButton("<10");
  130. left1.addActionListener(new AbstractAction() {
  131. @Override
  132. public void actionPerformed(ActionEvent e) {
  133. Activator.daemonInterface.rel(-10, 20);
  134. }
  135. });
  136. box.add(left1);
  137. JButton right1 = new JButton("10>");
  138. right1.addActionListener(new AbstractAction() {
  139. @Override
  140. public void actionPerformed(ActionEvent e) {
  141. Activator.daemonInterface.rel(10, 20);
  142. }
  143. });
  144. box.add(right1);
  145. JButton right10 = new JButton("100>>");
  146. right10.addActionListener(new AbstractAction() {
  147. @Override
  148. public void actionPerformed(ActionEvent e) {
  149. Activator.daemonInterface.rel(100, 20);
  150. }
  151. });
  152. box.add(right10);
  153. JButton right100 = new JButton("1000>>>");
  154. right100.addActionListener(new AbstractAction() {
  155. @Override
  156. public void actionPerformed(ActionEvent e) {
  157. Activator.daemonInterface.rel(1000, 20);
  158. }
  159. });
  160. box.add(right100);
  161. return box;
  162. }
  163. private Box createInfo() {
  164. Box infoBox = Box.createVerticalBox();
  165. infoBox.setAlignmentX(Component.CENTER_ALIGNMENT);
  166. demoToolStatus = new JLabel();
  167. demoToolStatus.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  168. infoBox.add(demoToolStatus);
  169. return infoBox;
  170. }
  171. private Component createVerticalSpace() {
  172. return Box.createRigidArea(new Dimension(0, VERTICAL_SPACE));
  173. }
  174. private void updateText(String text) {
  175. SwingUtilities.invokeLater(() -> demoToolStatus.setText("<HTML>" + text +"</HTML>"));
  176. }
  177. }