MyToolbarContribution.java 7.4 KB

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