MyDaemonProgramNodeContribution.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.InputTextField;
  10. import com.ur.urcap.api.ui.component.LabelComponent;
  11. import java.awt.*;
  12. import java.util.Timer;
  13. import java.util.TimerTask;
  14. public class MyDaemonProgramNodeContribution implements ProgramNodeContribution {
  15. private static final String NAME = "name";
  16. private final DataModel model;
  17. private final URCapAPI api;
  18. private Timer uiTimer;
  19. public MyDaemonProgramNodeContribution(URCapAPI api, DataModel model) {
  20. this.api = api;
  21. this.model = model;
  22. }
  23. @Input(id = "yourname")
  24. private InputTextField nameTextField;
  25. @Label(id = "titlePreviewLabel")
  26. private LabelComponent titlePreviewLabel;
  27. @Label(id = "messagePreviewLabel")
  28. private LabelComponent messagePreviewLabel;
  29. @Input(id = "yourname")
  30. public void onInput(InputEvent event) {
  31. if (event.getEventType() == InputEvent.EventType.ON_CHANGE) {
  32. setName(nameTextField.getText());
  33. updatePreview();
  34. }
  35. }
  36. @Override
  37. public void openView() {
  38. nameTextField.setText(getName());
  39. //UI updates from non-GUI threads must use EventQueue.invokeLater (or SwingUtilities.invokeLater)
  40. uiTimer = new Timer(true);
  41. uiTimer.schedule(new TimerTask() {
  42. @Override
  43. public void run() {
  44. EventQueue.invokeLater(new Runnable() {
  45. @Override
  46. public void run() {
  47. updatePreview();
  48. }
  49. });
  50. }
  51. }, 0, 1000);
  52. }
  53. @Override
  54. public void closeView() {
  55. uiTimer.cancel();
  56. }
  57. @Override
  58. public String getTitle() {
  59. return "My Daemon: " + (model.isSet(NAME) ? getName() : "");
  60. }
  61. @Override
  62. public boolean isDefined() {
  63. // return getInstallation().isDefined() && !getName().isEmpty();
  64. return !getName().isEmpty();
  65. }
  66. @Override
  67. public void generateScript(ScriptWriter writer) {
  68. // Interact with the daemon process through XML-RPC calls
  69. // Note, alternatively plain sockets can be used.
  70. writer.assign("mydaemon_message", getInstallation().getXMLRPCVariable() + ".get_message(\"" + getName() + "\")");
  71. writer.assign("mydaemon_title", getInstallation().getXMLRPCVariable() + ".get_title()");
  72. writer.appendLine("popup(mydaemon_message, mydaemon_title, False, False, blocking=True)");
  73. writer.writeChildren();
  74. }
  75. private void updatePreview() {
  76. String title = "";
  77. String message = "";
  78. try {
  79. // Provide a real-time preview of the daemon state
  80. title = getInstallation().getDaemonInterface().getTitle();
  81. message = getInstallation().getDaemonInterface().getMessage(getName());
  82. } catch (Exception e) {
  83. System.err.println("Could not retrieve essential data from the daemon process for the preview.");
  84. title = message = "<Daemon disconnected>";
  85. }
  86. titlePreviewLabel.setText(title);
  87. messagePreviewLabel.setText(message);
  88. }
  89. private String getName() {
  90. return model.get(NAME, "");
  91. }
  92. private void setName(String name) {
  93. if ("".equals(name)){
  94. model.remove(NAME);
  95. }else{
  96. model.set(NAME, name);
  97. }
  98. }
  99. private MyDaemonInstallationNodeContribution getInstallation(){
  100. return api.getInstallationNode(MyDaemonInstallationNodeContribution.class);
  101. }
  102. }