<commit_before>/*******************************************************************************
 * Copyright (c) 2011 Red Hat, Inc.
 *  All rights reserved.
 * This program is made available under the terms of the
 * Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Red Hat, Inc. - initial API and implementation
 *
 * @author <NAME>
 ******************************************************************************/

package org.eclipse.bpmn2.modeler.core.adapters;

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

import org.eclipse.bpmn2.modeler.core.utils.ModelUtil;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EContentAdapter;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.util.TransactionUtil;

/**
 * This adapter will insert an EObject into its container feature when the EObject's
 * content changes. This allows the UI to construct new objects without inserting
 * them into their container unless the user changes some feature in the new object.
 * Thus, an empty EObject is available for use by the UI for rendering only, without
 * creating an EMF transaction, and hence, a useless entry on the undo stack.
 */
public class InsertionAdapter extends EContentAdapter {
	
	protected Resource resource;
	protected EObject object;
	protected EStructuralFeature feature;
	protected EObject value;
	
	public InsertionAdapter(EObject object, EStructuralFeature feature, EObject value) {
		this(null,object,feature,value);
	}

	public InsertionAdapter(Resource resource, EObject object, EStructuralFeature feature, EObject value) {
		// in order for this to work, the object must be contained in a Resource,
		// the value must NOT YET be contained in a Resource,
		// and the value must be an instance of the feature EType.
//		assert(object.eResource()!=null);
//		assert(value.eResource()==null);
//		assert(feature.getEType().isInstance(value));
		if (resource==null)
			this.resource = object.eResource();
		else
			this.resource = resource;
		this.object = object;
		this.feature = feature;
		this.value = value;
	}
	
	public InsertionAdapter(EObject object, String featureName, EObject value) {
		this(object, object.eClass().getEStructuralFeature(featureName), value);
	}
	
	public static EObject add(EObject object, EStructuralFeature feature, EObject value) {
		value.eAdapters().add(
				new InsertionAdapter(object, feature, value));
		return value;
	}
	
	public static EObject add(EObject object, String featureName, EObject value) {
		value.eAdapters().add(
				new InsertionAdapter(object, featureName, value));
		return value;
	}

	public void notifyChanged(Notification notification) {
		super.notifyChanged(notification);
		if (notification.getNotifier() == value) {
			// execute if an attribute in the new value has changed
			executeIfNeeded(value);
		}
		else if (notification.getNotifier()==object && notification.getNewValue()==value) {
			// if the new value has been added to the object, we can remove this adapter
			object.eAdapters().remove(this);
		}
	}

	@SuppressWarnings("unchecked")
	public void execute() {
		// remove this adapter from the value - this adapter is a one-shot deal!
		object.eAdapters().remove(this);
		executeIfNeeded(object);
		value.eAdapters().remove(this);
		// set the value in the object
		boolean valueChanged = false;<commit_msg>https://bugs.eclipse.org/bugs/show_bug.cgi?id=386842 - fix in InsertionAdapter<commit_after>/*******************************************************************************
 * Copyright (c) 2011 Red Hat, Inc.
 *  All rights reserved.
 * This program is made available under the terms of the
 * Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Red Hat, Inc. - initial API and implementation
 *
 * @author <NAME>
 ******************************************************************************/

package org.eclipse.bpmn2.modeler.core.adapters;

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

import org.eclipse.bpmn2.modeler.core.utils.ModelUtil;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EContentAdapter;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.util.TransactionUtil;

/**
 * This adapter will insert an EObject into its container feature when the EObject's
 * content changes. This allows the UI to construct new objects without inserting
 * them into their container unless the user changes some feature in the new object.
 * Thus, an empty EObject is available for use by the UI for rendering only, without
 * creating an EMF transaction, and hence, a useless entry on the undo stack.
 */
public class InsertionAdapter extends EContentAdapter {
	
	protected Resource resource;
	protected EObject object;
	protected EStructuralFeature feature;
	protected EObject value;
	
	public InsertionAdapter(EObject object, EStructuralFeature feature, EObject value) {
		this(null,object,feature,value);
	}

	public InsertionAdapter(Resource resource, EObject object, EStructuralFeature feature, EObject value) {
		// in order for this to work, the object must be contained in a Resource,
		// the value must NOT YET be contained in a Resource,
		// and the value must be an instance of the feature EType.
//		assert(object.eResource()!=null);
//		assert(value.eResource()==null);
//		assert(feature.getEType().isInstance(value));
		if (resource==null)
			this.resource = object.eResource();
		else
			this.resource = resource;
		this.object = object;
		this.feature = feature;
		this.value = value;
	}
	
	public InsertionAdapter(EObject object, String featureName, EObject value) {
		this(object, object.eClass().getEStructuralFeature(featureName), value);
	}
	
	public static EObject add(EObject object, EStructuralFeature feature, EObject value) {
		value.eAdapters().add(
				new InsertionAdapter(object, feature, value));
		return value;
	}
	
	public static EObject add(EObject object, String featureName, EObject value) {
		value.eAdapters().add(
				new InsertionAdapter(object, featureName, value));
		return value;
	}

	public void notifyChanged(Notification notification) {
		if (notification.getNotifier() == value) {
			// execute if an attribute in the new value has changed
			executeIfNeeded(value);
		}
		else if (notification.getNotifier()==object && notification.getNewValue()==value) {
			// if the new value has been added to the object, we can remove this adapter
			object.eAdapters().remove(this);
		}
	}

	@SuppressWarnings("unchecked")
	public void execute() {
		// remove this adapter from the value - this adapter is a one-shot deal!
		object.eAdapters().remove(this);
		executeIfNeeded(object);
		value.eAdapters().remove(this);
		// set the value in the object
		boolean valueChanged = false;
{'fraction_non_alphanumeric': 0.13577472236911686, 'fraction_numerical': 0.003437334743521946, 'mean_word_length': 4.788064269319051, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, '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': '3441875', 'n_tokens_mistral': 2255, 'n_tokens_neox': 2090, 'n_words': 665}