<commit_before>package org.cytoscape.task.internal.layout;


import static org.cytoscape.view.presentation.property.MinimalVisualLexicon.NODE_X_LOCATION;
import static org.cytoscape.view.presentation.property.MinimalVisualLexicon.NODE_Y_LOCATION;
import static org.cytoscape.view.presentation.property.RichVisualLexicon.NODE_Z_LOCATION;
import static org.cytoscape.view.presentation.property.MinimalVisualLexicon.NETWORK_CENTER_X_LOCATION;
import static org.cytoscape.view.presentation.property.MinimalVisualLexicon.NETWORK_CENTER_Y_LOCATION;
import static org.cytoscape.view.presentation.property.RichVisualLexicon.NETWORK_CENTER_Z_LOCATION;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.cytoscape.event.CyEventHelper;
import org.cytoscape.model.CyNode;
import org.cytoscape.view.model.CyNetworkView;
import org.cytoscape.view.model.View;
import org.cytoscape.work.undo.AbstractCyEdit;


/** An undoable edit that will undo and redo the zooming of a network view. */
final class LayoutEdit extends AbstractCyEdit {
	private final CyEventHelper eventHelper;
	private final CyNetworkView view;
	private List<NodeViewAndLocations> nodeViewsAndLocations;
	private double networkCenterX;
	private double networkCenterY;
	private double networkCenterZ;

	LayoutEdit(final CyEventHelper eventHelper, final CyNetworkView view) {
		super("Apply Preferred Layout");

		this.eventHelper = eventHelper;
		this.view        = view;

		saveNodeViewsAndLocations();
	}

	public void redo() {
		;

		saveAndRestore();
	}

	public void undo() {
		;

		saveAndRestore();
	}

	private void saveAndRestore() {
		final List<NodeViewAndLocations> oldNodeViewsAndLocations = nodeViewsAndLocations;
		saveNodeViewsAndLocations();
		for (final NodeViewAndLocations nodeViewAndLocation : oldNodeViewsAndLocations)
			nodeViewAndLocation.restoreLocations();

		view.setVisualProperty(NETWORK_CENTER_X_LOCATION, networkCenterX);
		view.setVisualProperty(NETWORK_CENTER_Y_LOCATION, networkCenterY);
		view.setVisualProperty(NETWORK_CENTER_Z_LOCATION, networkCenterZ);

		eventHelper.flushPayloadEvents();
		view.updateView();
	}

	private void saveNodeViewsAndLocations() {
		networkCenterX = view.getVisualProperty(NETWORK_CENTER_X_LOCATION);
		networkCenterY = view.getVisualProperty(NETWORK_CENTER_Y_LOCATION);
		networkCenterZ = view.getVisualProperty(NETWORK_CENTER_Z_LOCATION);

		final Collection<View<CyNode>> nodeViews = view.getNodeViews();
		nodeViewsAndLocations = new ArrayList<NodeViewAndLocations>(nodeViews.size());
		for (final View<CyNode> nodeView : nodeViews)
			nodeViewsAndLocations.add(new NodeViewAndLocations(nodeView));
	}
}


final class NodeViewAndLocations {
	private final View<CyNode> nodeView;
	private final double xLocation;
	private final double yLocation;
	private final double zLocation;

