Hi Ebbe
Sorry for the late response. I have reproduced the problem in the following code (zip attached):
Contribution:
package com.DVInc.TestURCap.impl;
import com.ur.urcap.api.contribution.InstallationNodeContribution;
import com.ur.urcap.api.contribution.installation.InstallationAPIProvider;
import com.ur.urcap.api.domain.data.DataModel;
import com.ur.urcap.api.domain.script.ScriptWriter;
import com.ur.urcap.api.domain.userinteraction.keyboard.KeyboardInputCallback;
import com.ur.urcap.api.domain.userinteraction.keyboard.KeyboardInputFactory;
import com.ur.urcap.api.domain.userinteraction.keyboard.KeyboardTextInput;
public class TestURCapInstallationNodeContribution implements InstallationNodeContribution{
private final KeyboardInputFactory keyboardFactory;
private final TestURCapInstallationNodeView view;
private DataModel model;
private static final String KEY_TEXTFIELDINPUT = "TestURCapTextFieldInput";
private static final String DEFAULTVALUE_TEXTFIELDINPUT = "Default";
public TestURCapInstallationNodeContribution(InstallationAPIProvider apiProvider, DataModel model, TestURCapInstallationNodeView view) {
this.keyboardFactory = apiProvider.getUserInterfaceAPI().getUserInteraction().getKeyboardInputFactory();
this.view = view;
this.model = model;
}
@Override
public void openView() {
String storedValue = model.get(KEY_TEXTFIELDINPUT, DEFAULTVALUE_TEXTFIELDINPUT);
System.out.println("In Openview(), value stored in DataModel: "+storedValue);
view.setDataModelContentText(storedValue);
}
@Override
public void closeView() {
}
@Override
public void generateScript(ScriptWriter writer) {
}
public void setValueDuringInitializing() {
String newModelValue = "Set after Initializing";
System.out.println("In method setValueDuringInitializin(), is key textFieldInput set in model?: "+Boolean.toString(model.isSet(KEY_TEXTFIELDINPUT)));
System.out.println("In method setValueDuringInitializin(), value stored in dataModel: "+model.get(KEY_TEXTFIELDINPUT, DEFAULTVALUE_TEXTFIELDINPUT));
System.out.println("In method setValueDuringInitializin(), new value stored in dataModel: "+newModelValue);
model.set(KEY_TEXTFIELDINPUT, newModelValue);
}
public KeyboardTextInput getKeyboardTextInput() {
return keyboardFactory.createStringKeyboardInput();
}
public KeyboardInputCallback<String> getKeyboardTextInputCallback() {
return new KeyboardInputCallback<String>() {
@Override
public void onOk(String value) {
view.setInputFieldText(value);
model.set(KEY_TEXTFIELDINPUT, value);
}
};
}
}
View:
package com.DVInc.TestURCap.impl;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import com.ur.urcap.api.contribution.installation.swing.SwingInstallationNodeView;
import com.ur.urcap.api.domain.userinteraction.keyboard.KeyboardTextInput;
public class TestURCapInstallationNodeView implements SwingInstallationNodeView<TestURCapInstallationNodeContribution>{
private Style style;
private JTextField inputText;
private JTextArea dataModelContentText;
public TestURCapInstallationNodeView(Style style) {
this.style = style;
}
@Override
public void buildUI(JPanel panel, TestURCapInstallationNodeContribution contribution) {
panel.setAlignmentX(Component.LEFT_ALIGNMENT);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(createInput(contribution));
panel.add(createVerticalSpacing());
panel.add(createDataModelCheck());
contribution.setValueDuringInitializing();
}
private JPanel createInput(final TestURCapInstallationNodeContribution contribution) {
JPanel inputPanel = new JPanel();
inputPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.Y_AXIS));
JTextArea messageText = new JTextArea();
messageText.setAlignmentX(Component.LEFT_ALIGNMENT);
messageText.setBorder(BorderFactory.createEmptyBorder());
messageText.setBackground(inputPanel.getBackground());
messageText.setText("Enter something to be stored in dataModel:");
messageText.setFont(style.getTextFont());
messageText.setMaximumSize(messageText.getPreferredSize());
inputText = new JTextField();
inputText.setAlignmentX(Component.LEFT_ALIGNMENT);
inputText.setPreferredSize(style.getInputFieldSize());
inputText.setMaximumSize(inputText.getPreferredSize());
inputText.setText("");
inputText.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
KeyboardTextInput keyboardInput = contribution.getKeyboardTextInput();
keyboardInput.show(inputText, contribution.getKeyboardTextInputCallback());
}
});
inputPanel.add(messageText);
inputPanel.add(createVerticalSpacing());
inputPanel.add(inputText);
return inputPanel;
}
private JPanel createDataModelCheck() {
JPanel dataModelContentPanel = new JPanel();
dataModelContentPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
dataModelContentPanel.setLayout(new BoxLayout(dataModelContentPanel, BoxLayout.Y_AXIS));
JTextArea messageText = new JTextArea();
messageText.setAlignmentX(Component.LEFT_ALIGNMENT);
messageText.setBorder(BorderFactory.createEmptyBorder());
messageText.setBackground(dataModelContentPanel.getBackground());
messageText.setText("Value of inputTextField stored in DataModel is:");
messageText.setFont(style.getTextFont());
messageText.setMaximumSize(messageText.getPreferredSize());
dataModelContentText = new JTextArea();
dataModelContentText.setAlignmentX(Component.LEFT_ALIGNMENT);
dataModelContentText.setBorder(BorderFactory.createEmptyBorder());
dataModelContentText.setBackground(dataModelContentPanel.getBackground());
dataModelContentText.setText("");
dataModelContentText.setFont(style.getTextFont());
dataModelContentPanel.add(messageText);
dataModelContentPanel.add(createVerticalSpacing());
dataModelContentPanel.add(dataModelContentText);
return dataModelContentPanel;
}
public void setInputFieldText(String message) {
inputText.setText(message);
}
public void setDataModelContentText(String message) {
dataModelContentText.setText(message);
}
private Component createVerticalSpacing() {
return Box.createRigidArea(new Dimension(0, style.getVerticalSpacing()));
}
}
If you install this on the robot, enter a value in the textField and save the installation. Then reboot and from the “System.out.println()” comments you will see that in the method “setValueDuringInitializing()” the value “Set after Initializing” should be stored in the DataModel. However, once you open the node (and OpenView is executed), the value returned from the model is not this one, but the one stored before the reboot.
Why is this? Why is it not the value written in the method “setValueDuringInitializing()”?
Is also tried to see if calling this method at the end of the contribution constructor or at the end of the View constructor would make a difference, but it didn’t.
Hopefully this makes the problem clear and hopefully there is a solution to this problem.
Furthermore, it could also be informative to know that I created the URCap (the one I developed and also this TestURCap) with API 1.5.0 such that it would already work for SW 3.8.0 and SW 5.2.0.
com.DVInc.TestURCap.zip (59.2 KB)