MyToolbarContribution.java 7.9 KB

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