<commit_before>/**
 * Copyright (c) 2013 committers of YAKINDU and others.
 * All rights reserved. This program and the accompanying materials
 * are 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:
 * 	committers of YAKINDU - initial API and implementation
 * 
 */
package org.yakindu.sct.ui.editor.partitioning;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramEditorInput;
import org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.DiagramDocument;
import org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.DiagramModificationListener;
import org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDiagramDocumentProvider;
import org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDocument;
import org.eclipse.gmf.runtime.diagram.ui.resources.editor.ide.document.FileDiagramDocumentProvider;
import org.eclipse.gmf.runtime.diagram.ui.resources.editor.ide.document.FileDiagramModificationListener;
import org.eclipse.gmf.runtime.diagram.ui.resources.editor.ide.document.FileEditorInputProxy;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IFileEditorInput;

/**
 * Implementation of the {@link IDiagramDocumentProvider} interface that extends
 * a {@link FileDiagramDocumentProvider}.
 * 
 * In addition to the {@link FileDiagramDocumentProvider}, this implementation
 * acts on a shared editing domain and can handle custom
 * {@link DiagramEditorInput}. It also unloads non used resources with help of
 * the {@link ResourceUnloadingTool}
 * 
 * @author <NAME> - Initial contribution and API
 * 
 */
public class DiagramPartitioningDocumentProvider extends FileDiagramDocumentProvider {

	/**
	 * Extension of {@link DiagramFileInfo} that stores the given
	 * {@link IEditorInput} which is required for the
	 * {@link ResourceUnloadingTool}
	 */
	protected class InputDiagramFileInfo extends DiagramFileInfo {

		private final IEditorInput editorInput;

		public InputDiagramFileInfo(IDocument document, FileSynchronizer fileSynchronizer,
				DiagramModificationListener listener, IEditorInput editorInput) {
			super(document, fileSynchronizer, listener);
			this.editorInput = editorInput;
		}

		public IEditorInput getEditorInput() {
			return editorInput;
		}
	}

	public IEditorInput createInputWithEditingDomain(IEditorInput editorInput, TransactionalEditingDomain domain) {
		if (editorInput instanceof DiagramEditorInput) {
			return editorInput;
		}
		return super.createInputWithEditingDomain(editorInput, DiagramPartitioningUtil.getSharedDomain());
	}

	/**
	 * Sets the {@link EditingDomain} to the shared instance
	 */
	@Override
	protected IDocument createEmptyDocument() {
		DiagramDocument diagramDocument = new DiagramDocument();
		diagramDocument.setEditingDomain(DiagramPartitioningUtil.getSharedDomain());
		return diagramDocument;
	}

	@Override
	protected ElementInfo createElementInfo(Object element) throws CoreException {
		ElementInfo info = super.createElementInfo(element);
		// the canBeSaved flag is calculated based on the dirty state of the
		// editingdomains resourceset
		info.fCanBeSaved = isDirty(DiagramPartitioningUtil.getSharedDomain());
		return info;
	}

	protected boolean isDirty(TransactionalEditingDomain domain) {
		for (final Resource resource : domain.getResourceSet().getResources()) {
			if (resource.isLoaded() && !domain.isReadOnly(resource) && resource.isModified()) {
				return true;
			}
		}
		return false;
	}

	protected FileInfo createFileInfo(IDocument document, FileSynchronizer synchronizer, IFileEditorInput input) {
		assert document instanceof DiagramDocument;<commit_msg>Bugfix: Dirty State flag was calculated wrong<commit_after>/**
 * Copyright (c) 2013 committers of YAKINDU and others.
 * All rights reserved. This program and the accompanying materials
 * are 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:
 * 	committers of YAKINDU - initial API and implementation
 * 
 */
package org.yakindu.sct.ui.editor.partitioning;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramEditorInput;
import org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.DiagramDocument;
import org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.DiagramModificationListener;
import org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDiagramDocumentProvider;
import org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDocument;
import org.eclipse.gmf.runtime.diagram.ui.resources.editor.ide.document.FileDiagramDocumentProvider;
import org.eclipse.gmf.runtime.diagram.ui.resources.editor.ide.document.FileDiagramModificationListener;
import org.eclipse.gmf.runtime.diagram.ui.resources.editor.ide.document.FileEditorInputProxy;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IFileEditorInput;

/**
 * Implementation of the {@link IDiagramDocumentProvider} interface that extends
 * a {@link FileDiagramDocumentProvider}.
 * 
 * In addition to the {@link FileDiagramDocumentProvider}, this implementation
 * acts on a shared editing domain and can handle custom
 * {@link DiagramEditorInput}. It also unloads non used resources with help of
 * the {@link ResourceUnloadingTool}
 * 
 * @author <NAME> - Initial contribution and API
 * 
 */
public class DiagramPartitioningDocumentProvider extends FileDiagramDocumentProvider {

	/**
	 * Extension of {@link DiagramFileInfo} that stores the given
	 * {@link IEditorInput} which is required for the
	 * {@link ResourceUnloadingTool}
	 */
	protected class InputDiagramFileInfo extends DiagramFileInfo {

		private final IEditorInput editorInput;

		public InputDiagramFileInfo(IDocument document, FileSynchronizer fileSynchronizer,
				DiagramModificationListener listener, IEditorInput editorInput) {
			super(document, fileSynchronizer, listener);
			this.editorInput = editorInput;
		}

		public IEditorInput getEditorInput() {
			return editorInput;
		}
	}

	public IEditorInput createInputWithEditingDomain(IEditorInput editorInput, TransactionalEditingDomain domain) {
		if (editorInput instanceof DiagramEditorInput) {
			return editorInput;
		}
		return super.createInputWithEditingDomain(editorInput, DiagramPartitioningUtil.getSharedDomain());
	}

	/**
	 * Sets the {@link EditingDomain} to the shared instance
	 */
	@Override
	protected IDocument createEmptyDocument() {
		DiagramDocument diagramDocument = new DiagramDocument();
		diagramDocument.setEditingDomain(DiagramPartitioningUtil.getSharedDomain());
		return diagramDocument;
	}

	@Override
	protected ElementInfo createElementInfo(Object element) throws CoreException {
		ElementInfo info = super.createElementInfo(element);
		if (element instanceof IDiagramEditorInput) {
			Resource eResource = ((IDiagramEditorInput) element).getDiagram().eResource();
			TransactionalEditingDomain sharedDomain = DiagramPartitioningUtil.getSharedDomain();
			if (eResource.isLoaded() && !sharedDomain.isReadOnly(eResource) && eResource.isModified()) {
				info.fCanBeSaved = true;
			}
		}
		return info;
	}

	protected FileInfo createFileInfo(IDocument document, FileSynchronizer synchronizer, IFileEditorInput input) {
		assert document instanceof DiagramDocument;
{'fraction_non_alphanumeric': 0.08579359071410547, 'fraction_numerical': 0.0020186727226848347, 'mean_word_length': 5.6557514693534845, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, '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': '3409381', 'n_tokens_mistral': 2294, 'n_tokens_neox': 2165, 'n_words': 527}