MyDaemonProgramNodeContribution.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. }
  70. @Override
  71. public String getTitle() {
  72. return "My Daemon: " + (model.isSet(POS) ? getPos() : "");
  73. }
  74. @Override
  75. public boolean isDefined() {
  76. // return getInstallation().isDefined() && !getName().isEmpty();
  77. return !getPos().isEmpty();
  78. }
  79. @Override
  80. public void generateScript(ScriptWriter writer) {
  81. // Interact with the daemon process through XML-RPC calls
  82. // Note, alternatively plain sockets can be used.
  83. // if(radioRelButton.isSelected()) {
  84. // writer.appendLine( getInstallation().getXMLRPCVariable() + ".rel(\"" + getName() + "\")");
  85. // }
  86. // else if(radioAbsButton.isSelected()) {
  87. // writer.appendLine( getInstallation().getXMLRPCVariable() + ".abs(\"" + getName() + "\")");
  88. // }
  89. writer.appendLine( getInstallation().getXMLRPCVariable() + ".rel(\"" + getPos() + "\")");
  90. writer.writeChildren();
  91. }
  92. private void updatePreview() {
  93. titlePreviewLabel.setText("pos: " + (radioAbsButton.isSelected()?"Abs":"Rel"));
  94. //messagePreviewLabel.setText(message);
  95. }
  96. private String getPos() {
  97. return model.get(POS, "0");
  98. }
  99. private void setPos(String name) {
  100. if ("".equals(name)){
  101. model.remove(POS);
  102. }else{
  103. model.set(POS, name);
  104. }
  105. }
  106. private boolean isAbsolute() {
  107. return model.get(ABSOLUTE, true);
  108. }
  109. private void setAbsolute(boolean absolute) {
  110. model.set(ABSOLUTE, absolute);
  111. }
  112. private MyDaemonInstallationNodeContribution getInstallation(){
  113. return api.getInstallationNode(MyDaemonInstallationNodeContribution.class);
  114. }
  115. }