MyDaemonProgramNodeContribution.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package uraxis.impl;
  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. }
  65. @Override
  66. public void generateScript(ScriptWriter writer) {
  67. // Interact with the daemon process through XML-RPC calls
  68. // Note, alternatively plain sockets can be used.
  69. writer.assign("mydaemon_message", getInstallation().getXMLRPCVariable() + ".get_message(\"" + getName() + "\")");
  70. writer.assign("mydaemon_title", getInstallation().getXMLRPCVariable() + ".get_title()");
  71. writer.appendLine("popup(mydaemon_message, mydaemon_title, False, False, blocking=True)");
  72. writer.writeChildren();
  73. }
  74. private void updatePreview() {
  75. String title = "";
  76. String message = "";
  77. try {
  78. // Provide a real-time preview of the daemon state
  79. title = getInstallation().getDaemonInterface().getTitle();
  80. message = getInstallation().getDaemonInterface().getMessage(getName());
  81. } catch (Exception e) {
  82. System.err.println("Could not retrieve essential data from the daemon process for the preview.");
  83. title = message = "<Daemon disconnected>";
  84. }
  85. titlePreviewLabel.setText(title);
  86. messagePreviewLabel.setText(message);
  87. }
  88. private String getName() {
  89. return model.get(NAME, "");
  90. }
  91. private void setName(String name) {
  92. if ("".equals(name)){
  93. model.remove(NAME);
  94. }else{
  95. model.set(NAME, name);
  96. }
  97. }
  98. private MyDaemonInstallationNodeContribution getInstallation(){
  99. return api.getInstallationNode(MyDaemonInstallationNodeContribution.class);
  100. }
  101. }