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

import org.eclipse.gef.ui.actions.SelectionAction;
import org.openlca.app.App;
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.GraphEditPart;
import org.openlca.app.editors.graphical.edit.NodeEditPart;
import org.openlca.app.editors.systems.ProductSystemInfoPage;
import org.openlca.app.rcp.images.Icon;

public class OpenEditorAction extends SelectionAction {

	private final GraphEditor editor;
	/**
	 * The selected on which the action is called.
	 */
	private Object object;

	public OpenEditorAction(GraphEditor part) {
		super(part);
		editor = part;
		setId(ActionIds.OPEN_EDITOR);
		setText(M.OpenInEditor);
		setImageDescriptor(Icon.FOLDER.descriptor());
	}

	@Override
	protected boolean calculateEnabled() {
		var objects = getSelectedObjects();
		if (objects.size() != 1)
			return false;
		object = objects.get(0);
		return ((object instanceof GraphEditPart)
			|| (NodeEditPart.class.isAssignableFrom(object.getClass()))
			|| (object instanceof ExchangeEditPart));
	}


	@Override
	public void run() {
		if (object instanceof GraphEditPart) {
			var systemEditor = editor.getProductSystemEditor();
			systemEditor.setActivePage(ProductSystemInfoPage.ID);
		}
		if (NodeEditPart.class.isAssignableFrom(object.getClass())) {
			var node = ((NodeEditPart) object).getModel();
			App.open(node.descriptor);
		}
		if (object instanceof ExchangeEditPart exchangeEditPart)
			App.open(exchangeEditPart.getModel().exchange.flow);
	}

}

@@ -780,35 +780,8 @@
 R);%0A
-%09%09setText(M.OpenInEditor);%0A
 %09%09se
@@ -820,24 +820,24 @@
 criptor());%0A
+
 %09%7D%0A%0A%09@Overri
@@ -988,16 +988,69 @@
 get(0);%0A
+%09%09setText(M.OpenInEditor + %22: %22 + getObjectName());%0A%0A
 %09%09return
@@ -1197,16 +1197,329 @@
 ));%0A%09%7D%0A%0A
+%09protected String getObjectName() %7B%0A%09%09if (object == null)%0A%09%09%09return %22%22;%0A%0A%09%09if (object instanceof GraphEditPart)%0A%09%09%09return M.ProductSystem;%0A%09%09else if (NodeEditPart.class.isAssignableFrom(object.getClass()))%0A%09%09%09return M.Process;%0A%09%09else if (object instanceof ExchangeEditPart)%0A%09%09%09return M.Flow;%0A%09%09else return %22%22;%0A%09%7D%0A
 %0A%09@Overr

Add more specific messages for the `OpenEditorAction`.