123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- package uraxis;
- import com.ur.urcap.api.contribution.ProgramNodeContribution;
- import com.ur.urcap.api.domain.URCapAPI;
- import com.ur.urcap.api.domain.data.DataModel;
- import com.ur.urcap.api.domain.script.ScriptWriter;
- import com.ur.urcap.api.ui.annotation.Input;
- import com.ur.urcap.api.ui.annotation.Label;
- import com.ur.urcap.api.ui.component.InputEvent;
- import com.ur.urcap.api.ui.component.InputRadioButton;
- import com.ur.urcap.api.ui.component.InputTextField;
- import com.ur.urcap.api.ui.component.LabelComponent;
- import java.awt.*;
- import java.util.Timer;
- import java.util.TimerTask;
- public class MyDaemonProgramNodeContribution implements ProgramNodeContribution {
- private static final String POS = "pos";
- private static final String ABSOLUTE = "absolute";
- private final DataModel model;
- private final URCapAPI api;
- private Timer uiTimer;
- public MyDaemonProgramNodeContribution(URCapAPI api, DataModel model) {
- this.api = api;
- this.model = model;
- }
- @Input(id = "pos")
- private InputTextField posTextField;
- @Input(id = "rbAbs")
- private InputRadioButton radioAbsButton;
- @Input(id = "rbRel")
- private InputRadioButton radioRelButton;
- @Label(id = "titlePreviewLabel")
- private LabelComponent titlePreviewLabel;
- @Label(id = "messagePreviewLabel")
- private LabelComponent messagePreviewLabel;
- @Input(id = "pos")
- public void onInput(InputEvent event) {
- if (event.getEventType() == InputEvent.EventType.ON_CHANGE) {
- setPos(posTextField.getText());
- updatePreview();
- }
- }
- @Override
- public void openView() {
- posTextField.setText(getPos());
- if(isAbsolute())
- radioAbsButton.setSelected();
- else
- radioRelButton.setSelected();
- //UI updates from non-GUI threads must use EventQueue.invokeLater (or SwingUtilities.invokeLater)
- uiTimer = new Timer(true);
- uiTimer.schedule(new TimerTask() {
- @Override
- public void run() {
- EventQueue.invokeLater(new Runnable() {
- @Override
- public void run() {
- updatePreview();
- }
- });
- }
- }, 0, 1000);
- }
- @Override
- public void closeView() {
- uiTimer.cancel();
- if(radioAbsButton.isSelected())
- model.set(ABSOLUTE, true);
- else model.set(ABSOLUTE, false);
- }
- @Override
- public String getTitle() {
- return "My Daemon: " + (model.isSet(POS) ? getPos() : "");
- }
- @Override
- public boolean isDefined() {
- // return getInstallation().isDefined() && !getName().isEmpty();
- return !getPos().isEmpty();
- }
- @Override
- public void generateScript(ScriptWriter writer) {
- // Interact with the daemon process through XML-RPC calls
- // Note, alternatively plain sockets can be used.
- // if(radioRelButton.isSelected()) {
- // writer.appendLine( getInstallation().getXMLRPCVariable() + ".rel(\"" + getName() + "\")");
- // }
- // else if(radioAbsButton.isSelected()) {
- // writer.appendLine( getInstallation().getXMLRPCVariable() + ".abs(\"" + getName() + "\")");
- // }
- writer.appendLine( getInstallation().getXMLRPCVariable() + ".rel(\"" + getPos() + "\")");
- writer.writeChildren();
- }
- private void updatePreview() {
- titlePreviewLabel.setText("pos: " + (radioAbsButton.isSelected()?"Abs":"Rel"));
- //messagePreviewLabel.setText(message);
- }
- private String getPos() {
- return model.get(POS, "");
- }
- private void setPos(String name) {
- if ("".equals(name)){
- model.remove(POS);
- }else{
- model.set(POS, name);
- }
- }
- private boolean isAbsolute() {
- return model.get(ABSOLUTE, true);
- }
- private void setAbsolute(boolean absolute) {
- model.set(ABSOLUTE, absolute);
- }
- private MyDaemonInstallationNodeContribution getInstallation(){
- return api.getInstallationNode(MyDaemonInstallationNodeContribution.class);
- }
- }
|