	NodeViewAndLocations(final View<CyNode> nodeView) {
		this.nodeView = nodeView;
		xLocation = nodeView.getVisualProperty(NODE_X_LOCATION);
		yLocation = nodeView.getVisualProperty(NODE_Y_LOCATION);
		zLocation = nodeView.getVisualProperty(NODE_Z_LOCATION);
	}<commit_msg>Fixes #547: Fixed LayoutEdit to save/restore network view state properly<commit_after>package org.cytoscape.task.internal.layout;


import static org.cytoscape.view.presentation.property.MinimalVisualLexicon.NODE_X_LOCATION;
import static org.cytoscape.view.presentation.property.MinimalVisualLexicon.NODE_Y_LOCATION;
import static org.cytoscape.view.presentation.property.RichVisualLexicon.NODE_Z_LOCATION;
import static org.cytoscape.view.presentation.property.MinimalVisualLexicon.NETWORK_CENTER_X_LOCATION;
import static org.cytoscape.view.presentation.property.MinimalVisualLexicon.NETWORK_CENTER_Y_LOCATION;
import static org.cytoscape.view.presentation.property.RichVisualLexicon.NETWORK_CENTER_Z_LOCATION;
import static org.cytoscape.view.presentation.property.MinimalVisualLexicon.NETWORK_SCALE_FACTOR;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.cytoscape.event.CyEventHelper;
import org.cytoscape.model.CyNode;
import org.cytoscape.view.model.CyNetworkView;
import org.cytoscape.view.model.View;
import org.cytoscape.work.undo.AbstractCyEdit;


/** An undoable edit that will undo and redo the zooming of a network view. */
final class LayoutEdit extends AbstractCyEdit {
	private final CyEventHelper eventHelper;
	private final CyNetworkView view;
	private List<NodeViewAndLocations> nodeViewsAndLocations;
	private double networkScale;
	private double networkCenterX;
	private double networkCenterY;
	private double networkCenterZ;

	LayoutEdit(final CyEventHelper eventHelper, final CyNetworkView view) {
		super("Apply Preferred Layout");

		this.eventHelper = eventHelper;
		this.view        = view;

		saveNodeViewsAndLocations();
	}

	public void redo() {
		;

		saveAndRestore();
	}

	public void undo() {
		;

		saveAndRestore();
	}

	private void saveAndRestore() {
		final List<NodeViewAndLocations> oldNodeViewsAndLocations = nodeViewsAndLocations;
		final double oldNetworkScale = networkScale;
		final double oldNetworkCenterX = networkCenterX;
		final double oldNetworkCenterY = networkCenterY;
		final double oldNetworkCenterZ = networkCenterZ;
		saveNodeViewsAndLocations();
		for (final NodeViewAndLocations nodeViewAndLocation : oldNodeViewsAndLocations)
			nodeViewAndLocation.restoreLocations();

		view.setVisualProperty(NETWORK_SCALE_FACTOR, oldNetworkScale);
		view.setVisualProperty(NETWORK_CENTER_X_LOCATION, oldNetworkCenterX);
		view.setVisualProperty(NETWORK_CENTER_Y_LOCATION, oldNetworkCenterY);
		view.setVisualProperty(NETWORK_CENTER_Z_LOCATION, oldNetworkCenterZ);

		eventHelper.flushPayloadEvents();
		view.updateView();
	}

	private void saveNodeViewsAndLocations() {
		networkScale = view.getVisualProperty(NETWORK_SCALE_FACTOR);
		networkCenterX = view.getVisualProperty(NETWORK_CENTER_X_LOCATION);
		networkCenterY = view.getVisualProperty(NETWORK_CENTER_Y_LOCATION);
		networkCenterZ = view.getVisualProperty(NETWORK_CENTER_Z_LOCATION);

		final Collection<View<CyNode>> nodeViews = view.getNodeViews();
		nodeViewsAndLocations = new ArrayList<NodeViewAndLocations>(nodeViews.size());
		for (final View<CyNode> nodeView : nodeViews)
			nodeViewsAndLocations.add(new NodeViewAndLocations(nodeView));
	}
}


final class NodeViewAndLocations {
	private final View<CyNode> nodeView;
	private final double xLocation;
	private final double yLocation;
	private final double zLocation;

	NodeViewAndLocations(final View<CyNode> nodeView) {
		this.nodeView = nodeView;
		xLocation = nodeView.getVisualProperty(NODE_X_LOCATION);
		yLocation = nodeView.getVisualProperty(NODE_Y_LOCATION);
		zLocation = nodeView.getVisualProperty(NODE_Z_LOCATION);
	}
{'fraction_non_alphanumeric': 0.07918886915334518, 'fraction_numerical': 0.0004440497335701599, 'mean_word_length': 6.884480746791132, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5714213', 'n_tokens_mistral': 2225, 'n_tokens_neox': 2049, 'n_words': 379}