package ca.phon.opgraph.nodes.general;

import ca.phon.opgraph.InputField;
import ca.phon.opgraph.OpContext;
import ca.phon.opgraph.OpNode;
import ca.phon.opgraph.OpNodeInfo;
import ca.phon.opgraph.app.GraphDocument;
import ca.phon.opgraph.app.extensions.NodeSettings;
import ca.phon.opgraph.exceptions.ProcessingException;
import org.w3c.dom.Node;

import javax.swing.*;
import java.awt.*;
import java.util.Properties;

@OpNodeInfo(name="Set Global", description = "Set global variable", category = "General", showInLibrary = true)
public class SetGlobalNode extends OpNode implements NodeSettings {

	private final InputField globalNameInput = new InputField("globalName", "Global variable name",
		true, true, String.class);

	private final InputField globalValueInput = new InputField("value", "Value for global variable",
			false, true, Object.class);

	private JPanel settingsPanel;
	private JTextField globalNameField;

	public SetGlobalNode() {
		super();

		putField(globalNameInput);
		putField(globalValueInput);

		putExtension(NodeSettings.class, this);
	}

	@Override
	public void operate(OpContext context) throws ProcessingException {
		OpContext globalContext = context;
		while(globalContext.getParent() != null)
			globalContext = globalContext.getParent();

		String globalName = context.get(globalNameInput).toString();
		Object value = context.get(globalValueInput);

		globalContext.put(globalName, value);
	}

	public String getGlobalName() {
		return (this.globalNameField != null ? this.globalNameField.getText().trim() :  this.globalName);
	}

	public void setGlobalName(String globalName) {
		this.globalName = globalName.trim();
		if(this.globalNameField != null)
			this.globalNameField.setText(this.globalName);
	}

	@Override
	public Component getComponent(GraphDocument document) {
		if(this.settingsPanel == null) {
			this.settingsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));

			this.settingsPanel.add(new JLabel("Global name:"));
			this.globalNameField = new JTextField();
			this.globalNameField.setColumns(30);
			this.globalNameField.setText(this.globalName);
			this.settingsPanel.add(this.globalNameField);
		}
		return this.settingsPanel;
	}

	@Override
	public Properties getSettings() {
		Properties props = new Properties();
		props.put("globalName", getGlobalName());
		return props;
	}

	@Override
	public void loadSettings(Properties properties) {
		String globalName = properties.getProperty("globalName");
		setGlobalName(globalName);
	}

}

@@ -921,16 +921,50 @@
 Field;%0A%0A
+%09private String globalName = %22%22;%0A%0A
 %09public 
@@ -1332,48 +1332,241 @@
 e = 
-context.get(globalNameInput).toString();
+(context.get(globalNameInput) != null%0A%09%09%09%09? context.get(globalNameInput).toString()%0A%09%09%09%09: getGlobalName());%0A%09%09if(globalName == null %7C%7C globalName.length() == 0)%0A%09%09%09throw new ProcessingException(null, %22Invalid global variable name%22);%0A
 %0A%09%09O
@@ -2501,16 +2501,47 @@
 ties();%0A
+%09%09if(getGlobalName() != null)%0A%09
 %09%09props.
@@ -2718,16 +2718,42 @@
 Name%22);%0A
+%09%09if(globalName != null)%0A%09
 %09%09setGlo

Fix save/load of settings