MyToolbarContribution.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. }
  46. private Box createHeader() {
  47. Box headerBox = Box.createHorizontalBox();
  48. headerBox.setAlignmentX(Component.CENTER_ALIGNMENT);
  49. JLabel header = new JLabel("Festo Axis Control");
  50. header.setFont(header.getFont().deriveFont(Font.BOLD, HEADER_FONT_SIZE));
  51. headerBox.add(header);
  52. return headerBox;
  53. }
  54. private Box createButtons() {
  55. Box box = Box.createHorizontalBox();
  56. JButton left100 = new JButton("<<<1000");
  57. left100.addActionListener(new AbstractAction() {
  58. @Override
  59. public void actionPerformed(ActionEvent e) {
  60. Activator.daemonInterface.rel(-1000, 20);
  61. }
  62. });
  63. box.add(left100);
  64. JButton left10 = new JButton("<<100");
  65. left10.addActionListener(new AbstractAction() {
  66. @Override
  67. public void actionPerformed(ActionEvent e) {
  68. Activator.daemonInterface.rel(-100, 20);
  69. }
  70. });
  71. box.add(left10);
  72. JButton left1 = new JButton("<10");
  73. left1.addActionListener(new AbstractAction() {
  74. @Override
  75. public void actionPerformed(ActionEvent e) {
  76. Activator.daemonInterface.rel(-10, 20);
  77. }
  78. });
  79. box.add(left1);
  80. JButton right1 = new JButton("10>");
  81. right1.addActionListener(new AbstractAction() {
  82. @Override
  83. public void actionPerformed(ActionEvent e) {
  84. Activator.daemonInterface.rel(10, 20);
  85. }
  86. });
  87. box.add(right1);
  88. JButton right10 = new JButton("100>>");
  89. right10.addActionListener(new AbstractAction() {
  90. @Override
  91. public void actionPerformed(ActionEvent e) {
  92. Activator.daemonInterface.rel(100, 20);
  93. }
  94. });
  95. box.add(right10);
  96. JButton right100 = new JButton("1000>>>");
  97. right100.addActionListener(new AbstractAction() {
  98. @Override
  99. public void actionPerformed(ActionEvent e) {
  100. Activator.daemonInterface.rel(1000, 20);
  101. }
  102. });
  103. box.add(right100);
  104. return box;
  105. }
  106. private Box createInfo() {
  107. Box infoBox = Box.createVerticalBox();
  108. infoBox.setAlignmentX(Component.CENTER_ALIGNMENT);
  109. // JLabel pane1 = new JLabel();
  110. // pane1.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  111. // pane1.setText("<HTML>This is a sample URCap Toolbar contribution. Feel free to use this as an example for creating new contributions.</HTML>");
  112. // pane1.setBackground(infoBox.getBackground());
  113. // infoBox.add(pane1);
  114. //
  115. // JLabel pane2 = new JLabel();
  116. // Locale locale = context.getAPIProvider().getSystemAPI().getSystemSettings().getLocalization().getLocale();
  117. // pane2.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  118. // pane2.setText("<HTML>Currently, the robot is configured to use the Locale: " + locale.getDisplayName() + "</HTML>");
  119. // infoBox.add(pane2);
  120. demoToolStatus = new JLabel();
  121. demoToolStatus.setText("<HTML>" + get3rdPartyStatus() +"</HTML>");
  122. demoToolStatus.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  123. infoBox.add(demoToolStatus);
  124. return infoBox;
  125. }
  126. private Component createVerticalSpace() {
  127. return Box.createRigidArea(new Dimension(0, VERTICAL_SPACE));
  128. }
  129. private String get3rdPartyStatus() {
  130. Date now = new Date();
  131. int curPos=Activator.daemonInterface.getpos();
  132. String curStatus=Activator.daemonInterface.getstatus();
  133. return String.format("Position: %d, read at %tF %tT.<br/>%s", curPos, now, now, curStatus);
  134. }
  135. }