package org.openlca.app.editors.graphical.actions;

import org.eclipse.gef.GraphicalViewer;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.ui.actions.SelectionAction;
import org.eclipse.osgi.util.NLS;
import org.openlca.app.M;
import org.openlca.app.editors.graphical.GraphEditor;
import org.openlca.app.editors.graphical.edit.ExchangeEditPart;
import org.openlca.app.editors.graphical.edit.IOPaneEditPart;
import org.openlca.app.rcp.images.Images;
import org.openlca.core.model.*;

import static org.openlca.app.editors.graphical.requests.GraphRequestConstants.REQ_ADD_INPUT_EXCHANGE;
import static org.openlca.app.editors.graphical.requests.GraphRequestConstants.REQ_ADD_OUTPUT_EXCHANGE;


public class AddExchangeAction extends SelectionAction {

	private final boolean forInput;
	Request request;

	public AddExchangeAction(GraphEditor part, boolean forInput) {
		super(part);
		this.forInput = forInput;
		request = new Request(forInput
			? REQ_ADD_INPUT_EXCHANGE
			: REQ_ADD_OUTPUT_EXCHANGE);
		setId(forInput
			?	ActionIds.ADD_INPUT_EXCHANGE
			: ActionIds.ADD_OUTPUT_EXCHANGE);
		setText(forInput ? M.AddInputFlow : M.AddOutputFlow);
		setImageDescriptor(Images.descriptor(ModelType.FLOW));
	}

	@Override
	protected boolean calculateEnabled() {
		var command = getCommand();
		if (command == null)
			return false;
		return command.canExecute();
	}

	@Override
	public void run() {
		execute(getCommand());
	}

	private Command getCommand() {
		if (getSelectedObjects().isEmpty())
			return null;

		CompoundCommand cc = new CompoundCommand();
		cc.setDebugLabel("Add " + (forInput ? "input" : "output") + " exchange");

		var parts = getSelectedObjects();
		for (Object o : parts) {
			var pane = o instanceof IOPaneEditPart paneEditPart
				? paneEditPart.getModel()
				: o instanceof ExchangeEditPart exchangeEditPart
				? exchangeEditPart.getModel().getIOPane()
				: null;
			if (pane == null || pane.isForInputs() != forInput)
				return null;
			var viewer = (GraphicalViewer) getWorkbenchPart().getAdapter(
				GraphicalViewer.class);
			var registry = viewer.getEditPartRegistry();
			var paneEditPart = (IOPaneEditPart) registry.get(pane);
			if (paneEditPart == null)
				continue;
			cc.add(paneEditPart.getCommand(request));
			}
		return cc.unwrap();
	}

}

@@ -2203,107 +2203,74 @@
 %0A%09%09%09
-var paneEditPart = (IOPaneEditPart) registry.get(pane);%0A%09%09%09if (paneEditPart == null)%0A%09%09%09%09continue;%0A
+if (registry.get(pane) instanceof IOPaneEditPart paneEditPart) %7B%0A%09
 %09%09%09c
@@ -2315,16 +2315,20 @@
 );%0A%09%09%09%7D%0A
+%09%09%7D%0A
 %09%09return

Add condition on null value in the AddExchangeAction.