my current situation is that I have created an own JPanel which is represented as one of my tabs in my NodeDialog.
This JPanel takes input data from previous nodes (That's why I use DataAwareNodeDialogPane).
The JPanel has two methods which will create two SQLQueries when the user has done his input.
I want to do two things:
1) I want to call both methods when the user presses "apply" or "ok" on the NodeDialog. I know there is a method onClose() which works perfectly. However the query is also generated when he just closes NodeDialog. It's not really a problem. I just wanted to ask if someone has a better idea/approach.
2) The real problem is that I can't store this query in a SettingsModelString because I need a DialogComponent for this. So I need to declare a public static String variable for this. Set the value when the dialog is closed.
And get the value of this String in my NodeModel by NodeDialog.'variablename'. Is there not any better for the communication between NodeModel and NodeDialog?
Could you make it register that the dialog has changes and trigger the configure method in the NodeModel class ot run your code? This should only excute after your dialog has closed and the user wishes ot enact the chances (cancel wouldn't run it). Although this would be useless if you need the dialog to update based on yout SQL statement.
You may be able to add a listener or identify in the onClose() method what button was pressed but I've not looked into this.
For point 2:
> The real problem is that I can't store this query in a SettingsModelString because I need a DialogComponent for this.
Using static variables to communicate changing values between nodes will fail when you have more than one node.
You need to use a SettingsModel class for this, override
public void saveAdditionalSettingsTo(NodeSettingsWO settings) throws InvalidSettingsException
in your NodeDialog if you want to save a SettingsModel object that isn't used in a DialogComponent. You can use them without using a DialogComponent. I find it easier to create my own DialogComponents as I can re-use them.
With public void saveAdditionalSettingsTo(NodeSettingsWO settings) throws InvalidSettingsException.
I just could save the two SQLQueries in the NodeSettings. So if it gets changed I could reset the node without creating a SettignsModel + DialogComponent.
I loaded the String I put into the NodeSettings in the NodeModel and could use it. This solved both my problems :D
The only remaining problem is that I use JTree for creating these queries. I hope I somehow can save them in the NodeSettings, too. It will be difficul because I can't save TreeNodes in the NodeSettings. (Especially with hierarchy) Let's see if I find a way!
The abstract port model I think uses a tree-like object of some sort, and saves to a settings model - it might be worth a look, along with some subclasses - https://tech.knime.org/docs/api/org/knime/core/node/port/AbstractSimplePortObject.html
I converted the DefaultTreeModel to an array of bytes and saved it to the settings.
I loaded the bytes with the settins and converted them back to the treeModel. After that I just set: jTree.setModel(treeModel) where the 'treeModel' is the loaded one.
public static byte[] convertToBytes(Object object) throws IOException {
try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(bos)) {
out.writeObject(object);
return bos.toByteArray();
}
}
public static Object convertFromBytes(byte[] bytes) throws IOException, ClassNotFoundException {
try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes); ObjectInput in = new ObjectInputStream(bis)) {
return in.readObject();
}
}
When the saveAdditionalSettingsTo method be called?
I have a scenario,i need load some config when use open dialog and then NodeModel to read the config.
and user will input information to update the config in a new JFrame.I need to track those values from JFrame in NodeModel