MyToolbarContribution.java 6.9 KB

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