MyDaemonProgramNodeContribution.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package uraxis;
  2. import com.ur.urcap.api.contribution.ProgramNodeContribution;
  3. import com.ur.urcap.api.domain.URCapAPI;
  4. import com.ur.urcap.api.domain.data.DataModel;
  5. import com.ur.urcap.api.domain.script.ScriptWriter;
  6. import com.ur.urcap.api.ui.annotation.Input;
  7. import com.ur.urcap.api.ui.annotation.Label;
  8. import com.ur.urcap.api.ui.component.InputEvent;
  9. import com.ur.urcap.api.ui.component.InputRadioButton;
  10. import com.ur.urcap.api.ui.component.InputTextField;
  11. import com.ur.urcap.api.ui.component.LabelComponent;
  12. import java.awt.*;
  13. import java.util.Timer;
  14. import java.util.TimerTask;
  15. public class MyDaemonProgramNodeContribution implements ProgramNodeContribution {
  16. private static final String POS = "pos";
  17. private static final String ABSOLUTE = "absolute";
  18. private final DataModel model;
  19. private final URCapAPI api;
  20. private Timer uiTimer;
  21. public MyDaemonProgramNodeContribution(URCapAPI api, DataModel model) {
  22. this.api = api;
  23. this.model = model;
  24. }
  25. @Input(id = "pos")
  26. private InputTextField posTextField;
  27. @Input(id = "rbAbs")
  28. private InputRadioButton radioAbsButton;
  29. @Input(id = "rbRel")
  30. private InputRadioButton radioRelButton;
  31. @Label(id = "titlePreviewLabel")
  32. private LabelComponent titlePreviewLabel;
  33. @Label(id = "messagePreviewLabel")
  34. private LabelComponent messagePreviewLabel;
  35. @Input(id = "pos")
  36. public void onInput(InputEvent event) {
  37. if (event.getEventType() == InputEvent.EventType.ON_CHANGE) {
  38. setPos(posTextField.getText());
  39. updatePreview();
  40. }
  41. }
  42. @Override
  43. public void openView() {
  44. posTextField.setText(getPos());
  45. if(isAbsolute())
  46. radioAbsButton.setSelected();
  47. else
  48. radioRelButton.setSelected();
  49. //UI updates from non-GUI threads must use EventQueue.invokeLater (or SwingUtilities.invokeLater)
  50. uiTimer = new Timer(true);
  51. uiTimer.schedule(new TimerTask() {
  52. @Override
  53. public void run() {
  54. EventQueue.invokeLater(new Runnable() {
  55. @Override
  56. public void run() {
  57. updatePreview();
  58. }
  59. });
  60. }
  61. }, 0, 1000);
  62. }
  63. @Override
  64. public void closeView() {
  65. uiTimer.cancel();
  66. if(radioAbsButton.isSelected())
  67. model.set(ABSOLUTE, true);
  68. else model.set(ABSOLUTE, false);
  69. System.out.println("closeView " + radioAbsButton.isSelected());
  70. }
  71. @Override
  72. public String getTitle() {
  73. return "Axis " + (isAbsolute()?"Abs":"Rel") + (model.isSet(POS) ? getPos() : "");
  74. }
  75. @Override
  76. public boolean isDefined() {
  77. // return getInstallation().isDefined() && !getName().isEmpty();
  78. return !getPos().isEmpty();
  79. }
  80. @Override
  81. public void generateScript(ScriptWriter writer) {
  82. // Interact with the daemon process through XML-RPC calls
  83. // Note, alternatively plain sockets can be used.
  84. // if(radioRelButton.isSelected()) {
  85. // writer.appendLine( getInstallation().getXMLRPCVariable() + ".rel(\"" + getName() + "\")");
  86. // }
  87. // else if(radioAbsButton.isSelected()) {
  88. // writer.appendLine( getInstallation().getXMLRPCVariable() + ".abs(\"" + getName() + "\")");
  89. // }
  90. if(isAbsolute()) {
  91. writer.appendLine( getInstallation().getXMLRPCVariable() + ".abs(\"" + getPos() + "\")");
  92. }
  93. else
  94. writer.appendLine( getInstallation().getXMLRPCVariable() + ".rel(\"" + getPos() + "\")");
  95. writer.writeChildren();
  96. }
  97. private void updatePreview() {
  98. titlePreviewLabel.setText("pos: " + (radioAbsButton.isSelected()?"Abs":"Rel"));
  99. //messagePreviewLabel.setText(message);
  100. }
  101. private String getPos() {
  102. return model.get(POS, "0");
  103. }
  104. private void setPos(String name) {
  105. if ("".equals(name)){
  106. model.remove(POS);
  107. }else{
  108. model.set(POS, name);
  109. }
  110. }
  111. private boolean isAbsolute() {
  112. return model.get(ABSOLUTE, true);
  113. }
  114. private void setAbsolute(boolean absolute) {
  115. model.set(ABSOLUTE, absolute);
  116. }
  117. private MyDaemonInstallationNodeContribution getInstallation(){
  118. return api.getInstallationNode(MyDaemonInstallationNodeContribution.class);
  119. }
  120. }